-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathwrap.js
223 lines (200 loc) · 5.58 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
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
/**
* @import {} from 'mdast-util-to-hast'
* @import {
* BlockContent,
* Delete,
* Link,
* Nodes,
* Paragraph,
* Parents,
* PhrasingContent,
* RootContent
* } from 'mdast'
*/
import structuredClone from '@ungap/structured-clone'
import {phrasing as hastPhrasing} from 'hast-util-phrasing'
import {whitespace} from 'hast-util-whitespace'
import {phrasing as mdastPhrasing} from 'mdast-util-phrasing'
import {dropSurroundingBreaks} from './drop-surrounding-breaks.js'
/**
* Check if there are phrasing mdast nodes.
*
* This is needed if a fragment is given, which could just be a sentence, and
* doesn’t need a wrapper paragraph.
*
* @param {Array<Nodes>} nodes
* @returns {boolean}
*/
export function wrapNeeded(nodes) {
let index = -1
while (++index < nodes.length) {
const node = nodes[index]
if (!phrasing(node) || ('children' in node && wrapNeeded(node.children))) {
return true
}
}
return false
}
/**
* Wrap runs of phrasing content into paragraphs, leaving the non-phrasing
* content as-is.
*
* @param {Array<RootContent>} nodes
* Content.
* @returns {Array<BlockContent>}
* Content where phrasing is wrapped in paragraphs.
*/
export function wrap(nodes) {
return runs(nodes, onphrasing, function (d) {
return d
})
/**
* @param {Array<PhrasingContent>} nodes
* @returns {Array<Paragraph>}
*/
function onphrasing(nodes) {
return nodes.every(function (d) {
return d.type === 'text' ? whitespace(d.value) : false
})
? []
: [{type: 'paragraph', children: dropSurroundingBreaks(nodes)}]
}
}
/**
* @param {Delete | Link} node
* @returns {Array<BlockContent>}
*/
function split(node) {
return runs(node.children, onphrasing, onnonphrasing)
/**
* Use `parent`, put the phrasing run inside it.
*
* @param {Array<PhrasingContent>} nodes
* @returns {Array<BlockContent>}
*/
function onphrasing(nodes) {
const newParent = cloneWithoutChildren(node)
newParent.children = nodes
// @ts-expect-error Assume fine.
return [newParent]
}
/**
* 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 {BlockContent} child
* @returns {BlockContent}
*/
function onnonphrasing(child) {
if ('children' in child && 'children' in node) {
const newParent = cloneWithoutChildren(node)
const newChild = cloneWithoutChildren(child)
// @ts-expect-error Assume fine.
newParent.children = child.children
// @ts-expect-error Assume fine.
newChild.children.push(newParent)
return newChild
}
return {...child}
}
}
/**
* Wrap all runs of mdast phrasing content in `paragraph` nodes.
*
* @param {Array<RootContent>} nodes
* List of input nodes.
* @param {(nodes: Array<PhrasingContent>) => Array<BlockContent>} onphrasing
* Turn phrasing content into block content.
* @param {(node: BlockContent) => BlockContent} onnonphrasing
* Map block content (defaults to keeping them as-is).
* @returns {Array<BlockContent>}
*/
function runs(nodes, onphrasing, onnonphrasing) {
const flattened = flatten(nodes)
/** @type {Array<BlockContent>} */
const result = []
/** @type {Array<PhrasingContent>} */
let queue = []
let index = -1
while (++index < flattened.length) {
const node = flattened[index]
if (phrasing(node)) {
queue.push(node)
} else {
if (queue.length > 0) {
result.push(...onphrasing(queue))
queue = []
}
// @ts-expect-error Assume non-phrasing.
result.push(onnonphrasing(node))
}
}
if (queue.length > 0) {
result.push(...onphrasing(queue))
queue = []
}
return result
}
/**
* Flatten a list of nodes.
*
* @param {Array<RootContent>} nodes
* List of nodes, will unravel `delete` and `link`.
* @returns {Array<RootContent>}
* Unraveled nodes.
*/
function flatten(nodes) {
/** @type {Array<RootContent>} */
const flattened = []
let index = -1
while (++index < nodes.length) {
const 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.push(...split(node))
} else {
flattened.push(node)
}
}
return flattened
}
/**
* Check if an mdast node is phrasing.
*
* Also supports checking embedded hast fields.
*
* @param {Nodes} node
* mdast node to check.
* @returns {node is PhrasingContent}
* Whether `node` is phrasing content (includes nodes with `hName` fields
* set to phrasing hast element names).
*/
function phrasing(node) {
const tagName = node.data && node.data.hName
return tagName
? hastPhrasing({type: 'element', tagName, properties: {}, children: []})
: mdastPhrasing(node)
}
/**
* @template {Parents} ParentType
* Parent type.
* @param {ParentType} node
* Node to clone.
* @returns {ParentType}
* Cloned node, without children.
*/
function cloneWithoutChildren(node) {
return structuredClone({...node, children: []})
}