| 1 | import assert from 'node:assert/strict'; |
| 2 | import { test } from 'node:test'; |
| 3 | import { formatBody, insideOpenFence } from '../src/lib/markdown.ts'; |
| 4 | |
| 5 | test('plain text stays unformatted', () => { |
| 6 | assert.equal(formatBody('hello\nthere', [], true), null); |
| 7 | assert.equal(formatBody('**bold**', [], false), null); |
| 8 | }); |
| 9 | |
| 10 | test('renders the common chat markdown', () => { |
| 11 | assert.equal(formatBody('*it* **bold** ~~gone~~', [], true), '<p><em>it</em> <strong>bold</strong> <del>gone</del></p>'); |
| 12 | assert.equal(formatBody('> quoted\nstill', [], true), '<blockquote>quoted<br></blockquote>still'); |
| 13 | assert.equal(formatBody('run `ls <x>`', [], true), '<p>run <code>ls <x></code></p>'); |
| 14 | assert.equal( |
| 15 | formatBody('```js\nlet a = "<b>";\n```', [], true), |
| 16 | '<pre><code class="language-js">let a = "<b>";\n</code></pre>' |
| 17 | ); |
| 18 | }); |
| 19 | |
| 20 | test('raw html is escaped, not passed through', () => { |
| 21 | assert.equal(formatBody('<b>hi</b> **x**', [], true), '<p><b>hi</b> <strong>x</strong></p>'); |
| 22 | assert.equal(formatBody('<script>alert(1)</script>', [], true), null); |
| 23 | }); |
| 24 | |
| 25 | test('enter stays a newline inside an open fence', () => { |
| 26 | assert.equal(insideOpenFence('```\ncode', 8), true); |
| 27 | assert.equal(insideOpenFence('```\ncode\n```\n', 13), false); |
| 28 | assert.equal(insideOpenFence('say ``` mid line', 16), false); |
| 29 | }); |