-
Notifications
You must be signed in to change notification settings - Fork 611
/
marks.ts
281 lines (274 loc) · 7.29 KB
/
marks.ts
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
import { getMarks } from '../../stringify'
import type * as Md from 'mdast'
import type * as Plate from '../../parse/plate'
import type { RichTextField } from '@tinacms/schema-tools'
import { stringifyPropsInline } from './acorn'
const matches = (a: string[], b: string[]) => {
return a.some((v) => b.includes(v))
}
type InlineElementWithCallback = Plate.InlineElement & {
linkifyTextNode?: (arg: Md.Text) => Md.Link
}
/**
*
* Links can contain marks inside them, and in the scenario that there is a link with a single
* child with a mark on it, we want to possibly merge those marks with adjacent ones:
* ```markdown
* *Hello [world](https://example.com)*
* ```
* Without "flattening" text nodes like this, this shape:
* ```js
* [
* {
* type: 'text',
* text: 'Hello',
* italic: true
* },
* {
* type: 'link',
* url: 'https://example.com',
* children: [{
* type: 'text',
* text: 'world',
* italic: true
* }]
* }
* ]
* ```
* Would result in this markdown:
* ```markdown
* *Hello **[world](https://example.com)*
* ```
* So instead we place a callback on the text node, treat is any other text node,
* and at the end we replace it with it's callback value (inside cleanNodes)
*/
const replaceLinksWithTextNodes = (content: Plate.InlineElement[]) => {
const newItems: InlineElementWithCallback[] = []
content?.forEach((item) => {
if (item.type === 'a') {
if (item.children.length === 1) {
const firstChild = item.children[0]
if (firstChild?.type === 'text') {
newItems.push({
...firstChild,
linkifyTextNode: (a) => {
return {
type: 'link',
url: item.url,
title: item.title,
children: [a],
}
},
})
} else {
newItems.push(item)
}
} else {
newItems.push(item)
}
} else {
newItems.push(item)
}
})
return newItems
}
/**
* Links should be processed via 'linkifyTextNode', otherwise handle phrasing content
*/
const inlineElementExceptLink = (
content: InlineElementWithCallback,
field: RichTextField,
imageCallback: (url: string) => string
): Md.PhrasingContent => {
switch (content.type) {
case 'a':
throw new Error(
`Unexpected node of type "a", link elements should be processed after all inline elements have resolved`
)
case 'img':
return {
type: 'image',
url: imageCallback(content.url),
alt: content.alt,
title: content.caption,
}
case 'break':
return {
type: 'break',
}
case 'mdxJsxTextElement': {
const { attributes, children } = stringifyPropsInline(
content,
field,
imageCallback
)
let c = children
if (children.length) {
const firstChild = children[0]
// @ts-ignore FIXME: we're going outside of the MDAST type, which can include a paragraph here
if (firstChild && firstChild.type === 'paragraph') {
// @ts-ignore
c = firstChild.children
}
}
return {
type: 'mdxJsxTextElement',
name: content.name,
attributes,
children: c,
}
}
case 'html_inline': {
return {
type: 'html',
value: content.value,
}
}
default:
// @ts-expect-error type is 'never'
if (!content.type && typeof content.text === 'string') {
return text(content)
}
throw new Error(`InlineElement: ${content.type} is not supported`)
}
}
const text = (content: { text: string }) => {
return {
type: 'text' as const,
value: content.text,
}
}
export const eat = (
c: InlineElementWithCallback[],
field: RichTextField,
imageCallback: (url: string) => string
): Md.PhrasingContent[] => {
const content = replaceLinksWithTextNodes(c)
const first = content[0]
if (!first) {
return []
}
if (first && first?.type !== 'text') {
if (first.type === 'a') {
return [
{
type: 'link',
url: first.url,
title: first.title,
children: eat(
first.children,
field,
imageCallback
) as Md.StaticPhrasingContent[],
},
...eat(content.slice(1), field, imageCallback),
]
}
// non-text nodes can't be merged. Eg. img, break. So process them and move on to the rest
return [
inlineElementExceptLink(first, field, imageCallback),
...eat(content.slice(1), field, imageCallback),
]
}
const marks = getMarks(first)
if (marks.length === 0) {
if (first.linkifyTextNode) {
return [
first.linkifyTextNode(text(first)),
...eat(content.slice(1), field, imageCallback),
]
} else {
return [text(first), ...eat(content.slice(1), field, imageCallback)]
}
}
let nonMatchingSiblingIndex: number = 0
if (
content.slice(1).every((content, index) => {
if (matches(marks, getMarks(content))) {
return true
} else {
nonMatchingSiblingIndex = index
return false
}
})
) {
// Every sibling matches, so capture all of them in this node
nonMatchingSiblingIndex = content.length - 1
}
const matchingSiblings = content.slice(1, nonMatchingSiblingIndex + 1)
const markCounts: { [key in 'strong' | 'emphasis' | 'inlineCode']?: number } =
{}
marks.forEach((mark) => {
let count = 1
matchingSiblings.every((sibling, index) => {
if (getMarks(sibling).includes(mark)) {
count = index + 1
return true
}
})
markCounts[mark] = count
})
let count = 0
let markToProcess: 'strong' | 'emphasis' | 'inlineCode' | null = null
Object.entries(markCounts).forEach(([mark, markCount]) => {
const m = mark as 'strong' | 'emphasis' | 'inlineCode'
if (markCount > count) {
count = markCount
markToProcess = m
}
})
if (!markToProcess) {
return [text(first), ...eat(content.slice(1), field, imageCallback)]
}
if (markToProcess === 'inlineCode') {
if (nonMatchingSiblingIndex) {
throw new Error(`Marks inside inline code are not supported`)
}
const node = {
type: markToProcess,
value: first.text,
}
return [
first.linkifyTextNode?.(node) ?? node,
...eat(content.slice(nonMatchingSiblingIndex + 1), field, imageCallback),
]
}
return [
{
type: markToProcess,
children: eat(
[
...[first, ...matchingSiblings].map((sibling) =>
cleanNode(sibling, markToProcess)
),
],
field,
imageCallback
),
},
...eat(content.slice(nonMatchingSiblingIndex + 1), field, imageCallback),
]
}
const cleanNode = (
node: InlineElementWithCallback,
mark: 'strong' | 'emphasis' | 'inlineCode' | null
): Plate.InlineElement => {
if (!mark) {
return node
}
const cleanedNode: Record<string, unknown> = {}
const markToClear = {
strong: 'bold',
emphasis: 'italic',
inlineCode: 'code',
}[mark]
Object.entries(node).map(([key, value]) => {
if (key !== markToClear) {
cleanedNode[key] = value
}
})
if (node.linkifyTextNode) {
cleanedNode.callback = node.linkifyTextNode
}
return cleanedNode as Plate.InlineElement
}