Skip to content

Commit 055a05d

Browse files
authored
feat: customization of allowedClasses (#85)
* feat: customization of allowedClasses * add newline * fix test
1 parent 6b01882 commit 055a05d

File tree

4 files changed

+78
-37
lines changed

4 files changed

+78
-37
lines changed

mod.ts

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export interface RenderOptions {
112112
allowMath?: boolean;
113113
disableHtmlSanitization?: boolean;
114114
renderer?: Renderer;
115+
allowedClasses?: { [index: string]: boolean | Array<string | RegExp> };
115116
}
116117

117118
export function render(markdown: string, opts: RenderOptions = {}): string {
@@ -199,6 +200,44 @@ export function render(markdown: string, opts: RenderOptions = {}): string {
199200
return { tagName, attribs };
200201
}
201202

203+
const defaultAllowedClasses = {
204+
div: [
205+
"highlight",
206+
"highlight-source-*",
207+
"notranslate",
208+
"markdown-alert",
209+
"markdown-alert-*",
210+
],
211+
span: [
212+
"token",
213+
"keyword",
214+
"operator",
215+
"number",
216+
"boolean",
217+
"function",
218+
"string",
219+
"comment",
220+
"class-name",
221+
"regex",
222+
"regex-delimiter",
223+
"tag",
224+
"attr-name",
225+
"punctuation",
226+
"script-punctuation",
227+
"script",
228+
"plain-text",
229+
"property",
230+
"prefix",
231+
"line",
232+
"deleted",
233+
"inserted",
234+
...(opts.allowMath ? KATEX_CLASSES : []),
235+
],
236+
a: ["anchor"],
237+
p: ["markdown-alert-title"],
238+
svg: ["octicon", "octicon-alert", "octicon-link"],
239+
};
240+
202241
return sanitizeHtml(html, {
203242
transformTags: {
204243
img: transformMedia,
@@ -236,43 +275,7 @@ export function render(markdown: string, opts: RenderOptions = {}): string {
236275
math: ["xmlns"], // Only enabled when math is enabled
237276
annotation: ["encoding"], // Only enabled when math is enabled
238277
},
239-
allowedClasses: {
240-
div: [
241-
"highlight",
242-
"highlight-source-*",
243-
"notranslate",
244-
"markdown-alert",
245-
"markdown-alert-*",
246-
],
247-
span: [
248-
"token",
249-
"keyword",
250-
"operator",
251-
"number",
252-
"boolean",
253-
"function",
254-
"string",
255-
"comment",
256-
"class-name",
257-
"regex",
258-
"regex-delimiter",
259-
"tag",
260-
"attr-name",
261-
"punctuation",
262-
"script-punctuation",
263-
"script",
264-
"plain-text",
265-
"property",
266-
"prefix",
267-
"line",
268-
"deleted",
269-
"inserted",
270-
...(opts.allowMath ? KATEX_CLASSES : []),
271-
],
272-
a: ["anchor"],
273-
p: ["markdown-alert-title"],
274-
svg: ["octicon", "octicon-alert", "octicon-link"],
275-
},
278+
allowedClasses: { ...defaultAllowedClasses, ...opts.allowedClasses },
276279
allowProtocolRelative: false,
277280
});
278281
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<ul class="list-disc"><li>a</li>
2+
<li>b</li>
3+
<li>c</li>
4+
</ul><ol class="list-decimal"><li>a</li>
5+
<li>b</li>
6+
<li>c</li>
7+
</ol>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
- a
2+
- b
3+
- c
4+
5+
1. a
6+
2. b
7+
3. c

test/test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,27 @@ Deno.test("Math rendering in code block", () => {
208208
const html = render(markdown, { allowMath: true });
209209
assertEquals(html, expected);
210210
});
211+
212+
Deno.test(
213+
"custom allowed classes",
214+
async () => {
215+
const markdown = await Deno.readTextFile(
216+
"./test/fixtures/customAllowedClasses.md",
217+
);
218+
const expected = await Deno.readTextFile(
219+
"./test/fixtures/customAllowedClasses.html",
220+
);
221+
class CustomRenderer extends Renderer {
222+
list(body: string, ordered: boolean): string {
223+
const type = ordered ? "list-decimal" : "list-disc";
224+
const tag = ordered ? "ol" : "ul";
225+
return `<${tag} class="${type}">${body}</${tag}>`;
226+
}
227+
}
228+
const html = render(markdown, {
229+
renderer: new CustomRenderer({}),
230+
allowedClasses: { ul: ["list-disc"], ol: ["list-decimal"] },
231+
});
232+
assertEquals(html, expected.trim());
233+
},
234+
);

0 commit comments

Comments
 (0)