mirror of
https://github.com/badlogic/pi-mono.git
synced 2026-05-23 21:25:27 +00:00
48 lines
1.7 KiB
TypeScript
48 lines
1.7 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { highlight, renderHighlightedHtml, supportsLanguage } from "../src/utils/syntax-highlight.js";
|
|
|
|
describe("syntax highlight renderer", () => {
|
|
it("renders highlighted spans with the provided theme", () => {
|
|
const rendered = renderHighlightedHtml('<span class="hljs-keyword">const</span> value', {
|
|
keyword: (text) => `[keyword:${text}]`,
|
|
});
|
|
expect(rendered).toBe("[keyword:const] value");
|
|
});
|
|
|
|
it("decodes HTML entities emitted by highlight.js", () => {
|
|
const rendered = renderHighlightedHtml("<tag attr="value">&#x41;A</tag>");
|
|
expect(rendered).toBe('<tag attr="value">AA</tag>');
|
|
});
|
|
|
|
it("inherits parent formatting for unmapped nested scopes", () => {
|
|
const interpolation = "$" + "{x}";
|
|
const rendered = renderHighlightedHtml(
|
|
`<span class="hljs-string">a<span class="hljs-subst">${interpolation}</span>b</span>`,
|
|
{
|
|
string: (text) => `[string:${text}]`,
|
|
},
|
|
);
|
|
expect(rendered).toBe(`[string:a][string:${interpolation}][string:b]`);
|
|
});
|
|
|
|
it("keeps parent formatting across unscoped nested spans", () => {
|
|
const rendered = renderHighlightedHtml('<span class="hljs-string">a<span class="language-xml">b</span>c</span>', {
|
|
string: (text) => `[string:${text}]`,
|
|
});
|
|
expect(rendered).toBe("[string:a][string:b][string:c]");
|
|
});
|
|
|
|
it("highlights code through highlight.js", () => {
|
|
expect(supportsLanguage("typescript")).toBe(true);
|
|
const rendered = highlight("const value = 1", {
|
|
language: "typescript",
|
|
ignoreIllegals: true,
|
|
theme: {
|
|
keyword: (text) => `[keyword:${text}]`,
|
|
number: (text) => `[number:${text}]`,
|
|
},
|
|
});
|
|
expect(rendered).toContain("[keyword:const]");
|
|
expect(rendered).toContain("[number:1]");
|
|
});
|
|
});
|