diff --git a/components/status/StatusActionsMore.vue b/components/status/StatusActionsMore.vue
index 7cf740ba..21be6eb9 100644
--- a/components/status/StatusActionsMore.vue
+++ b/components/status/StatusActionsMore.vue
@@ -68,7 +68,7 @@ const deleteAndRedraft = async () => {
}
const { text } = await useMasto().statuses.remove(status.id)
- openPublishDialog('dialog', getDraftFromStatus(status, text), true)
+ openPublishDialog('dialog', await getDraftFromStatus(status, text), true)
}
const reply = () => {
@@ -81,9 +81,9 @@ const reply = () => {
}
}
-function editStatus() {
+async function editStatus() {
openPublishDialog(`edit-${status.id}`, {
- ...getDraftFromStatus(status),
+ ...await getDraftFromStatus(status),
editingStatus: status,
})
}
diff --git a/composables/content.ts b/composables/content.ts
index 4d44ad59..b8b81ed3 100644
--- a/composables/content.ts
+++ b/composables/content.ts
@@ -1,48 +1,45 @@
import type { Emoji } from 'masto'
-import type { DefaultTreeAdapterMap } from 'parse5'
-import { parseFragment, serialize } from 'parse5'
+import type { Node } from 'ultrahtml'
+import { TEXT_NODE, parse, render, walkSync } from 'ultrahtml'
import type { VNode } from 'vue'
import { Fragment, h, isVNode } from 'vue'
import { RouterLink } from 'vue-router'
import ContentCode from '~/components/content/ContentCode.vue'
import AccountHoverWrapper from '~/components/account/AccountHoverWrapper.vue'
-type Node = DefaultTreeAdapterMap['childNode']
-type Element = DefaultTreeAdapterMap['element']
-
-function handleMention(el: Element) {
+function handleMention(el: Node) {
// Redirect mentions to the user page
- if (el.tagName === 'a' && el.attrs.find(i => i.name === 'class' && i.value.includes('mention'))) {
- const href = el.attrs.find(i => i.name === 'href')
+ if (el.name === 'a' && el.attributes.class?.includes('mention')) {
+ const href = el.attributes.href
if (href) {
- const matchUser = href.value.match(UserLinkRE)
+ const matchUser = href.match(UserLinkRE)
if (matchUser) {
const [, server, username] = matchUser
const handle = `@${username}@${server.replace(/(.+\.)(.+\..+)/, '$2')}`
- href.value = `/${server}/@${username}`
+ el.attributes.href = `/${server}/@${username}`
return h(AccountHoverWrapper, { handle, class: 'inline-block' }, () => nodeToVNode(el))
}
- const matchTag = href.value.match(TagLinkRE)
+ const matchTag = href.match(TagLinkRE)
if (matchTag) {
const [, , name] = matchTag
- href.value = `/${currentServer.value}/tags/${name}`
+ el.attributes.href = `/${currentServer.value}/tags/${name}`
}
}
}
return undefined
}
-function handleCodeBlock(el: Element) {
- if (el.tagName === 'pre' && el.childNodes[0]?.nodeName === 'code') {
- const codeEl = el.childNodes[0] as Element
- const classes = codeEl.attrs.find(i => i.name === 'class')?.value
+function handleCodeBlock(el: Node) {
+ if (el.name === 'pre' && el.children[0]?.name === 'code') {
+ const codeEl = el.children[0] as Node
+ const classes = codeEl.attributes.class as string
const lang = classes?.split(/\s/g).find(i => i.startsWith('language-'))?.replace('language-', '')
- const code = codeEl.childNodes[0] ? treeToText(codeEl.childNodes[0]) : ''
+ const code = codeEl.children[0] ? treeToText(codeEl.children[0]) : ''
return h(ContentCode, { lang, code: encodeURIComponent(code) })
}
}
-function handleNode(el: Element) {
+function handleNode(el: Node) {
return handleCodeBlock(el) || handleMention(el) || el
}
@@ -51,7 +48,7 @@ function handleNode(el: Element) {
* with interop of custom emojis and inline Markdown syntax
*/
export function parseMastodonHTML(html: string, customEmojis: Record Testing code block Testing code block
`
})
- const tree = parseFragment(processed)
-
- function walk(node: Node) {
- if ('childNodes' in node)
- node.childNodes = node.childNodes.flatMap(n => walk(n))
-
- if (node.nodeName === '#text') {
- // @ts-expect-error casing
- const text = node.value as string
- const converted = text
- .replace(/\*\*\*(.*?)\*\*\*/g, '$1')
- .replace(/\*\*(.*?)\*\*/g, '$1')
- .replace(/\*(.*?)\*/g, '$1')
- .replace(/~~(.*?)~~/g, '${code}
$1')
- .replace(/`([^`]+?)`/g, '$1
')
-
- if (converted !== text)
- return parseFragment(converted).childNodes
+ walkSync(parse(processed), (node) => {
+ if (node.type !== TEXT_NODE)
+ return
+ const replacements = [
+ [/\*\*\*(.*?)\*\*\*/g, '$1'],
+ [/\*\*(.*?)\*\*/g, '$1'],
+ [/\*(.*?)\*/g, '$1'],
+ [/~~(.*?)~~/g, '$1'],
+ [/`([^`]+?)`/g, '$1
'],
+ ] as const
+ 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 [node]
- }
+ })
- tree.childNodes = tree.childNodes.flatMap(n => walk(n))
-
- return tree
+ return parse(processed)
}
-export function convertMastodonHTML(html: string, customEmojis: Record
-import { useMouse, usePreferredDark } from '@vueuse/core'
-
+"
import { useMouse, usePreferredDark } from '@vueuse/core'
// tracks mouse position
const { x, y } = useMouse()
// is the user prefers dark theme
-const isDark = usePreferredDark()
-
-"
+const isDark = usePreferredDark()
const a = hello
-const a = hello
-
"
`;
-exports[`content-rich > code frame empty 1`] = `
-"
-
-hello world-
hello world
Testing code block
- -import { useMouse, usePreferredDark } from '@vueuse/core'
-
+"Testing code block
import { useMouse, usePreferredDark } from '@vueuse/core'
// tracks mouse position
const { x, y } = useMouse()
// is the user prefers dark theme
-const isDark = usePreferredDark()
-
-"
+const isDark = usePreferredDark()
"
`;
exports[`html-parse > code frame > text 1`] = `
"Testing code block
\`\`\`ts
-import { useMouse, usePreferredDark } from '@vueuse/core'
-
+import { useMouse, usePreferredDark } from '@vueuse/core'
// tracks mouse position
const { x, y } = useMouse()
// is the user prefers dark theme
@@ -34,9 +28,8 @@ exports[`html-parse > code frame 2 > html 1`] = `
>
Testingconst a = hello
-const a = hello
-
"
`;
@@ -65,13 +58,7 @@ exports[`html-parse > empty > html 1`] = `""`;
exports[`html-parse > empty > text 1`] = `""`;
-exports[`html-parse > inline markdown > html 1`] = `
-"text code
bold italic del
code block
-
-"
-`;
+exports[`html-parse > inline markdown > html 1`] = `"text code
bold italic del
code block
"`;
exports[`html-parse > inline markdown > text 1`] = `
"text \`code\` **bold** *italic* ~~del~~
diff --git a/tests/html-parse.test.ts b/tests/html-parse.test.ts
index e4703919..6308a94c 100644
--- a/tests/html-parse.test.ts
+++ b/tests/html-parse.test.ts
@@ -1,7 +1,7 @@
import type { Emoji } from 'masto'
import { describe, expect, it } from 'vitest'
import { format } from 'prettier'
-import { serialize } from 'parse5'
+import { render as renderTree } from 'ultrahtml'
import { parseMastodonHTML, treeToText } from '~/composables/content'
describe('html-parse', () => {
@@ -53,9 +53,9 @@ describe('html-parse', () => {
async function render(input: string, emojis?: Record