fix: don't parse rich content in display name (#449)
Co-authored-by: 三咲智子 <sxzz@sxzz.moe>
This commit is contained in:
parent
9395b7031e
commit
7887629954
|
@ -81,6 +81,7 @@ 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>
|
||||||
|
|
|
@ -5,13 +5,17 @@ defineOptions({
|
||||||
name: 'ContentRich',
|
name: 'ContentRich',
|
||||||
})
|
})
|
||||||
|
|
||||||
const props = defineProps<{
|
const { content, emojis, markdown = true } = 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(props.content, emojisArrayToObject(props.emojis || [])),
|
contentToVNode(content, {
|
||||||
|
emojis: emojisArrayToObject(emojis || []),
|
||||||
|
markdown,
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
|
|
|
@ -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> = {}) {
|
export function parseMastodonHTML(html: string, customEmojis: Record<string, Emoji> = {}, markdown = true) {
|
||||||
let processed = html
|
let processed = html
|
||||||
// custom emojis
|
// custom emojis
|
||||||
.replace(/:([\w-]+?):/g, (_, name) => {
|
.replace(/:([\w-]+?):/g, (_, name) => {
|
||||||
|
@ -21,7 +21,10 @@ 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}"` : ''
|
||||||
|
@ -53,13 +56,14 @@ export function parseMastodonHTML(html: string, customEmojis: Record<string, Emo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return parse(processed)
|
return parse(processed)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function convertMastodonHTML(html: string, customEmojis: Record<string, Emoji> = {}) {
|
export function convertMastodonHTML(html: string, customEmojis: Record<string, Emoji> = {}) {
|
||||||
const tree = parseMastodonHTML(html, customEmojis)
|
const tree = parseMastodonHTML(html, customEmojis)
|
||||||
return await render(tree)
|
return render(tree)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function htmlToText(html: string) {
|
export function htmlToText(html: string) {
|
||||||
|
|
|
@ -13,9 +13,12 @@ import AccountHoverWrapper from '~/components/account/AccountHoverWrapper.vue'
|
||||||
*/
|
*/
|
||||||
export function contentToVNode(
|
export function contentToVNode(
|
||||||
content: string,
|
content: string,
|
||||||
customEmojis: Record<string, Emoji> = {},
|
{ emojis = {}, markdown = true }: {
|
||||||
|
emojis?: Record<string, Emoji>
|
||||||
|
markdown?: boolean
|
||||||
|
} = {},
|
||||||
): VNode {
|
): VNode {
|
||||||
const tree = parseMastodonHTML(content, customEmojis)
|
const tree = parseMastodonHTML(content, emojis, markdown)
|
||||||
return h(Fragment, (tree.children as Node[]).map(n => treeToVNode(n)))
|
return h(Fragment, (tree.children as Node[]).map(n => treeToVNode(n)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -55,7 +55,7 @@ describe('content-rich', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
async function render(content: string, emojis?: Record<string, Emoji>) {
|
async function render(content: string, emojis?: Record<string, Emoji>) {
|
||||||
const vnode = contentToVNode(content, emojis)
|
const vnode = contentToVNode(content, { emojis })
|
||||||
const html = (await renderToString(vnode))
|
const html = (await renderToString(vnode))
|
||||||
.replace(/<!--[\[\]]-->/g, '')
|
.replace(/<!--[\[\]]-->/g, '')
|
||||||
let formatted = ''
|
let formatted = ''
|
||||||
|
|
Loading…
Reference in a new issue