-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathwrap.js
189 lines (165 loc) · 4.48 KB
/
wrap.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
/**
* @typedef {import('../types.js').H} H
* @typedef {import('../types.js').MdastNode} MdastNode
* @typedef {import('../types.js').MdastPhrasingContent} MdastPhrasingContent
*/
import extend from 'extend'
import {phrasing} from 'mdast-util-phrasing'
/**
* @param {Array.<MdastNode>} nodes
*/
export function wrap(nodes) {
return runs(nodes, onphrasing)
/**
* @param {Array.<MdastPhrasingContent>} nodes
* @returns {MdastNode|Array.<MdastNode>}
*/
function onphrasing(nodes) {
const head = nodes[0]
if (
nodes.length === 1 &&
head.type === 'text' &&
(head.value === ' ' || head.value === '\n')
) {
return []
}
return {type: 'paragraph', children: nodes}
}
}
/**
* Check if there are non-phrasing mdast nodes returned.
* This is needed if a fragment is given, which could just be a sentence, and
* doesn’t need a wrapper paragraph.
*
* @param {Array.<MdastNode>} nodes
* @returns {boolean}
*/
export function wrapNeeded(nodes) {
let index = -1
/** @type {MdastNode} */
let node
while (++index < nodes.length) {
node = nodes[index]
if (!phrasing(node) || ('children' in node && wrapNeeded(node.children))) {
return true
}
}
return false
}
/**
* Wrap all runs of mdast phrasing content in `paragraph` nodes.
*
* @param {Array.<MdastNode>} nodes
* @param {(nodes: Array.<MdastPhrasingContent>) => MdastNode|Array.<MdastNode>} onphrasing
* @param {(node: MdastNode) => MdastNode} [onnonphrasing]
*/
function runs(nodes, onphrasing, onnonphrasing) {
const nonphrasing = onnonphrasing || identity
/** @type {Array.<MdastNode>} */
const flattened = flatten(nodes)
/** @type {Array.<MdastNode>} */
let result = []
let index = -1
/** @type {Array.<MdastPhrasingContent>|undefined} */
let queue
/** @type {MdastNode} */
let node
while (++index < flattened.length) {
node = flattened[index]
if (phrasing(node)) {
if (!queue) queue = []
queue.push(node)
} else {
if (queue) {
result = result.concat(onphrasing(queue))
queue = undefined
}
result = result.concat(nonphrasing(node))
}
}
if (queue) {
result = result.concat(onphrasing(queue))
}
return result
}
/**
* Flatten a list of nodes.
*
* @param {Array.<MdastNode>} nodes
* @returns {Array.<MdastNode>}
*/
function flatten(nodes) {
/** @type {Array.<MdastNode>} */
let flattened = []
let index = -1
/** @type {MdastNode} */
let node
while (++index < nodes.length) {
node = nodes[index]
// Straddling: some elements are *weird*.
// Namely: `map`, `ins`, `del`, and `a`, as they are hybrid elements.
// See: <https://html.spec.whatwg.org/#paragraphs>.
// Paragraphs are the weirdest of them all.
// See the straddling fixture for more info!
// `ins` is ignored in mdast, so we don’t need to worry about that.
// `map` maps to its content, so we don’t need to worry about that either.
// `del` maps to `delete` and `a` to `link`, so we do handle those.
// What we’ll do is split `node` over each of its children.
if (
(node.type === 'delete' || node.type === 'link') &&
wrapNeeded(node.children)
) {
flattened = flattened.concat(split(node))
} else {
flattened.push(node)
}
}
return flattened
}
/**
* @param {MdastNode} node
* @returns {Array.<MdastNode>}
*/
function split(node) {
// @ts-expect-error Assume parent.
return runs(node.children, onphrasing, onnonphrasing)
/**
* Use `child`, add `parent` as its first child, put the original children
* into `parent`.
* If `child` is not a parent, `parent` will not be added.
*
* @param {MdastNode} child
* @returns {MdastNode}
*/
function onnonphrasing(child) {
if ('children' in child && 'children' in node) {
const {children, ...rest} = node
return {
...child,
// @ts-expect-error: assume matching parent & child.
children: [{...extend(true, {}, rest), children: child.children}]
}
}
return {...child}
}
/**
* Use `parent`, put the phrasing run inside it.
*
* @param {Array.<MdastPhrasingContent>} nodes
* @returns {MdastNode}
*/
function onphrasing(nodes) {
// @ts-expect-error: assume parent.
const {children, ...rest} = node
// @ts-expect-error: assume matching parent & child.
return {...extend(true, {}, rest), children: nodes}
}
}
/**
* @template {unknown} T
* @param {T} n
* @returns {T}
*/
function identity(n) {
return n
}