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

feat: customization of allowedClasses #85

Merged
merged 5 commits into from
Jan 16, 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
77 changes: 40 additions & 37 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export interface RenderOptions {
allowMath?: boolean;
disableHtmlSanitization?: boolean;
renderer?: Renderer;
allowedClasses?: { [index: string]: boolean | Array<string | RegExp> };
}

export function render(markdown: string, opts: RenderOptions = {}): string {
Expand Down Expand Up @@ -199,6 +200,44 @@ export function render(markdown: string, opts: RenderOptions = {}): string {
return { tagName, attribs };
}

const defaultAllowedClasses = {
div: [
"highlight",
"highlight-source-*",
"notranslate",
"markdown-alert",
"markdown-alert-*",
],
span: [
"token",
"keyword",
"operator",
"number",
"boolean",
"function",
"string",
"comment",
"class-name",
"regex",
"regex-delimiter",
"tag",
"attr-name",
"punctuation",
"script-punctuation",
"script",
"plain-text",
"property",
"prefix",
"line",
"deleted",
"inserted",
...(opts.allowMath ? KATEX_CLASSES : []),
],
a: ["anchor"],
p: ["markdown-alert-title"],
svg: ["octicon", "octicon-alert", "octicon-link"],
};

return sanitizeHtml(html, {
transformTags: {
img: transformMedia,
Expand Down Expand Up @@ -236,43 +275,7 @@ export function render(markdown: string, opts: RenderOptions = {}): string {
math: ["xmlns"], // Only enabled when math is enabled
annotation: ["encoding"], // Only enabled when math is enabled
},
allowedClasses: {
div: [
"highlight",
"highlight-source-*",
"notranslate",
"markdown-alert",
"markdown-alert-*",
],
span: [
"token",
"keyword",
"operator",
"number",
"boolean",
"function",
"string",
"comment",
"class-name",
"regex",
"regex-delimiter",
"tag",
"attr-name",
"punctuation",
"script-punctuation",
"script",
"plain-text",
"property",
"prefix",
"line",
"deleted",
"inserted",
...(opts.allowMath ? KATEX_CLASSES : []),
],
a: ["anchor"],
p: ["markdown-alert-title"],
svg: ["octicon", "octicon-alert", "octicon-link"],
},
allowedClasses: { ...defaultAllowedClasses, ...opts.allowedClasses },
allowProtocolRelative: false,
});
}
7 changes: 7 additions & 0 deletions test/fixtures/customAllowedClasses.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<ul class="list-disc"><li>a</li>
<li>b</li>
<li>c</li>
</ul><ol class="list-decimal"><li>a</li>
<li>b</li>
<li>c</li>
</ol>
7 changes: 7 additions & 0 deletions test/fixtures/customAllowedClasses.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- a
- b
- c

1. a
2. b
3. c
24 changes: 24 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,27 @@ Deno.test("Math rendering in code block", () => {
const html = render(markdown, { allowMath: true });
assertEquals(html, expected);
});

Deno.test(
"custom allowed classes",
async () => {
const markdown = await Deno.readTextFile(
"./test/fixtures/customAllowedClasses.md",
);
const expected = await Deno.readTextFile(
"./test/fixtures/customAllowedClasses.html",
);
class CustomRenderer extends Renderer {
list(body: string, ordered: boolean): string {
const type = ordered ? "list-decimal" : "list-disc";
const tag = ordered ? "ol" : "ul";
return `<${tag} class="${type}">${body}</${tag}>`;
}
}
const html = render(markdown, {
renderer: new CustomRenderer({}),
allowedClasses: { ul: ["list-disc"], ol: ["list-decimal"] },
});
assertEquals(html, expected.trim());
},
);