feat: collapse mentions (#1034)

This commit is contained in:
Anthony Fu 2023-01-13 01:08:56 +01:00 committed by GitHub
parent d39ea9a6de
commit 36ae8be40a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 145 additions and 15 deletions

View file

@ -7,6 +7,7 @@ import { decode } from 'tiny-decode'
import type { ContentParseOptions } from './content-parse'
import { parseMastodonHTML } from './content-parse'
import ContentCode from '~/components/content/ContentCode.vue'
import ContentMentionGroup from '~/components/content/ContentMentionGroup.vue'
import AccountHoverWrapper from '~/components/account/AccountHoverWrapper.vue'
/**
@ -17,13 +18,16 @@ export function contentToVNode(
options?: ContentParseOptions,
): VNode {
const tree = parseMastodonHTML(content, options)
return h(Fragment, (tree.children as Node[]).map(n => treeToVNode(n)))
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 (node.name === 'mention-group')
return h(ContentMentionGroup, node.attributes, () => node.children.map(treeToVNode))
if ('children' in node) {
if (node.name === 'a' && (node.attributes.href?.startsWith('/') || node.attributes.href?.startsWith('.'))) {
node.attributes.to = node.attributes.href