Skip to content

Commit

Permalink
doc: Update README.md (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Aug 10, 2023
1 parent 8b54ef7 commit 76264dc
Showing 1 changed file with 65 additions and 19 deletions.
84 changes: 65 additions & 19 deletions core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,6 @@ npm install katex

```jsx mdx:preview
import React from "react";
import ReactDOM from "react-dom";
import MDEditor from '@uiw/react-md-editor';
import { getCodeString } from 'rehype-rewrite';
import katex from 'katex';
Expand All @@ -515,9 +514,11 @@ c = \\pm\\sqrt{a^2 + b^2}
`;

export default function App() {
const [value, setValue] = React.useState(mdKaTeX);
return (
<MDEditor
value={mdKaTeX}
value={value}
onChange={(val) => setValue(val)}
previewOptions={{
components: {
code: ({ inline, children = [], className, ...props }) => {
Expand Down Expand Up @@ -611,10 +612,51 @@ Using [mermaid](https://github.com/mermaid-js/mermaid) to generation of diagram
npm install mermaid
```

```jsx
export default function App() {
const [value, setValue] = React.useState(mdKaTeX);
return (
<MDEditor
value={value}
onChange={(val) => setValue(val)}
previewOptions={{
components: {
code: ({ inline, children = [], className, ...props }) => {
const txt = children[0] || '';
if (inline) {
if (typeof txt === 'string' && /^\$\$(.*)\$\$/.test(txt)) {
const html = katex.renderToString(txt.replace(/^\$\$(.*)\$\$/, '$1'), {
throwOnError: false,
});
return <code dangerouslySetInnerHTML={{ __html: html }} />;
}
return <code>{txt}</code>;
}
const code = props.node && props.node.children ? getCodeString(props.node.children) : txt;
if (
typeof code === 'string' &&
typeof className === 'string' &&
/^language-katex/.test(className.toLocaleLowerCase())
) {
const html = katex.renderToString(code, {
throwOnError: false,
});
return <code style={{ fontSize: '150%' }} dangerouslySetInnerHTML={{ __html: html }} />;
}
return <code className={String(className)}>{txt}</code>;
},
},
}}
/>
);
}
```

```jsx mdx:preview
import React, { useState, useRef, useEffect } from "react";
import React, { useState, useRef, useEffect, Fragment, useCallback } from "react";
import ReactDOM from "react-dom";
import MDEditor from "@uiw/react-md-editor";
import { getCodeString } from 'rehype-rewrite';
import mermaid from "mermaid";

const mdMermaid = `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.
Expand Down Expand Up @@ -643,32 +685,36 @@ Bob-->>John: Jolly good!
const randomid = () => parseInt(String(Math.random() * 1e15), 10).toString(36);
const Code = ({ inline, children = [], className, ...props }) => {
const demoid = useRef(`dome${randomid()}`);
const code = getCode(children);
const demo = useRef(null);
const [container, setContainer] = useState(null);
const isMermaid = className && /^language-mermaid/.test(className.toLocaleLowerCase());
const txt = children[0] || '';
const code = props.node && props.node.children ? getCodeString(props.node.children) : txt;
useEffect(() => {
if (demo.current) {
if (container && isMermaid) {
try {
const str = mermaid.render(demoid.current, code, () => null, demo.current);
// @ts-ignore
demo.current.innerHTML = str;
const str = mermaid.render(demoid.current, code);
container.innerHTML = str;
} catch (error) {
// @ts-ignore
demo.current.innerHTML = error;
container.innerHTML = error;
}
}
}, [code, demo]);
}, [container, isMermaid, code, demoid]);

const refElement = useCallback((node) => {
if (node !== null) {
setContainer(node);
}
}, []);

if (
typeof code === "string" && typeof className === "string" &&
/^language-mermaid/.test(className.toLocaleLowerCase())
) {
if (isMermaid) {
return (
<code ref={demo}>
<Fragment>
<code id={demoid.current} style={{ display: "none" }} />
</code>
<code ref={refElement} data-name="mermaid" />
</Fragment>
);
}
return <code className={String(className)}>{children}</code>;
return <code>{children}</code>;
};

const getCode = (arr = []) => arr.map((dt) => {
Expand Down

1 comment on commit 76264dc

@vercel
Copy link

@vercel vercel bot commented on 76264dc Aug 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.