Skip to content

Add elk layout support to mermaid#36486

Merged
wxiaoguang merged 41 commits intogo-gitea:mainfrom
silverwind:mermaidelk
Feb 7, 2026
Merged

Add elk layout support to mermaid#36486
wxiaoguang merged 41 commits intogo-gitea:mainfrom
silverwind:mermaidelk

Conversation

@silverwind
Copy link
Copy Markdown
Member

@silverwind silverwind commented Jan 30, 2026

Fixes: #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.

When configuration for elk layout is detect, it causes a new webpack dynamic chunk to be loaded:

image

If no elk layout configuration is detected, the layout is not loaded:

image

Before:

Screenshot 2026-01-30 at 02 07 27

After:
Screenshot 2026-01-30 at 02 07 39

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jan 30, 2026
@silverwind silverwind added the type/enhancement An improvement of existing functionality label Jan 30, 2026
@silverwind silverwind changed the title Add layout: elk support to mermaid Add elk layout support to mermaid Jan 30, 2026
@silverwind silverwind requested a review from Copilot January 30, 2026 01:16
@silverwind silverwind changed the title Add elk layout support to mermaid Add elk layout support to mermaid Jan 30, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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-elk as 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 check if (!pre || pre.hasAttribute('data-render-done')) return;. This means pages that re-run initMarkupCodeMermaid on 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.

@silverwind
Copy link
Copy Markdown
Member Author

silverwind commented Jan 30, 2026

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.

@silverwind
Copy link
Copy Markdown
Member Author

silverwind commented Jan 30, 2026

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.

This comment was marked as resolved.

This comment was marked as resolved.

@silverwind silverwind requested a review from Copilot January 30, 2026 02:39
@silverwind
Copy link
Copy Markdown
Member Author

silverwind commented Feb 5, 2026

dedent test helper added because those tests were a eyesore.

}

function configContainsElk(config: MermaidConfig | null) {
return isElk(config?.layout) || Object.values(config || {}).some((value) => isElk(value?.defaultRenderer));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think anyone can really understand why the logic looks so strange.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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>
Copy link
Copy Markdown
Contributor

@wxiaoguang wxiaoguang left a comment

Choose a reason for hiding this comment

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

Quite unclear.

As a reader, I still don't understand by reading the comment:

  1. Why "check config.*.defaultRender contains a elk layout" but not clearly check the chart types which support "defaultRender"
  2. 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.

@GiteaBot GiteaBot added lgtm/blocked A maintainer has reservations with the PR and thus it cannot be merged and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Feb 6, 2026
@wxiaoguang wxiaoguang dismissed their stale review February 6, 2026 14:32

enough arguments

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/blocked A maintainer has reservations with the PR and thus it cannot be merged labels Feb 6, 2026
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Feb 6, 2026
@wxiaoguang wxiaoguang enabled auto-merge (squash) February 7, 2026 02:22
@wxiaoguang wxiaoguang merged commit 49e6d5f into go-gitea:main Feb 7, 2026
24 checks passed
@GiteaBot GiteaBot added this to the 1.26.0 milestone Feb 7, 2026
@wxiaoguang wxiaoguang deleted the mermaidelk branch February 7, 2026 02:24
zjjhot added a commit to zjjhot/gitea that referenced this pull request Feb 9, 2026
* 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)
Sirherobrine23 pushed a commit to Sirherobrine23/gitea that referenced this pull request Mar 4, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/dependencies modifies/frontend type/feature Completely new functionality. Can only be merged if feature freeze is not active.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mermaid: Allow using elk layout

5 participants