From b545efeacc243e0ea532b81c3ac2180b0f80ae0b Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 17 Dec 2022 16:06:45 +0000 Subject: [PATCH] fix: revert removal of parsing rich content in display name This reverts commit 5be48b102ff005f2e5a8f753893c98d67829e268. --- components/account/AccountHeader.vue | 1 - components/content/ContentRich.setup.ts | 8 +--- composables/content-parse.ts | 64 ++++++++++++------------- composables/content-render.ts | 7 +-- 4 files changed, 34 insertions(+), 46 deletions(-) diff --git a/components/account/AccountHeader.vue b/components/account/AccountHeader.vue index 24013e03..68e054b0 100644 --- a/components/account/AccountHeader.vue +++ b/components/account/AccountHeader.vue @@ -81,7 +81,6 @@ watchEffect(() => { font-bold sm:text-2xl text-xl :content="getDisplayName(account, { rich: true })" :emojis="account.emojis" - :markdown="false" /> diff --git a/components/content/ContentRich.setup.ts b/components/content/ContentRich.setup.ts index 497e4add..88c1bfdb 100644 --- a/components/content/ContentRich.setup.ts +++ b/components/content/ContentRich.setup.ts @@ -5,17 +5,13 @@ defineOptions({ name: 'ContentRich', }) -const { content, emojis, markdown = true } = defineProps<{ +const props = defineProps<{ content: string - markdown?: boolean emojis?: Emoji[] }>() export default () => h( 'span', { class: 'content-rich' }, - contentToVNode(content, { - emojis: emojisArrayToObject(emojis || []), - markdown, - }), + contentToVNode(props.content, emojisArrayToObject(props.emojis || [])), ) diff --git a/composables/content-parse.ts b/composables/content-parse.ts index 020969e7..1075e8f7 100644 --- a/composables/content-parse.ts +++ b/composables/content-parse.ts @@ -12,7 +12,7 @@ function decode(text: string) { * Parse raw HTML form Mastodon server to AST, * with interop of custom emojis and inline Markdown syntax */ -export function parseMastodonHTML(html: string, customEmojis: Record = {}, markdown = true) { +export function parseMastodonHTML(html: string, customEmojis: Record = {}) { let processed = html // custom emojis .replace(/:([\w-]+?):/g, (_, name) => { @@ -21,49 +21,45 @@ export function parseMastodonHTML(html: string, customEmojis: Record` return `:${name}:` }) - - if (markdown) { // handle code blocks - processed = processed - .replace(/>(```|~~~)(\w*)([\s\S]+?)\1/g, (_1, _2, lang, raw) => { - const code = htmlToText(raw) - const classes = lang ? ` class="language-${lang}"` : '' - return `>
${code}
` - }) + .replace(/>(```|~~~)(\w*)([\s\S]+?)\1/g, (_1, _2, lang, raw) => { + const code = htmlToText(raw) + const classes = lang ? ` class="language-${lang}"` : '' + return `>
${code}
` + }) - walkSync(parse(processed), (node) => { - if (node.type !== TEXT_NODE) - return - const replacements = [ - [/\*\*\*(.*?)\*\*\*/g, '$1'], - [/\*\*(.*?)\*\*/g, '$1'], - [/\*(.*?)\*/g, '$1'], - [/~~(.*?)~~/g, '$1'], - [/`([^`]+?)`/g, '$1'], - [/&[^;]+;/g, (val: string) => decode(val)], - ] as any + walkSync(parse(processed), (node) => { + if (node.type !== TEXT_NODE) + return + const replacements = [ + [/\*\*\*(.*?)\*\*\*/g, '$1'], + [/\*\*(.*?)\*\*/g, '$1'], + [/\*(.*?)\*/g, '$1'], + [/~~(.*?)~~/g, '$1'], + [/`([^`]+?)`/g, '$1'], + [/&[^;]+;/g, (val: string) => decode(val)], + ] as any - for (const [re, replacement] of replacements) { - for (const match of node.value.matchAll(re)) { - if (node.loc) { - const start = match.index! + 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) - } - else { - processed = processed.replace(match[0], match[0].replace(re, replacement)) - } + for (const [re, replacement] of replacements) { + for (const match of node.value.matchAll(re)) { + if (node.loc) { + const start = match.index! + 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) + } + else { + processed = processed.replace(match[0], match[0].replace(re, replacement)) } } - }) - } + } + }) return parse(processed) } -export function convertMastodonHTML(html: string, customEmojis: Record = {}) { +export async function convertMastodonHTML(html: string, customEmojis: Record = {}) { const tree = parseMastodonHTML(html, customEmojis) - return render(tree) + return await render(tree) } export function htmlToText(html: string) { diff --git a/composables/content-render.ts b/composables/content-render.ts index b0a6ef61..337a851f 100644 --- a/composables/content-render.ts +++ b/composables/content-render.ts @@ -13,12 +13,9 @@ import AccountHoverWrapper from '~/components/account/AccountHoverWrapper.vue' */ export function contentToVNode( content: string, - { emojis = {}, markdown = true }: { - emojis?: Record - markdown?: boolean - } = {}, + customEmojis: Record = {}, ): VNode { - const tree = parseMastodonHTML(content, emojis, markdown) + const tree = parseMastodonHTML(content, customEmojis) return h(Fragment, (tree.children as Node[]).map(n => treeToVNode(n))) }