pi-syntax-highlight 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/CHANGELOG.md +81 -40
  2. package/DEVELOPER.md +100 -0
  3. package/LICENSE +21 -0
  4. package/README.md +42 -44
  5. package/extensions/index.ts +5 -10
  6. package/package.json +20 -5
  7. package/src/detect-language.ts +205 -8
  8. package/src/emphasis.ts +82 -1
  9. package/src/highlight.ts +25 -9
  10. package/src/lru-cache.ts +30 -0
  11. package/src/markdown-fence-lang.ts +20 -87
  12. package/src/render-bash.ts +29 -2
  13. package/src/render-edit.ts +0 -6
  14. package/src/render-generic.ts +10 -21
  15. package/src/render-read.ts +8 -149
  16. package/src/render-utils.ts +8 -17
  17. package/src/resolve-renderers.ts +181 -240
  18. package/src/tokens.ts +1 -34
  19. package/src/active-theme.ts +0 -26
  20. package/src/markdown-style.ts +0 -134
  21. package/src/markdown-theme.ts +0 -53
  22. package/src/render-path.ts +0 -54
  23. package/src/render-write.ts +0 -81
  24. package/test/unit/active-theme.test.ts +0 -38
  25. package/test/unit/bash-preview.test.ts +0 -57
  26. package/test/unit/detect-language.test.ts +0 -124
  27. package/test/unit/emphasis.test.ts +0 -113
  28. package/test/unit/extensions-index.test.ts +0 -116
  29. package/test/unit/grep-parse.test.ts +0 -125
  30. package/test/unit/highlight.test.ts +0 -107
  31. package/test/unit/markdown-fence-lang.test.ts +0 -131
  32. package/test/unit/markdown-pipeline.test.ts +0 -153
  33. package/test/unit/markdown-render-parity.test.ts +0 -127
  34. package/test/unit/markdown-style.test.ts +0 -152
  35. package/test/unit/markdown-theme.test.ts +0 -86
  36. package/test/unit/render-bash.test.ts +0 -194
  37. package/test/unit/render-edit.test.ts +0 -214
  38. package/test/unit/render-find.test.ts +0 -139
  39. package/test/unit/render-generic.test.ts +0 -152
  40. package/test/unit/render-grep.test.ts +0 -338
  41. package/test/unit/render-ls.test.ts +0 -132
  42. package/test/unit/render-read.test.ts +0 -375
  43. package/test/unit/render-utils.test.ts +0 -324
  44. package/test/unit/render-write.test.ts +0 -143
  45. package/test/unit/resolve-renderers.test.ts +0 -474
  46. package/test/unit/style-path.test.ts +0 -74
  47. package/tsconfig.json +0 -15
