-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathstate.js
389 lines (361 loc) · 11 KB
/
state.js
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/**
* @import {Element, Nodes, Parents} from 'hast'
* @import {
* BlockContent as MdastBlockContent,
* DefinitionContent as MdastDefinitionContent,
* Nodes as MdastNodes,
* Parents as MdastParents,
* RootContent as MdastRootContent
* } from 'mdast'
*/
/**
* @typedef {MdastBlockContent | MdastDefinitionContent} MdastFlowContent
*/
/**
* @callback All
* Transform the children of a hast parent to mdast.
* @param {Parents} parent
* Parent.
* @returns {Array<MdastRootContent>}
* mdast children.
*
* @callback Handle
* Handle a particular element.
* @param {State} state
* Info passed around about the current state.
* @param {Element} element
* Element to transform.
* @param {Parents | undefined} parent
* Parent of `element`.
* @returns {Array<MdastNodes> | MdastNodes | undefined | void}
* mdast node or nodes.
*
* Note: `void` is included until TS nicely infers `undefined`.
*
* @callback NodeHandle
* Handle a particular node.
* @param {State} state
* Info passed around about the current state.
* @param {any} node
* Node to transform.
* @param {Parents | undefined} parent
* Parent of `node`.
* @returns {Array<MdastNodes> | MdastNodes | undefined | void}
* mdast node or nodes.
*
* Note: `void` is included until TS nicely infers `undefined`.
*
* @callback One
* Transform a hast node to mdast.
* @param {Nodes} node
* Expected hast node.
* @param {Parents | undefined} parent
* Parent of `node`.
* @returns {Array<MdastNodes> | MdastNodes | undefined}
* mdast result.
*
* @typedef Options
* Configuration.
* @property {string | null | undefined} [checked='[x]']
* Value to use for a checked checkbox or radio input (default: `'[x]'`)
* @property {boolean | null | undefined} [document]
* Whether the given tree represents a complete document (optional).
*
* Applies when the `tree` is a `root` node.
* When the tree represents a complete document, then things are wrapped in
* paragraphs when needed, and otherwise they’re left as-is.
* The default checks for whether there’s mixed content: some phrasing nodes
* *and* some non-phrasing nodes.
* @property {Record<string, Handle | null | undefined> | null | undefined} [handlers]
* Object mapping tag names to functions handling the corresponding elements
* (optional).
*
* Merged into the defaults.
* @property {boolean | null | undefined} [newlines=false]
* Keep line endings when collapsing whitespace (default: `false`).
*
* The default collapses to a single space.
* @property {Record<string, NodeHandle | null | undefined> | null | undefined} [nodeHandlers]
* Object mapping node types to functions handling the corresponding nodes
* (optional).
*
* Merged into the defaults.
* @property {Array<string> | null | undefined} [quotes=['"']]
* List of quotes to use (default: `['"']`).
*
* Each value can be one or two characters.
* When two, the first character determines the opening quote and the second
* the closing quote at that level.
* When one, both the opening and closing quote are that character.
*
* The order in which the preferred quotes appear determines which quotes to
* use at which level of nesting.
* So, to prefer `‘’` at the first level of nesting, and `“”` at the second,
* pass `['‘’', '“”']`.
* If `<q>`s are nested deeper than the given amount of quotes, the markers
* wrap around: a third level of nesting when using `['«»', '‹›']` should
* have double guillemets, a fourth single, a fifth double again, etc.
* @property {string | null | undefined} [unchecked='[ ]']
* Value to use for an unchecked checkbox or radio input (default: `'[ ]'`).
*
* @callback Patch
* Copy a node’s positional info.
* @param {Nodes} from
* hast node to copy from.
* @param {MdastNodes} to
* mdast node to copy into.
* @returns {undefined}
* Nothing.
*
* @callback Resolve
* Resolve a URL relative to a base.
* @param {string | null | undefined} url
* Possible URL value.
* @returns {string}
* URL, resolved to a `base` element, if any.
*
* @typedef State
* Info passed around about the current state.
* @property {All} all
* Transform the children of a hast parent to mdast.
* @property {boolean} baseFound
* Whether a `<base>` element was seen.
* @property {Map<string, Element>} elementById
* Elements by their `id`.
* @property {string | undefined} frozenBaseUrl
* `href` of `<base>`, if any.
* @property {Record<string, Handle>} handlers
* Applied element handlers.
* @property {boolean} inTable
* Whether we’re in a table.
* @property {Record<string, NodeHandle>} nodeHandlers
* Applied node handlers.
* @property {One} one
* Transform a hast node to mdast.
* @property {Options} options
* User configuration.
* @property {Patch} patch
* Copy a node’s positional info.
* @property {number} qNesting
* Non-negative finite integer representing how deep we’re in `<q>`s.
* @property {Resolve} resolve
* Resolve a URL relative to a base.
* @property {ToFlow} toFlow
* Transform a list of mdast nodes to flow.
* @property {<ChildType extends MdastNodes, ParentType extends MdastParents & {'children': Array<ChildType>}>(nodes: Array<MdastRootContent>, build: (() => ParentType)) => Array<ParentType>} toSpecificContent
* Turn arbitrary content into a list of a particular node type.
*
* This is useful for example for lists, which must have list items as
* content.
* in this example, when non-items are found, they will be queued, and
* inserted into an adjacent item.
* When no actual items exist, one will be made with `build`.
*
* @callback ToFlow
* Transform a list of mdast nodes to flow.
* @param {Array<MdastRootContent>} nodes
* mdast nodes.
* @returns {Array<MdastFlowContent>}
* mdast flow children.
*/
import {position} from 'unist-util-position'
import {handlers, nodeHandlers} from './handlers/index.js'
import {wrap} from './util/wrap.js'
const own = {}.hasOwnProperty
/**
* Create a state.
*
* @param {Readonly<Options>} options
* User configuration.
* @returns {State}
* State.
*/
export function createState(options) {
return {
all,
baseFound: false,
elementById: new Map(),
frozenBaseUrl: undefined,
handlers: {...handlers, ...options.handlers},
inTable: false,
nodeHandlers: {...nodeHandlers, ...options.nodeHandlers},
one,
options,
patch,
qNesting: 0,
resolve,
toFlow,
toSpecificContent
}
}
/**
* Transform the children of a hast parent to mdast.
*
* You might want to combine this with `toFlow` or `toSpecificContent`.
*
* @this {State}
* Info passed around about the current state.
* @param {Parents} parent
* Parent.
* @returns {Array<MdastRootContent>}
* mdast children.
*/
function all(parent) {
const children = parent.children || []
/** @type {Array<MdastRootContent>} */
const results = []
let index = -1
while (++index < children.length) {
const child = children[index]
// Content -> content.
const result =
/** @type {Array<MdastRootContent> | MdastRootContent | undefined} */ (
this.one(child, parent)
)
if (Array.isArray(result)) {
results.push(...result)
} else if (result) {
results.push(result)
}
}
return results
}
/**
* Transform a hast node to mdast.
*
* @this {State}
* Info passed around about the current state.
* @param {Nodes} node
* hast node to transform.
* @param {Parents | undefined} parent
* Parent of `node`.
* @returns {Array<MdastNodes> | MdastNodes | undefined}
* mdast result.
*/
function one(node, parent) {
if (node.type === 'element') {
if (node.properties && node.properties.dataMdast === 'ignore') {
return
}
if (own.call(this.handlers, node.tagName)) {
return this.handlers[node.tagName](this, node, parent) || undefined
}
} else if (own.call(this.nodeHandlers, node.type)) {
return this.nodeHandlers[node.type](this, node, parent) || undefined
}
// Unknown literal.
if ('value' in node && typeof node.value === 'string') {
/** @type {MdastRootContent} */
const result = {type: 'text', value: node.value}
this.patch(node, result)
return result
}
// Unknown parent.
if ('children' in node) {
return this.all(node)
}
}
/**
* Copy a node’s positional info.
*
* @param {Nodes} origin
* hast node to copy from.
* @param {MdastNodes} node
* mdast node to copy into.
* @returns {undefined}
* Nothing.
*/
function patch(origin, node) {
if (origin.position) node.position = position(origin)
}
/**
* @this {State}
* Info passed around about the current state.
* @param {string | null | undefined} url
* Possible URL value.
* @returns {string}
* URL, resolved to a `base` element, if any.
*/
function resolve(url) {
const base = this.frozenBaseUrl
if (url === null || url === undefined) {
return ''
}
if (base) {
return String(new URL(url, base))
}
return url
}
/**
* Transform a list of mdast nodes to flow.
*
* @this {State}
* Info passed around about the current state.
* @param {Array<MdastRootContent>} nodes
* Parent.
* @returns {Array<MdastFlowContent>}
* mdast flow children.
*/
function toFlow(nodes) {
return wrap(nodes)
}
/**
* Turn arbitrary content into a particular node type.
*
* This is useful for example for lists, which must have list items as content.
* in this example, when non-items are found, they will be queued, and
* inserted into an adjacent item.
* When no actual items exist, one will be made with `build`.
*
* @template {MdastNodes} ChildType
* Node type of children.
* @template {MdastParents & {'children': Array<ChildType>}} ParentType
* Node type of parent.
* @param {Array<MdastRootContent>} nodes
* Nodes, which are either `ParentType`, or will be wrapped in one.
* @param {() => ParentType} build
* Build a parent if needed (must have empty `children`).
* @returns {Array<ParentType>}
* List of parents.
*/
function toSpecificContent(nodes, build) {
const reference = build()
/** @type {Array<ParentType>} */
const results = []
/** @type {Array<ChildType>} */
let queue = []
let index = -1
while (++index < nodes.length) {
const node = nodes[index]
if (expectedParent(node)) {
if (queue.length > 0) {
node.children.unshift(...queue)
queue = []
}
results.push(node)
} else {
// Assume `node` can be a child of `ParentType`.
// If we start checking nodes, we’d run into problems with unknown nodes,
// which we do want to support.
const child = /** @type {ChildType} */ (node)
queue.push(child)
}
}
if (queue.length > 0) {
let node = results[results.length - 1]
if (!node) {
node = build()
results.push(node)
}
node.children.push(...queue)
queue = []
}
return results
/**
* @param {MdastNodes} node
* @returns {node is ParentType}
*/
function expectedParent(node) {
return node.type === reference.type
}
}