2022-11-21 16:14:07 +09:00
|
|
|
import type { Emoji } from 'masto'
|
2022-12-05 07:10:10 +09:00
|
|
|
import type { Node } from 'ultrahtml'
|
|
|
|
import { TEXT_NODE, parse, render, walkSync } from 'ultrahtml'
|
2022-11-30 14:27:24 +09:00
|
|
|
import type { VNode } from 'vue'
|
2022-11-24 12:42:03 +09:00
|
|
|
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-30 16:08:10 +09:00
|
|
|
import AccountHoverWrapper from '~/components/account/AccountHoverWrapper.vue'
|
2022-11-21 06:21:53 +09:00
|
|
|
|
2022-12-05 07:10:10 +09:00
|
|
|
function handleMention(el: Node) {
|
2022-11-21 06:21:53 +09:00
|
|
|
// Redirect mentions to the user page
|
2022-12-05 07:10:10 +09:00
|
|
|
if (el.name === 'a' && el.attributes.class?.includes('mention')) {
|
|
|
|
const href = el.attributes.href
|
2022-11-21 06:21:53 +09:00
|
|
|
if (href) {
|
2022-12-05 07:10:10 +09:00
|
|
|
const matchUser = href.match(UserLinkRE)
|
2022-11-21 06:21:53 +09:00
|
|
|
if (matchUser) {
|
|
|
|
const [, server, username] = matchUser
|
2022-11-30 16:08:10 +09:00
|
|
|
const handle = `@${username}@${server.replace(/(.+\.)(.+\..+)/, '$2')}`
|
2022-12-05 07:10:10 +09:00
|
|
|
el.attributes.href = `/${server}/@${username}`
|
2022-11-30 16:08:10 +09:00
|
|
|
return h(AccountHoverWrapper, { handle, class: 'inline-block' }, () => nodeToVNode(el))
|
2022-11-21 06:21:53 +09:00
|
|
|
}
|
2022-12-05 07:10:10 +09:00
|
|
|
const matchTag = href.match(TagLinkRE)
|
2022-11-21 06:21:53 +09:00
|
|
|
if (matchTag) {
|
|
|
|
const [, , name] = matchTag
|
2022-12-05 07:10:10 +09:00
|
|
|
el.attributes.href = `/${currentServer.value}/tags/${name}`
|
2022-11-21 06:21:53 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-24 12:42:03 +09:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2022-12-05 07:10:10 +09:00
|
|
|
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
|
2022-11-30 14:27:24 +09:00
|
|
|
const lang = classes?.split(/\s/g).find(i => i.startsWith('language-'))?.replace('language-', '')
|
2022-12-05 07:10:10 +09:00
|
|
|
const code = codeEl.children[0] ? treeToText(codeEl.children[0]) : ''
|
2022-11-30 14:27:24 +09:00
|
|
|
return h(ContentCode, { lang, code: encodeURIComponent(code) })
|
2022-11-24 12:42:03 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 07:10:10 +09:00
|
|
|
function handleNode(el: Node) {
|
2022-11-30 14:27:24 +09:00
|
|
|
return handleCodeBlock(el) || handleMention(el) || el
|
2022-11-21 06:21:53 +09:00
|
|
|
}
|
|
|
|
|
2022-12-05 09:30:20 +09:00
|
|
|
const decoder = document.createElement('textarea')
|
|
|
|
function decode(text: string) {
|
|
|
|
decoder.innerHTML = text
|
|
|
|
return decoder.value
|
|
|
|
}
|
|
|
|
|
2022-11-30 14:27:24 +09:00
|
|
|
/**
|
|
|
|
* Parse raw HTML form Mastodon server to AST,
|
|
|
|
* with interop of custom emojis and inline Markdown syntax
|
|
|
|
*/
|
|
|
|
export function parseMastodonHTML(html: string, customEmojis: Record<string, Emoji> = {}) {
|
2022-12-05 07:10:10 +09:00
|
|
|
let processed = html
|
2022-11-30 14:27:24 +09:00
|
|
|
// custom emojis
|
2022-11-24 12:42:03 +09:00
|
|
|
.replace(/:([\w-]+?):/g, (_, name) => {
|
|
|
|
const emoji = customEmojis[name]
|
|
|
|
if (emoji)
|
2022-11-30 15:50:47 +09:00
|
|
|
return `<img src="${emoji.url}" alt=":${name}:" class="custom-emoji" data-emoji-id="${name}" />`
|
2022-11-24 12:42:03 +09:00
|
|
|
return `:${name}:`
|
2022-11-26 06:21:44 +09:00
|
|
|
})
|
2022-11-30 14:27:24 +09:00
|
|
|
// handle code blocks
|
2022-12-02 11:19:31 +09:00
|
|
|
.replace(/>(```|~~~)(\w*)([\s\S]+?)\1/g, (_1, _2, lang, raw) => {
|
|
|
|
const code = htmlToText(raw)
|
2022-11-30 14:27:24 +09:00
|
|
|
const classes = lang ? ` class="language-${lang}"` : ''
|
2022-12-02 11:19:31 +09:00
|
|
|
return `><pre><code${classes}>${code}</code></pre>`
|
2022-11-26 06:21:44 +09:00
|
|
|
})
|
2022-11-24 12:42:03 +09:00
|
|
|
|
2022-12-05 07:10:10 +09:00
|
|
|
walkSync(parse(processed), (node) => {
|
|
|
|
if (node.type !== TEXT_NODE)
|
|
|
|
return
|
|
|
|
const replacements = [
|
|
|
|
[/\*\*\*(.*?)\*\*\*/g, '<b><em>$1</em></b>'],
|
|
|
|
[/\*\*(.*?)\*\*/g, '<b>$1</b>'],
|
|
|
|
[/\*(.*?)\*/g, '<em>$1</em>'],
|
|
|
|
[/~~(.*?)~~/g, '<del>$1</del>'],
|
|
|
|
[/`([^`]+?)`/g, '<code>$1</code>'],
|
2022-12-05 09:30:20 +09:00
|
|
|
[/&[^;]+;/g, (val: string) => decode(val)],
|
2022-12-05 09:15:17 +09:00
|
|
|
] as any
|
|
|
|
|
2022-12-05 07:10:10 +09:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 14:27:24 +09:00
|
|
|
}
|
2022-12-05 07:10:10 +09:00
|
|
|
})
|
2022-11-30 14:27:24 +09:00
|
|
|
|
2022-12-05 07:10:10 +09:00
|
|
|
return parse(processed)
|
2022-11-30 14:27:24 +09:00
|
|
|
}
|
|
|
|
|
2022-12-05 07:10:10 +09:00
|
|
|
export async function convertMastodonHTML(html: string, customEmojis: Record<string, Emoji> = {}) {
|
2022-11-30 14:27:24 +09:00
|
|
|
const tree = parseMastodonHTML(html, customEmojis)
|
2022-12-05 07:10:10 +09:00
|
|
|
return await render(tree)
|
2022-11-30 14:27:24 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Raw HTML to VNodes
|
|
|
|
*/
|
|
|
|
export function contentToVNode(
|
|
|
|
content: string,
|
|
|
|
customEmojis: Record<string, Emoji> = {},
|
|
|
|
): VNode {
|
|
|
|
const tree = parseMastodonHTML(content, customEmojis)
|
2022-12-05 07:10:10 +09:00
|
|
|
return h(Fragment, (tree.children as Node[]).map(n => treeToVNode(n)))
|
2022-11-21 06:21:53 +09:00
|
|
|
}
|
|
|
|
|
2022-11-30 16:08:10 +09:00
|
|
|
function nodeToVNode(node: Node): VNode | string | null {
|
2022-12-05 07:10:10 +09:00
|
|
|
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
|
|
|
|
delete node.attributes.href
|
|
|
|
delete node.attributes.target
|
2022-11-21 06:21:53 +09:00
|
|
|
return h(
|
|
|
|
RouterLink as any,
|
2022-12-05 07:10:10 +09:00
|
|
|
node.attributes,
|
|
|
|
() => node.children.map(treeToVNode),
|
2022-11-21 06:21:53 +09:00
|
|
|
)
|
|
|
|
}
|
|
|
|
return h(
|
2022-12-05 07:10:10 +09:00
|
|
|
node.name,
|
|
|
|
node.attributes,
|
|
|
|
node.children.map(treeToVNode),
|
2022-11-21 06:21:53 +09:00
|
|
|
)
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
2022-11-24 12:42:03 +09:00
|
|
|
|
2022-11-30 16:08:10 +09:00
|
|
|
function treeToVNode(
|
|
|
|
input: Node,
|
|
|
|
): VNode | string | null {
|
2022-12-05 07:10:10 +09:00
|
|
|
if (input.type === TEXT_NODE)
|
2022-11-30 16:08:10 +09:00
|
|
|
return input.value as string
|
|
|
|
|
2022-12-05 07:10:10 +09:00
|
|
|
if ('children' in input) {
|
2022-11-30 16:08:10 +09:00
|
|
|
const node = handleNode(input)
|
|
|
|
if (node == null)
|
|
|
|
return null
|
|
|
|
if (isVNode(node))
|
|
|
|
return node
|
|
|
|
return nodeToVNode(node)
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2022-11-26 01:17:15 +09:00
|
|
|
export function htmlToText(html: string) {
|
2022-12-05 07:10:10 +09:00
|
|
|
const tree = parse(html)
|
|
|
|
return (tree.children as Node[]).map(n => treeToText(n)).join('').trim()
|
2022-11-24 12:42:03 +09:00
|
|
|
}
|
|
|
|
|
2022-11-30 15:50:47 +09:00
|
|
|
export function treeToText(input: Node): string {
|
2022-11-24 12:42:03 +09:00
|
|
|
let pre = ''
|
2022-11-26 01:17:15 +09:00
|
|
|
let body = ''
|
|
|
|
let post = ''
|
2022-11-24 12:42:03 +09:00
|
|
|
|
2022-12-05 07:10:10 +09:00
|
|
|
if (input.type === TEXT_NODE)
|
2022-11-24 12:42:03 +09:00
|
|
|
return input.value
|
|
|
|
|
2022-12-05 07:10:10 +09:00
|
|
|
if (input.name === 'br')
|
2022-11-24 12:42:03 +09:00
|
|
|
return '\n'
|
|
|
|
|
2022-12-05 07:10:10 +09:00
|
|
|
if (['p', 'pre'].includes(input.name))
|
2022-11-24 12:42:03 +09:00
|
|
|
pre = '\n'
|
|
|
|
|
2022-12-05 07:10:10 +09:00
|
|
|
if (input.name === 'code') {
|
|
|
|
if (input.parent?.name === 'pre') {
|
|
|
|
const lang = input.attributes.class?.replace('language-', '')
|
2022-11-26 04:21:53 +09:00
|
|
|
|
2022-11-30 13:50:29 +09:00
|
|
|
pre = `\`\`\`${lang || ''}\n`
|
|
|
|
post = '\n```'
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pre = '`'
|
|
|
|
post = '`'
|
|
|
|
}
|
|
|
|
}
|
2022-12-05 07:10:10 +09:00
|
|
|
else if (input.name === 'b' || input.name === 'strong') {
|
2022-11-30 13:50:29 +09:00
|
|
|
pre = '**'
|
|
|
|
post = '**'
|
|
|
|
}
|
2022-12-05 07:10:10 +09:00
|
|
|
else if (input.name === 'i' || input.name === 'em') {
|
2022-11-30 13:50:29 +09:00
|
|
|
pre = '*'
|
|
|
|
post = '*'
|
2022-11-26 01:17:15 +09:00
|
|
|
}
|
2022-12-05 07:10:10 +09:00
|
|
|
else if (input.name === 'del') {
|
2022-11-30 15:50:47 +09:00
|
|
|
pre = '~~'
|
|
|
|
post = '~~'
|
|
|
|
}
|
2022-11-26 01:17:15 +09:00
|
|
|
|
2022-12-05 07:10:10 +09:00
|
|
|
if ('children' in input)
|
|
|
|
body = (input.children as Node[]).map(n => treeToText(n)).join('')
|
2022-11-24 12:42:03 +09:00
|
|
|
|
2022-12-05 07:10:10 +09:00
|
|
|
if (input.name === 'img' && input.attributes.class?.includes('custom-emoji'))
|
|
|
|
return `:${input.attributes['data-emoji-id']}:`
|
2022-11-30 15:50:47 +09:00
|
|
|
|
2022-11-26 01:17:15 +09:00
|
|
|
return pre + body + post
|
2022-11-24 12:42:03 +09:00
|
|
|
}
|