Skip to content

Commit ffb2b09

Browse files
committed
fix: inline code bug
1 parent e54373b commit ffb2b09

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

src/components/mdRender/MDRender.tsx

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ReactMarkdown from 'react-markdown';
22
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
3-
import { atomDark, vs } from 'react-syntax-highlighter/dist/cjs/styles/prism';
3+
import { vs, vsDark } from 'react-syntax-highlighter/dist/cjs/styles/prism';
44
import remarkGfm from 'remark-gfm';
55

66
import clsxm from '@/lib/clsxm';
@@ -26,7 +26,7 @@ export const CodeRender = ({ code, lanuage }: CodeProps) => {
2626
);
2727
} else {
2828
return (
29-
<SyntaxHighlighter style={atomDark} language={lanuage} PreTag='div'>
29+
<SyntaxHighlighter style={vsDark} language={lanuage} PreTag='div'>
3030
{code}
3131
</SyntaxHighlighter>
3232
);
@@ -51,43 +51,39 @@ export const MDRender = ({
5151
const getComponents = (theme: string) => {
5252
return {
5353
// 代码块渲染
54-
code({ node: _node, inline, className, children, ...props }: any) {
54+
code(props: any) {
55+
const { inline, className, children } = props;
56+
const isInline =
57+
typeof inline === 'boolean'
58+
? inline
59+
: typeof children === 'string' && !children.includes('\n');
60+
if (isInline) {
61+
return (
62+
<code className='rounded bg-gray-100 px-1.5 py-0.5 font-mono text-sm text-gray-800 dark:bg-gray-700 dark:text-gray-200'>
63+
{children}
64+
</code>
65+
);
66+
}
5567
const match = /language-(\w+)/.exec(className || '') || 'shell';
56-
if (!inline && match) {
57-
children = String(children).replace(/\n$/, '');
58-
if (theme != 'dark') {
59-
return (
60-
<SyntaxHighlighter
61-
style={vs}
62-
language={match[1]}
63-
PreTag='div'
64-
{...props}
65-
>
66-
{children}
67-
</SyntaxHighlighter>
68-
);
69-
} else {
70-
return (
71-
<SyntaxHighlighter
72-
style={atomDark}
73-
language={match[1]}
74-
PreTag='div'
75-
{...props}
76-
>
77-
{children}
78-
</SyntaxHighlighter>
79-
);
80-
}
81-
} else {
68+
if (match) {
8269
return (
83-
<span
84-
className='rounded-sm bg-gray-100 px-1.5 py-0.5 text-sm font-medium dark:bg-gray-600'
85-
{...props}
70+
<SyntaxHighlighter
71+
style={theme !== 'dark' ? vs : vsDark}
72+
language={match[1]}
73+
PreTag='pre'
8674
>
87-
{children}
88-
</span>
75+
{String(children).replace(/\n$/, '')}
76+
</SyntaxHighlighter>
8977
);
9078
}
79+
return (
80+
<span
81+
className='rounded-sm bg-gray-100 px-1.5 py-0.5 text-sm
82+
font-medium dark:bg-gray-600'
83+
>
84+
{children}
85+
</span>
86+
);
9187
},
9288
};
9389
};

src/styles/globals.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,8 @@ svg {
301301
background: #3282f6;
302302
height: 3px;
303303
}
304+
305+
code::before,
306+
code::after {
307+
content: none !important;
308+
}

0 commit comments

Comments
 (0)