refactor: use tiny-decode
for parsing html entities (#922)
This commit is contained in:
parent
3e0a29dff5
commit
6f7bdf274f
4 changed files with 16 additions and 16 deletions
|
@ -3,6 +3,7 @@ import type { mastodon } from 'masto'
|
|||
import type { Node } from 'ultrahtml'
|
||||
import { DOCUMENT_NODE, ELEMENT_NODE, TEXT_NODE, h, parse, render } from 'ultrahtml'
|
||||
import { findAndReplaceEmojisInText } from '@iconify/utils'
|
||||
import { decode } from 'tiny-decode'
|
||||
import { emojiRegEx, getEmojiAttributes } from '../config/emojis'
|
||||
|
||||
export interface ContentParseOptions {
|
||||
|
@ -34,15 +35,6 @@ const sanitizer = sanitize({
|
|||
},
|
||||
})
|
||||
|
||||
const decoder = process.client ? document.createElement('textarea') : null
|
||||
export function decodeHtml(text: string) {
|
||||
if (!decoder)
|
||||
// not available when SSR
|
||||
return text
|
||||
decoder.innerHTML = text
|
||||
return decoder.value
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse raw HTML form Mastodon server to AST,
|
||||
* with interop of custom emojis and inline Markdown syntax
|
||||
|
@ -113,7 +105,7 @@ export function treeToText(input: Node): string {
|
|||
let post = ''
|
||||
|
||||
if (input.type === TEXT_NODE)
|
||||
return decodeHtml(input.value)
|
||||
return decode(input.value)
|
||||
|
||||
if (input.name === 'br')
|
||||
return '\n'
|
||||
|
|
|
@ -3,8 +3,9 @@ import type { Node } from 'ultrahtml'
|
|||
import { Fragment, h, isVNode } from 'vue'
|
||||
import type { VNode } from 'vue'
|
||||
import { RouterLink } from 'vue-router'
|
||||
import { decode } from 'tiny-decode'
|
||||
import type { ContentParseOptions } from './content-parse'
|
||||
import { decodeHtml, parseMastodonHTML } from './content-parse'
|
||||
import { parseMastodonHTML } from './content-parse'
|
||||
import ContentCode from '~/components/content/ContentCode.vue'
|
||||
import AccountHoverWrapper from '~/components/account/AccountHoverWrapper.vue'
|
||||
|
||||
|
@ -47,7 +48,7 @@ function treeToVNode(
|
|||
input: Node,
|
||||
): VNode | string | null {
|
||||
if (input.type === TEXT_NODE)
|
||||
return decodeHtml(input.value)
|
||||
return decode(input.value)
|
||||
|
||||
if ('children' in input) {
|
||||
const node = handleNode(input)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue