Add built-in mermaid diagram support for the markdown parser#201
Open
andrewjoelpeters wants to merge 4 commits intofacelessuser:masterfrom
Open
Add built-in mermaid diagram support for the markdown parser#201andrewjoelpeters wants to merge 4 commits intofacelessuser:masterfrom
andrewjoelpeters wants to merge 4 commits intofacelessuser:masterfrom
Conversation
Owner
|
Yep, I haven't updated Mermaid support here for a bit. I will take a look, hopefully within the next couple of weeks. A lot of the logic comes from https://github.com/facelessuser/pymdown-extensions/blob/main/docs/src/js/uml.js here, which is more up to date. Regardless, we can update all of this. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Mermaid diagrams now render out of the box when using the
markdownparser, without requiring manual JS configuration. Closes #183. Note that this does not render mermaid diagrams when using thegithubparser (more on that below).Why wasn't mermaid working before?
The infrastructure for mermaid was already partially in place —
js/mermaid.js,js/mermaid_config.js, and the SuperFences custom fence for mermaid were all configured in the default settings. However,MarkdownCompiler.default_jswas empty ([]), so when thejssetting resolved"default"for the markdown parser, no scripts were included. Users had to manually add the mermaid CDN URL and loader scripts to theirjssettings to get mermaid working.Additionally, the existing
mermaid.jsandgitlab_config.jsused mermaid v8 APIs (mermaid.mermaidAPI.globalReset(), callback-basedmermaid.mermaidAPI.render(),mermaid.init()) that were removed in mermaid v10+.What changed?
markdown_preview.py: Addeddefault_jstoMarkdownCompilerwith the mermaid CDN (v11), config, and loader scripts. Also bumped theGitlabCompilermermaid CDN from v8 to v11.js/mermaid.js: Updated from the removed callback-basedmermaid.mermaidAPI.render()to the modernawait mermaid.render()async API. Removed the deprecatedmermaid.mermaidAPI.globalReset()call.js/gitlab_config.js: UpdatedrenderMermaid()from the removedmermaid.init()to the modernawait mermaid.render()async API.extras.mdto reflect that mermaid is now built-in for the markdown parser, updated CDN references from v8 to v11, and fixed the UML docs to use the current dict format for thejssetting.Why only the markdown parser?
The
githubparser delegates conversion entirely to the GitHub API, which applies its own syntax highlighting to mermaid blocks. The API returns mermaid source wrapped in syntax-highlighted HTML like<div class="highlight highlight-source-mermaid"><pre><span class="pl-k">flowchart</span>..., rather than the<pre class="mermaid"><code>...</code></pre>format thatmermaid.jsexpects. The raw mermaid source text is broken up across multiple<span>elements with Pygments CSS classes, so it can't be passed directly to the mermaid renderer.Adding client-side mermaid rendering on top of GitHub's API output would mean layering custom DOM-scraping logic over a third-party response format — reconstructing the original mermaid source by stripping spans and joining text nodes from GitHub's syntax-highlighted output. This works against the current, thin design of the GitHub parser, which just sends markdown to GitHub, and renders the result. Adding mermaid support there would couple us to GitHub's HTML output format in a way that's potentially fragile and harder to maintain, and maybe less intuitive to users.
If there's interest in a follow-up PR, enabling mermaid for the GitHub parser would require:
GithubCompiler.default_jsmermaid.js(or adding a separate GitHub-specific loader) to finddiv.highlight-source-mermaidelements, extract and concatenate the text content from the nested<span>elements to recover the original mermaid source, and then pass that tomermaid.render()Potential risks
jsin their settings to exclude it.gitlab_config.jschanges are a side-effect of modernizing the mermaid API usage. The GitLab parser's mermaid rendering was already broken on mermaid v10+ due to the removedmermaid.init()API, so this is effectively a bug fix.Co-Authored-By: Claude Opus 4.6 noreply@anthropic.com