Urara-Blog/node_modules/.pnpm-store/v3/files/0f/77a0b86ae1b05121270f33a908f05ae27f35d661b3f3540a2c50fbcf00c9b00af5ef47b6e042da45e21f434dfd24e0951d778cb80f804a9c62d41cca45030d
2022-08-14 01:14:53 +08:00

151 lines
3.4 KiB
Text

/**
* @typedef {import('micromark-util-types').Effects} Effects
* @typedef {import('micromark-util-types').State} State
*/
import {
asciiControl,
markdownLineEndingOrSpace,
markdownLineEnding
} from 'micromark-util-character'
/**
* @param {Effects} effects
* @param {State} ok
* @param {State} nok
* @param {string} type
* @param {string} literalType
* @param {string} literalMarkerType
* @param {string} rawType
* @param {string} stringType
* @param {number} [max=Infinity]
* @returns {State}
*/
// eslint-disable-next-line max-params
export function factoryDestination(
effects,
ok,
nok,
type,
literalType,
literalMarkerType,
rawType,
stringType,
max
) {
const limit = max || Number.POSITIVE_INFINITY
let balance = 0
return start
/** @type {State} */
function start(code) {
if (code === 60) {
effects.enter(type)
effects.enter(literalType)
effects.enter(literalMarkerType)
effects.consume(code)
effects.exit(literalMarkerType)
return destinationEnclosedBefore
}
if (code === null || code === 41 || asciiControl(code)) {
return nok(code)
}
effects.enter(type)
effects.enter(rawType)
effects.enter(stringType)
effects.enter('chunkString', {
contentType: 'string'
})
return destinationRaw(code)
}
/** @type {State} */
function destinationEnclosedBefore(code) {
if (code === 62) {
effects.enter(literalMarkerType)
effects.consume(code)
effects.exit(literalMarkerType)
effects.exit(literalType)
effects.exit(type)
return ok
}
effects.enter(stringType)
effects.enter('chunkString', {
contentType: 'string'
})
return destinationEnclosed(code)
}
/** @type {State} */
function destinationEnclosed(code) {
if (code === 62) {
effects.exit('chunkString')
effects.exit(stringType)
return destinationEnclosedBefore(code)
}
if (code === null || code === 60 || markdownLineEnding(code)) {
return nok(code)
}
effects.consume(code)
return code === 92 ? destinationEnclosedEscape : destinationEnclosed
}
/** @type {State} */
function destinationEnclosedEscape(code) {
if (code === 60 || code === 62 || code === 92) {
effects.consume(code)
return destinationEnclosed
}
return destinationEnclosed(code)
}
/** @type {State} */
function destinationRaw(code) {
if (code === 40) {
if (++balance > limit) return nok(code)
effects.consume(code)
return destinationRaw
}
if (code === 41) {
if (!balance--) {
effects.exit('chunkString')
effects.exit(stringType)
effects.exit(rawType)
effects.exit(type)
return ok(code)
}
effects.consume(code)
return destinationRaw
}
if (code === null || markdownLineEndingOrSpace(code)) {
if (balance) return nok(code)
effects.exit('chunkString')
effects.exit(stringType)
effects.exit(rawType)
effects.exit(type)
return ok(code)
}
if (asciiControl(code)) return nok(code)
effects.consume(code)
return code === 92 ? destinationRawEscape : destinationRaw
}
/** @type {State} */
function destinationRawEscape(code) {
if (code === 40 || code === 41 || code === 92) {
effects.consume(code)
return destinationRaw
}
return destinationRaw(code)
}
}