Skip to content

Commit

Permalink
🐛 fix: Fix LaTeX Render (lobehub#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 authored Jun 24, 2024
1 parent bd99a10 commit ee0b740
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
"@types/react": "18.2.40",
"@types/react-dom": "^18.3.0",
"@types/uuid": "^9.0.8",
"@vitest/coverage-v8": "^1.6.0",
"@vitest/coverage-v8": "~1.2.2",
"antd-style": "^3.6.2",
"babel-plugin-antd-style": "^1.0.4",
"commitlint": "^19.3.0",
Expand Down
15 changes: 12 additions & 3 deletions src/Markdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { CodeFullFeatured, CodeLite } from './CodeBlock';
import type { TypographyProps } from './Typography';
import { useStyles as useMarkdownStyles } from './markdown.style';
import { useStyles } from './style';
import { escapeBrackets, escapeDollarNumber, escapeMhchem } from './utils';

export interface MarkdownProps extends TypographyProps {
allowHtml?: boolean;
Expand All @@ -33,6 +34,7 @@ export interface MarkdownProps extends TypographyProps {
video?: Partial<VideoProps>;
};
enableImageGallery?: boolean;
enableLatex?: boolean;
fullFeaturedCodeBlock?: boolean;
onDoubleClick?: () => void;
style?: CSSProperties;
Expand All @@ -46,6 +48,7 @@ const Markdown = memo<MarkdownProps>(
style,
fullFeaturedCodeBlock,
onDoubleClick,
enableLatex = true,
enableImageGallery = true,
componentProps,
allowHtml,
Expand All @@ -60,6 +63,11 @@ const Markdown = memo<MarkdownProps>(
const { styles: mdStyles } = useMarkdownStyles({ fontSize, headerMultiple, marginMultiple });
const isChatMode = variant === 'chat';

const escapedContent = useMemo(() => {
if (!enableLatex) return children;
return escapeMhchem(escapeBrackets(escapeDollarNumber(children)));
}, [children, enableLatex]);

const components: Components = useMemo(
() => ({
a: (props: any) => <Link {...props} {...componentProps?.a} />,
Expand Down Expand Up @@ -88,11 +96,12 @@ const Markdown = memo<MarkdownProps>(
);

const rehypePlugins = useMemo(
() => [allowHtml && rehypeRaw, rehypeKatex].filter(Boolean) as any,
() => [allowHtml && rehypeRaw, enableLatex && rehypeKatex].filter(Boolean) as any,
[allowHtml],
);
const remarkPlugins = useMemo(
() => [remarkGfm, remarkMath, isChatMode && remarkBreaks].filter(Boolean) as any,
() =>
[remarkGfm, enableLatex && remarkMath, isChatMode && remarkBreaks].filter(Boolean) as any,
[isChatMode],
);

Expand Down Expand Up @@ -128,7 +137,7 @@ const Markdown = memo<MarkdownProps>(
remarkPlugins={remarkPlugins}
{...rest}
>
{children}
{escapedContent}
</ReactMarkdown>
</ImageGallery>
</article>
Expand Down
34 changes: 34 additions & 0 deletions src/Markdown/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export function escapeDollarNumber(text: string) {
let escapedText = '';

for (let i = 0; i < text.length; i += 1) {
let char = text[i];
const nextChar = text[i + 1] || ' ';

if (char === '$' && nextChar >= '0' && nextChar <= '9') {
char = '\\$';
}

escapedText += char;
}

return escapedText;
}

export function escapeBrackets(text: string) {
const pattern = /(```[\S\s]*?```|`.*?`)|\\\[([\S\s]*?[^\\])\\]|\\\((.*?)\\\)/g;
return text.replaceAll(pattern, (match, codeBlock, squareBracket, roundBracket) => {
if (codeBlock) {
return codeBlock;
} else if (squareBracket) {
return `$$${squareBracket}$$`;
} else if (roundBracket) {
return `$${roundBracket}$`;
}
return match;
});
}

export function escapeMhchem(text: string) {
return text.replaceAll('$\\ce{', '$\\\\ce{').replaceAll('$\\pu{', '$\\\\pu{');
}

0 comments on commit ee0b740

Please sign in to comment.