From b3136143aa4f3ed08543c43cae1a48a3f73dc0a0 Mon Sep 17 00:00:00 2001 From: EGOIST Date: Tue, 24 May 2022 11:15:47 +0800 Subject: [PATCH] fix: alias svelte and vue to html --- package.json | 1 + pnpm-lock.yaml | 2 ++ src/markdown/index.ts | 13 +++++++++++-- src/markdown/rehype-image.ts | 1 + src/markdown/rehyper-external-link.ts | 24 ++++++++++++++++++++++++ 5 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 src/markdown/rehyper-external-link.ts diff --git a/package.json b/package.json index 05d5762a..94d8c3f9 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "react-hot-toast": "^2.2.0", "react-query": "^3.39.0", "react-sortablejs": "^6.1.1", + "refractor": "^4.7.0", "rehype-prism-plus": "^1.4.0", "rehype-raw": "^6.1.1", "rehype-sanitize": "^5.0.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6720b8ee..be42fc46 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -58,6 +58,7 @@ specifiers: react-hot-toast: ^2.2.0 react-query: ^3.39.0 react-sortablejs: ^6.1.1 + refractor: ^4.7.0 rehype-prism-plus: ^1.4.0 rehype-raw: ^6.1.1 rehype-sanitize: ^5.0.1 @@ -114,6 +115,7 @@ dependencies: react-hot-toast: 2.2.0_ef5jwxihqo6n7gxfmzogljlgcm react-query: 3.39.0_ef5jwxihqo6n7gxfmzogljlgcm react-sortablejs: 6.1.1_kjz72dk5lh6hp4ogspefhm7r2u + refractor: 4.7.0 rehype-prism-plus: 1.4.0 rehype-raw: 6.1.1 rehype-sanitize: 5.0.1 diff --git a/src/markdown/index.ts b/src/markdown/index.ts index db9443fa..725940f3 100644 --- a/src/markdown/index.ts +++ b/src/markdown/index.ts @@ -5,11 +5,13 @@ import rehypeStringify from "rehype-stringify" import remarkGfm from "remark-gfm" import rehypeSanitize, { defaultSchema } from "rehype-sanitize" import rehypeRaw from "rehype-raw" -import rehypePrism from "rehype-prism-plus" +import { refractor } from "refractor/lib/common" +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 { rehypeExternalLink } from "./rehyper-external-link" export type MarkdownEnv = { excerpt: string @@ -21,6 +23,10 @@ export type Rendered = { excerpt: string } +refractor.alias("html", ["svelte", "vue"]) + +const rehypePrism = rehypePrismGenerator(refractor) + export const renderPageContent = async (content: string): Promise => { const env: MarkdownEnv = { excerpt: "", __internal: {} } @@ -40,9 +46,12 @@ export const renderPageContent = async (content: string): Promise => { blockquote: allowedBlockquoteAttrs, }, }) - .use(rehypePrism) + .use(rehypePrism, { + ignoreMissing: true, + }) .use(rehypeTable) .use(rehypeStringify) + .use(rehypeExternalLink) .process(content) const contentHTML = result.toString() diff --git a/src/markdown/rehype-image.ts b/src/markdown/rehype-image.ts index 195b50d2..b78092e4 100644 --- a/src/markdown/rehype-image.ts +++ b/src/markdown/rehype-image.ts @@ -20,6 +20,7 @@ export const rehypeImage: Plugin, Root> = () => { if (!url.startsWith("https:")) { throw new Error(`External image url must start with https`) } + return } node.properties.src = getUserContentsUrl(url) diff --git a/src/markdown/rehyper-external-link.ts b/src/markdown/rehyper-external-link.ts new file mode 100644 index 00000000..a6f31cf3 --- /dev/null +++ b/src/markdown/rehyper-external-link.ts @@ -0,0 +1,24 @@ +import { Root } from "rehype-raw" +import { Plugin } from "unified" +import { visit } from "unist-util-visit" + +const isExternLink = (url: string) => /^https?:\/\//.test(url) + +export const rehypeExternalLink: Plugin, Root> = () => { + return (tree) => { + visit(tree, { type: "element", tagName: "a" }, (node) => { + if (!node.properties) return + + const url = node.properties.href + + if (!url || typeof url !== "string") { + return + } + + if (isExternLink(url)) { + node.properties.target = "_blank" + node.properties.rel = "noopener noreferrer" + } + }) + } +}