astro-mermaid 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/astro-mermaid-integration.js +54 -6
- package/package.json +1 -1
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
import { fileURLToPath } from 'node:url';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Remark plugin to transform mermaid code blocks at the markdown level
|
|
6
|
+
*/
|
|
7
|
+
function remarkMermaidPlugin(options = {}) {
|
|
8
|
+
return async function transformer(tree, file) {
|
|
9
|
+
const { visit } = await import('unist-util-visit');
|
|
10
|
+
|
|
11
|
+
let mermaidCount = 0;
|
|
12
|
+
|
|
13
|
+
visit(tree, 'code', (node, index, parent) => {
|
|
14
|
+
if (node.lang === 'mermaid') {
|
|
15
|
+
mermaidCount++;
|
|
16
|
+
|
|
17
|
+
// Transform to html node with pre.mermaid
|
|
18
|
+
const htmlNode = {
|
|
19
|
+
type: 'html',
|
|
20
|
+
value: `<pre class="mermaid">${node.value}</pre>`
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// Replace the code node with html node
|
|
24
|
+
if (parent && typeof index === 'number') {
|
|
25
|
+
parent.children[index] = htmlNode;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (options.logger) {
|
|
29
|
+
options.logger.info(`Remark transformed mermaid block #${mermaidCount} in ${file.path || 'unknown file'}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
if (mermaidCount > 0 && options.logger) {
|
|
35
|
+
options.logger.info(`Remark total mermaid blocks transformed: ${mermaidCount}`);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
4
40
|
/**
|
|
5
41
|
* Rehype plugin to transform mermaid code blocks
|
|
6
42
|
* Converts ```mermaid code blocks to <pre class="mermaid">
|
|
@@ -39,14 +75,14 @@ function rehypeMermaidPlugin(options = {}) {
|
|
|
39
75
|
}];
|
|
40
76
|
|
|
41
77
|
if (options.logger) {
|
|
42
|
-
options.logger.info(`
|
|
78
|
+
options.logger.info(`Rehype transformed mermaid block #${mermaidCount} in ${file.path || 'unknown file'}`);
|
|
43
79
|
}
|
|
44
80
|
}
|
|
45
81
|
}
|
|
46
82
|
});
|
|
47
83
|
|
|
48
84
|
if (mermaidCount > 0 && options.logger) {
|
|
49
|
-
options.logger.info(`
|
|
85
|
+
options.logger.info(`Rehype total mermaid blocks transformed: ${mermaidCount}`);
|
|
50
86
|
}
|
|
51
87
|
};
|
|
52
88
|
}
|
|
@@ -78,9 +114,13 @@ export default function astroMermaid(options = {}) {
|
|
|
78
114
|
// Log existing rehype plugins
|
|
79
115
|
logger.info('Existing rehype plugins:', config.markdown?.rehypePlugins?.length || 0);
|
|
80
116
|
|
|
81
|
-
// Update markdown config to use
|
|
117
|
+
// Update markdown config to use both remark and rehype plugins
|
|
82
118
|
updateConfig({
|
|
83
119
|
markdown: {
|
|
120
|
+
remarkPlugins: [
|
|
121
|
+
...(config.markdown?.remarkPlugins || []),
|
|
122
|
+
[remarkMermaidPlugin, { logger }]
|
|
123
|
+
],
|
|
84
124
|
rehypePlugins: [
|
|
85
125
|
...(config.markdown?.rehypePlugins || []),
|
|
86
126
|
[rehypeMermaidPlugin, { logger }]
|
|
@@ -146,13 +186,16 @@ if (hasMermaidDiagrams()) {
|
|
|
146
186
|
return;
|
|
147
187
|
}
|
|
148
188
|
|
|
149
|
-
// Get current theme
|
|
189
|
+
// Get current theme from multiple sources
|
|
150
190
|
let currentTheme = defaultConfig.theme;
|
|
151
191
|
|
|
152
192
|
if (${autoTheme}) {
|
|
153
|
-
|
|
193
|
+
// Check both html and body for data-theme attribute
|
|
194
|
+
const htmlTheme = document.documentElement.getAttribute('data-theme');
|
|
195
|
+
const bodyTheme = document.body.getAttribute('data-theme');
|
|
196
|
+
const dataTheme = htmlTheme || bodyTheme;
|
|
154
197
|
currentTheme = themeMap[dataTheme] || defaultConfig.theme;
|
|
155
|
-
console.log('[astro-mermaid] Using theme:', currentTheme);
|
|
198
|
+
console.log('[astro-mermaid] Using theme:', currentTheme, 'from', htmlTheme ? 'html' : 'body');
|
|
156
199
|
}
|
|
157
200
|
|
|
158
201
|
// Configure mermaid with gitGraph support
|
|
@@ -221,10 +264,15 @@ if (hasMermaidDiagrams()) {
|
|
|
221
264
|
}
|
|
222
265
|
});
|
|
223
266
|
|
|
267
|
+
// Observe both html and body for data-theme changes
|
|
224
268
|
observer.observe(document.documentElement, {
|
|
225
269
|
attributes: true,
|
|
226
270
|
attributeFilter: ['data-theme']
|
|
227
271
|
});
|
|
272
|
+
observer.observe(document.body, {
|
|
273
|
+
attributes: true,
|
|
274
|
+
attributeFilter: ['data-theme']
|
|
275
|
+
});
|
|
228
276
|
}
|
|
229
277
|
|
|
230
278
|
// Handle view transitions (for Astro View Transitions API)
|
package/package.json
CHANGED