2022-12-13 23:03:30 +09:00
|
|
|
import { TEXT_NODE } from 'ultrahtml'
|
|
|
|
import type { Node } from 'ultrahtml'
|
|
|
|
import { Fragment, h, isVNode } from 'vue'
|
|
|
|
import type { VNode } from 'vue'
|
|
|
|
import { RouterLink } from 'vue-router'
|
2023-01-07 18:31:48 +09:00
|
|
|
import type { ContentParseOptions } from './content-parse'
|
2022-12-20 09:23:06 +09:00
|
|
|
import { decodeHtml, parseMastodonHTML } from './content-parse'
|
2022-12-13 23:03:30 +09:00
|
|
|
import ContentCode from '~/components/content/ContentCode.vue'
|
|
|
|
import AccountHoverWrapper from '~/components/account/AccountHoverWrapper.vue'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Raw HTML to VNodes
|
|
|
|
*/
|
|
|
|
export function contentToVNode(
|
|
|
|
content: string,
|
2023-01-07 18:31:48 +09:00
|
|
|
options?: ContentParseOptions,
|
2022-12-13 23:03:30 +09:00
|
|
|
): VNode {
|
2023-01-07 18:31:48 +09:00
|
|
|
const tree = parseMastodonHTML(content, options)
|
2022-12-13 23:03:30 +09:00
|
|
|
return h(Fragment, (tree.children as Node[]).map(n => treeToVNode(n)))
|
|
|
|
}
|
|
|
|
|
|
|
|
export function nodeToVNode(node: Node): VNode | string | null {
|
|
|
|
if (node.type === TEXT_NODE)
|
|
|
|
return node.value
|
|
|
|
|
|
|
|
if ('children' in node) {
|
|
|
|
if (node.name === 'a' && (node.attributes.href?.startsWith('/') || node.attributes.href?.startsWith('.'))) {
|
|
|
|
node.attributes.to = node.attributes.href
|
2022-12-27 03:39:29 +09:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
const { href, target, ...attrs } = node.attributes
|
2022-12-13 23:03:30 +09:00
|
|
|
return h(
|
|
|
|
RouterLink as any,
|
2022-12-27 03:39:29 +09:00
|
|
|
attrs,
|
2022-12-13 23:03:30 +09:00
|
|
|
() => node.children.map(treeToVNode),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return h(
|
|
|
|
node.name,
|
|
|
|
node.attributes,
|
|
|
|
node.children.map(treeToVNode),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
2022-12-20 09:23:06 +09:00
|
|
|
|
2022-12-13 23:03:30 +09:00
|
|
|
function treeToVNode(
|
|
|
|
input: Node,
|
|
|
|
): VNode | string | null {
|
|
|
|
if (input.type === TEXT_NODE)
|
2022-12-20 09:23:06 +09:00
|
|
|
return decodeHtml(input.value)
|
2022-12-13 23:03:30 +09:00
|
|
|
|
|
|
|
if ('children' in input) {
|
|
|
|
const node = handleNode(input)
|
|
|
|
if (node == null)
|
|
|
|
return null
|
|
|
|
if (isVNode(node))
|
|
|
|
return node
|
|
|
|
return nodeToVNode(node)
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleMention(el: Node) {
|
|
|
|
// Redirect mentions to the user page
|
|
|
|
if (el.name === 'a' && el.attributes.class?.includes('mention')) {
|
|
|
|
const href = el.attributes.href
|
|
|
|
if (href) {
|
|
|
|
const matchUser = href.match(UserLinkRE)
|
|
|
|
if (matchUser) {
|
|
|
|
const [, server, username] = matchUser
|
|
|
|
const handle = `@${username}@${server.replace(/(.+\.)(.+\..+)/, '$2')}`
|
|
|
|
el.attributes.href = `/${server}/@${username}`
|
|
|
|
return h(AccountHoverWrapper, { handle, class: 'inline-block' }, () => nodeToVNode(el))
|
|
|
|
}
|
|
|
|
const matchTag = href.match(TagLinkRE)
|
|
|
|
if (matchTag) {
|
|
|
|
const [, , name] = matchTag
|
|
|
|
el.attributes.href = `/${currentServer.value}/tags/${name}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
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.children[0] ? treeToText(codeEl.children[0]) : ''
|
|
|
|
return h(ContentCode, { lang, code: encodeURIComponent(code) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleNode(el: Node) {
|
|
|
|
return handleCodeBlock(el) || handleMention(el) || el
|
|
|
|
}
|