syntax-highlight.test.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { resetCapabilitiesCache, setCapabilities } from "@earendil-works/pi-tui";
  2. import { afterEach, beforeEach, describe, expect, it } from "vitest";
  3. import { highlightCode, initTheme } from "../src/modes/interactive/theme/theme.ts";
  4. import { highlight, renderHighlightedHtml, supportsLanguage } from "../src/utils/syntax-highlight.ts";
  5. describe("syntax highlight renderer", () => {
  6. it("renders highlighted spans with the provided theme", () => {
  7. const rendered = renderHighlightedHtml('<span class="hljs-keyword">const</span> value', {
  8. keyword: (text) => `[keyword:${text}]`,
  9. });
  10. expect(rendered).toBe("[keyword:const] value");
  11. });
  12. it("decodes HTML entities emitted by highlight.js", () => {
  13. const rendered = renderHighlightedHtml("&lt;tag attr=&quot;value&quot;&gt;&amp;#x41;&#65;&lt;/tag&gt;");
  14. expect(rendered).toBe('<tag attr="value">&#x41;A</tag>');
  15. });
  16. it("inherits parent formatting for unmapped nested scopes", () => {
  17. const interpolation = "$" + "{x}";
  18. const rendered = renderHighlightedHtml(
  19. `<span class="hljs-string">a<span class="hljs-subst">${interpolation}</span>b</span>`,
  20. {
  21. string: (text) => `[string:${text}]`,
  22. },
  23. );
  24. expect(rendered).toBe(`[string:a][string:${interpolation}][string:b]`);
  25. });
  26. it("keeps parent formatting across unscoped nested spans", () => {
  27. const rendered = renderHighlightedHtml('<span class="hljs-string">a<span class="language-xml">b</span>c</span>', {
  28. string: (text) => `[string:${text}]`,
  29. });
  30. expect(rendered).toBe("[string:a][string:b][string:c]");
  31. });
  32. it("highlights code through highlight.js", () => {
  33. expect(supportsLanguage("typescript")).toBe(true);
  34. const rendered = highlight("const value = 1", {
  35. language: "typescript",
  36. ignoreIllegals: true,
  37. theme: {
  38. keyword: (text) => `[keyword:${text}]`,
  39. number: (text) => `[number:${text}]`,
  40. },
  41. });
  42. expect(rendered).toContain("[keyword:const]");
  43. expect(rendered).toContain("[number:1]");
  44. });
  45. });
  46. describe("theme syntax highlighting", () => {
  47. beforeEach(() => {
  48. setCapabilities({ images: null, trueColor: true, hyperlinks: false });
  49. initTheme("dark");
  50. });
  51. afterEach(() => {
  52. resetCapabilitiesCache();
  53. });
  54. it("colors diff additions and deletions in fenced diff blocks", () => {
  55. const lines = highlightCode("-old\n+new\n", "diff");
  56. expect(lines[0]).toBe("\x1b[38;2;204;102;102m-old\x1b[39m");
  57. expect(lines[1]).toBe("\x1b[38;2;181;189;104m+new\x1b[39m");
  58. });
  59. it("keeps cli-highlight default styled scopes mapped to theme styles", () => {
  60. expect(highlightCode("const re = /foo+/gi;", "javascript")[0]).toContain(
  61. "\x1b[38;2;206;145;120m/foo+/gi\x1b[39m",
  62. );
  63. expect(highlightCode("@decorator", "python")[0]).toBe("\x1b[38;2;128;128;128m@decorator\x1b[39m");
  64. expect(highlightCode("<div></div>", "html")[0]).toContain("\x1b[38;2;86;156;214mdiv\x1b[39m");
  65. });
  66. });