File size: 1,999 Bytes
bf2abd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Auto-detection of syntax highlighting for language-less code blocks.
 *
 * Code blocks authored without an explicit language must still be highlighted
 * (in both the editor and the published output). These tests cover the
 * publisher path end-to-end plus the shared detector in isolation.
 */
import { describe, it, expect } from "vitest";
import { renderArticleHTML, type PublishMeta } from "../src/publisher/html-renderer.js";
import type { PublishCSS } from "../src/publisher/index.js";
import { detectShikiLang } from "../src/shared/detect-lang.js";

const EMPTY_CSS: PublishCSS = {
  variables: "",
  reset: "",
  base: "",
  layout: "",
  print: "",
  editorTokens: "",
  article: "",
  components: "",
  publisher: "",
};

const META: PublishMeta = {
  title: "Code Article",
  description: "code highlighting",
  authors: [{ name: "Alice", affiliationIndices: [1], affiliationNames: ["MIT"] }],
  affiliations: [{ name: "MIT" }],
  date: "2025-01-01",
};

const PY = `import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

def load(name):
    return AutoModelForCausalLM.from_pretrained(name)`;

describe("detectShikiLang", () => {
  it("detects python from a typical snippet", () => {
    expect(detectShikiLang(PY)).toBe("python");
  });

  it("returns empty for trivially short input", () => {
    expect(detectShikiLang("x")).toBe("");
  });
});

describe("publisher highlights language-less code blocks", () => {
  it("colors a code block that has no language attribute", async () => {
    const json = {
      type: "doc",
      content: [
        // Note: no `attrs.language` - mirrors how these blocks are authored.
        { type: "codeBlock", content: [{ type: "text", text: PY }] },
      ],
    };
    const html = await renderArticleHTML(json as any, META, EMPTY_CSS);

    // Detected language label and per-token color vars must be present.
    expect(html).toContain('data-lang="python"');
    expect(html).toContain("--shiki-light:");
  });
});