Skip to content

Commit 2a7937b

Browse files
committed
support iframes
1 parent f4c4470 commit 2a7937b

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

example/content.md

+2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ async function serve(conn: Deno.Conn) {
8989
You can find a deeper introduction, examples, and environment setup guides in
9090
the [manual](https://deno.land/manual).
9191

92+
<iframe width="100%" height="600" src="https://embed.deno.com/playground/urlpattern?layout=both"></iframe>
93+
9294
The complete API reference is available at the runtime
9395
[documentation](https://doc.deno.land).
9496

example/main.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ const CONTENT_PATH = new URL("./content.md", import.meta.url);
1212
async function handler(_req: Request): Promise<Response> {
1313
try {
1414
const markdown = await Deno.readTextFile(CONTENT_PATH);
15-
const body = render(markdown, "/");
15+
const body = render(markdown, {
16+
allowIframes: true,
17+
});
1618
const html = `<!DOCTYPE html>
1719
<html lang="en">
1820
<head>

mod.ts

+17-7
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,30 @@ class Renderer extends marked.Renderer {
3434
}
3535
}
3636

37-
export function render(markdown: string, baseUrl: string | undefined): string {
37+
export interface RenderOptions {
38+
baseUrl?: string;
39+
allowIframes?: boolean;
40+
}
41+
42+
export function render(markdown: string, opts: RenderOptions = {}): string {
3843
markdown = emojify(markdown);
3944

4045
const html = marked(markdown, {
41-
baseUrl,
46+
baseUrl: opts.baseUrl,
4247
gfm: true,
4348
renderer: new Renderer(),
4449
});
4550

51+
const allowedTags = sanitizeHtml.defaults.allowedTags.concat([
52+
"img",
53+
"svg",
54+
"path",
55+
]);
56+
if (opts.allowIframes) {
57+
allowedTags.push("iframe");
58+
}
4659
return sanitizeHtml(html, {
47-
allowedTags: sanitizeHtml.defaults.allowedTags.concat([
48-
"img",
49-
"svg",
50-
"path",
51-
]),
60+
allowedTags,
5261
allowedAttributes: {
5362
...sanitizeHtml.defaults.allowedAttributes,
5463
img: ["src", "alt", "height", "width", "align"],
@@ -61,6 +70,7 @@ export function render(markdown: string, baseUrl: string | undefined): string {
6170
h4: ["id"],
6271
h5: ["id"],
6372
h6: ["id"],
73+
iframe: ["src", "width", "height"], // Only used when iframe tags are allowed in the first place.
6474
},
6575
allowedClasses: {
6676
div: ["highlight"],

style.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

style/main.scss

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
ul {
1616
list-style: disc;
1717
}
18+
19+
iframe {
20+
background-color: white;
21+
border: 0;
22+
margin-bottom: 16px;
23+
}
1824
}
1925

2026
@import "@primer/css/markdown/index.scss";

0 commit comments

Comments
 (0)