astro-mermaid 2.0.2 → 2.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/LICENSE +21 -0
- package/README.md +19 -2
- package/astro-mermaid-integration.d.ts +14 -2
- package/astro-mermaid-integration.js +77 -26
- package/package.json +10 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Jose Sebastian
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -131,17 +131,34 @@ mermaid({
|
|
|
131
131
|
|
|
132
132
|
## Icon Packs
|
|
133
133
|
|
|
134
|
-
You can register icon packs to use custom icons in your diagrams.
|
|
134
|
+
You can register icon packs to use custom icons in your diagrams. There are three ways to provide a pack:
|
|
135
135
|
|
|
136
136
|
```js
|
|
137
137
|
iconPacks: [
|
|
138
|
+
// 1. url — preferred: a JSON endpoint, fetched safely at runtime
|
|
138
139
|
{
|
|
139
140
|
name: 'logos',
|
|
140
|
-
|
|
141
|
+
url: 'https://unpkg.com/@iconify-json/logos@1/icons.json'
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
// 2. icons — pass icon data directly (e.g. an imported JSON file).
|
|
145
|
+
// No serialization concerns, so this works with imports and shared data.
|
|
146
|
+
{
|
|
147
|
+
name: 'my-icons',
|
|
148
|
+
icons: myIcons // import myIcons from './my-icons.json'
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
// 3. loader — legacy. The function source is inspected for a fetch('...')
|
|
152
|
+
// URL. Prefer `url` or `icons` instead.
|
|
153
|
+
{
|
|
154
|
+
name: 'iconoir',
|
|
155
|
+
loader: () => fetch('https://unpkg.com/@iconify-json/iconoir@1/icons.json').then(res => res.json())
|
|
141
156
|
}
|
|
142
157
|
]
|
|
143
158
|
```
|
|
144
159
|
|
|
160
|
+
> The integration never serializes arbitrary function bodies to the client. A `loader` is only used to extract its `fetch(...)` URL; if no URL can be found, the pack is skipped with a warning. Use `url` or `icons` for reliable results.
|
|
161
|
+
|
|
145
162
|
Then use icons in your diagrams:
|
|
146
163
|
|
|
147
164
|
````markdown
|
|
@@ -12,10 +12,22 @@ export interface IconPack {
|
|
|
12
12
|
*/
|
|
13
13
|
url?: string;
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Inline icon data passed directly (e.g. imported from a JSON file).
|
|
17
|
+
* Avoids any serialization concerns since it is plain data.
|
|
18
|
+
* @example
|
|
19
|
+
* ```js
|
|
20
|
+
* import myIcons from './my-icons.json';
|
|
21
|
+
* // ...
|
|
22
|
+
* { name: 'my-icons', icons: myIcons }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
icons?: Record<string, any>;
|
|
26
|
+
|
|
15
27
|
/**
|
|
16
28
|
* Legacy: loader function whose source is inspected for a fetch() URL.
|
|
17
|
-
* Prefer using the `url` property instead for safer serialization.
|
|
18
|
-
* @deprecated Use `url` instead.
|
|
29
|
+
* Prefer using the `url` or `icons` property instead for safer serialization.
|
|
30
|
+
* @deprecated Use `url` or `icons` instead.
|
|
19
31
|
*/
|
|
20
32
|
loader?: () => Promise<any>;
|
|
21
33
|
}
|
|
@@ -255,34 +255,79 @@ export default function astroMermaid(options = {}) {
|
|
|
255
255
|
viteOptimizeDepsInclude.push('@mermaid-js/layout-elk');
|
|
256
256
|
}
|
|
257
257
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
258
|
+
const remarkEntry = [remarkMermaidPlugin, { logger }];
|
|
259
|
+
const rehypeEntry = [rehypeMermaidPlugin, { logger }];
|
|
260
|
+
const viteConfig = { optimizeDeps: { include: viteOptimizeDepsInclude } };
|
|
261
|
+
|
|
262
|
+
// Astro 6.4+ deprecated `markdown.remarkPlugins` / `markdown.rehypePlugins`
|
|
263
|
+
// in favor of passing plugins to `unified({...})` via `markdown.processor`.
|
|
264
|
+
// On 6.4+ Astro always supplies a default unified processor, so its
|
|
265
|
+
// presence is our signal to use the new API and avoid the deprecation
|
|
266
|
+
// warning. Older Astro versions have no processor — fall back to the
|
|
267
|
+
// (still-supported there) plugin arrays. Fixes #62.
|
|
268
|
+
const existingProcessor = config.markdown?.processor;
|
|
269
|
+
let usedProcessor = false;
|
|
270
|
+
|
|
271
|
+
if (existingProcessor) {
|
|
272
|
+
try {
|
|
273
|
+
const { unified, isUnifiedProcessor } = await import('@astrojs/markdown-remark');
|
|
274
|
+
if (isUnifiedProcessor(existingProcessor)) {
|
|
275
|
+
const existingOptions = existingProcessor.options || {};
|
|
276
|
+
updateConfig({
|
|
277
|
+
markdown: {
|
|
278
|
+
processor: unified({
|
|
279
|
+
...existingOptions,
|
|
280
|
+
remarkPlugins: [...(existingOptions.remarkPlugins || []), remarkEntry],
|
|
281
|
+
rehypePlugins: [...(existingOptions.rehypePlugins || []), rehypeEntry]
|
|
282
|
+
})
|
|
283
|
+
},
|
|
284
|
+
vite: viteConfig
|
|
285
|
+
});
|
|
286
|
+
usedProcessor = true;
|
|
273
287
|
}
|
|
288
|
+
} catch (error) {
|
|
289
|
+
// Dynamic import failed, the unified processor helpers are not
|
|
290
|
+
// exported, or updateConfig rejected the processor — fall through
|
|
291
|
+
// to the legacy plugin arrays below.
|
|
292
|
+
logger.warn(
|
|
293
|
+
`Could not configure the unified markdown processor, falling back ` +
|
|
294
|
+
`to remark/rehype plugin arrays: ${error.message}`
|
|
295
|
+
);
|
|
274
296
|
}
|
|
275
|
-
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if (!usedProcessor) {
|
|
300
|
+
// Update markdown config to use both remark and rehype plugins
|
|
301
|
+
updateConfig({
|
|
302
|
+
markdown: {
|
|
303
|
+
remarkPlugins: [
|
|
304
|
+
...(config.markdown?.remarkPlugins || []),
|
|
305
|
+
remarkEntry
|
|
306
|
+
],
|
|
307
|
+
rehypePlugins: [
|
|
308
|
+
...(config.markdown?.rehypePlugins || []),
|
|
309
|
+
rehypeEntry
|
|
310
|
+
]
|
|
311
|
+
},
|
|
312
|
+
vite: viteConfig
|
|
313
|
+
});
|
|
314
|
+
}
|
|
276
315
|
|
|
277
316
|
// Validate and serialize icon packs for client-side use.
|
|
278
|
-
// Only the pack name and a JSON URL string
|
|
279
|
-
// client — we never serialize arbitrary function
|
|
317
|
+
// Only the pack name and either inline icon data or a JSON URL string
|
|
318
|
+
// are forwarded to the client — we never serialize arbitrary function
|
|
319
|
+
// bodies.
|
|
280
320
|
const iconPacksConfig = iconPacks.map(pack => {
|
|
281
321
|
if (typeof pack.name !== 'string' || !pack.name) {
|
|
282
322
|
throw new Error('astro-mermaid: each iconPack must have a non-empty "name" string');
|
|
283
323
|
}
|
|
324
|
+
if (pack.icons) {
|
|
325
|
+
// Preferred: inline icon data passed directly (e.g. imported JSON).
|
|
326
|
+
// Plain data, so there are no serialization concerns. Fixes #18.
|
|
327
|
+
return { name: pack.name, icons: pack.icons };
|
|
328
|
+
}
|
|
284
329
|
if (typeof pack.url === 'string') {
|
|
285
|
-
//
|
|
330
|
+
// Explicit URL
|
|
286
331
|
return { name: pack.name, url: pack.url };
|
|
287
332
|
}
|
|
288
333
|
if (typeof pack.loader === 'function') {
|
|
@@ -296,13 +341,13 @@ export default function astroMermaid(options = {}) {
|
|
|
296
341
|
logger.warn(
|
|
297
342
|
`astro-mermaid: iconPack "${pack.name}" uses a loader function ` +
|
|
298
343
|
`that could not be safely serialized. Please provide a "url" ` +
|
|
299
|
-
`property instead. This pack will be skipped.`
|
|
344
|
+
`or "icons" property instead. This pack will be skipped.`
|
|
300
345
|
);
|
|
301
346
|
return null;
|
|
302
347
|
}
|
|
303
348
|
throw new Error(
|
|
304
|
-
`astro-mermaid: iconPack "${pack.name}" must have a "url" string ` +
|
|
305
|
-
`or a "loader" function`
|
|
349
|
+
`astro-mermaid: iconPack "${pack.name}" must have a "url" string, ` +
|
|
350
|
+
`an "icons" object, or a "loader" function`
|
|
306
351
|
);
|
|
307
352
|
}).filter(Boolean);
|
|
308
353
|
|
|
@@ -331,10 +376,16 @@ async function loadMermaid() {
|
|
|
331
376
|
const iconPacks = ${sanitizeJsonForScript(JSON.stringify(iconPacksConfig))};
|
|
332
377
|
if (iconPacks && iconPacks.length > 0) {
|
|
333
378
|
log('Registering', iconPacks.length, 'icon packs');
|
|
334
|
-
const packs = iconPacks.map(pack =>
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
379
|
+
const packs = iconPacks.map(pack => {
|
|
380
|
+
if (pack.icons) {
|
|
381
|
+
// Inline icon data — register directly, no fetch needed
|
|
382
|
+
return { name: pack.name, icons: pack.icons };
|
|
383
|
+
}
|
|
384
|
+
return {
|
|
385
|
+
name: pack.name,
|
|
386
|
+
loader: () => fetch(pack.url).then(res => res.json())
|
|
387
|
+
};
|
|
388
|
+
});
|
|
338
389
|
await mermaid.registerIconPacks(packs);
|
|
339
390
|
}
|
|
340
391
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "astro-mermaid",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
4
4
|
"description": "An Astro integration for rendering Mermaid diagrams with automatic theme switching and client-side rendering",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./astro-mermaid-integration.js",
|
|
@@ -43,6 +43,14 @@
|
|
|
43
43
|
"mdast-util-to-string": "^4.0.0",
|
|
44
44
|
"unist-util-visit": "^5.0.0"
|
|
45
45
|
},
|
|
46
|
+
"overrides": {
|
|
47
|
+
"vite": "^7.3.5",
|
|
48
|
+
"esbuild": "^0.28.1",
|
|
49
|
+
"ip-address": "^10.1.1",
|
|
50
|
+
"js-yaml": "^4.2.0",
|
|
51
|
+
"picomatch": "^4.0.4",
|
|
52
|
+
"tar": "^7.5.16"
|
|
53
|
+
},
|
|
46
54
|
"scripts": {
|
|
47
55
|
"test": "vitest",
|
|
48
56
|
"test:ui": "vitest --ui",
|
|
@@ -52,7 +60,7 @@
|
|
|
52
60
|
"devDependencies": {
|
|
53
61
|
"@types/hast": "^3.0.4",
|
|
54
62
|
"@vitest/ui": "^4.1.8",
|
|
55
|
-
"astro": "^6.
|
|
63
|
+
"astro": "^6.4.6",
|
|
56
64
|
"hast-util-from-html": "^2.0.3",
|
|
57
65
|
"mermaid": "^11.0.0",
|
|
58
66
|
"rehype-parse": "^9.0.1",
|