Skip to content

Commit f4ae669

Browse files
authored
fix: correctly handle yaml highlighting (#96)
1 parent a7a63b2 commit f4ae669

File tree

8 files changed

+88
-2
lines changed

8 files changed

+88
-2
lines changed

mod.ts

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import katex from "katex";
1111

1212
import { CSS, KATEX_CLASSES, KATEX_CSS } from "./style.ts";
1313
export { CSS, KATEX_CSS, Marked };
14+
import "https://esm.sh/[email protected]/components/prism-yaml";
1415

1516
Marked.marked.use(markedAlert());
1617
Marked.marked.use(gfmHeadingId());
@@ -254,6 +255,8 @@ export function render(markdown: string, opts: RenderOptions = {}): string {
254255
"line",
255256
"deleted",
256257
"inserted",
258+
"key",
259+
"atrule",
257260
...(opts.allowMath ? KATEX_CLASSES : []),
258261
],
259262
a: ["anchor"],

style.ts

+1-1
Large diffs are not rendered by default.

style/main.scss

+8
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@
7575
color: var(--color-prettylights-syntax-constant);
7676
}
7777

78+
&.atrule {
79+
color: var(--color-prettylights-syntax-keyword);
80+
}
81+
82+
&.punctuation {
83+
color: var(--color-prettylights-syntax-entity);
84+
}
85+
7886
&.function {
7987
color: var(--color-prettylights-syntax-entity);
8088
}

test/fixtures/yaml.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<div class="highlight highlight-source-yaml notranslate"><pre><span class="token key atrule">doe</span><span class="token punctuation">:</span> <span class="token string">"a deer, a female deer"</span>
2+
<span class="token key atrule">ray</span><span class="token punctuation">:</span> <span class="token string">"a drop of golden sun"</span>
3+
<span class="token key atrule">pi</span><span class="token punctuation">:</span> <span class="token number">3.14159</span>
4+
<span class="token key atrule">xmas</span><span class="token punctuation">:</span> <span class="token boolean">true</span>
5+
<span class="token key atrule">french-hens</span><span class="token punctuation">:</span> <span class="token number">3</span>
6+
<span class="token key atrule">calling-birds</span><span class="token punctuation">:</span>
7+
<span class="token punctuation">-</span> huey
8+
<span class="token punctuation">-</span> dewey
9+
<span class="token punctuation">-</span> louie
10+
<span class="token punctuation">-</span> fred
11+
<span class="token key atrule">xmas-fifth-day</span><span class="token punctuation">:</span>
12+
<span class="token key atrule">calling-birds</span><span class="token punctuation">:</span> four
13+
<span class="token key atrule">french-hens</span><span class="token punctuation">:</span> <span class="token number">3</span>
14+
<span class="token key atrule">golden-rings</span><span class="token punctuation">:</span> <span class="token number">5</span>
15+
<span class="token key atrule">partridges</span><span class="token punctuation">:</span>
16+
<span class="token key atrule">count</span><span class="token punctuation">:</span> <span class="token number">1</span>
17+
<span class="token key atrule">location</span><span class="token punctuation">:</span> <span class="token string">"a pear tree"</span>
18+
<span class="token key atrule">turtle-doves</span><span class="token punctuation">:</span> two</pre></div>

test/fixtures/yaml.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
```yaml
2+
doe: "a deer, a female deer"
3+
ray: "a drop of golden sun"
4+
pi: 3.14159
5+
xmas: true
6+
french-hens: 3
7+
calling-birds:
8+
- huey
9+
- dewey
10+
- louie
11+
- fred
12+
xmas-fifth-day:
13+
calling-birds: four
14+
french-hens: 3
15+
golden-rings: 5
16+
partridges:
17+
count: 1
18+
location: "a pear tree"
19+
turtle-doves: two
20+
```

test/server_test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,29 @@ Deno.test("footnote with style", async () => {
152152
assertEquals(footnoteStyle, "rgb(31, 35, 40)");
153153
});
154154
});
155+
156+
Deno.test("yaml style", async () => {
157+
await browserTest("yaml", async (page) => {
158+
const nameStyle = await page.evaluate(() => {
159+
const element = document.querySelector(
160+
"body > main > div > pre > span:nth-child(1)", // doe in the first line
161+
);
162+
if (element) {
163+
return globalThis.getComputedStyle(element).color;
164+
}
165+
return null;
166+
});
167+
assertEquals(nameStyle, "rgb(207, 34, 46)");
168+
169+
const colonStyle = await page.evaluate(() => {
170+
const element = document.querySelector(
171+
"body > main > div > pre > span:nth-child(2)", // : in the first line
172+
);
173+
if (element) {
174+
return globalThis.getComputedStyle(element).color;
175+
}
176+
return null;
177+
});
178+
assertEquals(colonStyle, "rgb(102, 57, 186)");
179+
});
180+
});

test/test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -440,3 +440,11 @@ Deno.test("example file", () => {
440440
assertEquals(strip(markdown), expectedStrip);
441441
assertEquals(stripSplitBySections(markdown), expectedJSON);
442442
});
443+
444+
Deno.test("yaml unit", () => {
445+
const markdown = Deno.readTextFileSync("./test/fixtures/yaml.md");
446+
const expected = Deno.readTextFileSync("./test/fixtures/yaml.html");
447+
448+
const html = render(markdown);
449+
assertEquals(html, expected.trim());
450+
});

test/test_utils.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type TestCase = {
99
renderOptions?: RenderOptions;
1010
};
1111

12-
export type TestCases = "basicMarkdownTable" | "footnotes";
12+
export type TestCases = "basicMarkdownTable" | "footnotes" | "yaml";
1313

1414
export const testCases: Record<TestCases, TestCase> = {
1515
"basicMarkdownTable": {
@@ -24,6 +24,9 @@ export const testCases: Record<TestCases, TestCase> = {
2424
"footnotes": {
2525
markdown: Deno.readTextFileSync("./test/fixtures/footnote.md"),
2626
},
27+
"yaml": {
28+
markdown: Deno.readTextFileSync("./test/fixtures/yaml.md"),
29+
},
2730
};
2831

2932
export async function browserTest(

0 commit comments

Comments
 (0)