-
-
Notifications
You must be signed in to change notification settings - Fork 584
/
Copy pathcontent-parse.ts
678 lines (583 loc) · 18.3 KB
/
content-parse.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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
// @unimport-disable
import type { mastodon } from 'masto'
import type { Node } from 'ultrahtml'
import { findAndReplaceEmojisInText } from '@iconify/utils'
import { decode } from 'tiny-decode'
import { DOCUMENT_NODE, ELEMENT_NODE, h, parse, render, TEXT_NODE } from 'ultrahtml'
import { emojiRegEx, getEmojiAttributes } from '../config/emojis'
export interface ContentParseOptions {
emojis?: Record<string, mastodon.v1.CustomEmoji>
hideEmojis?: boolean
mentions?: mastodon.v1.StatusMention[]
markdown?: boolean
replaceUnicodeEmoji?: boolean
astTransforms?: Transform[]
convertMentionLink?: boolean
collapseMentionLink?: boolean
status?: mastodon.v1.Status
inReplyToStatus?: mastodon.v1.Status
}
const sanitizerBasicClasses = filterClasses(/^h-\S*|p-\S*|u-\S*|dt-\S*|e-\S*|mention|hashtag|ellipsis|invisible$/u)
const sanitizer = sanitize({
// Allow basic elements as seen in https://github.com/mastodon/mastodon/blob/17f79082b098e05b68d6f0d38fabb3ac121879a9/lib/sanitize_ext/sanitize_config.rb
br: {},
p: {},
a: {
href: filterHref(),
class: sanitizerBasicClasses,
rel: set('nofollow noopener noreferrer'),
target: set('_blank'),
},
span: {
class: sanitizerBasicClasses,
},
// Allow elements potentially created for Markdown code blocks above
pre: {},
code: {
class: filterClasses(/^language-\w+$/),
},
// Other elements supported in glitch, as seen in
// https://github.com/glitch-soc/mastodon/blob/13227e1dafd308dfe1a3effc3379b766274809b3/lib/sanitize_ext/sanitize_config.rb#L75
abbr: {
title: keep,
},
del: {},
blockquote: {
cite: filterHref(),
},
b: {},
strong: {},
u: {},
sub: {},
sup: {},
i: {},
em: {},
h1: {},
h2: {},
h3: {},
h4: {},
h5: {},
ul: {},
ol: {
start: keep,
reversed: keep,
},
li: {
value: keep,
},
})
/**
* Parse raw HTML form Mastodon server to AST,
* with interop of custom emojis and inline Markdown syntax
* @param html The content to parse
* @param options The parsing options
*/
export function parseMastodonHTML(
html: string,
options: ContentParseOptions = {},
) {
const {
markdown = true,
replaceUnicodeEmoji = true,
convertMentionLink = false,
collapseMentionLink = false,
hideEmojis = false,
mentions,
status,
inReplyToStatus,
} = options
if (markdown) {
// Handle code blocks
html = html
/* eslint-disable regexp/no-super-linear-backtracking, regexp/no-misleading-capturing-group */
.replace(/>(```|~~~)(\w*)([\s\S]+?)\1/g, (_1, _2, lang: string, raw: string) => {
const code = htmlToText(raw)
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/`/g, '`')
const classes = lang ? ` class="language-${lang}"` : ''
return `><pre><code${classes}>${code}</code></pre>`
})
.replace(/`([^`\n]*)`/g, (_1, raw) => {
return raw ? `<code>${htmlToText(raw).replace(/</g, '<').replace(/>/g, '>')}</code>` : ''
})
}
// Always sanitize the raw HTML data *after* it has been modified
const transforms: Transform[] = [
sanitizer,
...options.astTransforms || [],
]
if (hideEmojis) {
transforms.push(removeUnicodeEmoji)
transforms.push(removeCustomEmoji(options.emojis ?? {}))
}
else {
if (replaceUnicodeEmoji)
transforms.push(transformUnicodeEmoji)
transforms.push(replaceCustomEmoji(options.emojis ?? {}))
}
if (markdown)
transforms.push(transformMarkdown)
if (mentions?.length)
transforms.push(createTransformNamedMentions(mentions))
if (convertMentionLink)
transforms.push(transformMentionLink)
transforms.push(transformParagraphs)
if (collapseMentionLink)
transforms.push(transformCollapseMentions(status, inReplyToStatus))
return transformSync(parse(html), transforms)
}
/**
* Converts raw HTML form Mastodon server to HTML for Tiptap editor
* @param html The content to parse
* @param customEmojis The custom emojis to use
*/
export function convertMastodonHTML(html: string, customEmojis: Record<string, mastodon.v1.CustomEmoji> = {}) {
const tree = parseMastodonHTML(html, {
emojis: customEmojis,
markdown: true,
convertMentionLink: true,
})
return render(tree)
}
export function sanitizeEmbeddedIframe(html: string): Node {
const transforms: Transform[] = [
sanitize({
iframe: {
src: (src) => {
if (typeof src !== 'string')
return undefined
const url = new URL(src)
return url.protocol === 'https:' ? src : undefined
},
allowfullscreen: set('true'),
},
}),
]
return transformSync(parse(html), transforms)
}
export function htmlToText(html: string) {
try {
const tree = parse(html)
return (tree.children as Node[]).map(n => treeToText(n)).join('').trim()
}
catch (err) {
console.error(err)
return ''
}
}
export function recursiveTreeToText(input: Node): string {
if (input && input.children && input.children.length > 0)
return input.children.map((n: Node) => recursiveTreeToText(n)).join('')
else
return treeToText(input)
}
const emojiIdNeedsWrappingRE = /^([\w\-])+$/
export function treeToText(input: Node): string {
let pre = ''
let body = ''
let post = ''
if (input.type === TEXT_NODE)
return decode(input.value)
if (input.name === 'br')
return '\n'
if (['p', 'pre'].includes(input.name))
pre = '\n'
if (input.attributes?.['data-type'] === 'mention') {
const acct = input.attributes['data-id']
if (acct)
return acct.startsWith('@') ? acct : `@${acct}`
}
if (input.name === 'code') {
if (input.parent?.name === 'pre') {
const lang = input.attributes.class?.replace('language-', '')
pre = `\`\`\`${lang || ''}\n`
post = '\n```'
}
else {
pre = '`'
post = '`'
}
}
else if (input.name === 'b' || input.name === 'strong') {
pre = '**'
post = '**'
}
else if (input.name === 'i' || input.name === 'em') {
pre = '*'
post = '*'
}
else if (input.name === 'del') {
pre = '~~'
post = '~~'
}
if ('children' in input)
body = (input.children as Node[]).map(n => treeToText(n)).join('')
if (input.name === 'img' || input.name === 'picture') {
if (input.attributes.class?.includes('custom-emoji')) {
const id = input.attributes['data-emoji-id'] ?? input.attributes.alt ?? input.attributes.title ?? 'unknown'
return id.match(emojiIdNeedsWrappingRE) ? `:${id}:` : id
}
if (input.attributes.class?.includes('iconify-emoji'))
return input.attributes.alt
}
return pre + body + post
}
// A tree transform function takes an ultrahtml Node object and returns
// new content that will replace the given node in the tree.
// Returning a null removes the node from the tree.
// Strings get converted to text nodes.
// The input node's children have been transformed before the node itself
// gets transformed.
type Transform = (node: Node, root: Node) => (Node | string)[] | Node | string | null
// Helpers for transforming (filtering, modifying, ...) a parsed HTML tree
// by running the given chain of transform functions one-by-one.
function transformSync(doc: Node, transforms: Transform[]) {
function visit(node: Node, transform: Transform, root: Node) {
if (Array.isArray(node.children)) {
const children = [] as (Node | string)[]
for (let i = 0; i < node.children.length; i++) {
const result = visit(node.children[i], transform, root)
if (Array.isArray(result))
children.push(...result)
else if (result)
children.push(result)
}
node.children = children.map((value) => {
if (typeof value === 'string')
return { type: TEXT_NODE, value, parent: node }
value.parent = node
return value
})
}
return transform(node, root)
}
for (const transform of transforms)
doc = visit(doc, transform, doc) as Node
return doc
}
// A tree transform for sanitizing elements & their attributes.
type AttrSanitizers = Record<string, (value: string | undefined) => string | undefined>
function sanitize(allowedElements: Record<string, AttrSanitizers>): Transform {
return (node) => {
if (node.type !== ELEMENT_NODE)
return node
if (!Object.prototype.hasOwnProperty.call(allowedElements, node.name))
return null
const attrSanitizers = allowedElements[node.name]
const attrs = {} as Record<string, string>
for (const [name, func] of Object.entries(attrSanitizers)) {
const value = func(node.attributes[name])
if (value !== undefined)
attrs[name] = value
}
node.attributes = attrs
return node
}
}
function filterClasses(allowed: RegExp) {
return (c: string | undefined) => {
if (!c)
return undefined
return c.split(/\s/g).filter(cls => allowed.test(cls)).join(' ')
}
}
function keep(value: string | undefined) {
return value
}
function set(value: string) {
return () => value
}
function filterHref() {
const LINK_PROTOCOLS = new Set([
'http:',
'https:',
'dat:',
'dweb:',
'ipfs:',
'ipns:',
'ssb:',
'gopher:',
'xmpp:',
'magnet:',
'gemini:',
])
return (href: string | undefined) => {
if (href === undefined)
return undefined
// Allow relative links
if (href.startsWith('/') || href.startsWith('.'))
return href
href = href.replace(/&/g, '&')
let url
try {
url = new URL(href)
}
catch (err) {
if (err instanceof TypeError)
return undefined
throw err
}
if (LINK_PROTOCOLS.has(url.protocol))
return url.toString()
return '#'
}
}
function removeUnicodeEmoji(node: Node) {
if (node.type !== TEXT_NODE)
return node
let start = 0
const matches = [] as (string | Node)[]
findAndReplaceEmojisInText(emojiRegEx, node.value, (match, result) => {
matches.push(result.slice(start).trimEnd())
start = result.length + match.match.length
return undefined
})
if (matches.length === 0)
return node
matches.push(node.value.slice(start))
return matches.filter(Boolean)
}
function transformUnicodeEmoji(node: Node) {
if (node.type !== TEXT_NODE)
return node
let start = 0
const matches = [] as (string | Node)[]
findAndReplaceEmojisInText(emojiRegEx, node.value, (match, result) => {
const attrs = getEmojiAttributes(match)
matches.push(result.slice(start))
matches.push(h('img', { src: attrs.src, alt: attrs.alt, class: attrs.class }))
start = result.length + match.match.length
return undefined
})
if (matches.length === 0)
return node
matches.push(node.value.slice(start))
return matches.filter(Boolean)
}
function removeCustomEmoji(customEmojis: Record<string, mastodon.v1.CustomEmoji>): Transform {
return (node) => {
if (node.type !== TEXT_NODE)
return node
const split = node.value.split(/\s?:([\w-]+):/g)
if (split.length === 1)
return node
return split.map((name, i) => {
if (i % 2 === 0)
return name
const emoji = customEmojis[name] as mastodon.v1.CustomEmoji
if (!emoji)
return `:${name}:`
return ''
}).filter(Boolean)
}
}
function replaceCustomEmoji(customEmojis: Record<string, mastodon.v1.CustomEmoji>): Transform {
return (node) => {
if (node.type !== TEXT_NODE)
return node
const split = node.value.split(/:([\w-]+):/g)
if (split.length === 1)
return node
return split.map((name, i) => {
if (i % 2 === 0)
return name
const emoji = customEmojis[name] as mastodon.v1.CustomEmoji
if (!emoji)
return `:${name}:`
return h(
'picture',
{
'alt': `:${name}:`,
'class': 'custom-emoji',
'data-emoji-id': name,
},
[
h(
'source',
{
srcset: emoji.staticUrl,
media: '(prefers-reduced-motion: reduce)',
},
),
h(
'img',
{
src: emoji.url,
alt: `:${name}:`,
},
),
],
)
}).filter(Boolean)
}
}
const _markdownReplacements: [RegExp, (c: (string | Node)[]) => Node][] = [
[/\*\*\*(.*?)\*\*\*/g, ([c]) => h('b', null, [h('em', null, c)])],
[/\*\*(.*?)\*\*/g, c => h('b', null, c)],
[/\*(.*?)\*/g, c => h('em', null, c)],
[/~~(.*?)~~/g, c => h('del', null, c)],
[/`([^`]+)`/g, c => h('code', null, c)],
// transform @username@twitter.com as links
[/\B@(\w+)@twitter\.com\b/gi, c => h('a', { href: `https://twitter.com/${c}`, target: '_blank', rel: 'nofollow noopener noreferrer', class: 'mention external' }, `@${c}@twitter.com`)],
]
function _markdownProcess(value: string) {
const results = [] as (string | Node)[]
let start = 0
while (true) {
let found: {
match: RegExpMatchArray
replacer: (c: (string | Node)[]) => Node
} | undefined
for (const [re, replacer] of _markdownReplacements) {
re.lastIndex = start
const match = re.exec(value)
if (match) {
if (!found || match.index < found.match.index!)
found = { match, replacer }
}
}
if (!found)
break
results.push(value.slice(start, found.match.index))
results.push(found.replacer(_markdownProcess(found.match[1])))
start = found.match.index! + found.match[0].length
}
results.push(value.slice(start))
return results.filter(Boolean)
}
function transformMarkdown(node: Node) {
if (node.type !== TEXT_NODE)
return node
return _markdownProcess(node.value)
}
function addBdiParagraphs(node: Node) {
if (node.name === 'p' && !('dir' in node.attributes) && node.children?.length && node.children.length > 1)
node.attributes.dir = 'auto'
return node
}
function transformParagraphs(node: Node): Node | Node[] {
// Add bdi to paragraphs
addBdiParagraphs(node)
// For top level paragraphs, inject an empty <p> to preserve status paragraphs in our editor (except for the last one)
if (node.parent?.type === DOCUMENT_NODE && node.name === 'p' && node.parent.children.at(-1) !== node)
return [node, h('p')]
return node
}
function isMention(node: Node) {
const child = node.children?.length === 1 ? node.children[0] : null
return Boolean(child?.name === 'a' && child.attributes.class?.includes('mention'))
}
function isSpacing(node: Node) {
return node.type === TEXT_NODE && !node.value.trim()
}
// Extract the username from a known mention node
function getMentionHandle(node: Node): string | undefined {
return hrefToHandle(node.children?.[0].attributes.href) ?? node.children?.[0]?.children?.[0]?.attributes?.['data-id']
}
function transformCollapseMentions(status?: mastodon.v1.Status, inReplyToStatus?: mastodon.v1.Status): Transform {
let processed = false
return (node: Node, root: Node): Node | Node[] => {
if (processed || node.parent !== root || !node.children)
return node
const mentions: (Node | undefined)[] = []
const children = node.children as Node[]
let trimContentStart: (() => void) | undefined
for (const child of children) {
// mention
if (isMention(child)) {
mentions.push(child)
}
// spaces in between
else if (isSpacing(child)) {
mentions.push(child)
}
// other content, stop collapsing
else {
if (child.type === TEXT_NODE) {
trimContentStart = () => {
child.value = child.value.trimStart()
}
}
// remove <br> after mention
if (child.name === 'br')
mentions.push(undefined)
break
}
}
processed = true
if (mentions.length === 0)
return node
let mentionsCount = 0
let contextualMentionsCount = 0
let removeNextSpacing = false
const contextualMentions = mentions.filter((mention) => {
if (!mention)
return false
if (removeNextSpacing && isSpacing(mention)) {
removeNextSpacing = false
return false
}
if (isMention(mention)) {
mentionsCount++
if (inReplyToStatus) {
const mentionHandle = getMentionHandle(mention)
if (inReplyToStatus.account.acct === mentionHandle || inReplyToStatus.mentions.some(m => m.acct === mentionHandle)) {
removeNextSpacing = true
return false
}
}
contextualMentionsCount++
}
return true
}) as Node[]
// We have a special case for single mentions that are part of a reply.
// We already have the replying to badge in this case or the status is connected to the previous one.
// This is needed because the status doesn't include the in Reply to handle, only the account id.
// But this covers the majority of cases.
const showMentions = !(contextualMentionsCount === 0 || (mentionsCount === 1 && status?.inReplyToAccountId))
const grouped = contextualMentionsCount > 2
if (!showMentions || grouped)
trimContentStart?.()
const contextualChildren = children.slice(mentions.length)
const mentionNodes = showMentions ? (grouped ? [h('mention-group', null, ...contextualMentions)] : contextualMentions) : []
return {
...node,
children: [...mentionNodes, ...contextualChildren],
}
}
}
function hrefToHandle(href: string): string | undefined {
const matchUser = href.match(UserLinkRE)
if (matchUser) {
const [, server, username] = matchUser
return `${username}@${server.replace(/(.+\.)(.+\..+)/, '$2')}`
}
}
function transformMentionLink(node: Node): string | Node | (string | Node)[] | null {
if (node.name === 'a' && node.attributes.class?.includes('mention')) {
const href = node.attributes.href
if (href) {
const handle = hrefToHandle(href)
if (handle) {
// convert to Tiptap mention node
return h('span', { 'data-type': 'mention', 'data-id': handle }, handle)
}
}
}
return node
}
function createTransformNamedMentions(mentions: mastodon.v1.StatusMention[]) {
return (node: Node): string | Node | (string | Node)[] | null => {
if (node.name === 'a' && node.attributes.class?.includes('mention')) {
const href = node.attributes.href
const mention = href && mentions.find(m => m.url === href)
if (mention) {
node.attributes.href = `/${currentServer.value}/@${mention.acct}`
node.children = [h('span', { 'data-type': 'mention', 'data-id': mention.acct }, `@${mention.username}`)]
return node
}
}
return node
}
}