2022-11-30 08:27:24 +03:00
|
|
|
|
import { format } from 'prettier'
|
2022-12-05 01:10:10 +03:00
|
|
|
|
import { render as renderTree } from 'ultrahtml'
|
2024-09-30 11:11:56 +03:00
|
|
|
|
import { describe, expect, it } from 'vitest'
|
2023-01-31 06:07:17 +03:00
|
|
|
|
import type { ContentParseOptions } from '~/composables/content-parse'
|
2022-11-30 08:27:24 +03:00
|
|
|
|
|
|
|
|
|
describe('html-parse', () => {
|
|
|
|
|
it('empty', async () => {
|
2022-11-30 09:50:47 +03:00
|
|
|
|
const { formatted, serializedText } = await render('')
|
|
|
|
|
expect(formatted).toMatchSnapshot('html')
|
|
|
|
|
expect(serializedText).toMatchSnapshot('text')
|
2022-11-30 08:27:24 +03:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('link + mention', async () => {
|
|
|
|
|
// https://fosstodon.org/@ayo/109383002937620723
|
2023-01-16 13:24:14 +03:00
|
|
|
|
const { formatted, serializedText } = await render('<p>Happy 🤗 we’re now using <span class="h-card"><a href="https://webtoo.ls/@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>')
|
2022-11-30 09:50:47 +03:00
|
|
|
|
expect(formatted).toMatchSnapshot('html')
|
|
|
|
|
expect(serializedText).toMatchSnapshot('text')
|
2022-11-30 08:27:24 +03:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('custom emoji', async () => {
|
2022-11-30 09:50:47 +03:00
|
|
|
|
const { formatted, serializedText } = await render('Daniel Roe :nuxt:', {
|
2023-01-18 18:59:37 +03:00
|
|
|
|
emojis: {
|
|
|
|
|
nuxt: {
|
|
|
|
|
shortcode: 'nuxt',
|
|
|
|
|
url: 'https://media.webtoo.ls/custom_emojis/images/000/000/366/original/73330dfc9dda4078.png',
|
|
|
|
|
staticUrl: 'https://media.webtoo.ls/custom_emojis/images/000/000/366/original/73330dfc9dda4078.png',
|
|
|
|
|
visibleInPicker: true,
|
|
|
|
|
},
|
2022-11-30 08:27:24 +03:00
|
|
|
|
},
|
|
|
|
|
})
|
2022-11-30 09:50:47 +03:00
|
|
|
|
expect(formatted).toMatchSnapshot('html')
|
|
|
|
|
expect(serializedText).toMatchSnapshot('text')
|
2022-11-30 08:27:24 +03:00
|
|
|
|
})
|
|
|
|
|
|
2022-12-27 23:42:58 +03:00
|
|
|
|
it('emojis', async () => {
|
|
|
|
|
const { formatted, serializedText } = await render('🇫🇷 👨👩👦 👩🚒🧑🏽🚀')
|
|
|
|
|
expect(formatted).toMatchSnapshot('html')
|
|
|
|
|
expect(serializedText).toMatchSnapshot('text')
|
|
|
|
|
})
|
|
|
|
|
|
2022-11-30 08:27:24 +03:00
|
|
|
|
it('code frame', async () => {
|
2023-01-16 13:24:14 +03:00
|
|
|
|
// https://webtoo.ls/@antfu/109396489827394721
|
2022-11-30 09:50:47 +03:00
|
|
|
|
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')
|
2022-11-30 08:27:24 +03:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('code frame 2', async () => {
|
2023-01-16 13:24:14 +03:00
|
|
|
|
const { formatted, serializedText } = await render('<p><span class=\"h-card\"><a href=\"https://webtoo.ls/@antfu\" class=\"u-url mention\">@<span>antfu</span></a></span> Testing<br />```ts<br />const a = hello<br />```</p>')
|
2022-11-30 09:50:47 +03:00
|
|
|
|
expect(formatted).toMatchSnapshot('html')
|
|
|
|
|
expect(serializedText).toMatchSnapshot('text')
|
2022-11-30 08:27:24 +03:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('inline markdown', async () => {
|
2022-11-30 09:50:47 +03:00
|
|
|
|
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')
|
2022-11-30 08:27:24 +03:00
|
|
|
|
})
|
2022-12-20 03:23:06 +03:00
|
|
|
|
|
|
|
|
|
it('html entities', async () => {
|
|
|
|
|
const { formatted, serializedText } = await render('<p>Hello <World />.</p>')
|
|
|
|
|
expect(formatted).toMatchSnapshot('html')
|
|
|
|
|
expect(serializedText).toMatchSnapshot('text')
|
|
|
|
|
})
|
2022-11-30 08:27:24 +03:00
|
|
|
|
})
|
|
|
|
|
|
2023-01-18 18:59:37 +03:00
|
|
|
|
async function render(input: string, options?: ContentParseOptions) {
|
|
|
|
|
const tree = parseMastodonHTML(input, options)
|
2022-12-05 01:10:10 +03:00
|
|
|
|
const html = await renderTree(tree)
|
2022-11-30 08:27:24 +03:00
|
|
|
|
let formatted = ''
|
2022-12-05 02:28:04 +03:00
|
|
|
|
const serializedText = treeToText(tree).trim()
|
2022-11-30 08:27:24 +03:00
|
|
|
|
|
|
|
|
|
try {
|
2023-08-02 13:37:43 +03:00
|
|
|
|
formatted = await format(html, {
|
2022-11-30 08:27:24 +03:00
|
|
|
|
parser: 'html',
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-08-16 17:52:08 +03:00
|
|
|
|
catch {
|
2022-11-30 08:27:24 +03:00
|
|
|
|
formatted = html
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
2022-11-30 09:50:47 +03:00
|
|
|
|
serializedText,
|
|
|
|
|
input,
|
|
|
|
|
tree,
|
2022-11-30 08:27:24 +03:00
|
|
|
|
html,
|
|
|
|
|
formatted,
|
|
|
|
|
}
|
|
|
|
|
}
|