feat(content): serialize custom emoji
This commit is contained in:
parent
e76aac3b56
commit
66393cd838
4 changed files with 80 additions and 26 deletions
|
@ -2,22 +2,24 @@ import type { Emoji } from 'masto'
|
|||
import { describe, expect, it } from 'vitest'
|
||||
import { format } from 'prettier'
|
||||
import { serialize } from 'parse5'
|
||||
import { parseMastodonHTML } from '~/composables/content'
|
||||
import { parseMastodonHTML, treeToText } from '~/composables/content'
|
||||
|
||||
describe('html-parse', () => {
|
||||
it('empty', async () => {
|
||||
const { formatted } = await render('')
|
||||
expect(formatted).toMatchSnapshot()
|
||||
const { formatted, serializedText } = await render('')
|
||||
expect(formatted).toMatchSnapshot('html')
|
||||
expect(serializedText).toMatchSnapshot('text')
|
||||
})
|
||||
|
||||
it('link + mention', async () => {
|
||||
// https://fosstodon.org/@ayo/109383002937620723
|
||||
const { formatted } = await render('<p>Happy 🤗 we’re now using <span class="h-card"><a href="https://mas.to/@vitest" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>vitest</span></a></span> (migrated from chai+mocha) <a href="https://github.com/ayoayco/astro-reactive-library/pull/203" rel="nofollow noopener noreferrer" target="_blank"><span class="invisible">https://</span><span class="ellipsis">github.com/ayoayco/astro-react</span><span class="invisible">ive-library/pull/203</span></a></p>')
|
||||
expect(formatted).toMatchSnapshot()
|
||||
const { formatted, serializedText } = await render('<p>Happy 🤗 we’re now using <span class="h-card"><a href="https://mas.to/@vitest" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@<span>vitest</span></a></span> (migrated from chai+mocha) <a href="https://github.com/ayoayco/astro-reactive-library/pull/203" rel="nofollow noopener noreferrer" target="_blank"><span class="invisible">https://</span><span class="ellipsis">github.com/ayoayco/astro-react</span><span class="invisible">ive-library/pull/203</span></a></p>')
|
||||
expect(formatted).toMatchSnapshot('html')
|
||||
expect(serializedText).toMatchSnapshot('text')
|
||||
})
|
||||
|
||||
it('custom emoji', async () => {
|
||||
const { formatted } = await render('Daniel Roe :nuxt:', {
|
||||
const { formatted, serializedText } = await render('Daniel Roe :nuxt:', {
|
||||
nuxt: {
|
||||
shortcode: 'nuxt',
|
||||
url: 'https://media.mas.to/masto-public/cache/custom_emojis/images/000/288/667/original/c96ba3cb0e0e1eac.png',
|
||||
|
@ -25,30 +27,35 @@ describe('html-parse', () => {
|
|||
visibleInPicker: true,
|
||||
},
|
||||
})
|
||||
expect(formatted).toMatchSnapshot()
|
||||
expect(formatted).toMatchSnapshot('html')
|
||||
expect(serializedText).toMatchSnapshot('text')
|
||||
})
|
||||
|
||||
it('code frame', async () => {
|
||||
// https://mas.to/@antfu/109396489827394721
|
||||
const { formatted } = await render('<p>Testing code block</p><p>```ts<br />import { useMouse, usePreferredDark } from '@vueuse/core'</p><p>// tracks mouse position<br />const { x, y } = useMouse()</p><p>// is the user prefers dark theme<br />const isDark = usePreferredDark()<br />```</p>')
|
||||
expect(formatted).toMatchSnapshot()
|
||||
const { formatted, serializedText } = await render('<p>Testing code block</p><p>```ts<br />import { useMouse, usePreferredDark } from '@vueuse/core'</p><p>// tracks mouse position<br />const { x, y } = useMouse()</p><p>// is the user prefers dark theme<br />const isDark = usePreferredDark()<br />```</p>')
|
||||
expect(formatted).toMatchSnapshot('html')
|
||||
expect(serializedText).toMatchSnapshot('text')
|
||||
})
|
||||
|
||||
it('code frame 2', async () => {
|
||||
const { formatted } = await render('<p><span class=\"h-card\"><a href=\"https://mas.to/@antfu\" class=\"u-url mention\">@<span>antfu</span></a></span> Testing<br />```ts<br />const a = hello<br />```</p>')
|
||||
expect(formatted).toMatchSnapshot()
|
||||
const { formatted, serializedText } = await render('<p><span class=\"h-card\"><a href=\"https://mas.to/@antfu\" class=\"u-url mention\">@<span>antfu</span></a></span> Testing<br />```ts<br />const a = hello<br />```</p>')
|
||||
expect(formatted).toMatchSnapshot('html')
|
||||
expect(serializedText).toMatchSnapshot('text')
|
||||
})
|
||||
|
||||
it('inline markdown', async () => {
|
||||
const { formatted } = await render('<p>text `code` **bold** *italic*</p><p>```js<br />code block<br />```</p>')
|
||||
expect(formatted).toMatchSnapshot()
|
||||
const { formatted, serializedText } = await render('<p>text `code` **bold** *italic* ~~del~~</p><p>```js<br />code block<br />```</p>')
|
||||
expect(formatted).toMatchSnapshot('html')
|
||||
expect(serializedText).toMatchSnapshot('text')
|
||||
})
|
||||
})
|
||||
|
||||
async function render(content: string, emojis?: Record<string, Emoji>) {
|
||||
const node = parseMastodonHTML(content, emojis)
|
||||
const html = serialize(node)
|
||||
async function render(input: string, emojis?: Record<string, Emoji>) {
|
||||
const tree = parseMastodonHTML(input, emojis)
|
||||
const html = serialize(tree)
|
||||
let formatted = ''
|
||||
const serializedText = tree.childNodes.map(n => treeToText(n)).join('').trim()
|
||||
|
||||
try {
|
||||
formatted = format(html, {
|
||||
|
@ -60,6 +67,9 @@ async function render(content: string, emojis?: Record<string, Emoji>) {
|
|||
}
|
||||
|
||||
return {
|
||||
serializedText,
|
||||
input,
|
||||
tree,
|
||||
html,
|
||||
formatted,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue