From 97072c5ce269245d1146e6eb1933670db8b7306d Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 18 Jan 2024 12:14:16 +0800 Subject: [PATCH] feat: add rehypeRemoveH1 and rehypeMention, remove rehypeRewrite --- package.json | 1 - pnpm-lock.yaml | 12 --------- src/markdown/index.ts | 46 +++----------------------------- src/markdown/rehype-mention.ts | 42 +++++++++++++++++++++++++++++ src/markdown/rehype-remove-h1.ts | 15 +++++++++++ 5 files changed, 61 insertions(+), 55 deletions(-) create mode 100644 src/markdown/rehype-mention.ts create mode 100644 src/markdown/rehype-remove-h1.ts diff --git a/package.json b/package.json index d316199670..f67cdd26ff 100644 --- a/package.json +++ b/package.json @@ -130,7 +130,6 @@ "rehype-katex": "7.0.0", "rehype-prism-plus": "2.0.0", "rehype-raw": "7.0.0", - "rehype-rewrite": "4.0.2", "rehype-sanitize": "6.0.0", "rehype-slug": "6.0.0", "remark-breaks": "4.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 01cb5a3b40..36bcf101f5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -309,9 +309,6 @@ dependencies: rehype-raw: specifier: 7.0.0 version: 7.0.0 - rehype-rewrite: - specifier: 4.0.2 - version: 4.0.2 rehype-sanitize: specifier: 6.0.0 version: 6.0.0 @@ -12651,15 +12648,6 @@ packages: vfile: 6.0.1 dev: false - /rehype-rewrite@4.0.2: - resolution: {integrity: sha512-rjLJ3z6fIV11phwCqHp/KRo8xuUCO8o9bFJCNw5o6O2wlLk6g8r323aRswdGBQwfXPFYeSuZdAjp4tzo6RGqEg==} - engines: {node: '>=16.0.0'} - dependencies: - hast-util-select: 6.0.2 - unified: 11.0.4 - unist-util-visit: 5.0.0 - dev: false - /rehype-sanitize@6.0.0: resolution: {integrity: sha512-CsnhKNsyI8Tub6L4sm5ZFsme4puGfc6pYylvXo1AeqaGbjOYyzNv3qZPwvs0oMJ39eryyeOdmxwUIo94IpEhqg==} dependencies: diff --git a/src/markdown/index.ts b/src/markdown/index.ts index dbd594b901..8d91b30416 100644 --- a/src/markdown/index.ts +++ b/src/markdown/index.ts @@ -17,7 +17,6 @@ import rehypeInferDescriptionMeta from "rehype-infer-description-meta" import rehypeKatex from "rehype-katex" import rehypePrismGenerator from "rehype-prism-plus/generator" import rehypeRaw, { Options as RehypeRawOptions } from "rehype-raw" -import rehypeRewrite from "rehype-rewrite" import rehypeSanitize from "rehype-sanitize" import rehypeSlug from "rehype-slug" import remarkBreaks from "remark-breaks" @@ -48,6 +47,8 @@ import { } from "./rehype-custom-wrapper" import { rehypeEmbed } from "./rehype-embed" import { rehypeIpfs } from "./rehype-ipfs" +import { rehypeMention } from "./rehype-mention" +import { rehypeRemoveH1 } from "./rehype-remove-h1" import { rehypeTable } from "./rehype-table" import { rehypeWrapCode } from "./rehype-wrap-code" import { rehypeExternalLink } from "./rehyper-external-link" @@ -129,6 +130,7 @@ export const renderPageContent = (content: string, strictMode?: boolean) => { .use(rehypeEmbed, { transformers, }) + .use(rehypeRemoveH1) .use(rehypePrism, { ignoreMissing: true, showLineNumbers: true, @@ -136,47 +138,7 @@ export const renderPageContent = (content: string, strictMode?: boolean) => { .use(rehypeKatex, { strict: false, }) - .use(rehypeRewrite, { - selector: "p, li, h1", - rewrite: (node: any) => { - if (node.tagName === "h1") { - node.tagName = "h2" - return - } - if (node.children) { - node.children = node.children.flatMap((child: any) => { - if (child.type === "text") { - const mentionRegex = /(@[\w-]+)/g - if (mentionRegex.test(child.value)) { - const parts = child.value.split(mentionRegex) - return parts.map((part: string) => { - if (part.startsWith("@")) { - return { - type: "element", - tagName: "mention", - children: [{ type: "text", value: part }], - } - } else { - return { - type: "text", - value: part, - } - } - }) - } else { - return child - } - } else { - return child - } - }) - } - }, - }) - // TODO - .use(rehypeRaw, { - passThrough: allowedCustomWrappers, - } as RehypeRawOptions) + .use(rehypeMention) // markdown abstract syntax tree mdastTree = pipeline.parse(file) diff --git a/src/markdown/rehype-mention.ts b/src/markdown/rehype-mention.ts new file mode 100644 index 0000000000..b618fc96fb --- /dev/null +++ b/src/markdown/rehype-mention.ts @@ -0,0 +1,42 @@ +import type { Root } from "hast" +import { Plugin } from "unified" +import { visit } from "unist-util-visit" + +export const rehypeMention: Plugin, Root> = () => { + return (tree: Root) => { + visit(tree, (node, i, parent) => { + if (node.type === "element") { + if (node.tagName === "p" || node.tagName === "li") { + if (node.children) { + node.children = node.children.flatMap((child: any) => { + if (child.type === "text") { + const mentionRegex = /(@[\w-]+)/g + if (mentionRegex.test(child.value)) { + const parts = child.value.split(mentionRegex) + return parts.map((part: string) => { + if (part.startsWith("@")) { + return { + type: "element", + tagName: "mention", + children: [{ type: "text", value: part }], + } + } else { + return { + type: "text", + value: part, + } + } + }) + } else { + return child + } + } else { + return child + } + }) + } + } + } + }) + } +} diff --git a/src/markdown/rehype-remove-h1.ts b/src/markdown/rehype-remove-h1.ts new file mode 100644 index 0000000000..6b7c69a80b --- /dev/null +++ b/src/markdown/rehype-remove-h1.ts @@ -0,0 +1,15 @@ +import type { Root } from "hast" +import { Plugin } from "unified" +import { visit } from "unist-util-visit" + +export const rehypeRemoveH1: Plugin, Root> = () => { + return (tree: Root) => { + visit(tree, (node, i, parent) => { + if (node.type === "element") { + if (node.tagName === "h1") { + node.tagName = "h2" + } + } + }) + } +}