Skip to content

Commit

Permalink
Merge branch 'main' into 60_yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
deer committed Jan 29, 2024
2 parents b9fc9f3 + 2582968 commit 7e9991e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
6 changes: 3 additions & 3 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ import "https://esm.sh/[email protected]/components/prism-yaml";
Marked.marked.use(markedAlert());
Marked.marked.use(gfmHeadingId());

const slugger = new GitHubSlugger();

export class Renderer extends Marked.Renderer {
allowMath: boolean;
baseUrl: string | undefined;
#slugger: GitHubSlugger;

constructor(options: Marked.MarkedOptions & RenderOptions = {}) {
super(options);
this.baseUrl = options.baseUrl;
this.allowMath = options.allowMath ?? false;
this.#slugger = new GitHubSlugger();
}

heading(
text: string,
level: 1 | 2 | 3 | 4 | 5 | 6,
raw: string,
): string {
const slug = slugger.slug(raw);
const slug = this.#slugger.slug(raw);
return `<h${level} id="${slug}"><a class="anchor" aria-hidden="true" tabindex="-1" href="#${slug}"><svg class="octicon octicon-link" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>${text}</h${level}>`;
}

Expand Down
6 changes: 3 additions & 3 deletions test/server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Deno.test("basic md table with dollar signs", async () => {
if (!element) {
return null;
}
const style = window.getComputedStyle(element);
const style = globalThis.getComputedStyle(element);
return style[property];
},
selector,
Expand Down Expand Up @@ -85,7 +85,7 @@ Deno.test("yaml style", async () => {
"body > main > div > pre > span:nth-child(1)",
);
if (element) {
return window.getComputedStyle(element).color;
return globalThis.getComputedStyle(element).color;
}
return null;
});
Expand All @@ -96,7 +96,7 @@ Deno.test("yaml style", async () => {
"body > main > div > pre > span:nth-child(2)",
);
if (element) {
return window.getComputedStyle(element).color;
return globalThis.getComputedStyle(element).color;
}
return null;
});
Expand Down
9 changes: 9 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,15 @@ Deno.test("expect console warning from invalid math", () => {
console.warn = originalWarn;
});

Deno.test("render github-slugger not reused", function () {
for (let i = 0; i < 2; i++) {
const html = render("## Hello");
const expected =
`<h2 id="hello"><a class="anchor" aria-hidden="true" tabindex="-1" href="#hello"><svg class="octicon octicon-link" viewbox="0 0 16 16" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a>Hello</h2>`;
assertEquals(html, expected);
}
});

Deno.test("yaml unit", () => {
const markdown = Deno.readTextFileSync("./test/fixtures/yaml.md");
const expected = Deno.readTextFileSync("./test/fixtures/yaml.html");
Expand Down
3 changes: 3 additions & 0 deletions test/test_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export function startServer() {
body = render(testCase.markdown, testCase.renderOptions);
} else if (route === "") {
body = render(generateIndexMarkdown());
} else if (route === "favicon.ico") {
// swallow
} else {
console.log(route);
throw new Error("Invalid route specified");
}
const htmlContent = wrapBody(body);
Expand Down

0 comments on commit 7e9991e

Please sign in to comment.