Skip to content

Commit

Permalink
feat: add rehypeRemoveH1 and rehypeMention, remove rehypeRewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Jan 18, 2024
1 parent 25aac4c commit 97072c5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 55 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 0 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 4 additions & 42 deletions src/markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -129,54 +130,15 @@ export const renderPageContent = (content: string, strictMode?: boolean) => {
.use(rehypeEmbed, {
transformers,
})
.use(rehypeRemoveH1)
.use(rehypePrism, {
ignoreMissing: true,
showLineNumbers: true,
})
.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)
Expand Down
42 changes: 42 additions & 0 deletions src/markdown/rehype-mention.ts
Original file line number Diff line number Diff line change
@@ -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
}
})
}
}
}
})
}
}
15 changes: 15 additions & 0 deletions src/markdown/rehype-remove-h1.ts
Original file line number Diff line number Diff line change
@@ -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"
}
}
})
}
}

0 comments on commit 97072c5

Please sign in to comment.