Skip to content

Commit 726b652

Browse files
ouuanaloisklink
authored andcommitted
feat: add svgId option
1 parent 32bccb4 commit 726b652

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ async function cli () {
109109
.addOption(new Option('-b, --backgroundColor [backgroundColor]', 'Background color for pngs/svgs (not pdfs). Example: transparent, red, \'#F0F0F0\'.').default('white'))
110110
.option('-c, --configFile [configFile]', 'JSON configuration file for mermaid.')
111111
.option('-C, --cssFile [cssFile]', 'CSS file for the page.')
112+
.option('-I, --svgId [svgId]', 'The id attribute for the SVG element to be rendered.')
112113
.addOption(new Option('-s, --scale [scale]', 'Puppeteer scale factor').argParser(parseCommanderInt).default(1))
113114
.option('-f, --pdfFit', 'Scale PDF to fit chart')
114115
.option('-q, --quiet', 'Suppress log output')
@@ -117,7 +118,7 @@ async function cli () {
117118

118119
const options = commander.opts()
119120

120-
let { theme, width, height, input, output, outputFormat, backgroundColor, configFile, cssFile, puppeteerConfigFile, scale, pdfFit, quiet } = options
121+
let { theme, width, height, input, output, outputFormat, backgroundColor, configFile, cssFile, svgId, puppeteerConfigFile, scale, pdfFit, quiet } = options
121122

122123
// check input file
123124
if (!input) {
@@ -187,7 +188,7 @@ async function cli () {
187188
quiet,
188189
outputFormat,
189190
parseMMDOptions: {
190-
mermaidConfig, backgroundColor, myCSS, pdfFit, viewport: { width, height, deviceScaleFactor: scale }
191+
mermaidConfig, backgroundColor, myCSS, pdfFit, viewport: { width, height, deviceScaleFactor: scale }, svgId
191192
}
192193
}
193194
)
@@ -200,6 +201,7 @@ async function cli () {
200201
* @property {Parameters<import("mermaid")["default"]["initialize"]>[0]} [mermaidConfig] - Mermaid config.
201202
* @property {CSSStyleDeclaration["cssText"]} [myCSS] - Optional CSS text.
202203
* @property {boolean} [pdfFit] - If set, scale PDF to fit chart.
204+
* @property {string} [svgId] - The id attribute for the SVG element to be rendered.
203205
*/
204206

205207
/**
@@ -229,7 +231,7 @@ async function parseMMD (browser, definition, outputFormat, opt) {
229231
* @returns {Promise<{title: string | null, desc: string | null, data: Buffer}>} The output file in bytes,
230232
* with optional metadata.
231233
*/
232-
async function renderMermaid (browser, definition, outputFormat, { viewport, backgroundColor = 'white', mermaidConfig = {}, myCSS, pdfFit } = {}) {
234+
async function renderMermaid (browser, definition, outputFormat, { viewport, backgroundColor = 'white', mermaidConfig = {}, myCSS, pdfFit, svgId } = {}) {
233235
const page = await browser.newPage()
234236
page.on('console', (msg) => {
235237
console.log(msg.text())
@@ -243,7 +245,7 @@ async function renderMermaid (browser, definition, outputFormat, { viewport, bac
243245
await page.$eval('body', (body, backgroundColor) => {
244246
body.style.background = backgroundColor
245247
}, backgroundColor)
246-
const metadata = await page.$eval('#container', async (container, definition, mermaidConfig, myCSS, backgroundColor) => {
248+
const metadata = await page.$eval('#container', async (container, definition, mermaidConfig, myCSS, backgroundColor, svgId) => {
247249
/**
248250
* @typedef {Object} GlobalThisWithMermaid
249251
* We've already imported these modules in our `index.html` file, so that they
@@ -256,7 +258,7 @@ async function renderMermaid (browser, definition, outputFormat, { viewport, bac
256258
await mermaid.registerExternalDiagrams([zenuml])
257259
mermaid.initialize({ startOnLoad: false, ...mermaidConfig })
258260
// should throw an error if mmd diagram is invalid
259-
const { svg: svgText } = await mermaid.render('my-svg', definition, container)
261+
const { svg: svgText } = await mermaid.render(svgId || 'my-svg', definition, container)
260262
container.innerHTML = svgText
261263

262264
const svg = container.getElementsByTagName?.('svg')?.[0]
@@ -292,7 +294,7 @@ async function renderMermaid (browser, definition, outputFormat, { viewport, bac
292294
return {
293295
title, desc
294296
}
295-
}, definition, mermaidConfig, myCSS, backgroundColor)
297+
}, definition, mermaidConfig, myCSS, backgroundColor, svgId)
296298

297299
if (outputFormat === 'svg') {
298300
const svgXML = await page.$eval('svg', (svg) => {

0 commit comments

Comments
 (0)