Add elk layout support to mermaid#36486
Conversation
layout: elk support to mermaidelk layout support to mermaid
There was a problem hiding this comment.
Pull request overview
Enables Mermaid diagrams rendered from Markdown to optionally use the elk layout engine (via Mermaid configuration in frontmatter or %%{ init }%% directives) without changing the default layout.
Changes:
- Adds
@mermaid-js/layout-elkas a frontend dependency. - Registers ELK layout loaders during Mermaid initialization for markup-rendered Mermaid blocks.
- Updates lockfile to include ELK-related transitive dependencies (e.g.,
elkjs).
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| web_src/js/markup/mermaid.ts | Dynamically loads and registers ELK layout loaders with Mermaid before rendering diagrams. |
| package.json | Adds @mermaid-js/layout-elk dependency. |
| pnpm-lock.yaml | Locks @mermaid-js/layout-elk and new transitive dependencies. |
Files not reviewed (1)
- pnpm-lock.yaml: Language not supported
Comments suppressed due to low confidence (1)
web_src/js/markup/mermaid.ts:30
- The heavy dynamic imports (now including
@mermaid-js/layout-elk) happen before the early-exit checkif (!pre || pre.hasAttribute('data-render-done')) return;. This means pages that re-runinitMarkupCodeMermaidon already-processed markup will still pay the cost of loading/registering Mermaid/elk.
Consider moving the pre/data-render-done check (and possibly the max-length check) before performing the dynamic imports, so already-rendered blocks can bail out without extra network/work.
mermaid.initialize({
startOnLoad: false,
theme: isDarkTheme() ? 'dark' : 'neutral',
securityLevel: 'strict',
suppressErrorRendering: true,
});
const pre = el.closest('pre');
if (!pre || pre.hasAttribute('data-render-done')) return;
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
The suggestion from Copilot to load the layout on-demand is good, but it would require to pre-parse the mermaid source for the frontmatter block with a yaml parser, and the init block as a variant of JSON. I think this will be too complicated to add. |
I've actually implemented lazy loading now with a crude regex detection that works for both yaml frontmatter and json init directive. |
|
|
web_src/js/markup/mermaid.ts
Outdated
| } | ||
|
|
||
| function configContainsElk(config: MermaidConfig | null) { | ||
| return isElk(config?.layout) || Object.values(config || {}).some((value) => isElk(value?.defaultRenderer)); |
There was a problem hiding this comment.
I don't think anyone can really understand why the logic looks so strange.
There was a problem hiding this comment.
That's a perfectly readable function imho, but I will add a comment.
Signed-off-by: silverwind <me@silverwind.io>
Signed-off-by: silverwind <me@silverwind.io>
Signed-off-by: silverwind <me@silverwind.io>
There was a problem hiding this comment.
Quite unclear.
As a reader, I still don't understand by reading the comment:
- Why "check
config.*.defaultRendercontains a elk layout" but not clearly check the chart types which support "defaultRender" - Why no test to cover
config.otherType.defaultRender
I can accept that it checks any value's defaultRender, the prerequisite is that the comment should be clear and there should be at least one test case for config.otherType.defaultRender
If other reviewer can fully understand the logic, free free to dismiss this change request.
* giteaofficial/main: Refactor merge conan and container auth preserve actions taskID (go-gitea#36560) Fix assignee sidebar links and empty placeholder after go-gitea#32465 refactor (go-gitea#36559) Fix various version parsing problems (go-gitea#36553) Fix highlight diff result (go-gitea#36539) Refactor Nuget Auth to reuse Basic Auth Token Validation (go-gitea#36558) Update go dependencies (go-gitea#36548) Prevent navigation keys from triggering actions during IME composition (go-gitea#36540) Fix various mermaid bugs (go-gitea#36547) Add `elk` layout support to mermaid (go-gitea#36486) Allow configuring default PR base branch (fixes go-gitea#36412) (go-gitea#36425) [skip ci] Updated translations via Crowdin Color command/error logs in Actions log (go-gitea#36538) Add paging headers (go-gitea#36521) Fix issues filter dropdown showing empty label scope section (go-gitea#36535) [SECURITY] fix: Adjust the toolchain version (go-gitea#36537) Hide `add-matcher` and `remove-matcher` from actions job logs (go-gitea#36520) Improve timeline entries for WIP prefix changes in pull requests (go-gitea#36518)
Fixes: go-gitea#34769 This allows the user to opt-in to using `elk` layouts using either YAML frontmatter or `%%{ init` directives inside the markup code block. The default layout is not changed. --------- Signed-off-by: silverwind <me@silverwind.io> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Fixes: #34769
This allows the user to opt-in to using
elklayouts using either YAML frontmatter or%%{ initdirectives inside the markup code block. The default layout is not changed.When configuration for
elklayout is detect, it causes a new webpack dynamic chunk to be loaded:If no
elklayout configuration is detected, the layout is not loaded:Before:
After:
