fix: revert removal of parsing rich content in display name

This reverts commit 5be48b102f.
This commit is contained in:
Daniel Roe 2022-12-17 16:06:45 +00:00
parent 5be48b102f
commit b545efeacc
No known key found for this signature in database
GPG key ID: 22D5008E4F5D9B55
4 changed files with 34 additions and 46 deletions

View file

@ -81,7 +81,6 @@ watchEffect(() => {
font-bold sm:text-2xl text-xl font-bold sm:text-2xl text-xl
:content="getDisplayName(account, { rich: true })" :content="getDisplayName(account, { rich: true })"
:emojis="account.emojis" :emojis="account.emojis"
:markdown="false"
/> />
<AccountBotIndicator v-if="account.bot" /> <AccountBotIndicator v-if="account.bot" />
</div> </div>

View file

@ -5,17 +5,13 @@ defineOptions({
name: 'ContentRich', name: 'ContentRich',
}) })
const { content, emojis, markdown = true } = defineProps<{ const props = defineProps<{
content: string content: string
markdown?: boolean
emojis?: Emoji[] emojis?: Emoji[]
}>() }>()
export default () => h( export default () => h(
'span', 'span',
{ class: 'content-rich' }, { class: 'content-rich' },
contentToVNode(content, { contentToVNode(props.content, emojisArrayToObject(props.emojis || [])),
emojis: emojisArrayToObject(emojis || []),
markdown,
}),
) )

View file

@ -12,7 +12,7 @@ function decode(text: string) {
* Parse raw HTML form Mastodon server to AST, * Parse raw HTML form Mastodon server to AST,
* with interop of custom emojis and inline Markdown syntax * with interop of custom emojis and inline Markdown syntax
*/ */
export function parseMastodonHTML(html: string, customEmojis: Record<string, Emoji> = {}, markdown = true) { export function parseMastodonHTML(html: string, customEmojis: Record<string, Emoji> = {}) {
let processed = html let processed = html
// custom emojis // custom emojis
.replace(/:([\w-]+?):/g, (_, name) => { .replace(/:([\w-]+?):/g, (_, name) => {
@ -21,49 +21,45 @@ export function parseMastodonHTML(html: string, customEmojis: Record<string, Emo
return `<img src="${emoji.url}" alt=":${name}:" class="custom-emoji" data-emoji-id="${name}" />` return `<img src="${emoji.url}" alt=":${name}:" class="custom-emoji" data-emoji-id="${name}" />`
return `:${name}:` return `:${name}:`
}) })
if (markdown) {
// handle code blocks // handle code blocks
processed = processed .replace(/>(```|~~~)(\w*)([\s\S]+?)\1/g, (_1, _2, lang, raw) => {
.replace(/>(```|~~~)(\w*)([\s\S]+?)\1/g, (_1, _2, lang, raw) => { const code = htmlToText(raw)
const code = htmlToText(raw) const classes = lang ? ` class="language-${lang}"` : ''
const classes = lang ? ` class="language-${lang}"` : '' return `><pre><code${classes}>${code}</code></pre>`
return `><pre><code${classes}>${code}</code></pre>` })
})
walkSync(parse(processed), (node) => { walkSync(parse(processed), (node) => {
if (node.type !== TEXT_NODE) if (node.type !== TEXT_NODE)
return return
const replacements = [ const replacements = [
[/\*\*\*(.*?)\*\*\*/g, '<b><em>$1</em></b>'], [/\*\*\*(.*?)\*\*\*/g, '<b><em>$1</em></b>'],
[/\*\*(.*?)\*\*/g, '<b>$1</b>'], [/\*\*(.*?)\*\*/g, '<b>$1</b>'],
[/\*(.*?)\*/g, '<em>$1</em>'], [/\*(.*?)\*/g, '<em>$1</em>'],
[/~~(.*?)~~/g, '<del>$1</del>'], [/~~(.*?)~~/g, '<del>$1</del>'],
[/`([^`]+?)`/g, '<code>$1</code>'], [/`([^`]+?)`/g, '<code>$1</code>'],
[/&[^;]+;/g, (val: string) => decode(val)], [/&[^;]+;/g, (val: string) => decode(val)],
] as any ] as any
for (const [re, replacement] of replacements) { for (const [re, replacement] of replacements) {
for (const match of node.value.matchAll(re)) { for (const match of node.value.matchAll(re)) {
if (node.loc) { if (node.loc) {
const start = match.index! + node.loc[0].start const start = match.index! + node.loc[0].start
const end = start + match[0].length + node.loc[0].start const end = start + match[0].length + node.loc[0].start
processed = processed.slice(0, start) + match[0].replace(re, replacement) + processed.slice(end) processed = processed.slice(0, start) + match[0].replace(re, replacement) + processed.slice(end)
} }
else { else {
processed = processed.replace(match[0], match[0].replace(re, replacement)) processed = processed.replace(match[0], match[0].replace(re, replacement))
}
} }
} }
}) }
} })
return parse(processed) return parse(processed)
} }
export function convertMastodonHTML(html: string, customEmojis: Record<string, Emoji> = {}) { export async function convertMastodonHTML(html: string, customEmojis: Record<string, Emoji> = {}) {
const tree = parseMastodonHTML(html, customEmojis) const tree = parseMastodonHTML(html, customEmojis)
return render(tree) return await render(tree)
} }
export function htmlToText(html: string) { export function htmlToText(html: string) {

View file

@ -13,12 +13,9 @@ import AccountHoverWrapper from '~/components/account/AccountHoverWrapper.vue'
*/ */
export function contentToVNode( export function contentToVNode(
content: string, content: string,
{ emojis = {}, markdown = true }: { customEmojis: Record<string, Emoji> = {},
emojis?: Record<string, Emoji>
markdown?: boolean
} = {},
): VNode { ): VNode {
const tree = parseMastodonHTML(content, emojis, markdown) const tree = parseMastodonHTML(content, customEmojis)
return h(Fragment, (tree.children as Node[]).map(n => treeToVNode(n))) return h(Fragment, (tree.children as Node[]).map(n => treeToVNode(n)))
} }