Skip to content

Commit 9bcaa17

Browse files
committed
refactor: Implement Mermaid YAML Frontmatter Theming (#8756)
1 parent d1a4eec commit 9bcaa17

2 files changed

Lines changed: 18 additions & 39 deletions

File tree

src/component/wrapper/Mermaid.mjs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ class Mermaid extends Component {
2525
* @protected
2626
*/
2727
ntype: 'mermaid',
28+
/**
29+
* @member {Object} themeMap
30+
*/
31+
themeMap: {
32+
'neo-theme-cyberpunk': 'dark',
33+
'neo-theme-dark' : 'dark',
34+
'neo-theme-light' : 'default',
35+
'neo-theme-neo-dark' : 'dark',
36+
'neo-theme-neo-light': 'neutral'
37+
},
2838
/**
2939
* The mermaid diagram code.
3040
* Changing this value will automatically trigger a re-render of the diagram.
@@ -115,10 +125,13 @@ class Mermaid extends Component {
115125
if (me.mounted && me.value) {
116126
await me.ready();
117127

128+
const
129+
theme = me.themeMap[me.theme] || 'default',
130+
code = `---\nconfig:\n theme: ${theme}\n---\n${me.value}`;
131+
118132
await me.addon.render({
119-
code : me.value,
133+
code,
120134
id : me.id,
121-
theme : me.theme,
122135
windowId: me.windowId
123136
})
124137
}

src/main/addon/Mermaid.mjs

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import DomAccess from '../DomAccess.mjs';
99
*
1010
* Key features:
1111
* - **Lazy Loading:** Dynamically loads the Mermaid library only when needed.
12-
* - **Theme Mapping:** Automatically translates Neo.mjs application themes (e.g., 'neo-theme-dark') to Mermaid's internal themes.
13-
* - **Dynamic Re-rendering:** Handles theme switches at runtime by re-initializing the library.
1412
* - **SVG Generation:** Uses `mermaid.render()` to generate idempotent SVG output, ensuring reliable DOM updates.
1513
*
1614
* It is primarily consumed by:
@@ -22,20 +20,6 @@ import DomAccess from '../DomAccess.mjs';
2220
* @see Neo.component.wrapper.Mermaid
2321
*/
2422
class Mermaid extends Base {
25-
/**
26-
* Maps Neo.mjs theme names to Mermaid.js theme names.
27-
* This allows the addon to automatically select the most appropriate visual style
28-
* based on the current application theme.
29-
* @member {Object} themeMap
30-
*/
31-
static themeMap = {
32-
'neo-theme-cyberpunk': 'dark',
33-
'neo-theme-dark' : 'dark',
34-
'neo-theme-light' : 'default',
35-
'neo-theme-neo-dark' : 'dark',
36-
'neo-theme-neo-light': 'default'
37-
}
38-
3923
static config = {
4024
/**
4125
* @member {String} className='Neo.main.addon.Mermaid'
@@ -70,12 +54,6 @@ class Mermaid extends Base {
7054
useLazyLoading: true
7155
}
7256

73-
/**
74-
* Tracks the currently active Mermaid theme to prevent redundant re-initialization.
75-
* @member {String} currentTheme='default'
76-
*/
77-
currentTheme = 'default'
78-
7957
/**
8058
* Loads the Mermaid library if it is not already present.
8159
* Initializes the library with `startOnLoad: false` to allow manual control over rendering.
@@ -92,32 +70,20 @@ class Mermaid extends Base {
9270
* Renders a Mermaid diagram into a specific DOM element.
9371
*
9472
* This method orchestrates the full rendering pipeline:
95-
* 1. **Theme Resolution:** Maps the requested Neo theme to a Mermaid theme.
96-
* 2. **Re-initialization:** If the theme has changed, it re-configures Mermaid globally.
97-
* 3. **SVG Generation:** Generates a fresh SVG string for the diagram code.
98-
* 4. **DOM Injection:** Safely injects the SVG into the target container.
73+
* 1. **SVG Generation:** Generates a fresh SVG string for the diagram code.
74+
* 2. **DOM Injection:** Safely injects the SVG into the target container.
9975
*
10076
* It includes error handling to display rendering failures inline (e.g., syntax errors)
10177
* instead of crashing the application.
10278
*
10379
* @param {Object} data
10480
* @param {String} [data.code] The mermaid diagram syntax/code.
10581
* @param {String} data.id The DOM ID of the container element.
106-
* @param {String} [data.theme] The neo theme to use (e.g. 'neo-theme-dark').
10782
*/
10883
async render(data) {
109-
const
110-
element = document.getElementById(data.id),
111-
me = this;
84+
const element = document.getElementById(data.id);
11285

11386
if (element) {
114-
const theme = me.getStaticConfig('themeMap')[data.theme] || 'default';
115-
116-
if (me.currentTheme !== theme) {
117-
me.currentTheme = theme;
118-
mermaid.initialize({startOnLoad: false, theme})
119-
}
120-
12187
try {
12288
const {svg} = await mermaid.render(data.id + '__svg', data.code);
12389
element.innerHTML = svg

0 commit comments

Comments
 (0)