1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Run: node desktop/assistant/test_md.js
// Md.js is a QML library, so its .pragma line is stripped before node
// evaluates it.
const fs = require("fs");
const assert = require("assert");
const src = fs.readFileSync(__dirname + "/Md.js", "utf8")
.replace(/^\.pragma library$/m, "");
const { split } = new Function(src + "\nreturn { split };")();
const prose = text => ({ kind: "prose", text });
const code = (lang, text, open = false) => ({ kind: "code", lang, text, open });
assert.deepStrictEqual(split(""), []);
assert.deepStrictEqual(split("Just **prose**.\n\nTwo paragraphs."),
[prose("Just **prose**.\n\nTwo paragraphs.")]);
// One fence, with a language, between prose.
assert.deepStrictEqual(split("Run:\n```bash\nls -l\n```\nDone."),
[prose("Run:"), code("bash", "ls -l"), prose("Done.")]);
// Two fences, no language, blank lines kept inside code.
assert.deepStrictEqual(split("```\na\n\nb\n```\n```\nc\n```"),
[code("", "a\n\nb"), code("", "c")]);
// Streaming: an unclosed fence is open code.
assert.deepStrictEqual(split("Here:\n```py\nprint(1)\npri"),
[prose("Here:"), code("py", "print(1)\npri", true)]);
assert.deepStrictEqual(split("```"), [code("", "", true)]);
// Inline backticks are not a fence.
assert.deepStrictEqual(split("Use `ls` or ``a`b``."), [prose("Use `ls` or ``a`b``.")]);
// A fence in a list item: its indent is stripped from the code.
assert.deepStrictEqual(split("1. Install:\n ```sh\n make\n make install\n ```\n2. Go"),
[prose("1. Install:"), code("sh", "make\n make install"), prose("2. Go")]);
// A shorter or different closing run does not close the fence.
assert.deepStrictEqual(split("````\n```\n~~~\n````"), [code("", "```\n~~~")]);
// Tilde fences.
assert.deepStrictEqual(split("~~~js\nx\n~~~"), [code("js", "x")]);
// A closing fence with text after it is content, not a close.
assert.deepStrictEqual(split("```\n``` not\n```"), [code("", "``` not")]);
console.log("ok");
|