@mermaid-js/mermaid-cli 10.8.0 → 10.9.1

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.
@@ -22,6 +22,10 @@ export type ParseMDDOptions = {
22
22
  * - If set, scale PDF to fit chart.
23
23
  */
24
24
  pdfFit?: boolean | undefined;
25
+ /**
26
+ * - The id attribute for the SVG element to be rendered.
27
+ */
28
+ svgId?: string | undefined;
25
29
  };
26
30
  /**
27
31
  * Markdown image properties
@@ -72,7 +76,7 @@ export function run(input: `${string}.${"md" | "markdown"}` | string | undefined
72
76
  * @returns {Promise<{title: string | null, desc: string | null, data: Buffer}>} The output file in bytes,
73
77
  * with optional metadata.
74
78
  */
75
- export function renderMermaid(browser: import("puppeteer").Browser, definition: string, outputFormat: "svg" | "png" | "pdf", { viewport, backgroundColor, mermaidConfig, myCSS, pdfFit }?: ParseMDDOptions | undefined): Promise<{
79
+ export function renderMermaid(browser: import("puppeteer").Browser, definition: string, outputFormat: "svg" | "png" | "pdf", { viewport, backgroundColor, mermaidConfig, myCSS, pdfFit, svgId }?: ParseMDDOptions | undefined): Promise<{
76
80
  title: string | null;
77
81
  desc: string | null;
78
82
  data: Buffer;
@@ -84,6 +88,7 @@ export function renderMermaid(browser: import("puppeteer").Browser, definition:
84
88
  * @property {Parameters<import("mermaid")["default"]["initialize"]>[0]} [mermaidConfig] - Mermaid config.
85
89
  * @property {CSSStyleDeclaration["cssText"]} [myCSS] - Optional CSS text.
86
90
  * @property {boolean} [pdfFit] - If set, scale PDF to fit chart.
91
+ * @property {string} [svgId] - The id attribute for the SVG element to be rendered.
87
92
  */
88
93
  /**
89
94
  * Parse and render a mermaid diagram.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;SA+Vc,MAAM;;;;SACN,MAAM;;;;;;AAqBpB;;;;;;;;;;;;;;GAcG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AAG,IAAI,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAC,CAAC,CAyH9E;AA3JD;;;;;;;;GAQG;AAEH;;;;;;;;;;;GAWG;AACH,kCAPW,OAAO,WAAW,EAAE,OAAO,cAC3B,MAAM,gBACN,KAAK,GAAG,KAAK,GAAG,KAAK,sCAGnB,QAAQ,MAAM,CAAC,CAK3B;AA3HD,qCAgGC;AApLD;;;;;GAKG;AACH,+BAHW,MAAM,GACJ,KAAK,CAKjB;sBA1BqB,WAAW"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mermaid-js/mermaid-cli",
3
- "version": "10.8.0",
3
+ "version": "10.9.1",
4
4
  "description": "Command-line interface for mermaid",
5
5
  "license": "MIT",
6
6
  "repository": "git@github.com:mermaid-js/mermaid-cli.git",
@@ -31,14 +31,14 @@
31
31
  "dependencies": {
32
32
  "chalk": "^5.0.1",
33
33
  "commander": "^10.0.0",
34
+ "mermaid": "^10.8.0",
34
35
  "puppeteer": "^19.0.0"
35
36
  },
36
37
  "devDependencies": {
37
38
  "@fortawesome/fontawesome-free": "^5.6.0",
38
- "@mermaid-js/mermaid-zenuml": "^0.1.1",
39
+ "@mermaid-js/mermaid-zenuml": "^0.2.0",
39
40
  "@tsconfig/node14": "^14.1.0",
40
41
  "jest": "^29.0.1",
41
- "mermaid": "^10.0.0",
42
42
  "standard": "^17.0.0",
43
43
  "typescript": "^5.0.1-rc",
44
44
  "vite": "^4.0.3",
package/src/index.js CHANGED
@@ -12,6 +12,13 @@ const pkg = require('../package.json')
12
12
  // __dirname is not available in ESM modules by default
13
13
  const __dirname = url.fileURLToPath(new url.URL('.', import.meta.url))
14
14
 
15
+ /**
16
+ * Mermaid.js IFFE path.
17
+ *
18
+ * Importing this in a browser adds a global `mermaid` object.
19
+ */
20
+ const mermaidIIFEPath = path.resolve(path.dirname(require.resolve('mermaid')), 'mermaid.js')
21
+
15
22
  /**
16
23
  * Prints an error to stderr, then closes with exit code 1
17
24
  *
@@ -103,12 +110,13 @@ async function cli () {
103
110
  .addOption(new Option('-t, --theme [theme]', 'Theme of the chart').choices(['default', 'forest', 'dark', 'neutral']).default('default'))
104
111
  .addOption(new Option('-w, --width [width]', 'Width of the page').argParser(parseCommanderInt).default(800))
105
112
  .addOption(new Option('-H, --height [height]', 'Height of the page').argParser(parseCommanderInt).default(600))
106
- .option('-i, --input <input>', 'Input mermaid file. Files ending in .md will be treated as Markdown and all charts (e.g. ```mermaid (...)```) will be extracted and generated. Use `-` to read from stdin.')
113
+ .option('-i, --input <input>', 'Input mermaid file. Files ending in .md will be treated as Markdown and all charts (e.g. ```mermaid (...)``` or :::mermaid (...):::) will be extracted and generated. Use `-` to read from stdin.')
107
114
  .option('-o, --output [output]', 'Output file. It should be either md, svg, png or pdf. Optional. Default: input + ".svg"')
108
115
  .addOption(new Option('-e, --outputFormat [format]', 'Output format for the generated image.').choices(['svg', 'png', 'pdf']).default(null, 'Loaded from the output file extension'))
109
116
  .addOption(new Option('-b, --backgroundColor [backgroundColor]', 'Background color for pngs/svgs (not pdfs). Example: transparent, red, \'#F0F0F0\'.').default('white'))
110
117
  .option('-c, --configFile [configFile]', 'JSON configuration file for mermaid.')
111
118
  .option('-C, --cssFile [cssFile]', 'CSS file for the page.')
119
+ .option('-I, --svgId [svgId]', 'The id attribute for the SVG element to be rendered.')
112
120
  .addOption(new Option('-s, --scale [scale]', 'Puppeteer scale factor').argParser(parseCommanderInt).default(1))
113
121
  .option('-f, --pdfFit', 'Scale PDF to fit chart')
114
122
  .option('-q, --quiet', 'Suppress log output')
@@ -117,7 +125,7 @@ async function cli () {
117
125
 
118
126
  const options = commander.opts()
119
127
 
120
- let { theme, width, height, input, output, outputFormat, backgroundColor, configFile, cssFile, puppeteerConfigFile, scale, pdfFit, quiet } = options
128
+ let { theme, width, height, input, output, outputFormat, backgroundColor, configFile, cssFile, svgId, puppeteerConfigFile, scale, pdfFit, quiet } = options
121
129
 
122
130
  // check input file
123
131
  if (!input) {
@@ -187,7 +195,7 @@ async function cli () {
187
195
  quiet,
188
196
  outputFormat,
189
197
  parseMMDOptions: {
190
- mermaidConfig, backgroundColor, myCSS, pdfFit, viewport: { width, height, deviceScaleFactor: scale }
198
+ mermaidConfig, backgroundColor, myCSS, pdfFit, viewport: { width, height, deviceScaleFactor: scale }, svgId
191
199
  }
192
200
  }
193
201
  )
@@ -200,6 +208,7 @@ async function cli () {
200
208
  * @property {Parameters<import("mermaid")["default"]["initialize"]>[0]} [mermaidConfig] - Mermaid config.
201
209
  * @property {CSSStyleDeclaration["cssText"]} [myCSS] - Optional CSS text.
202
210
  * @property {boolean} [pdfFit] - If set, scale PDF to fit chart.
211
+ * @property {string} [svgId] - The id attribute for the SVG element to be rendered.
203
212
  */
204
213
 
205
214
  /**
@@ -229,7 +238,7 @@ async function parseMMD (browser, definition, outputFormat, opt) {
229
238
  * @returns {Promise<{title: string | null, desc: string | null, data: Buffer}>} The output file in bytes,
230
239
  * with optional metadata.
231
240
  */
232
- async function renderMermaid (browser, definition, outputFormat, { viewport, backgroundColor = 'white', mermaidConfig = {}, myCSS, pdfFit } = {}) {
241
+ async function renderMermaid (browser, definition, outputFormat, { viewport, backgroundColor = 'white', mermaidConfig = {}, myCSS, pdfFit, svgId } = {}) {
233
242
  const page = await browser.newPage()
234
243
  page.on('console', (msg) => {
235
244
  console.log(msg.text())
@@ -243,11 +252,14 @@ async function renderMermaid (browser, definition, outputFormat, { viewport, bac
243
252
  await page.$eval('body', (body, backgroundColor) => {
244
253
  body.style.background = backgroundColor
245
254
  }, backgroundColor)
246
- const metadata = await page.$eval('#container', async (container, definition, mermaidConfig, myCSS, backgroundColor) => {
255
+ await page.addScriptTag({ path: mermaidIIFEPath })
256
+ const metadata = await page.$eval('#container', async (container, definition, mermaidConfig, myCSS, backgroundColor, svgId) => {
257
+ await Promise.all(Array.from(document.fonts, (font) => font.load()))
258
+
247
259
  /**
248
260
  * @typedef {Object} GlobalThisWithMermaid
249
- * We've already imported these modules in our `index.html` file, so that they
250
- * get correctly bundled.
261
+ * We've already imported these modules in our `index.html` file (or by running `page.addScriptTag`),
262
+ * so that they get correctly bundled.
251
263
  * @property {import("mermaid")["default"]} mermaid Already imported mermaid instance
252
264
  * @property {import("@mermaid-js/mermaid-zenuml")["default"]} zenuml Already imported mermaid-zenuml instance
253
265
  */
@@ -256,7 +268,7 @@ async function renderMermaid (browser, definition, outputFormat, { viewport, bac
256
268
  await mermaid.registerExternalDiagrams([zenuml])
257
269
  mermaid.initialize({ startOnLoad: false, ...mermaidConfig })
258
270
  // should throw an error if mmd diagram is invalid
259
- const { svg: svgText } = await mermaid.render('my-svg', definition, container)
271
+ const { svg: svgText } = await mermaid.render(svgId || 'my-svg', definition, container)
260
272
  container.innerHTML = svgText
261
273
 
262
274
  const svg = container.getElementsByTagName?.('svg')?.[0]
@@ -292,7 +304,7 @@ async function renderMermaid (browser, definition, outputFormat, { viewport, bac
292
304
  return {
293
305
  title, desc
294
306
  }
295
- }, definition, mermaidConfig, myCSS, backgroundColor)
307
+ }, definition, mermaidConfig, myCSS, backgroundColor, svgId)
296
308
 
297
309
  if (outputFormat === 'svg') {
298
310
  const svgXML = await page.$eval('svg', (svg) => {
@@ -399,7 +411,7 @@ async function run (input, output, { puppeteerConfig = {}, quiet = false, output
399
411
  }
400
412
 
401
413
  // TODO: should we use a Markdown parser like remark instead of rolling our own parser?
402
- const mermaidChartsInMarkdown = /^[^\S\n]*```(?:mermaid)([^\S\n]*\r?\n([\s\S]*?))```[^\S\n]*$/
414
+ const mermaidChartsInMarkdown = /^[^\S\n]*[`:]{3}(?:mermaid)([^\S\n]*\r?\n([\s\S]*?))[`:]{3}[^\S\n]*$/
403
415
  const mermaidChartsInMarkdownRegexGlobal = new RegExp(mermaidChartsInMarkdown, 'gm')
404
416
  const browser = await puppeteer.launch(puppeteerConfig)
405
417
  try {