Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: yaml is correctly highlighted #96

Merged
merged 8 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import katex from "katex";

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is pulling in yaml for everyone necessary? It seems like the actual fix is the additional types below. Won't that still work if the user app pulls in YAML (like it has to pull in most languages)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly I think it is. The necessary thing is making sure this returns something:

const grammar =

The only way I could do that is by importing the package. For whatever reason I couldn't find any way to invoke the loadlanguages function that prism has.


Marked.marked.use(markedAlert());
Marked.marked.use(gfmHeadingId());
Expand Down Expand Up @@ -254,6 +255,8 @@ export function render(markdown: string, opts: RenderOptions = {}): string {
"line",
"deleted",
"inserted",
"key",
"atrule",
...(opts.allowMath ? KATEX_CLASSES : []),
],
a: ["anchor"],
Expand Down
2 changes: 1 addition & 1 deletion style.ts

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions style/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@
color: var(--color-prettylights-syntax-constant);
}

&.atrule {
color: var(--color-prettylights-syntax-keyword);
}

&.punctuation {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You added key to the options above, but didn't add a matching style. Was that intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the css produced purely by prism is also lacking css for this, although I could be wrong. Is it the styling looking incorrect?

color: var(--color-prettylights-syntax-entity);
}

&.function {
color: var(--color-prettylights-syntax-entity);
}
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/yaml.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<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>
<span class="token key atrule">ray</span><span class="token punctuation">:</span> <span class="token string">"a drop of golden sun"</span>
<span class="token key atrule">pi</span><span class="token punctuation">:</span> <span class="token number">3.14159</span>
<span class="token key atrule">xmas</span><span class="token punctuation">:</span> <span class="token boolean">true</span>
<span class="token key atrule">french-hens</span><span class="token punctuation">:</span> <span class="token number">3</span>
<span class="token key atrule">calling-birds</span><span class="token punctuation">:</span>
<span class="token punctuation">-</span> huey
<span class="token punctuation">-</span> dewey
<span class="token punctuation">-</span> louie
<span class="token punctuation">-</span> fred
<span class="token key atrule">xmas-fifth-day</span><span class="token punctuation">:</span>
<span class="token key atrule">calling-birds</span><span class="token punctuation">:</span> four
<span class="token key atrule">french-hens</span><span class="token punctuation">:</span> <span class="token number">3</span>
<span class="token key atrule">golden-rings</span><span class="token punctuation">:</span> <span class="token number">5</span>
<span class="token key atrule">partridges</span><span class="token punctuation">:</span>
<span class="token key atrule">count</span><span class="token punctuation">:</span> <span class="token number">1</span>
<span class="token key atrule">location</span><span class="token punctuation">:</span> <span class="token string">"a pear tree"</span>
<span class="token key atrule">turtle-doves</span><span class="token punctuation">:</span> two</pre></div>
20 changes: 20 additions & 0 deletions test/fixtures/yaml.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
```yaml
doe: "a deer, a female deer"
ray: "a drop of golden sun"
pi: 3.14159
xmas: true
french-hens: 3
calling-birds:
- huey
- dewey
- louie
- fred
xmas-fifth-day:
calling-birds: four
french-hens: 3
golden-rings: 5
partridges:
count: 1
location: "a pear tree"
turtle-doves: two
```
26 changes: 26 additions & 0 deletions test/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,29 @@ Deno.test("footnote with style", async () => {
assertEquals(footnoteStyle, "rgb(31, 35, 40)");
});
});

Deno.test("yaml style", async () => {
await browserTest("yaml", async (page) => {
const nameStyle = await page.evaluate(() => {
const element = document.querySelector(
"body > main > div > pre > span:nth-child(1)", // doe in the first line
);
if (element) {
return globalThis.getComputedStyle(element).color;
}
return null;
});
assertEquals(nameStyle, "rgb(207, 34, 46)");

const colonStyle = await page.evaluate(() => {
const element = document.querySelector(
"body > main > div > pre > span:nth-child(2)", // : in the first line
);
if (element) {
return globalThis.getComputedStyle(element).color;
}
return null;
});
assertEquals(colonStyle, "rgb(102, 57, 186)");
});
});
8 changes: 8 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,11 @@ Deno.test("example file", () => {
assertEquals(strip(markdown), expectedStrip);
assertEquals(stripSplitBySections(markdown), expectedJSON);
});

Deno.test("yaml unit", () => {
const markdown = Deno.readTextFileSync("./test/fixtures/yaml.md");
const expected = Deno.readTextFileSync("./test/fixtures/yaml.html");

const html = render(markdown);
assertEquals(html, expected.trim());
});
5 changes: 4 additions & 1 deletion test/test_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type TestCase = {
renderOptions?: RenderOptions;
};

export type TestCases = "basicMarkdownTable" | "footnotes";
export type TestCases = "basicMarkdownTable" | "footnotes" | "yaml";

export const testCases: Record<TestCases, TestCase> = {
"basicMarkdownTable": {
Expand All @@ -24,6 +24,9 @@ export const testCases: Record<TestCases, TestCase> = {
"footnotes": {
markdown: Deno.readTextFileSync("./test/fixtures/footnote.md"),
},
"yaml": {
markdown: Deno.readTextFileSync("./test/fixtures/yaml.md"),
},
};

export async function browserTest(
Expand Down