@mermaid-js/mermaid-cli 9.3.0 → 10.0.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mermaid-js/mermaid-cli",
3
- "version": "9.3.0",
3
+ "version": "10.0.0",
4
4
  "description": "Command-line interface for mermaid",
5
5
  "license": "MIT",
6
6
  "repository": "git@github.com:mermaid-js/mermaid-cli.git",
@@ -22,13 +22,13 @@
22
22
  },
23
23
  "dependencies": {
24
24
  "chalk": "^5.0.1",
25
- "commander": "^9.0.0",
25
+ "commander": "^10.0.0",
26
26
  "puppeteer": "^19.0.0"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@fortawesome/fontawesome-free-webfonts": "^1.0.9",
30
30
  "@mermaid-js/mermaid-mindmap": "^9.2.2",
31
- "mermaid": "^9.2.2",
31
+ "mermaid": "^10.0.0",
32
32
  "jest": "^29.0.1",
33
33
  "standard": "^17.0.0",
34
34
  "vite": "^4.0.3",
package/src/index.js CHANGED
@@ -18,7 +18,7 @@ const error = message => {
18
18
  }
19
19
 
20
20
  const warn = message => {
21
- console.log(chalk.yellow(`\n${message}\n`))
21
+ console.warn(chalk.yellow(`\n${message}\n`))
22
22
  }
23
23
 
24
24
  const checkConfigFile = file => {
@@ -27,8 +27,6 @@ const checkConfigFile = file => {
27
27
  }
28
28
  }
29
29
 
30
- const inputPipedFromStdin = () => fs.fstatSync(0).isFIFO()
31
-
32
30
  const getInputData = async inputFile => new Promise((resolve, reject) => {
33
31
  // if an input file has been specified using '-i', it takes precedence over
34
32
  // piping from stdin
@@ -84,7 +82,7 @@ async function cli () {
84
82
  .addOption(new Option('-t, --theme [theme]', 'Theme of the chart').choices(['default', 'forest', 'dark', 'neutral']).default('default'))
85
83
  .addOption(new Option('-w, --width [width]', 'Width of the page').argParser(parseCommanderInt).default(800))
86
84
  .addOption(new Option('-H, --height [height]', 'Height of the page').argParser(parseCommanderInt).default(600))
87
- .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. Required.')
85
+ .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.')
88
86
  .option('-o, --output [output]', 'Output file. It should be either md, svg, png or pdf. Optional. Default: input + ".svg"')
89
87
  .addOption(new Option('-e, --outputFormat [format]', 'Output format for the generated image.').choices(['svg', 'png', 'pdf']).default(null, 'Loaded from the output file extension'))
90
88
  .addOption(new Option('-b, --backgroundColor [backgroundColor]', 'Background color for pngs/svgs (not pdfs). Example: transparent, red, \'#F0F0F0\'.').default('white'))
@@ -101,12 +99,15 @@ async function cli () {
101
99
  let { theme, width, height, input, output, outputFormat, backgroundColor, configFile, cssFile, puppeteerConfigFile, scale, pdfFit, quiet } = options
102
100
 
103
101
  // check input file
104
- if (!(input || inputPipedFromStdin())) {
105
- console.error(chalk.red('\nPlease specify input file: -i <input>\n'))
106
- // Log to stderr, and return with error exitCode
107
- commander.help({ error: true })
108
- }
109
- if (input && !fs.existsSync(input)) {
102
+ if (!input) {
103
+ warn('No input file specfied, reading from stdin. ' +
104
+ 'If you want to specify an input file, please use `-i <input>.` ' +
105
+ 'You can use `-i -` to read from stdin and to suppress this warning.'
106
+ )
107
+ } else if (input === '-') {
108
+ // `--input -` means read from stdin, but suppress the above warning
109
+ input = undefined
110
+ } else if (!fs.existsSync(input)) {
110
111
  error(`Input file "${input}" doesn't exist`)
111
112
  }
112
113
 
@@ -121,8 +122,8 @@ async function cli () {
121
122
  output = input ? (`${input}.svg`) : 'out.svg'
122
123
  }
123
124
  }
124
- if (!/\.(?:svg|png|pdf|md)$/.test(output)) {
125
- error('Output file must end with ".md", ".svg", ".png" or ".pdf"')
125
+ if (!/\.(?:svg|png|pdf|md|markdown)$/.test(output)) {
126
+ error('Output file must end with ".md"/".markdown", ".svg", ".png" or ".pdf"')
126
127
  }
127
128
  const outputDir = path.dirname(output)
128
129
  if (!fs.existsSync(outputDir)) {
@@ -225,7 +226,10 @@ async function renderMermaid (browser, definition, outputFormat, { viewport, bac
225
226
  mermaid.initialize(mermaidConfig)
226
227
  // should throw an error if mmd diagram is invalid
227
228
  try {
228
- await mermaid.initThrowsErrorsAsync(undefined, container)
229
+ await mermaid.run({
230
+ nodes: [container],
231
+ suppressErrors: false
232
+ })
229
233
  } catch (error) {
230
234
  if (error instanceof Error) {
231
235
  // mermaid-js doesn't currently throws JS Errors, but let's leave this
@@ -346,10 +350,11 @@ function markdownImage ({ url, title, alt }) {
346
350
  /**
347
351
  * Renders a mermaid diagram or mermaid markdown file.
348
352
  *
349
- * @param {`${string}.md` | string} [input] - If this ends with `.md`, path to a markdown file containing mermaid.
353
+ * @param {`${string}.${"md" | "markdown"}` | string} [input] - If this ends with `.md`/`.markdown`,
354
+ * path to a markdown file containing mermaid.
350
355
  * If this is a string, loads the mermaid definition from the given file.
351
356
  * If this is `undefined`, loads the mermaid definition from stdin.
352
- * @param {`${string}.${"md" | "svg" | "png" | "pdf"}`} output - Path to the output file.
357
+ * @param {`${string}.${"md" | "markdown" | "svg" | "png" | "pdf"}`} output - Path to the output file.
353
358
  * @param {Object} [opts] - Options
354
359
  * @param {puppeteer.LaunchOptions} [opts.puppeteerConfig] - Puppeteer launch options.
355
360
  * @param {boolean} [opts.quiet] - If set, suppress log output.
@@ -372,7 +377,7 @@ async function run (input, output, { puppeteerConfig = {}, quiet = false, output
372
377
  if (!outputFormat) {
373
378
  outputFormat = path.extname(output).replace('.', '')
374
379
  }
375
- if (outputFormat === 'md') {
380
+ if (outputFormat === 'md' || outputFormat === 'markdown') {
376
381
  // fallback to svg in case no outputFormat is given and output file is MD
377
382
  outputFormat = 'svg'
378
383
  }
@@ -381,7 +386,7 @@ async function run (input, output, { puppeteerConfig = {}, quiet = false, output
381
386
  }
382
387
 
383
388
  const definition = await getInputData(input)
384
- if (/\.md$/.test(input)) {
389
+ if (/\.(md|markdown)$/.test(input)) {
385
390
  const imagePromises = []
386
391
  for (const mermaidCodeblockMatch of definition.matchAll(mermaidChartsInMarkdownRegexGlobal)) {
387
392
  const mermaidDefinition = mermaidCodeblockMatch[1]
@@ -391,7 +396,10 @@ async function run (input, output, { puppeteerConfig = {}, quiet = false, output
391
396
  // I.e. if "out.png", use "out-1.png", "out-2.png", etc
392
397
  // If it is an output `.md` file, use that to base .svg numbered diagrams on
393
398
  // I.e. if "out.md". use "out-1.svg", "out-2.svg", etc
394
- const outputFile = output.replace(/(\.(md|png|svg|pdf))$/, `-${imagePromises.length + 1}$1`).replace(/(\.md)$/, `.${outputFormat}`)
399
+ const outputFile = output.replace(
400
+ /(\.(md|markdown|png|svg|pdf))$/,
401
+ `-${imagePromises.length + 1}$1`
402
+ ).replace(/\.(md|markdown)$/, `.${outputFormat}`)
395
403
  const outputFileRelative = `./${path.relative(path.dirname(path.resolve(output)), path.resolve(outputFile))}`
396
404
 
397
405
  const imagePromise = (async () => {
@@ -416,7 +424,7 @@ async function run (input, output, { puppeteerConfig = {}, quiet = false, output
416
424
 
417
425
  const images = await Promise.all(imagePromises)
418
426
 
419
- if (/\.md$/.test(output)) {
427
+ if (/\.(md|markdown)$/.test(output)) {
420
428
  const outDefinition = definition.replace(mermaidChartsInMarkdownRegexGlobal, (_mermaidMd) => {
421
429
  // pop first image from front of array
422
430
  const { url, title, alt } = images.shift()