Skip to content

Commit

Permalink
feat: add disable_html_sanitization frontmatter option (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
lino-levan authored Feb 3, 2023
1 parent 06641e4 commit 0a43328
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 17 deletions.
4 changes: 3 additions & 1 deletion blog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import {
callsites,
ColorScheme,
createReporter,
dirname,
Feed,
Expand Down Expand Up @@ -101,6 +102,7 @@ function errorHandler(err: unknown) {
*/
export default async function blog(settings?: BlogSettings) {
html.use(UnoCSS(settings?.unocss)); // Load custom unocss module if provided
html.use(ColorScheme("auto"));

const url = callsites()[1].getFileName()!;
const blogState = await configureBlog(url, IS_DEV, settings);
Expand Down Expand Up @@ -266,6 +268,7 @@ async function loadPost(postsDirectory: string, path: string) {
ogImage: data.get("og:image"),
tags: data.get("tags"),
allowIframes: data.get("allow_iframes"),
disableHtmlSanitization: data.get("disable_html_sanitization"),
readTime: readingTime(content),
};
POSTS.set(pathname, post);
Expand Down Expand Up @@ -310,7 +313,6 @@ export async function handler(
}

const sharedHtmlOptions: HtmlOptions = {
colorScheme: blogState.theme ?? "auto",
lang: blogState.lang ?? "en",
scripts: IS_DEV ? [{ src: "/hmr.js" }] : undefined,
links: [
Expand Down
26 changes: 24 additions & 2 deletions blog_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
assert,
assertEquals,
assertStringIncludes,
} from "https://deno.land/std@0.171.0/testing/asserts.ts";
import { fromFileUrl, join } from "https://deno.land/std@0.171.0/path/mod.ts";
} from "https://deno.land/std@0.176.0/testing/asserts.ts";
import { fromFileUrl, join } from "https://deno.land/std@0.176.0/path/mod.ts";

const BLOG_URL = new URL("./testdata/main.js", import.meta.url).href;
const TESTDATA_PATH = fromFileUrl(new URL("./testdata/", import.meta.url));
Expand Down Expand Up @@ -138,6 +138,28 @@ Deno.test("posts/ third", async () => {
assertStringIncludes(body, `<p>Lorem Ipsum is simply dummy text`);
});

Deno.test("posts/ fourth", async () => {
const resp = await testHandler(new Request("https://blog.deno.dev/fourth"));
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text();
assertStringIncludes(body, `<html lang="en-GB">`);
assertStringIncludes(
body,
`<link rel="canonical" href="https://blog.deno.dev/fourth" />`,
);
assertStringIncludes(body, `Fourth post`);
assertStringIncludes(
body,
`<time dateTime="2023-01-30T00:00:00.000Z">`,
);
assertStringIncludes(
body,
`<button onclick="alert('hi!')">Click me!!!!!!</button>`,
);
});

Deno.test("posts/ 中文", async () => {
const resp = await testHandler(new Request("https://blog.deno.dev/中文"));
assert(resp);
Expand Down
1 change: 1 addition & 0 deletions components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ interface PostPageProps {
export function PostPage({ post, state }: PostPageProps) {
const html = gfm.render(post.markdown, {
allowIframes: post.allowIframes,
disableHtmlSanitization: post.disableHtmlSanitization,
});
return (
<div className={`post ${post.pathname.substring(1)}`}>
Expand Down
25 changes: 13 additions & 12 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
// Copyright 2022 the Deno authors. All rights reserved. MIT license.

export { serveDir } from "https://deno.land/std@0.171.0/http/file_server.ts";
export { walk } from "https://deno.land/std@0.171.0/fs/walk.ts";
export { serveDir } from "https://deno.land/std@0.176.0/http/file_server.ts";
export { walk } from "https://deno.land/std@0.176.0/fs/walk.ts";
export {
dirname,
fromFileUrl,
join,
relative,
} from "https://deno.land/std@0.171.0/path/mod.ts";
} from "https://deno.land/std@0.176.0/path/mod.ts";
export {
type ConnInfo,
serve,
} from "https://deno.land/std@0.171.0/http/mod.ts";
export { extract as frontMatter } from "https://deno.land/std@0.171.0/encoding/front_matter.ts";
} from "https://deno.land/std@0.176.0/http/mod.ts";
export { extract as frontMatter } from "https://deno.land/std@0.176.0/encoding/front_matter/any.ts";

export * as gfm from "https://deno.land/x/[email protected]/mod.ts";
export * as gfm from "https://deno.land/x/[email protected]/mod.ts";
export { Fragment, h } from "https://deno.land/x/[email protected]/mod.ts";
export {
Fragment,
h,
html,
default as html,
type HtmlOptions,
type VNode,
} from "https://deno.land/x/[email protected]/mod.tsx";
import { UnoCSS } from "https://deno.land/x/[email protected]/plugins.ts";
} from "https://deno.land/x/[email protected]/html.tsx";
import UnoCSS from "https://deno.land/x/[email protected]/plugins/unocss.ts";
import ColorScheme from "https://deno.land/x/[email protected]/plugins/color-scheme.ts";

export {
createReporter,
type Reporter as GaReporter,
Expand All @@ -34,7 +35,7 @@ export { default as removeMarkdown } from "https://esm.sh/[email protected]"
// Add syntax highlighting support for C by default
import "https://esm.sh/[email protected]/components/prism-c?no-check";

export { UnoCSS };
export { ColorScheme, UnoCSS };
export type UnoConfig = typeof UnoCSS extends (
arg: infer P | undefined,
) => unknown ? P
Expand Down
4 changes: 2 additions & 2 deletions init.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2022 the Deno authors. All rights reserved. MIT license.

import { join, resolve } from "https://deno.land/std@0.171.0/path/mod.ts";
import { join, resolve } from "https://deno.land/std@0.176.0/path/mod.ts";

const HELP = `deno_blog
Expand Down Expand Up @@ -71,7 +71,7 @@ const DENO_JSONC_CONTENTS = `{
const IMPORT_MAP_JSON_NAME = "import_map.json";
const IMPORT_MAP_JSON_CONTENTS = `{
"imports": {
"blog": "https://deno.land/x/blog@0.4.2/blog.tsx"
"blog": "https://deno.land/x/blog@0.5.0/blog.tsx"
}
}
`;
Expand Down
10 changes: 10 additions & 0 deletions testdata/posts/fourth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: Fourth post
publish_date: 2023-01-30
abstract: Image deno_blog but with unsantized HTML...
disable_html_sanitization: true
---

## Cool Button Demo

<button onclick="alert('hi!')">Click me!!!!!!</button>
1 change: 1 addition & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,6 @@ export interface Post {
ogImage?: string;
tags?: string[];
allowIframes?: boolean;
disableHtmlSanitization?: boolean;
readTime: number;
}

1 comment on commit 0a43328

@capogreco
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am having trouble getting this working - should this work out of the box in the latest version?

Please sign in to comment.