feat: only replace the mention syntax within the p tag

This commit is contained in:
DIYgod
2023-04-15 06:17:42 +01:00
parent ee47a59bc6
commit d7a9c1dc1b
5 changed files with 51 additions and 10 deletions
+2
View File
@@ -63,6 +63,7 @@
"emoji-mart": "5.5.2",
"fast-average-color": "^9.3.0",
"fast-deep-equal": "3.1.3",
"hast-util-to-text": "^3.1.2",
"i18next": "^22.4.14",
"idb-keyval": "6.2.0",
"immer": "9.0.21",
@@ -98,6 +99,7 @@
"rehype-prism-plus": "1.5.1",
"rehype-raw": "6.1.1",
"rehype-react": "7.2.0",
"rehype-rewrite": "^3.0.6",
"rehype-sanitize": "5.0.1",
"rehype-slug": "5.1.0",
"rehype-stringify": "9.0.3",
+17 -2
View File
@@ -118,6 +118,9 @@ dependencies:
fast-deep-equal:
specifier: 3.1.3
version: 3.1.3
hast-util-to-text:
specifier: ^3.1.2
version: 3.1.2
i18next:
specifier: ^22.4.14
version: 22.4.14
@@ -223,6 +226,9 @@ dependencies:
rehype-react:
specifier: 7.2.0
version: 7.2.0(@types/react@18.0.35)
rehype-rewrite:
specifier: ^3.0.6
version: 3.0.6
rehype-sanitize:
specifier: 5.0.1
version: 5.0.1
@@ -9059,7 +9065,7 @@ packages:
dependencies:
universalify: 2.0.0
optionalDependencies:
graceful-fs: 4.2.10
graceful-fs: 4.2.11
dev: true
/jsonparse@1.3.1:
@@ -9551,7 +9557,7 @@ packages:
resolution: {integrity: sha512-SobxkQXFAdd4b5WmEakmkVoh18icjQRxGy5OWTCzgsLRm1Fu/KCtwD1HIQSsmq5ZRjVH0Ehwg6/Fn3xIUk+nKw==}
dependencies:
escape-string-regexp: 5.0.0
unist-util-is: 5.1.1
unist-util-is: 5.2.0
unist-util-visit-parents: 5.1.1
dev: false
@@ -11660,6 +11666,15 @@ packages:
unified: 10.1.2
dev: false
/rehype-rewrite@3.0.6:
resolution: {integrity: sha512-REDTNCvsKcAazy8IQWzKp66AhSUDSOIKssSCqNqCcT9sN7JCwAAm3mWGTUdUzq80ABuy8d0D6RBwbnewu1aY1g==}
engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
dependencies:
hast-util-select: 5.0.4
unified: 10.1.2
unist-util-visit: 4.1.2
dev: false
/rehype-sanitize@5.0.1:
resolution: {integrity: sha512-da/jIOjq8eYt/1r9GN6GwxIR3gde7OZ+WV8pheu1tL8K0D9KxM2AyMh+UEfke+FfdM3PvGHeYJU0Td5OWa7L5A==}
dependencies:
+1 -1
View File
@@ -111,7 +111,7 @@ export const ConnectButton: React.FC<{
icon: "i-mingcute:home-1-line",
label: t("My xLog") || "",
url: getSiteLink({
subdomain: userSites.data?.[0].username || "",
subdomain: userSites.data?.[0]?.username || "",
}),
},
{
+1 -4
View File
@@ -1,5 +1,4 @@
import { FC, useEffect, useState } from "react"
import { useTranslation } from "react-i18next"
import { useIsDark } from "~/hooks/useDarkMode"
import { useIsUnmounted } from "~/hooks/useLifecycle"
import { nanoid } from "nanoid"
@@ -52,11 +51,9 @@ export const Mermaid: FC<{
}
}, [props.children])
const { t } = useTranslation("common")
return loading ? (
<div className="h-[50px] rounded-lg flex items-center justify-center bg-[#ECECFD] dark:bg-[#1F2020] text-sm">
Mermaid {t("Loading")}...
Mermaid Loading...
</div>
) : svg ? (
<div dangerouslySetInnerHTML={{ __html: svg }} />
+30 -3
View File
@@ -33,6 +33,8 @@ import rehypeInferDescriptionMeta from "rehype-infer-description-meta"
import remarkBreaks from "remark-breaks"
import { remarkMermaid } from "./remark-mermaid"
import { Mermaid } from "~/components/ui/Mermaid"
import rehypeRewrite from "rehype-rewrite"
import { toText } from "hast-util-to-text"
export type MarkdownEnv = {
excerpt: string
@@ -70,8 +72,6 @@ export const renderPageContent = (
tree: null,
}
content = content.replace(/(@\w+)/g, "<mention>$1</mention>")
let contentHTML = ""
let result: any = null
try {
@@ -119,7 +119,34 @@ export const renderPageContent = (
.use(rehypeWrapCode)
.use(rehypeInferDescriptionMeta)
.use(rehypeSlug)
.use(rehypeRewrite, {
selector: "p",
rewrite: (node: any) => {
if (node.children) {
node.children = node.children.flatMap((child: any) => {
if (child.type === "text") {
const parts = toText(child).split(/(@\w+)/g)
return parts.map((part) => {
if (part.startsWith("@")) {
return {
type: "element",
tagName: "mention",
children: [{ type: "text", value: part }],
}
} else {
return {
type: "text",
value: part,
}
}
})
} else {
return child
}
})
}
},
})
.use(rehypeAutolinkHeadings, {
properties: {
className: ["xlog-anchor"],