File size: 3,464 Bytes
bc20498 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
/**
* @typedef {import('micromark-util-types').Effects} Effects
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
* @typedef {import('micromark-util-types').TokenType} TokenType
*/
import {markdownLineEnding, markdownSpace} from 'micromark-util-character'
/**
* Parse labels.
*
* > 👉 **Note**: labels in markdown are capped at 999 characters in the string.
*
* ###### Examples
*
* ```markdown
* [a]
* [a
* b]
* [a\]b]
* ```
*
* @this {TokenizeContext}
* Tokenize context.
* @param {Effects} effects
* Context.
* @param {State} ok
* State switched to when successful.
* @param {State} nok
* State switched to when unsuccessful.
* @param {TokenType} type
* Type of the whole label (`[a]`).
* @param {TokenType} markerType
* Type for the markers (`[` and `]`).
* @param {TokenType} stringType
* Type for the identifier (`a`).
* @returns {State}
* Start state.
*/ // eslint-disable-next-line max-params
export function factoryLabel(effects, ok, nok, type, markerType, stringType) {
const self = this
let size = 0
/** @type {boolean} */
let seen
return start
/**
* Start of label.
*
* ```markdown
* > | [a]
* ^
* ```
*
* @type {State}
*/
function start(code) {
effects.enter(type)
effects.enter(markerType)
effects.consume(code)
effects.exit(markerType)
effects.enter(stringType)
return atBreak
}
/**
* In label, at something, before something else.
*
* ```markdown
* > | [a]
* ^
* ```
*
* @type {State}
*/
function atBreak(code) {
if (
size > 999 ||
code === null ||
code === 91 ||
(code === 93 && !seen) ||
// To do: remove in the future once we’ve switched from
// `micromark-extension-footnote` to `micromark-extension-gfm-footnote`,
// which doesn’t need this.
// Hidden footnotes hook.
/* c8 ignore next 3 */
(code === 94 &&
!size &&
'_hiddenFootnoteSupport' in self.parser.constructs)
) {
return nok(code)
}
if (code === 93) {
effects.exit(stringType)
effects.enter(markerType)
effects.consume(code)
effects.exit(markerType)
effects.exit(type)
return ok
}
// To do: indent? Link chunks and EOLs together?
if (markdownLineEnding(code)) {
effects.enter('lineEnding')
effects.consume(code)
effects.exit('lineEnding')
return atBreak
}
effects.enter('chunkString', {
contentType: 'string'
})
return labelInside(code)
}
/**
* In label, in text.
*
* ```markdown
* > | [a]
* ^
* ```
*
* @type {State}
*/
function labelInside(code) {
if (
code === null ||
code === 91 ||
code === 93 ||
markdownLineEnding(code) ||
size++ > 999
) {
effects.exit('chunkString')
return atBreak(code)
}
effects.consume(code)
if (!seen) seen = !markdownSpace(code)
return code === 92 ? labelEscape : labelInside
}
/**
* After `\`, at a special character.
*
* ```markdown
* > | [a\*a]
* ^
* ```
*
* @type {State}
*/
function labelEscape(code) {
if (code === 91 || code === 92 || code === 93) {
effects.consume(code)
size++
return labelInside
}
return labelInside(code)
}
}
|