File size: 3,205 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 |
/**
* @typedef {import('micromark-util-types').Code} Code
* @typedef {import('micromark-util-types').Effects} Effects
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').TokenType} TokenType
*/
import {factorySpace} from 'micromark-factory-space'
import {markdownLineEnding} from 'micromark-util-character'
/**
* Parse titles.
*
* ###### Examples
*
* ```markdown
* "a"
* 'b'
* (c)
* "a
* b"
* 'a
* b'
* (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 of the whole title (`"a"`, `'b'`, `(c)`).
* @param {TokenType} markerType
* Type for the markers (`"`, `'`, `(`, and `)`).
* @param {TokenType} stringType
* Type for the value (`a`).
* @returns {State}
* Start state.
*/ // eslint-disable-next-line max-params
export function factoryTitle(effects, ok, nok, type, markerType, stringType) {
/** @type {NonNullable<Code>} */
let marker
return start
/**
* Start of title.
*
* ```markdown
* > | "a"
* ^
* ```
*
* @type {State}
*/
function start(code) {
if (code === 34 || code === 39 || code === 40) {
effects.enter(type)
effects.enter(markerType)
effects.consume(code)
effects.exit(markerType)
marker = code === 40 ? 41 : code
return begin
}
return nok(code)
}
/**
* After opening marker.
*
* This is also used at the closing marker.
*
* ```markdown
* > | "a"
* ^
* ```
*
* @type {State}
*/
function begin(code) {
if (code === marker) {
effects.enter(markerType)
effects.consume(code)
effects.exit(markerType)
effects.exit(type)
return ok
}
effects.enter(stringType)
return atBreak(code)
}
/**
* At something, before something else.
*
* ```markdown
* > | "a"
* ^
* ```
*
* @type {State}
*/
function atBreak(code) {
if (code === marker) {
effects.exit(stringType)
return begin(marker)
}
if (code === null) {
return nok(code)
}
// Note: blank lines can’t exist in content.
if (markdownLineEnding(code)) {
// To do: use `space_or_tab_eol_with_options`, connect.
effects.enter('lineEnding')
effects.consume(code)
effects.exit('lineEnding')
return factorySpace(effects, atBreak, 'linePrefix')
}
effects.enter('chunkString', {
contentType: 'string'
})
return inside(code)
}
/**
*
*
* @type {State}
*/
function inside(code) {
if (code === marker || code === null || markdownLineEnding(code)) {
effects.exit('chunkString')
return atBreak(code)
}
effects.consume(code)
return code === 92 ? escape : inside
}
/**
* After `\`, at a special character.
*
* ```markdown
* > | "a\*b"
* ^
* ```
*
* @type {State}
*/
function escape(code) {
if (code === marker || code === 92) {
effects.consume(code)
return inside
}
return inside(code)
}
}
|