From 5ba43d179f27d1a55666cc2737ad19bbd1fb9d77 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Mon, 3 Oct 2022 17:39:22 +0100 Subject: [PATCH] refactor: markdown sanitize schema module --- src/markdown/index.ts | 45 ++------------------- src/markdown/sanitize-schema.ts | 46 ++++++++++++++++++++++ src/pages/dashboard/[subdomain]/editor.tsx | 1 - 3 files changed, 50 insertions(+), 42 deletions(-) create mode 100644 src/markdown/sanitize-schema.ts diff --git a/src/markdown/index.ts b/src/markdown/index.ts index d4f084a2..f1ab1de2 100644 --- a/src/markdown/index.ts +++ b/src/markdown/index.ts @@ -4,14 +4,14 @@ import remarkRehype from "remark-rehype" import remarkFrontmatter from "remark-frontmatter" import rehypeStringify from "rehype-stringify" import remarkGfm from "remark-gfm" -import rehypeSanitize, { defaultSchema } from "rehype-sanitize" +import rehypeSanitize from "rehype-sanitize" import rehypeRaw from "rehype-raw" import { refractor } from "refractor" import rehypePrismGenerator from "rehype-prism-plus/generator" import { rehypeImage } from "./rehype-image" import { remarkExcerpt } from "./remark-excerpt" import { rehypeTable } from "./rehype-table" -import { allowedBlockquoteAttrs, remarkCallout } from "./remark-callout" +import { remarkCallout } from "./remark-callout" import { rehypeExternalLink } from "./rehyper-external-link" import { rehypeWrapCode } from "./rehype-wrap-code" import jsYaml from "js-yaml" @@ -21,6 +21,7 @@ import { Image } from "~/components/ui/Image" import remarkDirective from "remark-directive" import remarkDirectiveRehype from "remark-directive-rehype" import { remarkYoutube } from "./remark-youtube" +import sanitizeScheme from "./sanitize-schema" export type MarkdownEnv = { excerpt: string @@ -76,45 +77,7 @@ export const renderPageContent = ( .use(rehypeStringify) .use(rehypeRaw) .use(rehypeImage, { env }) - .use(rehypeSanitize, { - ...defaultSchema, - tagNames: [ - ...(defaultSchema.tagNames || []), - "video", - "iframe", - "style", - "youtube", - ], - attributes: { - ...defaultSchema.attributes, - div: [ - ...(defaultSchema.attributes?.div || []), - ["className"], - ["style"], - ["id"], - ], - code: [["className"]], - blockquote: allowedBlockquoteAttrs, - video: [ - ["className"], - ["src"], - ["controls"], - ["loop"], - ["muted"], - ["autoplay"], - ["playsinline"], - ], - iframe: [ - ["className"], - ["src"], - ["allowFullScreen"], - ["frameborder"], - ["width"], - ["height"], - ["allow"], - ], - }, - }) + .use(rehypeSanitize, sanitizeScheme) .use(rehypePrism, { ignoreMissing: true, showLineNumbers: true, diff --git a/src/markdown/sanitize-schema.ts b/src/markdown/sanitize-schema.ts new file mode 100644 index 00000000..1f1fd24f --- /dev/null +++ b/src/markdown/sanitize-schema.ts @@ -0,0 +1,46 @@ +import { defaultSchema } from "rehype-sanitize" +import { allowedBlockquoteAttrs } from "./remark-callout" +// @ts-ignore +import type { Schema } from "hast-util-sanitize/lib" + +const scheme: Schema = { + ...defaultSchema, + tagNames: [ + ...(defaultSchema.tagNames || []), + "video", + "iframe", + "style", + "youtube", + ], + attributes: { + ...defaultSchema.attributes, + div: [ + ...(defaultSchema.attributes?.div || []), + ["className"], + ["style"], + ["id"], + ], + code: [["className"]], + blockquote: allowedBlockquoteAttrs, + video: [ + ["className"], + ["src"], + ["controls"], + ["loop"], + ["muted"], + ["autoplay"], + ["playsinline"], + ], + iframe: [ + ["className"], + ["src"], + ["allowFullScreen"], + ["frameborder"], + ["width"], + ["height"], + ["allow"], + ], + }, +} + +export default scheme diff --git a/src/pages/dashboard/[subdomain]/editor.tsx b/src/pages/dashboard/[subdomain]/editor.tsx index d45d914c..caa3bbe2 100644 --- a/src/pages/dashboard/[subdomain]/editor.tsx +++ b/src/pages/dashboard/[subdomain]/editor.tsx @@ -193,7 +193,6 @@ export default function SubdomainEditor() { }) useEffect(() => { - console.log("!page.data", page.data) if (!page.data || !draftKey) return const local = getStorage(draftKey)