feat: add rehypeRemoveH1 and rehypeMention, remove rehypeRewrite

This commit is contained in:
DIYgod 2024-01-18 12:14:16 +08:00
parent 25aac4c7ea
commit 97072c5ce2
No known key found for this signature in database
5 changed files with 61 additions and 55 deletions

View File

@ -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",

View File

@ -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:

View File

@ -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)

View File

@ -0,0 +1,42 @@
import type { Root } from "hast"
import { Plugin } from "unified"
import { visit } from "unist-util-visit"
export const rehypeMention: Plugin<Array<void>, 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
}
})
}
}
}
})
}
}

View File

@ -0,0 +1,15 @@
import type { Root } from "hast"
import { Plugin } from "unified"
import { visit } from "unist-util-visit"
export const rehypeRemoveH1: Plugin<Array<void>, Root> = () => {
return (tree: Root) => {
visit(tree, (node, i, parent) => {
if (node.type === "element") {
if (node.tagName === "h1") {
node.tagName = "h2"
}
}
})
}
}