1
0
mirror of https://github.com/elk-zone/elk synced 2024-11-24 23:26:09 +09:00
elk/composables/content.ts

140 lines
3.8 KiB
TypeScript
Raw Normal View History

2022-11-21 16:14:07 +09:00
import type { Emoji } from 'masto'
2022-11-21 06:21:53 +09:00
import type { DefaultTreeAdapterMap } from 'parse5'
2022-11-21 06:25:26 +09:00
import { parseFragment } from 'parse5'
2022-11-24 12:42:03 +09:00
import type { Component, VNode } from 'vue'
import { Fragment, h, isVNode } from 'vue'
2022-11-21 06:21:53 +09:00
import { RouterLink } from 'vue-router'
2022-11-24 12:42:03 +09:00
import ContentCode from '~/components/content/ContentCode.vue'
2022-11-21 06:21:53 +09:00
type Node = DefaultTreeAdapterMap['childNode']
type Element = DefaultTreeAdapterMap['element']
2022-11-24 12:42:03 +09:00
const CUSTOM_BLOCKS: Record<string, Component> = {
'custom-code': ContentCode,
}
function handleMention(el: Element) {
2022-11-21 06:21:53 +09:00
// 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 (href) {
const matchUser = href.value.match(UserLinkRE)
if (matchUser) {
const [, server, username] = matchUser
// Handles need to ignore server subdomains
href.value = `/@${username}@${server.replace(/(.+\.)(.+\..+)/, '$2')}`
2022-11-21 06:21:53 +09:00
}
const matchTag = href.value.match(TagLinkRE)
if (matchTag) {
const [, , name] = matchTag
href.value = `/tags/${name}`
}
}
}
2022-11-24 12:42:03 +09:00
return undefined
}
function handleBlocks(el: Element) {
if (el.tagName in CUSTOM_BLOCKS) {
const block = CUSTOM_BLOCKS[el.tagName]
const attrs = Object.fromEntries(el.attrs.map(i => [i.name, i.value]))
return h(block, attrs, () => el.childNodes.map(treeToVNode))
}
}
function handleNode(el: Element) {
return handleBlocks(el) || handleMention(el) || el
2022-11-21 06:21:53 +09:00
}
export function contentToVNode(
content: string,
2022-11-21 16:14:07 +09:00
customEmojis: Record<string, Emoji> = {},
2022-11-21 06:21:53 +09:00
): VNode {
2022-11-24 12:42:03 +09:00
content = content
.trim()
// handle custom emojis
.replace(/:([\w-]+?):/g, (_, name) => {
const emoji = customEmojis[name]
if (emoji)
return `<img src="${emoji.url}" alt="${name}" class="custom-emoji" />`
return `:${name}:`
})
2022-11-24 17:57:24 +09:00
// handle code frames
2022-11-25 16:25:44 +09:00
.replace(/<p>(```|~~~)([\s\S]+?)\1/g, (_1, _2, raw) => {
2022-11-24 12:42:03 +09:00
const plain = htmlToText(`<p>${raw}</p>`).trim()
const [lang, ...rest] = plain.split(/\n/)
2022-11-25 16:11:33 +09:00
return `<custom-code lang="${lang?.trim().toLowerCase() || ''}" code="${encodeURIComponent(rest.join('\n'))}"></custom-code>`
2022-11-24 12:42:03 +09:00
})
2022-11-21 06:21:53 +09:00
const tree = parseFragment(content)
2022-11-24 12:42:03 +09:00
return h(Fragment, tree.childNodes.map(n => treeToVNode(n)))
2022-11-21 06:21:53 +09:00
}
export function treeToVNode(
input: Node,
): VNode | string | null {
if (input.nodeName === '#text')
// @ts-expect-error casing
return input.value
if ('childNodes' in input) {
2022-11-24 12:42:03 +09:00
const node = handleNode(input)
2022-11-21 06:21:53 +09:00
if (node == null)
return null
2022-11-24 12:42:03 +09:00
if (isVNode(node))
return node
2022-11-21 06:21:53 +09:00
const attrs = Object.fromEntries(node.attrs.map(i => [i.name, i.value]))
if (node.nodeName === 'a' && (attrs.href?.startsWith('/') || attrs.href?.startsWith('.'))) {
attrs.to = attrs.href
delete attrs.href
delete attrs.target
return h(
RouterLink as any,
attrs,
2022-11-24 12:42:03 +09:00
() => node.childNodes.map(treeToVNode),
2022-11-21 06:21:53 +09:00
)
}
return h(
node.nodeName,
attrs,
2022-11-24 12:42:03 +09:00
node.childNodes.map(treeToVNode),
2022-11-21 06:21:53 +09:00
)
}
return null
}
2022-11-24 12:42:03 +09:00
2022-11-26 01:17:15 +09:00
export function htmlToText(html: string) {
2022-11-24 12:42:03 +09:00
const tree = parseFragment(html)
2022-11-26 01:17:15 +09:00
return tree.childNodes.map(n => treeToText(n)).join('').trim()
2022-11-24 12:42:03 +09:00
}
function treeToText(input: Node): string {
let pre = ''
2022-11-26 01:17:15 +09:00
let body = ''
let post = ''
2022-11-24 12:42:03 +09:00
if (input.nodeName === '#text')
// @ts-expect-error casing
return input.value
if (input.nodeName === 'br')
return '\n'
2022-11-26 01:17:15 +09:00
if (['p', 'pre'].includes(input.nodeName))
2022-11-24 12:42:03 +09:00
pre = '\n'
2022-11-26 01:17:15 +09:00
if (input.nodeName === 'code') {
2022-11-26 04:21:53 +09:00
const clz = input.attrs.find(attr => attr.name === 'class')
const lang = clz?.value.replace('language-', '')
pre = `\`\`\`${lang || ''}\n`
2022-11-26 04:16:47 +09:00
post = '\n```'
2022-11-26 01:17:15 +09:00
}
2022-11-24 12:42:03 +09:00
if ('childNodes' in input)
2022-11-26 01:17:15 +09:00
body = input.childNodes.map(n => treeToText(n)).join('')
2022-11-24 12:42:03 +09:00
2022-11-26 01:17:15 +09:00
return pre + body + post
2022-11-24 12:42:03 +09:00
}