Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ› fix: Fix LaTeX Render #168

Merged
merged 4 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{');
}
Loading