Skip to content

Commit 2df2fef

Browse files
committed
fix(tiptap): mention transform, close #521
1 parent c7f4b33 commit 2df2fef

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

composables/content-parse.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface ContentParseOptions {
1010
markdown?: boolean
1111
replaceUnicodeEmoji?: boolean
1212
astTransforms?: Transform[]
13+
convertMentionLink?: boolean
1314
}
1415

1516
const sanitizerBasicClasses = filterClasses(/^(h-\S*|p-\S*|u-\S*|dt-\S*|e-\S*|mention|hashtag|ellipsis|invisible)$/u)
@@ -53,6 +54,7 @@ export function parseMastodonHTML(
5354
const {
5455
markdown = true,
5556
replaceUnicodeEmoji = true,
57+
convertMentionLink = false,
5658
} = options
5759

5860
if (markdown) {
@@ -77,6 +79,9 @@ export function parseMastodonHTML(
7779
if (markdown)
7880
transforms.push(transformMarkdown)
7981

82+
if (convertMentionLink)
83+
transforms.push(transformMentionLink)
84+
8085
transforms.push(replaceCustomEmoji(options.emojis || {}))
8186

8287
transforms.push(transformParagraphs)
@@ -92,6 +97,7 @@ export function convertMastodonHTML(html: string, customEmojis: Record<string, m
9297
emojis: customEmojis,
9398
markdown: true,
9499
replaceUnicodeEmoji: false,
100+
convertMentionLink: true,
95101
})
96102
return render(tree)
97103
}
@@ -360,3 +366,19 @@ function transformParagraphs(node: Node): Node | Node[] {
360366
return [node, h('p')]
361367
return node
362368
}
369+
370+
function transformMentionLink(node: Node): string | Node | (string | Node)[] | null {
371+
if (node.name === 'a' && node.attributes.class?.includes('mention')) {
372+
const href = node.attributes.href
373+
if (href) {
374+
const matchUser = href.match(UserLinkRE)
375+
if (matchUser) {
376+
const [, server, username] = matchUser
377+
const handle = `${username}@${server.replace(/(.+\.)(.+\..+)/, '$2')}`
378+
// convert to TipTap mention node
379+
return h('span', { 'data-type': 'mention', 'data-id': handle }, handle)
380+
}
381+
}
382+
}
383+
return node
384+
}

0 commit comments

Comments
 (0)