@@ -1,116 +0,0 @@
1
- import { test } from "node:test";
2
- import assert from "node:assert/strict";
3
- import type {
4
- ExtensionAPI,
5
- ExtensionContext,
6
- MarkdownTransformer,
7
- SessionStartEvent,
8
- Theme,
9
- ToolRendererResolver,
10
- } from "@earendil-works/pi-coding-agent";
11
- import piSyntaxHighlight from "../../extensions/index.ts";
12
- import { activeTheme, resetActiveTheme } from "../../src/active-theme.ts";
13
- import { resolveToolRenderers } from "../../src/resolve-renderers.ts";
14
-
15
- const ACCENT = "\x1b[38;5;5m";
16
-
17
- // The host's `Theme` cannot be constructed outside a session, so the fake supplies
18
- // the only two members the markdown pass reads, with the accent a colored theme
19
- // would give it.
20
- function fakeTheme(): Theme {
21
- const ansi: Record<string, string> = { accent: ACCENT, mdQuote: "\x1b[39m\x1b[2m" };
22
- return {
23
- getFgAnsi: (color: string) => ansi[color] ?? "",
24
- getBgAnsi: () => "",
25
- } as unknown as Theme;
26
- }
27
-
28
- function fakeContext(theme: Theme): ExtensionContext {
29
- return { ui: { theme } } as unknown as ExtensionContext;
30
- }
31
-
32
- // The factory takes the host's `ExtensionAPI`, which carries dozens of members.
33
- // This fake declares exactly the three registrations E1 makes; `ExtensionAPI` is a
34
- // superset of it, so the fake narrows to it without going through `any`.
35
- interface FakePi {
36
- registerToolRenderer(resolver: ToolRendererResolver): void;
37
- registerMarkdownTransformer(transformer: MarkdownTransformer): void;
38
- on(event: "session_start", handler: (event: SessionStartEvent, ctx: ExtensionContext) => void): () => void;
39
- }
40
-
41
- function loadExtension(): {
42
- renderers: ToolRendererResolver[];
43
- transformers: MarkdownTransformer[];
44
- sessionStart: Array<(event: SessionStartEvent, ctx: ExtensionContext) => void>;
45
- } {
46
- const renderers: ToolRendererResolver[] = [];
47
- const transformers: MarkdownTransformer[] = [];
48
- const sessionStart: Array<(event: SessionStartEvent, ctx: ExtensionContext) => void> = [];
49
- const fakePi: FakePi = {
50
- registerToolRenderer: (resolver) => {
51
- renderers.push(resolver);
52
- },
53
- registerMarkdownTransformer: (transformer) => {
54
- transformers.push(transformer);
55
- },
56
- on: (_event, handler) => {
57
- sessionStart.push(handler);
58
- return () => {};
59
- },
60
- };
61
- piSyntaxHighlight(fakePi as ExtensionAPI);
62
- return { renderers, transformers, sessionStart };
63
- }
64
-
65
- test("the extension registers exactly one tool renderer and one markdown transformer", () => {
66
- const { renderers, transformers } = loadExtension();
67
- assert.equal(renderers.length, 1);
68
- assert.equal(transformers.length, 1);
69
- assert.equal(renderers[0], resolveToolRenderers);
70
- });
71
-
72
- test("the markdown transformer labels a fence that arrives without a language", () => {
73
- const { transformers } = loadExtension();
74
- const markdown = "```\n{\n \"name\": \"pi\"\n}\n```\n";
75
- assert.equal(
76
- transformers[0](markdown, { messageType: "assistant", isStreaming: false, availableWidth: 80 }),
77
- "```json\n{\n \"name\": \"pi\"\n}\n```\n",
78
- );
79
- });
80
-
81
- test("the markdown transformer leaves a fence that already names a language alone", () => {
82
- const { transformers } = loadExtension();
83
- const markdown = "# Title\n\n```ts\nconst x = 1;\n```\n";
84
- assert.equal(transformers[0](markdown, { messageType: "assistant", isStreaming: false, availableWidth: 80 }), markdown);
85
- });
86
-
87
- test("the extension registers exactly one session_start handler", () => {
88
- const { sessionStart } = loadExtension();
89
- assert.equal(sessionStart.length, 1);
90
- });
91
-
92
- test("the session_start handler captures the theme the context hands out", () => {
93
- resetActiveTheme();
94
- const { sessionStart } = loadExtension();
95
- const theme = fakeTheme();
96
- sessionStart[0]?.({ type: "session_start", reason: "startup" }, fakeContext(theme));
97
- assert.equal(activeTheme(), theme);
98
- resetActiveTheme();
99
- });
100
-
101
- test("without a captured theme the transformer adds no sequence", () => {
102
- resetActiveTheme();
103
- const { transformers } = loadExtension();
104
- const markdown = "# Title\n\n> quoted\n";
105
- assert.equal(transformers[0](markdown, { messageType: "assistant", isStreaming: false, availableWidth: 80 }), markdown);
106
- });
107
-
108
- test("with a captured theme the transformer styles the assistant message", () => {
109
- resetActiveTheme();
110
- const { transformers, sessionStart } = loadExtension();
111
- sessionStart[0]?.({ type: "session_start", reason: "startup" }, fakeContext(fakeTheme()));
112
- const styled = transformers[0]("# Title\n", { messageType: "assistant", isStreaming: false, availableWidth: 80 });
113
- // The capture is the only thing that changed between this and the previous case.
114
- assert.ok(styled.includes(ACCENT));
115
- resetActiveTheme();
116
- });
@@ -1,125 +0,0 @@
1
- import { test } from "node:test";
2
- import assert from "node:assert/strict";
3
- import { GREP_LINE_PATTERN, parseGrepLine, parseGrepOutput } from "../../src/grep-parse.ts";
4
-
5
- test("parseGrepLine: a plain match carries path, line number and text", () => {
6
- assert.deepEqual(parseGrepLine("src/a.ts:12: const x = 1;"), {
7
- kind: "match",
8
- path: "src/a.ts",
9
- line: 12,
10
- text: "const x = 1;",
11
- });
12
- });
13
-
14
- test("parseGrepLine: colons and separators inside the matched text stay in the text", () => {
15
- assert.deepEqual(parseGrepLine("a.ts:3: foo:: bar: baz"), {
16
- kind: "match",
17
- path: "a.ts",
18
- line: 3,
19
- text: "foo:: bar: baz",
20
- });
21
- });
22
-
23
- test("parseGrepLine: a repeated dash separator is a context line", () => {
24
- assert.deepEqual(parseGrepLine("a.ts-12- some text"), {
25
- kind: "context",
26
- path: "a.ts",
27
- line: 12,
28
- text: "some text",
29
- });
30
- });
31
-
32
- test("parseGrepLine: a path containing a colon is not split at the colon", () => {
33
- assert.deepEqual(parseGrepLine("weird:name.ts:42: some text"), {
34
- kind: "match",
35
- path: "weird:name.ts",
36
- line: 42,
37
- text: "some text",
38
- });
39
- });
40
-
41
- test("parseGrepLine: a path containing a colon and digits is not split early", () => {
42
- assert.deepEqual(parseGrepLine("dir/sub:3/file.ts:9: some text"), {
43
- kind: "match",
44
- path: "dir/sub:3/file.ts",
45
- line: 9,
46
- text: "some text",
47
- });
48
- });
49
-
50
- // pi always writes a space after the line number, so a digit directly after
51
- // the second separator can only belong to the path (`foo:3` is the file name).
52
- test("parseGrepLine: digits without the trailing space belong to the path", () => {
53
- assert.deepEqual(parseGrepLine("foo:3:9: text"), {
54
- kind: "match",
55
- path: "foo:3",
56
- line: 9,
57
- text: "text",
58
- });
59
- });
60
-
61
- test("parseGrepLine: numeric text after the line number is not mistaken for the line", () => {
62
- assert.deepEqual(parseGrepLine("a.ts:7: 12: looks numeric"), {
63
- kind: "match",
64
- path: "a.ts",
65
- line: 7,
66
- text: "12: looks numeric",
67
- });
68
- });
69
-
70
- test("parseGrepLine: the unreadable marker drops the text", () => {
71
- assert.deepEqual(parseGrepLine("a.ts:9: (unable to read file)"), {
72
- kind: "unreadable",
73
- path: "a.ts",
74
- line: 9,
75
- });
76
- });
77
-
78
- test("parseGrepLine: mismatched separators are plain text", () => {
79
- assert.deepEqual(parseGrepLine("a-5: x"), { kind: "plain", text: "a-5: x" });
80
- });
81
-
82
- test("parseGrepLine: sentinels and blank lines are plain text", () => {
83
- for (const line of [
84
- "No matches found",
85
- "No files found matching pattern",
86
- "(empty directory)",
87
- "",
88
- " ",
89
- ]) {
90
- assert.deepEqual(parseGrepLine(line), { kind: "plain", text: line });
91
- }
92
- });
93
-
94
- test("parseGrepLine: a match with empty text is still a match", () => {
95
- assert.deepEqual(parseGrepLine("a.ts:5: "), { kind: "match", path: "a.ts", line: 5, text: "" });
96
- });
97
-
98
- test("parseGrepOutput: keeps the block order and distinguishes matches from context", () => {
99
- const parsed = parseGrepOutput("a.ts:1: one\na.ts-2- two\na.ts:3: three");
100
- assert.deepEqual(
101
- parsed.map((entry) => entry.kind),
102
- ["match", "context", "match"],
103
- );
104
- assert.deepEqual(
105
- parsed.map((entry) =>
106
- entry.kind === "match" || entry.kind === "context" || entry.kind === "unreadable" ? entry.line : undefined,
107
- ),
108
- [1, 2, 3],
109
- );
110
- assert.deepEqual(parseGrepOutput(""), []);
111
- });
112
-
113
- test("parseGrepLine: a notice line is not classified as a match", () => {
114
- for (const line of ["[2 matches limit]", "[Truncated: 100 matches limit, some lines truncated]"]) {
115
- assert.deepEqual(parseGrepLine(line), { kind: "notice", text: line });
116
- }
117
- });
118
-
119
- test("GREP_LINE_PATTERN: named groups are the ones the parser reads", () => {
120
- const groups = GREP_LINE_PATTERN.exec("a.ts-8- text")?.groups;
121
- assert.equal(groups?.path, "a.ts");
122
- assert.equal(groups?.sep, "-");
123
- assert.equal(groups?.line, "8");
124
- assert.equal(groups?.text, "text");
125
- });
@@ -1,107 +0,0 @@
1
- import { test } from "node:test";
2
- import assert from "node:assert/strict";
3
- import { clearHighlightCache, highlight, syncHighlightTheme } from "../../src/highlight.ts";
4
-
5
- const fakeTheme = {
6
- fg: (c: string, t: string) => `<${c}>${t}</${c}>`,
7
- bg: (c: string, t: string) => `<bg:${c}>${t}</bg:${c}>`,
8
- bold: (t: string) => `<b>${t}</b>`,
9
- };
10
-
11
- // `highlightCode` paints through the host's module-global theme, which its proxy
12
- // resolves from `globalThis` and throws without. Installing one here is what
13
- // lets the real `highlight` run instead of failing before it reaches the cache.
14
- const HOST_THEME_KEY = Symbol.for("@earendil-works/pi-coding-agent:theme");
15
- Object.assign(globalThis, { [HOST_THEME_KEY]: fakeTheme });
16
-
17
- const THEME_A = { ...fakeTheme };
18
- const THEME_B = { ...fakeTheme };
19
-
20
- // A language the highlighter does not know makes `highlightCode` paint every
21
- // line with one color, so the assertions do not depend on tokenizer output.
22
- const UNKNOWN_LANG = "no-such-language";
23
-
24
- // A theme that answers `getFgAnsi` behaves like the host's: one object whose
25
- // colors change under it. The map keeps the colors mutable, so a theme switch is
26
- // simulated without swapping the reference.
27
- function valueTheme(): { theme: { getFgAnsi: (color: string) => string }; ansi: Map<string, string> } {
28
- const ansi = new Map<string, string>();
29
- return { ansi, theme: { getFgAnsi: (color: string) => ansi.get(color) ?? `<${color}>` } };
30
- }
31
-
32
- test("highlight: the same language and code come back from the cache", () => {
33
- clearHighlightCache();
34
- const first = highlight("plain text", UNKNOWN_LANG);
35
- const second = highlight("plain text", UNKNOWN_LANG);
36
- // A hit returns the cached array itself, not a repainted copy.
37
- assert.equal(first, second);
38
- assert.deepEqual(first, [`<mdCodeBlock>plain text</mdCodeBlock>`]);
39
- });
40
-
41
- test("highlight: a different language is a different cache entry", () => {
42
- clearHighlightCache();
43
- const first = highlight("plain text", UNKNOWN_LANG);
44
- const other = highlight("plain text", "another-missing-language");
45
- assert.notEqual(first, other);
46
- });
47
-
48
- test("syncHighlightTheme: a new theme reference invalidates the cache", () => {
49
- clearHighlightCache();
50
- syncHighlightTheme(THEME_A);
51
- const first = highlight("plain text", UNKNOWN_LANG);
52
- assert.equal(highlight("plain text", UNKNOWN_LANG), first);
53
-
54
- syncHighlightTheme(THEME_B);
55
- const recomputed = highlight("plain text", UNKNOWN_LANG);
56
- assert.notEqual(recomputed, first);
57
- // Only the paint was dropped: the content is what the source says.
58
- assert.deepEqual(recomputed, first);
59
- });
60
-
61
- test("syncHighlightTheme: the same theme reference keeps the cache", () => {
62
- clearHighlightCache();
63
- syncHighlightTheme(THEME_A);
64
- const first = highlight("plain text", UNKNOWN_LANG);
65
- syncHighlightTheme(THEME_A);
66
- assert.equal(highlight("plain text", UNKNOWN_LANG), first);
67
- });
68
-
69
- test("syncHighlightTheme: a theme with unchanged colors keeps the cache", () => {
70
- clearHighlightCache();
71
- const { theme } = valueTheme();
72
- syncHighlightTheme(theme);
73
- const first = highlight("plain text", UNKNOWN_LANG);
74
- // Every frame of a session renders with the same colors, so the second sync
75
- // must not throw away what the first one painted.
76
- syncHighlightTheme(theme);
77
- assert.equal(highlight("plain text", UNKNOWN_LANG), first);
78
- });
79
-
80
- test("syncHighlightTheme: a color that changed under the same reference invalidates the cache", () => {
81
- clearHighlightCache();
82
- const { theme, ansi } = valueTheme();
83
- syncHighlightTheme(theme);
84
- const first = highlight("plain text", UNKNOWN_LANG);
85
- ansi.set("syntaxKeyword", "\x1b[38;5;1m");
86
- syncHighlightTheme(theme);
87
- const repainted = highlight("plain text", UNKNOWN_LANG);
88
- assert.notEqual(repainted, first);
89
- // Only the paint was dropped: the content is what the source says.
90
- assert.deepEqual(repainted, first);
91
- });
92
-
93
- test("syncHighlightTheme: a theme whose getFgAnsi throws is compared by reference", () => {
94
- clearHighlightCache();
95
- const getFgAnsi = (): string => {
96
- throw new Error("Theme not initialized");
97
- };
98
- const throwing = { getFgAnsi };
99
- syncHighlightTheme(throwing);
100
- const first = highlight("plain text", UNKNOWN_LANG);
101
- // The reference is the only signal left, and it did not move.
102
- syncHighlightTheme(throwing);
103
- assert.equal(highlight("plain text", UNKNOWN_LANG), first);
104
- // A different reference is the one thing that still invalidates.
105
- syncHighlightTheme({ getFgAnsi });
106
- assert.notEqual(highlight("plain text", UNKNOWN_LANG), first);
107
- });
@@ -1,131 +0,0 @@
1
- import { test } from "node:test";
2
- import assert from "node:assert/strict";
3
- import { normalizeInfoString, transformFenceLanguages } from "../../src/markdown-fence-lang.ts";
4
-
5
- const PROSE = "This paragraph explains why the transformer runs before rendering.\nIt keeps the fence character and the body untouched.";
6
-
7
- test("normalizeInfoString: an empty info string takes the detected language", () => {
8
- assert.equal(normalizeInfoString("", `{\n "name": "pi"\n}`), "json");
9
- // Nothing detected and nothing declared stays empty: the fence keeps the flat
10
- // color instead of gaining a wrong one.
11
- assert.equal(normalizeInfoString(" ", PROSE), "");
12
- });
13
-
14
- test("normalizeInfoString: a known name or alias is returned as written", () => {
15
- assert.equal(normalizeInfoString("ts", "const x = 1;"), "ts");
16
- assert.equal(normalizeInfoString("{.ts}", "const x = 1;"), "ts");
17
- assert.equal(normalizeInfoString(`js title="a"`, "const x = 1;"), "js");
18
- });
19
-
20
- test("normalizeInfoString: a token the author wrote is returned as written, whatever the body is", () => {
21
- assert.equal(normalizeInfoString("foo", PROSE), "foo");
22
- // Detection only fills a gap: a recognizable body never overrides a label.
23
- assert.equal(normalizeInfoString("foo", "git clone https://example.com/repo.git\ncd repo"), "foo");
24
- assert.equal(normalizeInfoString("powershell", `{\n "name": "pi"\n}`), "powershell");
25
- });
26
-
27
- test("an unlabeled fence gets the detected language and the text around it is copied", () => {
28
- const markdown = `Here is the config:\n\n\`\`\`\n{\n "name": "pi"\n}\n\`\`\`\n\nThat is all.\n`;
29
- assert.equal(
30
- transformFenceLanguages(markdown),
31
- `Here is the config:\n\n\`\`\`json\n{\n "name": "pi"\n}\n\`\`\`\n\nThat is all.\n`,
32
- );
33
- });
34
-
35
- test("a fence that already names a language is left alone", () => {
36
- const markdown = "```js\nconst x = 1;\n```\n";
37
- assert.equal(transformFenceLanguages(markdown), markdown);
38
- });
39
-
40
- test("an info string with attributes keeps only its language", () => {
41
- assert.equal(
42
- transformFenceLanguages(`\`\`\`js title="a"\nconst x = 1;\n\`\`\`\n`),
43
- "```js\nconst x = 1;\n```\n",
44
- );
45
- });
46
-
47
- test("an unknown info string survives, and the body is not detected for it", () => {
48
- const markdown = "```foo\ngit clone https://example.com/repo.git\ncd repo\nnpm install\n```\n";
49
- assert.equal(transformFenceLanguages(markdown), markdown);
50
- });
51
-
52
- test("an unknown info string survives when the body is prose", () => {
53
- const markdown = `\`\`\`foo\n${PROSE}\n\`\`\`\n`;
54
- assert.equal(transformFenceLanguages(markdown), markdown);
55
- });
56
-
57
- test("an unclosed fence is untouched", () => {
58
- const markdown = `\`\`\`\n{\n "name": "pi"\n}\n`;
59
- assert.equal(transformFenceLanguages(markdown), markdown);
60
- });
61
-
62
- test("a block indented four spaces is not a fence and stays untouched", () => {
63
- const markdown = ` \`\`\`\n {"name": "pi"}\n \`\`\`\n`;
64
- assert.equal(transformFenceLanguages(markdown), markdown);
65
- });
66
-
67
- test("a tilde fence is rewritten the same way", () => {
68
- assert.equal(
69
- transformFenceLanguages(`~~~\n{\n "name": "pi"\n}\n~~~\n`),
70
- `~~~json\n{\n "name": "pi"\n}\n~~~\n`,
71
- );
72
- });
73
-
74
- test("with the last fence open only the closed one is rewritten", () => {
75
- const markdown = `\`\`\`\n{\n "name": "pi"\n}\n\`\`\`\n\n\`\`\`\nconst x = 1;\n`;
76
- assert.equal(
77
- transformFenceLanguages(markdown),
78
- `\`\`\`json\n{\n "name": "pi"\n}\n\`\`\`\n\n\`\`\`\nconst x = 1;\n`,
79
- );
80
- });
81
-
82
- test("a closing fence may be longer than the opening one", () => {
83
- assert.equal(
84
- transformFenceLanguages(`\`\`\`\n{\n "name": "pi"\n}\n\`\`\`\`\`\n`),
85
- `\`\`\`json\n{\n "name": "pi"\n}\n\`\`\`\`\`\n`,
86
- );
87
- });
88
-
89
- test("a pandoc attribute list naming the language is unwrapped", () => {
90
- assert.equal(
91
- transformFenceLanguages(`\`\`\`{.ts}\ninterface User {\n id: number;\n}\n\`\`\`\n`),
92
- "```ts\ninterface User {\n id: number;\n}\n```\n",
93
- );
94
- });
95
-
96
- test("the transform is idempotent", () => {
97
- const markdown = `Intro\n\n\`\`\`\n{\n "name": "pi"\n}\n\`\`\`\n\n\`\`\`js title="a"\nconst x = 1;\n\`\`\`\n\n~~~foo\ngit status\n~~~\n`;
98
- const once = transformFenceLanguages(markdown);
99
- assert.notEqual(once, markdown);
100
- assert.equal(transformFenceLanguages(once), once);
101
- });
102
-
103
- test("a correctly labeled fence keeps its token even when the body would be detected", () => {
104
- const powershell = "```powershell\nGet-ChildItem | Where-Object { $_.Name }\n```\n";
105
- assert.equal(transformFenceLanguages(powershell), powershell);
106
- });
107
-
108
- test("a mermaid fence is left to the built-in transformer", () => {
109
- const markdown = "```mermaid\ngraph TD;\n A-->B;\n```\n";
110
- assert.equal(transformFenceLanguages(markdown), markdown);
111
- });
112
-
113
- test("a mermaid fence whose body looks like JSON is still left alone", () => {
114
- const markdown = `\`\`\`mermaid\n{\n "name": "pi"\n}\n\`\`\`\n`;
115
- assert.equal(transformFenceLanguages(markdown), markdown);
116
- });
117
-
118
- test("a reserved info string is compared case-insensitively", () => {
119
- const markdown = `\`\`\`Mermaid\n{\n "name": "pi"\n}\n\`\`\`\n`;
120
- assert.equal(transformFenceLanguages(markdown), markdown);
121
- });
122
-
123
- test("a backtick in the info string of a backtick fence opens nothing", () => {
124
- const markdown = "```js``` is the info string.\n\n```js\nconst x = 1;\n```\n";
125
- assert.equal(transformFenceLanguages(markdown), markdown);
126
- });
127
-
128
- test("a tilde in the info string of a tilde fence opens nothing", () => {
129
- const markdown = "~~~js~~~ is the info string.\n\n~~~js\nconst x = 1;\n~~~\n";
130
- assert.equal(transformFenceLanguages(markdown), markdown);
131
- });
@@ -1,153 +0,0 @@
1
- import { test } from "node:test";
2
- import assert from "node:assert/strict";
3
- import type { MarkdownTransformContext, MarkdownTransformer } from "@earendil-works/pi-coding-agent";
4
- import { createMarkdownTransformer, transformFenceLanguages } from "../../src/markdown-fence-lang.ts";
5
- import {
6
- DIM_OFF,
7
- DIM_ON,
8
- FG_DEFAULT,
9
- ITALIC_OFF,
10
- ITALIC_ON,
11
- UNDERLINE_OFF,
12
- UNDERLINE_ON,
13
- type AnsiTheme,
14
- } from "../../src/tokens.ts";
15
-
16
- const ACCENT = "\x1b[38;5;5m";
17
- const NEXT_ACCENT = "\x1b[38;5;6m";
18
- const ESCAPE = "\u001b";
19
-
20
- // Same shape as the host's theme: the sequences live in a map that a theme switch
21
- // rewrites under the same object, so the fake can show the memo reacting to a value
22
- // change instead of a reference change.
23
- function fakeTheme(): { theme: AnsiTheme; ansi: Record<string, string> } {
24
- const ansi: Record<string, string> = { accent: ACCENT, mdQuote: FG_DEFAULT + DIM_ON };
25
- return {
26
- ansi,
27
- theme: {
28
- getFgAnsi: (color) => ansi[color] ?? "",
29
- getBgAnsi: () => "",
30
- },
31
- };
32
- }
33
-
34
- function context(messageType: MarkdownTransformContext["messageType"]): MarkdownTransformContext {
35
- return { messageType, isStreaming: false, availableWidth: 80 };
36
- }
37
-
38
- function transformerFor(theme: AnsiTheme | undefined): MarkdownTransformer {
39
- return createMarkdownTransformer({ resolveTheme: () => theme });
40
- }
41
-
42
- // Every sequence the pass injects is opened and closed inside the same line, so a
43
- // line wrapped by the renderer can never inherit a style from the one before it.
44
- const SEQUENCE_PAIRS = [
45
- [ACCENT, FG_DEFAULT],
46
- [ITALIC_ON, ITALIC_OFF],
47
- [UNDERLINE_ON, UNDERLINE_OFF],
48
- [DIM_OFF, DIM_ON],
49
- ] as const;
50
-
51
- function assertClosedPerLine(output: string): void {
52
- for (const line of output.split("\n")) {
53
- for (const [on, off] of SEQUENCE_PAIRS) {
54
- assert.equal(occurrences(line, on), occurrences(line, off), `unbalanced ${JSON.stringify(on)} in ${line}`);
55
- }
56
- }
57
- }
58
-
59
- function occurrences(text: string, part: string): number {
60
- return text.split(part).length - 1;
61
- }
62
-
63
- test("the pipeline: an assistant message gets the heading and the quote styles", () => {
64
- const output = transformerFor(fakeTheme().theme)("# Title\n\n> quoted\n", context("assistant"));
65
- assert.ok(output.includes(`${ACCENT}${ITALIC_ON}Title${ITALIC_OFF}${FG_DEFAULT}`));
66
- assert.ok(output.includes(`> ${DIM_OFF}${ACCENT}quoted${FG_DEFAULT}${DIM_ON}`));
67
- });
68
-
69
- test("the pipeline: a user message is exactly what the fence labeling produced", () => {
70
- const markdown = "# Title\n\n```\n{\n \"name\": \"pi\"\n}\n```\n\n- use `tsc`\n";
71
- const output = transformerFor(fakeTheme().theme)(markdown, context("user"));
72
- assert.equal(output, transformFenceLanguages(markdown));
73
- // The escape of the restored delimiters would be printed verbatim in a user
74
- // message (`preserveBackslashEscapes`), so nothing is styled there.
75
- assert.ok(!output.includes(ESCAPE));
76
- });
77
-
78
- test("the pipeline: thinking text is exactly what the fence labeling produced", () => {
79
- const markdown = "# Title\n\n> quoted\n";
80
- const output = transformerFor(fakeTheme().theme)(markdown, context("assistant-thinking"));
81
- assert.equal(output, transformFenceLanguages(markdown));
82
- });
83
-
84
- test("the pipeline: with no captured theme the message is only fence labeled", () => {
85
- const markdown = "# Title\n\n```\n{\n \"name\": \"pi\"\n}\n```\n";
86
- const output = transformerFor(undefined)(markdown, context("assistant"));
87
- assert.equal(output, transformFenceLanguages(markdown));
88
- assert.ok(output.includes("```json"));
89
- assert.ok(!output.includes(ESCAPE));
90
- });
91
-
92
- test("the pipeline: a theme that throws keeps the fence labeling", () => {
93
- const markdown = "```\n{\n \"name\": \"pi\"\n}\n```\n\n# Title\n";
94
- const transformer = createMarkdownTransformer({
95
- resolveTheme: () => {
96
- throw new Error("Theme not initialized. Call initTheme() first.");
97
- },
98
- });
99
- const output = transformer(markdown, context("assistant"));
100
- assert.equal(output, transformFenceLanguages(markdown));
101
- assert.ok(output.startsWith("```json"));
102
- assert.ok(!output.includes(ESCAPE));
103
- });
104
-
105
- test("the pipeline: no injected sequence stays open past its line", () => {
106
- const markdown = "# Title\n\n> quoted `code`\n\n#### Four\n\n- item `x`\n";
107
- const output = transformerFor(fakeTheme().theme)(markdown, context("assistant"));
108
- assertClosedPerLine(output);
109
- });
110
-
111
- test("the pipeline: an unclosed fence leaves the rest of the message alone", () => {
112
- const markdown = "```js\n# not a heading\n> not a quote\n";
113
- // Streaming: the fence has no closing line yet, so its body is only a prefix of
114
- // the final one and styling it would flicker as the stream grows.
115
- assert.equal(transformerFor(fakeTheme().theme)(markdown, context("assistant")), markdown);
116
- });
117
-
118
- test("the pipeline: a lone backtick leaves its line alone", () => {
119
- const markdown = "Run `npm test\n";
120
- assert.equal(transformerFor(fakeTheme().theme)(markdown, context("assistant")), markdown);
121
- });
122
-
123
- test("the pipeline: a heading still streaming keeps its style as the text grows", () => {
124
- const transformer = transformerFor(fakeTheme().theme);
125
- const closes = `${UNDERLINE_OFF}${FG_DEFAULT}`;
126
- assert.equal(transformer("## ", context("assistant")), "## ");
127
- const partial = transformer("## Tit", context("assistant"));
128
- assert.ok(partial.startsWith(`## ${ACCENT}${UNDERLINE_ON}Tit`));
129
- assert.ok(partial.endsWith(closes));
130
- assert.ok(transformer("## Titl", context("assistant")).endsWith(closes));
131
- });
132
-
133
- test("the pipeline: the memo is keyed by the palette signature", () => {
134
- const { theme, ansi } = fakeTheme();
135
- const transformer = transformerFor(theme);
136
- const markdown = "# Title\n";
137
- assert.ok(transformer(markdown, context("assistant")).includes(ACCENT));
138
- ansi.accent = NEXT_ACCENT;
139
- const restyled = transformer(markdown, context("assistant"));
140
- assert.ok(restyled.includes(NEXT_ACCENT));
141
- assert.ok(!restyled.includes(ACCENT));
142
- });
143
-
144
- test("the pipeline: a theme change the pass cannot see keeps the memoized render", () => {
145
- const { theme, ansi } = fakeTheme();
146
- const transformer = transformerFor(theme);
147
- const markdown = "# Title\n";
148
- const first = transformer(markdown, context("assistant"));
149
- // A token the pass never reads: the injected sequences are byte identical, so
150
- // there is nothing to recompute.
151
- ansi.warning = "\x1b[38;5;3m";
152
- assert.equal(transformer(markdown, context("assistant")), first);
153
- });