import { describe, expect, it } from 'vitest' import { format } from 'prettier' import { render as renderTree } from 'ultrahtml' import type { ContentParseOptions } from '~~/composables/content-parse' describe('html-parse', () => { it('empty', async () => { const { formatted, serializedText } = await render('') expect(formatted).toMatchSnapshot('html') expect(serializedText).toMatchSnapshot('text') }) it('link + mention', async () => { // https://fosstodon.org/@ayo/109383002937620723 const { formatted, serializedText } = await render('

Happy πŸ€— we’re now using @vitest (migrated from chai+mocha) github.com/ayoayco/astro-react

') expect(formatted).toMatchSnapshot('html') expect(serializedText).toMatchSnapshot('text') }) it('custom emoji', async () => { const { formatted, serializedText } = await render('Daniel Roe :nuxt:', { 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, }, }, }) expect(formatted).toMatchSnapshot('html') expect(serializedText).toMatchSnapshot('text') }) it('emojis', async () => { const { formatted, serializedText } = await render('πŸ‡«πŸ‡· πŸ‘¨β€πŸ‘©β€πŸ‘¦ πŸ‘©β€πŸš’πŸ§‘πŸ½β€πŸš€') expect(formatted).toMatchSnapshot('html') expect(serializedText).toMatchSnapshot('text') }) it('code frame', async () => { // https://webtoo.ls/@antfu/109396489827394721 const { formatted, serializedText } = await render('

Testing code block

```ts
import { useMouse, usePreferredDark } from '@vueuse/core'

// tracks mouse position
const { x, y } = useMouse()

// is the user prefers dark theme
const isDark = usePreferredDark()
```

') expect(formatted).toMatchSnapshot('html') expect(serializedText).toMatchSnapshot('text') }) it('code frame 2', async () => { const { formatted, serializedText } = await render('

@antfu Testing
```ts
const a = hello
```

') expect(formatted).toMatchSnapshot('html') expect(serializedText).toMatchSnapshot('text') }) it('inline markdown', async () => { const { formatted, serializedText } = await render('

text `code` **bold** *italic* ~~del~~

```js
code block
```

') expect(formatted).toMatchSnapshot('html') expect(serializedText).toMatchSnapshot('text') }) it('html entities', async () => { const { formatted, serializedText } = await render('

Hello <World />.

') expect(formatted).toMatchSnapshot('html') expect(serializedText).toMatchSnapshot('text') }) }) async function render(input: string, options?: ContentParseOptions) { const tree = parseMastodonHTML(input, options) const html = await renderTree(tree) let formatted = '' const serializedText = treeToText(tree).trim() try { formatted = format(html, { parser: 'html', }) } catch (e) { formatted = html } return { serializedText, input, tree, html, formatted, } }