国庆修改
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import React, { useMemo, useState } from 'react';
|
||||
import React, { useMemo, useState } from 'react';
|
||||
import ReactMarkdown from 'react-markdown';
|
||||
import remarkGfm from 'remark-gfm';
|
||||
import remarkMath from 'remark-math';
|
||||
@@ -12,6 +12,34 @@ import './MarkdownRenderer.css';
|
||||
import 'katex/dist/katex.min.css';
|
||||
import 'highlight.js/styles/github.css';
|
||||
|
||||
// 自定义插件:禁用内联代码解析
|
||||
function remarkDisableInlineCode() {
|
||||
return (tree) => {
|
||||
// 移除所有内联代码节点,将其转换为普通文本
|
||||
function visit(node, parent, index) {
|
||||
if (node.type === 'inlineCode') {
|
||||
// 将内联代码节点替换为文本节点
|
||||
const textNode = {
|
||||
type: 'text',
|
||||
value: node.value
|
||||
};
|
||||
if (parent && typeof index === 'number') {
|
||||
parent.children[index] = textNode;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (node.children) {
|
||||
for (let i = 0; i < node.children.length; i++) {
|
||||
visit(node.children[i], node, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
visit(tree);
|
||||
};
|
||||
}
|
||||
|
||||
function Breadcrumbs({ filePath }) {
|
||||
const breadcrumbs = generateBreadcrumbs(filePath);
|
||||
if (breadcrumbs.length === 0) return null;
|
||||
@@ -32,14 +60,28 @@ function CodeBlock({ inline, className, children, ...props }) {
|
||||
const [copied, setCopied] = useState(false);
|
||||
|
||||
if (inline) {
|
||||
return (
|
||||
<code className="inline-code" {...props}>
|
||||
{children}
|
||||
</code>
|
||||
);
|
||||
// 不渲染为代码,直接返回普通文本
|
||||
return <span>{children}</span>;
|
||||
}
|
||||
|
||||
const codeText = React.Children.toArray(children).join('').replace(/\n$/, '');
|
||||
// 改进的文本提取逻辑,处理React元素和纯文本
|
||||
const extractText = (node) => {
|
||||
if (typeof node === 'string') {
|
||||
return node;
|
||||
}
|
||||
if (typeof node === 'number') {
|
||||
return String(node);
|
||||
}
|
||||
if (React.isValidElement(node)) {
|
||||
return extractText(node.props.children);
|
||||
}
|
||||
if (Array.isArray(node)) {
|
||||
return node.map(extractText).join('');
|
||||
}
|
||||
return '';
|
||||
};
|
||||
|
||||
const codeText = extractText(children).replace(/\n$/, '');
|
||||
const match = /language-(\w+)/.exec(className || '');
|
||||
const language = match ? match[1] : 'text';
|
||||
const buttonClass = 'code-copy-button' + (copied ? ' copied' : '');
|
||||
@@ -152,7 +194,14 @@ export default function MarkdownRenderer() {
|
||||
|
||||
const components = useMemo(
|
||||
() => ({
|
||||
code: CodeBlock,
|
||||
code: ({ inline, className, children, ...props }) => {
|
||||
if (inline) {
|
||||
// 内联代码直接返回普通文本,不做任何特殊处理
|
||||
return <span>{children}</span>;
|
||||
}
|
||||
// 代码块使用原来的CodeBlock组件
|
||||
return <CodeBlock inline={inline} className={className} {...props}>{children}</CodeBlock>;
|
||||
},
|
||||
a: CustomLink,
|
||||
table: CustomTable,
|
||||
...headingComponents,
|
||||
@@ -205,7 +254,6 @@ export default function MarkdownRenderer() {
|
||||
return (
|
||||
<div className={contentAreaClass}>
|
||||
<div className="content-header">
|
||||
<Breadcrumbs filePath={currentFile} />
|
||||
<h1 className="content-title">{fileTitle}</h1>
|
||||
</div>
|
||||
|
||||
@@ -219,7 +267,7 @@ export default function MarkdownRenderer() {
|
||||
) : (
|
||||
<div className="markdown-content">
|
||||
<ReactMarkdown
|
||||
remarkPlugins={[remarkGfm, remarkMath, remarkBreaks]}
|
||||
remarkPlugins={[remarkDisableInlineCode, remarkGfm, remarkMath, remarkBreaks]}
|
||||
rehypePlugins={[rehypeRaw, rehypeKatex, rehypeHighlight]}
|
||||
components={components}
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user