Skip to content

Commit

Permalink
feat: customization of allowedClasses (#85)
Browse files Browse the repository at this point in the history
* feat: customization of allowedClasses

* add newline

* fix test
  • Loading branch information
deer authored Jan 16, 2024
1 parent 6b01882 commit 055a05d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 37 deletions.
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());
},
);

0 comments on commit 055a05d

Please sign in to comment.