| 1 | import assert from 'node:assert/strict'; |
| 2 | import { test } from 'node:test'; |
| 3 | import { codeToHtml, parseCode } from '../src/lib/code.ts'; |
| 4 | |
| 5 | test('plain text has no formatted body', () => { |
| 6 | assert.equal(codeToHtml('hello <b>'), null); |
| 7 | }); |
| 8 | |
| 9 | test('splits inline code', () => { |
| 10 | assert.deepEqual(parseCode('run `ls -l` now'), [ |
| 11 | { kind: 'text', text: 'run ' }, |
| 12 | { kind: 'inline', text: 'ls -l' }, |
| 13 | { kind: 'text', text: ' now' } |
| 14 | ]); |
| 15 | }); |
| 16 | |
| 17 | test('parses fenced blocks with language', () => { |
| 18 | assert.deepEqual(parseCode('see\n```rust\nfn main() {}\n```\nok'), [ |
| 19 | { kind: 'text', text: 'see\n' }, |
| 20 | { kind: 'block', text: 'fn main() {}', lang: 'rust' }, |
| 21 | { kind: 'text', text: 'ok' } |
| 22 | ]); |
| 23 | }); |
| 24 | |
| 25 | test('backticks inside a block stay literal', () => { |
| 26 | assert.deepEqual(parseCode('```\na `b` c\n```'), [{ kind: 'block', text: 'a `b` c', lang: null }]); |
| 27 | }); |
| 28 | |
| 29 | test('unclosed backtick is text', () => { |
| 30 | assert.deepEqual(parseCode('a ` b'), [{ kind: 'text', text: 'a ` b' }]); |
| 31 | }); |
| 32 | |
| 33 | test('html escapes and keeps line breaks', () => { |
| 34 | assert.equal( |
| 35 | codeToHtml('x<y\n`a&b`\n```js\n<p>\n```'), |
| 36 | 'x<y<br><code>a&b</code><br><pre><code class="language-js"><p></code></pre>' |
| 37 | ); |
| 38 | }); |