2022-12-27 22:13:50 +03:00
|
|
|
// @unimport-disable
|
2022-11-21 10:14:07 +03:00
|
|
|
import type { Emoji } from 'masto'
|
2022-12-05 01:10:10 +03:00
|
|
|
import type { Node } from 'ultrahtml'
|
|
|
|
import { TEXT_NODE, parse, render, walkSync } from 'ultrahtml'
|
2023-01-02 07:53:53 +03:00
|
|
|
import { findAndReplaceEmojisInText } from '@iconify/utils'
|
|
|
|
import { emojiRegEx, getEmojiAttributes } from '../config/emojis'
|
2022-12-27 21:37:22 +03:00
|
|
|
|
2022-12-17 19:55:29 +03:00
|
|
|
const decoder = process.client ? document.createElement('textarea') : null as any as HTMLTextAreaElement
|
2022-12-20 03:23:06 +03:00
|
|
|
export function decodeHtml(text: string) {
|
2022-12-05 03:30:20 +03:00
|
|
|
decoder.innerHTML = text
|
|
|
|
return decoder.value
|
|
|
|
}
|
|
|
|
|
2022-11-30 08:27:24 +03:00
|
|
|
/**
|
|
|
|
* Parse raw HTML form Mastodon server to AST,
|
|
|
|
* with interop of custom emojis and inline Markdown syntax
|
|
|
|
*/
|
2023-01-02 07:53:53 +03:00
|
|
|
export function parseMastodonHTML(html: string, customEmojis: Record<string, Emoji> = {}, markdown = true, forTiptap = false) {
|
|
|
|
// unicode emojis to images, but only if not converting HTML for Tiptap
|
|
|
|
let processed = forTiptap ? html : replaceUnicodeEmoji(html)
|
|
|
|
|
|
|
|
// custom emojis
|
|
|
|
processed = processed.replace(/:([\w-]+?):/g, (_, name) => {
|
|
|
|
const emoji = customEmojis[name]
|
|
|
|
if (emoji)
|
|
|
|
return `<img src="${emoji.url}" alt=":${name}:" class="custom-emoji" data-emoji-id="${name}" />`
|
|
|
|
return `:${name}:`
|
|
|
|
})
|
2022-12-18 00:01:20 +03:00
|
|
|
|
|
|
|
if (markdown) {
|
2022-11-30 08:27:24 +03:00
|
|
|
// handle code blocks
|
2022-12-18 00:01:20 +03:00
|
|
|
processed = processed
|
|
|
|
.replace(/>(```|~~~)(\w*)([\s\S]+?)\1/g, (_1, _2, lang, raw) => {
|
|
|
|
const code = htmlToText(raw)
|
|
|
|
const classes = lang ? ` class="language-${lang}"` : ''
|
|
|
|
return `><pre><code${classes}>${code}</code></pre>`
|
|
|
|
})
|
2022-11-24 06:42:03 +03:00
|
|
|
|
2022-12-18 00:01:20 +03: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>'],
|
|
|
|
] as any
|
2022-12-05 03:15:17 +03:00
|
|
|
|
2022-12-18 00:01:20 +03: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-12-05 01:10:10 +03:00
|
|
|
}
|
|
|
|
}
|
2022-12-18 00:01:20 +03:00
|
|
|
})
|
|
|
|
}
|
2022-11-30 08:27:24 +03:00
|
|
|
|
2022-12-05 01:10:10 +03:00
|
|
|
return parse(processed)
|
2022-11-30 08:27:24 +03:00
|
|
|
}
|
|
|
|
|
2023-01-02 07:53:53 +03:00
|
|
|
/**
|
|
|
|
* Converts raw HTML form Mastodon server to HTML for Tiptap editor
|
|
|
|
*/
|
2022-12-18 00:01:20 +03:00
|
|
|
export function convertMastodonHTML(html: string, customEmojis: Record<string, Emoji> = {}) {
|
2023-01-02 07:53:53 +03:00
|
|
|
const tree = parseMastodonHTML(html, customEmojis, true, true)
|
2022-12-18 00:01:20 +03:00
|
|
|
return render(tree)
|
2022-11-30 08:27:24 +03:00
|
|
|
}
|
|
|
|
|
2022-11-25 19:17:15 +03:00
|
|
|
export function htmlToText(html: string) {
|
2022-12-05 01:10:10 +03:00
|
|
|
const tree = parse(html)
|
|
|
|
return (tree.children as Node[]).map(n => treeToText(n)).join('').trim()
|
2022-11-24 06:42:03 +03:00
|
|
|
}
|
|
|
|
|
2022-11-30 09:50:47 +03:00
|
|
|
export function treeToText(input: Node): string {
|
2022-11-24 06:42:03 +03:00
|
|
|
let pre = ''
|
2022-11-25 19:17:15 +03:00
|
|
|
let body = ''
|
|
|
|
let post = ''
|
2022-11-24 06:42:03 +03:00
|
|
|
|
2022-12-05 01:10:10 +03:00
|
|
|
if (input.type === TEXT_NODE)
|
2022-12-20 03:23:06 +03:00
|
|
|
return decodeHtml(input.value)
|
2022-11-24 06:42:03 +03:00
|
|
|
|
2022-12-05 01:10:10 +03:00
|
|
|
if (input.name === 'br')
|
2022-11-24 06:42:03 +03:00
|
|
|
return '\n'
|
|
|
|
|
2022-12-05 01:10:10 +03:00
|
|
|
if (['p', 'pre'].includes(input.name))
|
2022-11-24 06:42:03 +03:00
|
|
|
pre = '\n'
|
|
|
|
|
2022-12-05 01:10:10 +03:00
|
|
|
if (input.name === 'code') {
|
|
|
|
if (input.parent?.name === 'pre') {
|
|
|
|
const lang = input.attributes.class?.replace('language-', '')
|
2022-11-25 22:21:53 +03:00
|
|
|
|
2022-11-30 07:50:29 +03:00
|
|
|
pre = `\`\`\`${lang || ''}\n`
|
|
|
|
post = '\n```'
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pre = '`'
|
|
|
|
post = '`'
|
|
|
|
}
|
|
|
|
}
|
2022-12-05 01:10:10 +03:00
|
|
|
else if (input.name === 'b' || input.name === 'strong') {
|
2022-11-30 07:50:29 +03:00
|
|
|
pre = '**'
|
|
|
|
post = '**'
|
|
|
|
}
|
2022-12-05 01:10:10 +03:00
|
|
|
else if (input.name === 'i' || input.name === 'em') {
|
2022-11-30 07:50:29 +03:00
|
|
|
pre = '*'
|
|
|
|
post = '*'
|
2022-11-25 19:17:15 +03:00
|
|
|
}
|
2022-12-05 01:10:10 +03:00
|
|
|
else if (input.name === 'del') {
|
2022-11-30 09:50:47 +03:00
|
|
|
pre = '~~'
|
|
|
|
post = '~~'
|
|
|
|
}
|
2022-11-25 19:17:15 +03:00
|
|
|
|
2022-12-05 01:10:10 +03:00
|
|
|
if ('children' in input)
|
|
|
|
body = (input.children as Node[]).map(n => treeToText(n)).join('')
|
2022-11-24 06:42:03 +03:00
|
|
|
|
2023-01-02 07:53:53 +03:00
|
|
|
if (input.name === 'img') {
|
|
|
|
if (input.attributes.class?.includes('custom-emoji'))
|
|
|
|
return `:${input.attributes['data-emoji-id']}:`
|
|
|
|
if (input.attributes.class?.includes('iconify-emoji'))
|
|
|
|
return input.attributes.alt
|
|
|
|
}
|
2022-11-30 09:50:47 +03:00
|
|
|
|
2022-11-25 19:17:15 +03:00
|
|
|
return pre + body + post
|
2022-11-24 06:42:03 +03:00
|
|
|
}
|
2023-01-02 07:53:53 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace unicode emojis with locally hosted images
|
|
|
|
*/
|
|
|
|
export function replaceUnicodeEmoji(html: string) {
|
|
|
|
return findAndReplaceEmojisInText(emojiRegEx, html, (match) => {
|
|
|
|
const attrs = getEmojiAttributes(match)
|
|
|
|
return `<img src="${attrs.src}" alt="${attrs.alt}" class="${attrs.class}" />`
|
|
|
|
}) || html
|
|
|
|
}
|