File size: 4,547 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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
/**
* @typedef {import('micromark-util-types').Effects} Effects
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').TokenType} TokenType
*/
import {
asciiControl,
markdownLineEndingOrSpace,
markdownLineEnding
} from 'micromark-util-character'
/**
* Parse destinations.
*
* ###### Examples
*
* ```markdown
* <a>
* <a\>b>
* <a b>
* <a)>
* a
* a\)b
* a(b)c
* a(b)
* ```
*
* @param {Effects} effects
* Context.
* @param {State} ok
* State switched to when successful.
* @param {State} nok
* State switched to when unsuccessful.
* @param {TokenType} type
* Type for whole (`<a>` or `b`).
* @param {TokenType} literalType
* Type when enclosed (`<a>`).
* @param {TokenType} literalMarkerType
* Type for enclosing (`<` and `>`).
* @param {TokenType} rawType
* Type when not enclosed (`b`).
* @param {TokenType} stringType
* Type for the value (`a` or `b`).
* @param {number | undefined} [max=Infinity]
* Depth of nested parens (inclusive).
* @returns {State}
* Start 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
/**
* Start of destination.
*
* ```markdown
* > | <aa>
* ^
* > | aa
* ^
* ```
*
* @type {State}
*/
function start(code) {
if (code === 60) {
effects.enter(type)
effects.enter(literalType)
effects.enter(literalMarkerType)
effects.consume(code)
effects.exit(literalMarkerType)
return enclosedBefore
}
// ASCII control, space, closing paren.
if (code === null || code === 32 || code === 41 || asciiControl(code)) {
return nok(code)
}
effects.enter(type)
effects.enter(rawType)
effects.enter(stringType)
effects.enter('chunkString', {
contentType: 'string'
})
return raw(code)
}
/**
* After `<`, at an enclosed destination.
*
* ```markdown
* > | <aa>
* ^
* ```
*
* @type {State}
*/
function enclosedBefore(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 enclosed(code)
}
/**
* In enclosed destination.
*
* ```markdown
* > | <aa>
* ^
* ```
*
* @type {State}
*/
function enclosed(code) {
if (code === 62) {
effects.exit('chunkString')
effects.exit(stringType)
return enclosedBefore(code)
}
if (code === null || code === 60 || markdownLineEnding(code)) {
return nok(code)
}
effects.consume(code)
return code === 92 ? enclosedEscape : enclosed
}
/**
* After `\`, at a special character.
*
* ```markdown
* > | <a\*a>
* ^
* ```
*
* @type {State}
*/
function enclosedEscape(code) {
if (code === 60 || code === 62 || code === 92) {
effects.consume(code)
return enclosed
}
return enclosed(code)
}
/**
* In raw destination.
*
* ```markdown
* > | aa
* ^
* ```
*
* @type {State}
*/
function raw(code) {
if (
!balance &&
(code === null || code === 41 || markdownLineEndingOrSpace(code))
) {
effects.exit('chunkString')
effects.exit(stringType)
effects.exit(rawType)
effects.exit(type)
return ok(code)
}
if (balance < limit && code === 40) {
effects.consume(code)
balance++
return raw
}
if (code === 41) {
effects.consume(code)
balance--
return raw
}
// ASCII control (but *not* `\0`) and space and `(`.
// Note: in `markdown-rs`, `\0` exists in codes, in `micromark-js` it
// doesn’t.
if (code === null || code === 32 || code === 40 || asciiControl(code)) {
return nok(code)
}
effects.consume(code)
return code === 92 ? rawEscape : raw
}
/**
* After `\`, at special character.
*
* ```markdown
* > | a\*a
* ^
* ```
*
* @type {State}
*/
function rawEscape(code) {
if (code === 40 || code === 41 || code === 92) {
effects.consume(code)
return raw
}
return raw(code)
}
}
|