astro-mermaid 2.0.1 → 2.0.3

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 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. Icon packs are loaded from Iconify JSON sources:
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
- loader: () => fetch('https://unpkg.com/@iconify-json/logos@1/icons.json').then(res => res.json())
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
  }
@@ -275,14 +275,20 @@ export default function astroMermaid(options = {}) {
275
275
  });
276
276
 
277
277
  // Validate and serialize icon packs for client-side use.
278
- // Only the pack name and a JSON URL string are forwarded to the
279
- // client — we never serialize arbitrary function bodies.
278
+ // Only the pack name and either inline icon data or a JSON URL string
279
+ // are forwarded to the client — we never serialize arbitrary function
280
+ // bodies.
280
281
  const iconPacksConfig = iconPacks.map(pack => {
281
282
  if (typeof pack.name !== 'string' || !pack.name) {
282
283
  throw new Error('astro-mermaid: each iconPack must have a non-empty "name" string');
283
284
  }
285
+ if (pack.icons) {
286
+ // Preferred: inline icon data passed directly (e.g. imported JSON).
287
+ // Plain data, so there are no serialization concerns. Fixes #18.
288
+ return { name: pack.name, icons: pack.icons };
289
+ }
284
290
  if (typeof pack.url === 'string') {
285
- // Preferred: explicit URL
291
+ // Explicit URL
286
292
  return { name: pack.name, url: pack.url };
287
293
  }
288
294
  if (typeof pack.loader === 'function') {
@@ -296,13 +302,13 @@ export default function astroMermaid(options = {}) {
296
302
  logger.warn(
297
303
  `astro-mermaid: iconPack "${pack.name}" uses a loader function ` +
298
304
  `that could not be safely serialized. Please provide a "url" ` +
299
- `property instead. This pack will be skipped.`
305
+ `or "icons" property instead. This pack will be skipped.`
300
306
  );
301
307
  return null;
302
308
  }
303
309
  throw new Error(
304
- `astro-mermaid: iconPack "${pack.name}" must have a "url" string ` +
305
- `or a "loader" function`
310
+ `astro-mermaid: iconPack "${pack.name}" must have a "url" string, ` +
311
+ `an "icons" object, or a "loader" function`
306
312
  );
307
313
  }).filter(Boolean);
308
314
 
@@ -331,10 +337,16 @@ async function loadMermaid() {
331
337
  const iconPacks = ${sanitizeJsonForScript(JSON.stringify(iconPacksConfig))};
332
338
  if (iconPacks && iconPacks.length > 0) {
333
339
  log('Registering', iconPacks.length, 'icon packs');
334
- const packs = iconPacks.map(pack => ({
335
- name: pack.name,
336
- loader: () => fetch(pack.url).then(res => res.json())
337
- }));
340
+ const packs = iconPacks.map(pack => {
341
+ if (pack.icons) {
342
+ // Inline icon data — register directly, no fetch needed
343
+ return { name: pack.name, icons: pack.icons };
344
+ }
345
+ return {
346
+ name: pack.name,
347
+ loader: () => fetch(pack.url).then(res => res.json())
348
+ };
349
+ });
338
350
  await mermaid.registerIconPacks(packs);
339
351
  }
340
352
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-mermaid",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
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",
@@ -51,7 +51,7 @@
51
51
  },
52
52
  "devDependencies": {
53
53
  "@types/hast": "^3.0.4",
54
- "@vitest/ui": "^3.2.4",
54
+ "@vitest/ui": "^4.1.8",
55
55
  "astro": "^6.0.0",
56
56
  "hast-util-from-html": "^2.0.3",
57
57
  "mermaid": "^11.0.0",
@@ -62,7 +62,7 @@
62
62
  "semantic-release": "^25.0.3",
63
63
  "typescript": "^5.0.0",
64
64
  "unified": "^11.0.5",
65
- "vitest": "^3.2.4"
65
+ "vitest": "^4.1.8"
66
66
  },
67
67
  "repository": {
68
68
  "type": "git",