File size: 3,804 Bytes
fb4d8fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { describe, expect, it } from "vitest";
import { markdownTheme } from "./theme.js";

describe("markdownTheme", () => {
  describe("highlightCode", () => {
    it("should return an array of lines for JavaScript code", () => {
      const code = `const x = 42;`;
      const result = markdownTheme.highlightCode!(code, "javascript");

      expect(result).toBeInstanceOf(Array);
      expect(result).toHaveLength(1);
      // Result should contain the original code (possibly with ANSI codes)
      expect(result[0]).toContain("const");
      expect(result[0]).toContain("42");
    });

    it("should return correct line count for multi-line code", () => {
      const code = `function greet(name: string) {
  return "Hello, " + name;
}`;
      const result = markdownTheme.highlightCode!(code, "typescript");

      expect(result).toHaveLength(3);
      expect(result[0]).toContain("function");
      expect(result[1]).toContain("return");
      expect(result[2]).toContain("}");
    });

    it("should handle Python code", () => {
      const code = `def hello():
    print("world")`;
      const result = markdownTheme.highlightCode!(code, "python");

      expect(result).toHaveLength(2);
      expect(result[0]).toContain("def");
      expect(result[1]).toContain("print");
    });

    it("should handle unknown languages gracefully", () => {
      const code = `const x = 42;`;
      const result = markdownTheme.highlightCode!(code, "not-a-real-language");

      expect(result).toBeInstanceOf(Array);
      expect(result).toHaveLength(1);
      // Should still return the code content
      expect(result[0]).toContain("const");
    });

    it("should handle code without language specifier", () => {
      const code = `echo "hello"`;
      const result = markdownTheme.highlightCode!(code, undefined);

      expect(result).toBeInstanceOf(Array);
      expect(result).toHaveLength(1);
      expect(result[0]).toContain("echo");
    });

    it("should handle empty code", () => {
      const result = markdownTheme.highlightCode!("", "javascript");

      expect(result).toBeInstanceOf(Array);
      expect(result).toHaveLength(1);
      expect(result[0]).toBe("");
    });

    it("should handle bash/shell code", () => {
      const code = `#!/bin/bash
echo "Hello"
for i in {1..5}; do
  echo $i
done`;
      const result = markdownTheme.highlightCode!(code, "bash");

      expect(result).toHaveLength(5);
      expect(result[0]).toContain("#!/bin/bash");
      expect(result[1]).toContain("echo");
    });

    it("should handle JSON", () => {
      const code = `{"name": "test", "count": 42, "active": true}`;
      const result = markdownTheme.highlightCode!(code, "json");

      expect(result).toHaveLength(1);
      expect(result[0]).toContain("name");
      expect(result[0]).toContain("42");
    });

    it("should handle code with special characters", () => {
      const code = `const regex = /\\d+/g;
const str = "Hello\\nWorld";`;
      const result = markdownTheme.highlightCode!(code, "javascript");

      expect(result).toHaveLength(2);
      // Should not throw and should return valid output
      expect(result[0].length).toBeGreaterThan(0);
      expect(result[1].length).toBeGreaterThan(0);
    });

    it("should preserve code content through highlighting", () => {
      const code = `const message = "Hello, World!";
console.log(message);`;
      const result = markdownTheme.highlightCode!(code, "javascript");

      // Strip ANSI codes to verify content is preserved
      const stripAnsi = (str: string) =>
        str.replace(new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*m`, "g"), "");
      expect(stripAnsi(result[0])).toBe(`const message = "Hello, World!";`);
      expect(stripAnsi(result[1])).toBe("console.log(message);");
    });
  });
});