Skip to content

Commit 9bae701

Browse files
committed
doc: add katex example.
1 parent 00d7fb1 commit 9bae701

File tree

2 files changed

+87
-7
lines changed

2 files changed

+87
-7
lines changed

core/README.md

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,66 @@ export default function Demo() {
241241
<!--rehype:ignore:start-->Ignored content<!--rehype:ignore:end-->
242242
```
243243

244+
## Support Custom KaTeX Preview
245+
246+
KaTeX is a fast, easy-to-use JavaScript library for TeX math rendering on the web, We perform math rendering through [`KaTeX`](https://github.com/KaTeX/KaTeX).
247+
248+
```bash
249+
npm install katex
250+
```
251+
252+
```jsx mdx:preview?background=#fff
253+
import React from 'react';
254+
import MarkdownPreview from '@uiw/react-markdown-preview';
255+
import { getCodeString } from 'rehype-rewrite';
256+
import katex from 'katex';
257+
import 'katex/dist/katex.css';
258+
259+
const source = `This is to display the
260+
\`\$\$\c = \\pm\\sqrt{a^2 + b^2}\$\$\`
261+
in one line
262+
263+
\`\`\`KaTeX
264+
c = \\pm\\sqrt{a^2 + b^2}
265+
\`\`\`
266+
`;
267+
268+
export default function Demo() {
269+
const [value, setValue] = React.useState(source);
270+
return (
271+
<MarkdownPreview
272+
source={source}
273+
components={{
274+
code: ({ children = [], className, ...props }) => {
275+
if (typeof children === 'string' && /^\$\$(.*)\$\$/.test(children)) {
276+
const html = katex.renderToString(children.replace(/^\$\$(.*)\$\$/, '$1'), {
277+
throwOnError: false,
278+
});
279+
return <code dangerouslySetInnerHTML={{ __html: html }} style={{ background: 'transparent' }} />;
280+
}
281+
const code = props.node && props.node.children ? getCodeString(props.node.children) : children;
282+
if (
283+
typeof code === 'string' &&
284+
typeof className === 'string' &&
285+
/^language-katex/.test(className.toLocaleLowerCase())
286+
) {
287+
const html = katex.renderToString(code, {
288+
throwOnError: false,
289+
});
290+
return <code style={{ fontSize: '150%' }} dangerouslySetInnerHTML={{ __html: html }} />;
291+
}
292+
return <code className={String(className)}>{children}</code>;
293+
},
294+
}}
295+
/>
296+
);
297+
}
298+
```
299+
244300
## Support Custom Mermaid Preview
245301

302+
Using [mermaid](https://github.com/mermaid-js/mermaid) to generation of diagram and flowchart from text in a similar manner as markdown
303+
246304
[![Open in CodeSandbox](https://img.shields.io/badge/Open%20in-CodeSandbox-blue?logo=codesandbox)](https://codesandbox.io/embed/react-markdown-preview-https-github-com-uiwjs-react-markdown-preview-issues-238-lw6vr5?fontsize=14&hidenavigation=1&theme=dark)
247305

248306
```jsx mdx:preview?background=#fff
@@ -289,16 +347,37 @@ const Code = ({ inline, children = [], className, ...props }) => {
289347
}
290348
return <code>{children}</code>;
291349
};
350+
const source = `The following are some examples of the diagrams, charts and graphs that can be made using Mermaid and the Markdown-inspired text specific to it.
351+
352+
\`\`\`mermaid
353+
graph TD
354+
A[Hard] -->|Text| B(Round)
355+
B --> C{Decision}
356+
C -->|One| D[Result 1]
357+
C -->|Two| E[Result 2]
358+
\`\`\`
292359
293-
const source = `
294360
\`\`\`mermaid
295-
graph TD;
296-
A-->B;
297-
A-->C;
298-
B-->D;
299-
C-->D;
361+
sequenceDiagram
362+
Alice->>John: Hello John, how are you?
363+
loop Healthcheck
364+
John->>John: Fight against hypochondria
365+
end
366+
Note right of John: Rational thoughts!
367+
John-->>Alice: Great!
368+
John->>Bob: How about you?
369+
Bob-->>John: Jolly good!
300370
\`\`\`
301371
`;
372+
// const source = `
373+
// \`\`\`mermaid
374+
// graph TD;
375+
// A-->B;
376+
// A-->C;
377+
// B-->D;
378+
// C-->D;
379+
// \`\`\`
380+
// `;
302381

303382
export default function Demo() {
304383
return (

website/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"dependencies": {
1111
"@uiw/react-markdown-preview-example": "^1.5.9",
1212
"@uiw/react-shields": "^2.0.1",
13-
"mermaid": "^10.4.0",
13+
"katex": "^0.16.9",
14+
"mermaid": "^10.6.1",
1415
"react": "~18.2.0",
1516
"react-dom": "~18.2.0",
1617
"react-router-dom": "^6.8.1",

0 commit comments

Comments
 (0)