import { jsonFromHTML } from '@prosekit/core' import { formatHTML } from 'diffable-html-snapshot' import { describe, expect, it } from 'vitest' import { setupTest } from '../testing/index.ts' import { htmlFromMarkdown, markdownFromHTML } from '../testing/markdown.ts' describe('defineCodeBlockSpec', () => { it('can parse and serialize code blocks', () => { const { editor, n } = setupTest() const doc = n.doc( n.codeBlock({ language: 'javascript' }, 'console.log("Hello, javascript!");'), n.codeBlock({ language: 'JS' }, 'print("Hello, JS!");'), n.codeBlock({ language: 'UNKNOWN_LANG' }, 'print hello world'), n.codeBlock('hello world'), ) editor.set(doc) const html = editor.getDocHTML() expect(formatHTML(html)).toMatchInlineSnapshot( ` "
console.log("Hello, javascript!");
print("Hello, JS!");
print hello world
hello world
1234
"
`)
const json = jsonFromHTML(html, { schema: editor.schema })
expect(json).toMatchObject(
{
content: [
{
attrs: {
language: 'javascript',
},
content: [
{
text: '1234' + '\n',
type: 'text',
},
],
type: 'codeBlock',
},
],
type: 'doc',
},
)
})
it('can generate html that can be parsed by remark', () => {
const { editor, n } = setupTest()
editor.set(n.doc(n.codeBlock({ language: 'javascript' }, '123')))
const html = editor.getDocHTML()
expect(html).toMatchInlineSnapshot(`"123