Add elk layout support to mermaid (#36486)

Fixes: https://github.com/go-gitea/gitea/issues/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>
This commit is contained in:
silverwind
2026-02-07 03:22:57 +01:00
committed by GitHub
parent e2104a1dd5
commit 49e6d5f6d6
5 changed files with 197 additions and 12 deletions
+59
View File
@@ -0,0 +1,59 @@
import {sourcesContainElk} from './mermaid.ts';
import {dedent} from '../utils/testhelper.ts';
test('sourcesContainElk', () => {
expect(sourcesContainElk([dedent(`
flowchart TB
elk --> B
`)])).toEqual(false);
expect(sourcesContainElk([dedent(`
---
config:
layout : elk
---
flowchart TB
A --> B
`)])).toEqual(true);
expect(sourcesContainElk([dedent(`
---
config:
layout: elk.layered
---
flowchart TB
A --> B
`)])).toEqual(true);
expect(sourcesContainElk([`
%%{ init : { "flowchart": { "defaultRenderer": "elk" } } }%%
flowchart TB
A --> B
`])).toEqual(true);
expect(sourcesContainElk([`
---
config:
layout: 123
---
%%{ init : { "class": { "defaultRenderer": "elk.any" } } }%%
flowchart TB
A --> B
`])).toEqual(true);
expect(sourcesContainElk([`
%%{init:{
"layout" : "elk.layered"
}}%%
flowchart TB
A --> B
`])).toEqual(true);
expect(sourcesContainElk([`
%%{ initialize: {
'layout' : 'elk.layered'
}}%%
flowchart TB
A --> B
`])).toEqual(true);
});