2024-01-04 19:51:32 +00:00
|
|
|
import { ELEMENT_NODE, TEXT_NODE } from 'ultrahtml'
|
|
|
|
import type { ElementNode, Node } from 'ultrahtml'
|
2022-12-13 14:03:30 +00:00
|
|
|
import { Fragment, h, isVNode } from 'vue'
|
|
|
|
import type { VNode } from 'vue'
|
|
|
|
import { RouterLink } from 'vue-router'
|
2023-01-09 22:08:42 +00:00
|
|
|
import { decode } from 'tiny-decode'
|
2023-01-07 09:31:48 +00:00
|
|
|
import type { ContentParseOptions } from './content-parse'
|
2023-01-09 22:08:42 +00:00
|
|
|
import { parseMastodonHTML } from './content-parse'
|
2023-12-22 12:16:46 +00:00
|
|
|
import Emoji from '~/components/emoji/Emoji.vue'
|
2022-12-13 14:03:30 +00:00
|
|
|
import ContentCode from '~/components/content/ContentCode.vue'
|
2023-01-13 00:08:56 +00:00
|
|
|
import ContentMentionGroup from '~/components/content/ContentMentionGroup.vue'
|
2022-12-13 14:03:30 +00:00
|
|
|
import AccountHoverWrapper from '~/components/account/AccountHoverWrapper.vue'
|
2024-03-04 16:45:25 +00:00
|
|
|
import TagHoverWrapper from '~/components/account/TagHoverWrapper.vue'
|
2022-12-13 14:03:30 +00:00
|
|
|
|
2023-03-19 12:12:20 +00:00
|
|
|
function getTextualAstComponents(astChildren: Node[]): string {
|
2023-02-04 17:02:05 +00:00
|
|
|
return astChildren
|
|
|
|
.filter(({ type }) => type === TEXT_NODE)
|
|
|
|
.map(({ value }) => value)
|
|
|
|
.reduce((accumulator, current) => accumulator + current, '')
|
|
|
|
.trim()
|
|
|
|
}
|
|
|
|
|
2022-12-13 14:03:30 +00:00
|
|
|
/**
|
2023-12-22 12:16:46 +00:00
|
|
|
* Raw HTML to VNodes.
|
|
|
|
*
|
|
|
|
* @param content HTML content.
|
|
|
|
* @param options Options.
|
|
|
|
*/
|
2022-12-13 14:03:30 +00:00
|
|
|
export function contentToVNode(
|
|
|
|
content: string,
|
2023-01-07 09:31:48 +00:00
|
|
|
options?: ContentParseOptions,
|
2022-12-13 14:03:30 +00:00
|
|
|
): VNode {
|
2023-02-04 17:02:05 +00:00
|
|
|
let tree = parseMastodonHTML(content, options)
|
|
|
|
|
2023-03-19 12:12:20 +00:00
|
|
|
const textContents = getTextualAstComponents(tree.children)
|
2023-02-04 17:02:05 +00:00
|
|
|
|
|
|
|
// if the username only contains emojis, we should probably show the emojis anyway to avoid a blank name
|
2023-02-05 13:00:25 +00:00
|
|
|
if (options?.hideEmojis && textContents.length === 0)
|
|
|
|
tree = parseMastodonHTML(content, { ...options, hideEmojis: false })
|
2023-02-04 17:02:05 +00:00
|
|
|
|
2023-01-13 00:08:56 +00:00
|
|
|
return h(Fragment, (tree.children as Node[] || []).map(n => treeToVNode(n)))
|
2022-12-13 14:03:30 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 09:57:44 +00:00
|
|
|
export function nodeToVNode(node: Node): VNode | string | null {
|
2022-12-13 14:03:30 +00:00
|
|
|
if (node.type === TEXT_NODE)
|
|
|
|
return node.value
|
|
|
|
|
2023-01-13 00:08:56 +00:00
|
|
|
if (node.name === 'mention-group')
|
|
|
|
return h(ContentMentionGroup, node.attributes, () => node.children.map(treeToVNode))
|
|
|
|
|
2023-12-22 12:16:46 +00:00
|
|
|
// add tooltip to emojis
|
|
|
|
if (node.name === 'picture' || (node.name === 'img' && node.attributes?.alt)) {
|
|
|
|
const props = node.attributes ?? {}
|
|
|
|
props.as = node.name
|
|
|
|
return h(
|
|
|
|
Emoji,
|
|
|
|
props,
|
|
|
|
() => node.children.map(treeToVNode),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-12-13 14:03:30 +00:00
|
|
|
if ('children' in node) {
|
|
|
|
if (node.name === 'a' && (node.attributes.href?.startsWith('/') || node.attributes.href?.startsWith('.'))) {
|
|
|
|
node.attributes.to = node.attributes.href
|
2023-03-19 12:12:20 +00:00
|
|
|
|
|
|
|
const { href: _href, target: _target, ...attrs } = node.attributes
|
2022-12-13 14:03:30 +00:00
|
|
|
return h(
|
|
|
|
RouterLink as any,
|
2022-12-26 18:39:29 +00:00
|
|
|
attrs,
|
2022-12-13 14:03:30 +00:00
|
|
|
() => node.children.map(treeToVNode),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return h(
|
|
|
|
node.name,
|
|
|
|
node.attributes,
|
|
|
|
node.children.map(treeToVNode),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
2022-12-20 00:23:06 +00:00
|
|
|
|
2022-12-13 14:03:30 +00:00
|
|
|
function treeToVNode(
|
|
|
|
input: Node,
|
|
|
|
): VNode | string | null {
|
2023-04-24 18:46:17 +00:00
|
|
|
if (!input)
|
|
|
|
return null
|
|
|
|
|
2022-12-13 14:03:30 +00:00
|
|
|
if (input.type === TEXT_NODE)
|
2023-01-09 22:08:42 +00:00
|
|
|
return decode(input.value)
|
2022-12-13 14:03:30 +00:00
|
|
|
|
|
|
|
if ('children' in input) {
|
|
|
|
const node = handleNode(input)
|
|
|
|
if (node == null)
|
|
|
|
return null
|
|
|
|
if (isVNode(node))
|
|
|
|
return node
|
|
|
|
return nodeToVNode(node)
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2024-01-04 19:51:32 +00:00
|
|
|
function addBdiNode(node: Node) {
|
|
|
|
if (node.children.length === 1 && node.children[0].type === ELEMENT_NODE && node.children[0].name === 'bdi')
|
|
|
|
return
|
|
|
|
|
|
|
|
const children = node.children.splice(0, node.children.length)
|
|
|
|
const bdi = {
|
|
|
|
name: 'bdi',
|
|
|
|
parent: node,
|
|
|
|
loc: node.loc,
|
|
|
|
type: ELEMENT_NODE,
|
|
|
|
attributes: {},
|
|
|
|
children,
|
|
|
|
} satisfies ElementNode
|
|
|
|
children.forEach((n: Node) => n.parent = bdi)
|
|
|
|
node.children.push(bdi)
|
|
|
|
}
|
|
|
|
|
2022-12-13 14:03:30 +00:00
|
|
|
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
|
2023-01-16 12:36:22 +00:00
|
|
|
const handle = `${username}@${server.replace(/(.+\.)(.+\..+)/, '$2')}`
|
2022-12-13 14:03:30 +00:00
|
|
|
el.attributes.href = `/${server}/@${username}`
|
2024-01-04 19:51:32 +00:00
|
|
|
addBdiNode(el)
|
2022-12-13 14:03:30 +00:00
|
|
|
return h(AccountHoverWrapper, { handle, class: 'inline-block' }, () => nodeToVNode(el))
|
|
|
|
}
|
2024-03-04 16:45:25 +00:00
|
|
|
|
2022-12-13 14:03:30 +00:00
|
|
|
const matchTag = href.match(TagLinkRE)
|
|
|
|
if (matchTag) {
|
2024-03-04 16:45:25 +00:00
|
|
|
const [, , tagName] = matchTag
|
2024-01-04 19:51:32 +00:00
|
|
|
addBdiNode(el)
|
2024-03-04 16:45:25 +00:00
|
|
|
el.attributes.href = `/${currentServer.value}/tags/${tagName}`
|
|
|
|
return h(TagHoverWrapper, { tagName, class: 'inline-block' }, () => nodeToVNode(el))
|
2022-12-13 14:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-', '')
|
2023-03-19 12:12:20 +00:00
|
|
|
const code = (codeEl.children && codeEl.children.length > 0)
|
|
|
|
? recursiveTreeToText(codeEl)
|
|
|
|
: ''
|
2022-12-13 14:03:30 +00:00
|
|
|
return h(ContentCode, { lang, code: encodeURIComponent(code) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleNode(el: Node) {
|
|
|
|
return handleCodeBlock(el) || handleMention(el) || el
|
|
|
|
}
|