From 0a43328bc1d890ed21ea8938bac049d78001250a Mon Sep 17 00:00:00 2001 From: Lino Le Van <11367844+lino-levan@users.noreply.github.com> Date: Fri, 3 Feb 2023 11:25:28 -0800 Subject: [PATCH] feat: add disable_html_sanitization frontmatter option (#114) --- blog.tsx | 4 +++- blog_test.ts | 26 ++++++++++++++++++++++++-- components.tsx | 1 + deps.ts | 25 +++++++++++++------------ init.ts | 4 ++-- testdata/posts/fourth.md | 10 ++++++++++ types.d.ts | 1 + 7 files changed, 54 insertions(+), 17 deletions(-) create mode 100644 testdata/posts/fourth.md diff --git a/blog.tsx b/blog.tsx index efb65f6..bdd8880 100644 --- a/blog.tsx +++ b/blog.tsx @@ -8,6 +8,7 @@ import { callsites, + ColorScheme, createReporter, dirname, Feed, @@ -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); @@ -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); @@ -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: [ diff --git a/blog_test.ts b/blog_test.ts index 08dca93..2da36eb 100644 --- a/blog_test.ts +++ b/blog_test.ts @@ -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)); @@ -138,6 +138,28 @@ Deno.test("posts/ third", async () => { assertStringIncludes(body, `

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, ``); + assertStringIncludes( + body, + ``, + ); + assertStringIncludes(body, `Fourth post`); + assertStringIncludes( + body, + `

diff --git a/deps.ts b/deps.ts index 2da62c3..bfba5ed 100644 --- a/deps.ts +++ b/deps.ts @@ -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/gfm@0.1.27/mod.ts"; +export * as gfm from "https://deno.land/x/gfm@0.1.30/mod.ts"; +export { Fragment, h } from "https://deno.land/x/htm@0.1.3/mod.ts"; export { - Fragment, - h, - html, + default as html, type HtmlOptions, type VNode, -} from "https://deno.land/x/htm@0.0.10/mod.tsx"; -import { UnoCSS } from "https://deno.land/x/htm@0.0.10/plugins.ts"; +} from "https://deno.land/x/htm@0.1.3/html.tsx"; +import UnoCSS from "https://deno.land/x/htm@0.1.3/plugins/unocss.ts"; +import ColorScheme from "https://deno.land/x/htm@0.1.3/plugins/color-scheme.ts"; + export { createReporter, type Reporter as GaReporter, @@ -34,7 +35,7 @@ export { default as removeMarkdown } from "https://esm.sh/remove-markdown@0.5.0" // Add syntax highlighting support for C by default import "https://esm.sh/prismjs@1.29.0/components/prism-c?no-check"; -export { UnoCSS }; +export { ColorScheme, UnoCSS }; export type UnoConfig = typeof UnoCSS extends ( arg: infer P | undefined, ) => unknown ? P diff --git a/init.ts b/init.ts index b452086..ca7f7d6 100644 --- a/init.ts +++ b/init.ts @@ -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 @@ -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" } } `; diff --git a/testdata/posts/fourth.md b/testdata/posts/fourth.md new file mode 100644 index 0000000..fdcccee --- /dev/null +++ b/testdata/posts/fourth.md @@ -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 + + diff --git a/types.d.ts b/types.d.ts index e6af427..03e12c1 100644 --- a/types.d.ts +++ b/types.d.ts @@ -97,5 +97,6 @@ export interface Post { ogImage?: string; tags?: string[]; allowIframes?: boolean; + disableHtmlSanitization?: boolean; readTime: number; }