-
-
Notifications
You must be signed in to change notification settings - Fork 127
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
feat: add support for mermaid diagrams in markdown #715
Conversation
I'm not a React or JS/TS developer so I'm not sure about best practices or good patterns to use here - I'd appreciate some feedback on the following things, so that I can make this better before taking it out of draft:
|
@j-h-a thanks for that PR 🍺 and sorry again for huge delay 😞 Just to clarify, we are not using It is the same with
After refactor you can check the size of bundled JS file and I bet it did not increase too much. Regarding tests, we have simple tests and in case of your change just add a test case to https://github.com/asyncapi/asyncapi-react/blob/next/library/src/helpers/__tests__/marked.test.ts called Some magic in tailwind configuration has to probably be introduced as by default the background of the diagram should be white. I think now default background for Also would make sense to mention somewhere in the readme support for mermaid |
…ript element (doesn't work)
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just left some suggestions 🙂 They are rough code suggestion, you might need to alter them slightly and add some dependencies.
markdownRenderer.code = (code, language, isEscaped) => { | ||
if (language === 'mermaid') { | ||
initializeMermaidOnce(); | ||
return '<pre class="mermaid">' + code + '</pre>'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dont you need to return the rendered mermaid diagram here? i.e. add this from the website:
let currentId = 0;
const uuid = () => `mermaid-${(currentId++).toString()}`;
function MermaidDiagram({ graph }) {
const [svg, setSvg] = useState(null);
useLayoutEffect(() => {
if (!graph) {
return;
}
try {
mermaid.mermaidAPI.render(uuid(), graph, renderedSvg => setSvg(renderedSvg));
} catch (e) {
setHtml(null)
console.error(e);
}
}, [graph]);
return svg ? <div dangerouslySetInnerHTML={{ __html: svg }} /> : null;
}
And then do the following:
return '<pre class="mermaid">' + code + '</pre>'; | |
return <MermaidDiagram graph={code} />; |
const markdownRenderer = new marked.Renderer(); | ||
markdownRenderer.code = (code, language, isEscaped) => { | ||
if (language === 'mermaid') { | ||
initializeMermaidOnce(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this.
@@ -15,7 +16,7 @@ hljs.registerLanguage('yaml', yaml); | |||
import bash from 'highlight.js/lib/languages/bash'; | |||
hljs.registerLanguage('bash', bash); | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Globally inline everything, just as the website does it:
let mermaidInitialized = false; | |
initializeMermaid(); | |
function initializeMermaid() { | |
if (mermaidInitialized) { | |
return; | |
} | |
mermaidInitialized = true; | |
mermaid.initialize({ | |
startOnLoad: true, | |
theme: "base", | |
securityLevel: "loose", | |
themeCSS: ``, | |
themeVariables: { | |
primaryColor: "#EDFAFF", | |
primaryBorderColor: "#47BCEE", | |
secondaryColor: "#F4EFFC", | |
secondaryBorderColor: "#875AE2", | |
fontFamily: "Inter, sans-serif", | |
fontSize: "18px", | |
primaryTextColor: "#242929", | |
tertiaryColor: "#F7F9FA", | |
tertiaryBorderColor: "#BFC6C7" | |
} | |
}); | |
} |
This will most likely remove the error you have, about re-exporting mermaid from the initializeMermaidOnce.
This pull request has been automatically marked as stale because it has not had recent activity 😴 It will be closed in 120 days if no further activity occurs. To unstale this pull request, add a comment with detailed explanation. There can be many reasons why some specific pull request has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model. Let us figure out together how to push this pull request forward. Connect with us through one of many communication channels we established here. Thank you for your patience ❤️ |
no more long living release branches! sorry for any trouble with your PR 😞 but we really had to finally release this v1 and get rid of release branch I close this PR as I think it is better for you to recreate a new fresh PR targeting |
Description
This change adds support to include mermaid diagrams directly in any markdown.
Related issue(s)
Fixes #712