Urara-Blog/node_modules/.pnpm-store/v3/files/69/29d8fffc190d9a5bd2b2a7f8a9ca8dabe3b6837b68196982b102e999341dd4d73473fe9b9019f186f7fe2604950d3594e2f167ed18f9ef4a8925b6dd7f35bf
2022-08-14 01:14:53 +08:00

134 lines
3.6 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @typedef {import('micromark-util-types').Construct} Construct
* @typedef {import('micromark-util-types').Resolver} Resolver
* @typedef {import('micromark-util-types').Tokenizer} Tokenizer
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').Code} Code
*/
import {factorySpace} from 'micromark-factory-space'
import {markdownLineEnding} from 'micromark-util-character'
/** @type {Construct} */
export const setextUnderline = {
name: 'setextUnderline',
tokenize: tokenizeSetextUnderline,
resolveTo: resolveToSetextUnderline
}
/** @type {Resolver} */
function resolveToSetextUnderline(events, context) {
let index = events.length
/** @type {number|undefined} */
let content
/** @type {number|undefined} */
let text
/** @type {number|undefined} */
let definition // Find the opening of the content.
// Itll always exist: we dont tokenize if it isnt there.
while (index--) {
if (events[index][0] === 'enter') {
if (events[index][1].type === 'content') {
content = index
break
}
if (events[index][1].type === 'paragraph') {
text = index
}
} // Exit
else {
if (events[index][1].type === 'content') {
// Remove the content end (if needed well add it later)
events.splice(index, 1)
}
if (!definition && events[index][1].type === 'definition') {
definition = index
}
}
}
const heading = {
type: 'setextHeading',
start: Object.assign({}, events[text][1].start),
end: Object.assign({}, events[events.length - 1][1].end)
} // Change the paragraph to setext heading text.
events[text][1].type = 'setextHeadingText' // If we have definitions in the content, well keep on having content,
// but we need move it.
if (definition) {
events.splice(text, 0, ['enter', heading, context])
events.splice(definition + 1, 0, ['exit', events[content][1], context])
events[content][1].end = Object.assign({}, events[definition][1].end)
} else {
events[content][1] = heading
} // Add the heading exit at the end.
events.push(['exit', heading, context])
return events
}
/** @type {Tokenizer} */
function tokenizeSetextUnderline(effects, ok, nok) {
const self = this
let index = self.events.length
/** @type {NonNullable<Code>} */
let marker
/** @type {boolean} */
let paragraph // Find an opening.
while (index--) {
// Skip enter/exit of line ending, line prefix, and content.
// We can now either have a definition or a paragraph.
if (
self.events[index][1].type !== 'lineEnding' &&
self.events[index][1].type !== 'linePrefix' &&
self.events[index][1].type !== 'content'
) {
paragraph = self.events[index][1].type === 'paragraph'
break
}
}
return start
/** @type {State} */
function start(code) {
if (!self.parser.lazy[self.now().line] && (self.interrupt || paragraph)) {
effects.enter('setextHeadingLine')
effects.enter('setextHeadingLineSequence')
marker = code
return closingSequence(code)
}
return nok(code)
}
/** @type {State} */
function closingSequence(code) {
if (code === marker) {
effects.consume(code)
return closingSequence
}
effects.exit('setextHeadingLineSequence')
return factorySpace(effects, closingSequenceEnd, 'lineSuffix')(code)
}
/** @type {State} */
function closingSequenceEnd(code) {
if (code === null || markdownLineEnding(code)) {
effects.exit('setextHeadingLine')
return ok(code)
}
return nok(code)
}
}