Urara-Blog/node_modules/.pnpm-store/v3/files/93/26bf822cf84c2a499d1fdc844f9db0bb9bdd75f63d22d4a4844a31e6ea4edbde5cc1692a0b528e168643c6f8aef821d2f1c4c212ef4f1ae5e7e299f175edb0
2022-08-14 01:14:53 +08:00

76 lines
1.8 KiB
Text

/**
* @typedef {import('micromark-util-types').Construct} Construct
* @typedef {import('micromark-util-types').Tokenizer} Tokenizer
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').Code} Code
*/
import {ok as assert} from 'uvu/assert'
import {factorySpace} from 'micromark-factory-space'
import {markdownLineEnding, markdownSpace} from 'micromark-util-character'
import {codes} from 'micromark-util-symbol/codes.js'
import {constants} from 'micromark-util-symbol/constants.js'
import {types} from 'micromark-util-symbol/types.js'
/** @type {Construct} */
export const thematicBreak = {
name: 'thematicBreak',
tokenize: tokenizeThematicBreak
}
/** @type {Tokenizer} */
function tokenizeThematicBreak(effects, ok, nok) {
let size = 0
/** @type {NonNullable<Code>} */
let marker
return start
/** @type {State} */
function start(code) {
assert(
code === codes.asterisk ||
code === codes.dash ||
code === codes.underscore,
'expected `*`, `-`, or `_`'
)
effects.enter(types.thematicBreak)
marker = code
return atBreak(code)
}
/** @type {State} */
function atBreak(code) {
if (code === marker) {
effects.enter(types.thematicBreakSequence)
return sequence(code)
}
if (markdownSpace(code)) {
return factorySpace(effects, atBreak, types.whitespace)(code)
}
if (
size < constants.thematicBreakMarkerCountMin ||
(code !== codes.eof && !markdownLineEnding(code))
) {
return nok(code)
}
effects.exit(types.thematicBreak)
return ok(code)
}
/** @type {State} */
function sequence(code) {
if (code === marker) {
effects.consume(code)
size++
return sequence
}
effects.exit(types.thematicBreakSequence)
return atBreak(code)
}
}