Repository navigation
Expand file tree
/
Copy pathcode-highlight.test.js
More file actions
45 lines (38 loc) · 2.6 KB
/
Copy pathcode-highlight.test.js
File metadata and controls
45 lines (38 loc) · 2.6 KB
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
import { test } from "node:test";
import assert from "node:assert/strict";
import { blockLanguage } from "./public/code-highlight.js";
const { default: hljs } = await import("./public/vendor/highlight.js");
test("a fence's own language picks the grammar, aliases included", () => {
assert.equal(blockLanguage(hljs, "ts", "const a = 1;"), "ts");
assert.equal(blockLanguage(hljs, "python", "class A: pass"), "python");
assert.equal(blockLanguage(hljs, "nonsense-lang", "x"), "");
});
test("text, trees, diffs and drafts stay plain", () => {
for (const language of ["text", "tree", "diff", "patch", "draft"]) assert.equal(blockLanguage(hljs, language, "def f(): return 1"), "");
});
test("an unlabeled fence is colored only on a confident guess", () => {
assert.equal(blockLanguage(hljs, "", "Hello there, this is a draft message with a link."), "");
const python = "import os\n\ndef handle(msg):\n \"\"\"Docs.\"\"\"\n for item in msg.items:\n yield item\n\nclass Roles(BaseModel):\n can_build: bool\n";
assert.equal(blockLanguage(hljs, "", python), "python");
});
test("a diff fence guesses its language from its added and context lines", () => {
const diff = " @ws_router.handle(CreateModel)\n async def handle_create_model(\n msg: CreateModel,\n-):\n+) -> AsyncIterator[CreateModelResponse]:";
assert.equal(blockLanguage(hljs, "diff", diff), "python");
assert.equal(blockLanguage(hljs, "diff", "-Some prose line\n+Some prose line, changed"), "");
});
test("a colored diff keeps its line kinds and word marks over the syntax colors", async () => {
const { codeBlock } = await import("./public/markdown.js");
const { splitHighlightedLines } = await import("./public/code-highlight.js");
const html = codeBlock(["-const delay = 1000;", "+const delay = 5000;"], "diff", (text) => splitHighlightedLines(hljs.highlight(text, { language: "typescript" }).value));
assert.match(html, /<span class="markdown-diff-del">.*hljs-keyword.*<\/span>/);
assert.match(html, /markdown-diff-add.*<span class="hljs-number"><mark class="code-word">5000/);
});
test("a diff fence can name its code's language", async () => {
assert.equal(blockLanguage(hljs, "diff", "-a = 1\n+a = 2", "python"), "python");
assert.equal(blockLanguage(hljs, "diff", "-a\n+b", "nonsense-lang"), "");
const { renderMarkdown } = await import("./public/markdown.js");
const html = renderMarkdown("```diff python\n-a = 1\n+a = 2\n```");
assert.match(html, /data-code-language="diff" data-code-grammar="python"/);
assert.match(html, /markdown-diff-add/);
assert.doesNotMatch(renderMarkdown("```ts extra\nx\n```"), /data-code-grammar/);
});