fix: Fix the issue of LaTeX content displaying incorrectly in TOC (#382)

This commit is contained in:
我永远喜欢英梨梨
2023-04-20 20:08:30 +01:00
committed by GitHub
parent 912663b10e
commit 129822d08d
4 changed files with 528 additions and 279 deletions
+3
View File
@@ -78,8 +78,10 @@
"idb-keyval": "6.2.0",
"immer": "9.0.21",
"ioredis": "5.3.2",
"isomorphic-dompurify": "^1.3.0",
"js-yaml": "4.1.0",
"jsonfeed-to-rss": "^3.0.6",
"katex": "^0.16.6",
"langchain": "^0.0.59",
"lottie-react": "^2.4.0",
"mdast-util-to-string": "3.2.0",
@@ -149,6 +151,7 @@
"@types/cookie": "0.5.1",
"@types/hast": "2.3.4",
"@types/js-yaml": "4.0.5",
"@types/katex": "^0.16.0",
"@types/mjml": "4.7.1",
"@types/node": "18.15.11",
"@types/prismjs": "1.26.0",
+491 -275
View File
File diff suppressed because it is too large Load Diff
+29 -4
View File
@@ -1,3 +1,5 @@
import DOMPurify from "isomorphic-dompurify"
import katex from "katex"
import type { Result as TocResult } from "mdast-util-toc"
import React, { useEffect, useRef, useState } from "react"
import { Link } from "react-scroll"
@@ -54,9 +56,17 @@ function renderItems(items: TocResult["map"], activeId: string, prefix = "") {
{items?.children?.map((item, index) => (
<li key={index}>
{item.children.map((child: any, i) => {
const content =
child.children[0].children?.[0]?.value ||
child.children[0].children?.[0]?.children?.[0]?.value
const children =
child.children[0].children?.[0]?.children ||
child.children[0].children
let content = "",
isInlineMath = false
children.map((child: any) => {
if (child.type === "inlineMath") {
isInlineMath = true
}
content += child.value
})
return (
<span key={index + "-" + i}>
{child.type === "paragraph" && child.children?.[0]?.url && (
@@ -77,7 +87,22 @@ function renderItems(items: TocResult["map"], activeId: string, prefix = "") {
" truncate inline-block max-w-full align-bottom hover:text-accent"
}
>
{`${prefix}${index + 1}. ${content}`}
{isInlineMath ? (
<span
dangerouslySetInnerHTML={{
__html: DOMPurify.sanitize(
katex.renderToString(
`${prefix}${index + 1}. ${content}`,
{
output: "html",
},
),
),
}}
/>
) : (
`${prefix}${index + 1}. ${content}`
)}
</Link>
)}
{child.type === "list" &&
+5
View File
@@ -393,3 +393,8 @@ textarea.input {
.xlog-lottie-logo path[fill="rgb(0,0,0)"] {
@apply fill-current;
}
.xlog-post-toc .katex {
font: unset;
line-height: unset;
}