Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add svgId option #645

Merged
merged 2 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src-test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,14 @@ describe('mermaid-cli', () => {
expectBytesAreFormat(await fs.readFile(expectedOutputFile), 'png')
}, timeout)

test('the id of <svg> can be set', async () => {
const outputFile = 'test-positive/flowchart1.mmd.svg'
await fs.rm(outputFile, { force: true })
await promisify(execFile)('node', ['src/cli.js', '-i', 'test-positive/flowchart1.mmd', '-o', outputFile, '-I', 'custom-id'])

expect((await fs.readFile(outputFile)).toString()).toMatch(/^<svg[^>]+id="custom-id"/)
}, timeout)

test.concurrent.each(['svg', 'png', 'pdf'])('should set red background to %s', async (format) => {
await promisify(execFile)('node', [
'src/cli.js', '-i', 'test-positive/flowchart1.mmd', '-o', `test-output/flowchart1-red-background.${format}`,
Expand Down
14 changes: 8 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ async function cli () {
.addOption(new Option('-b, --backgroundColor [backgroundColor]', 'Background color for pngs/svgs (not pdfs). Example: transparent, red, \'#F0F0F0\'.').default('white'))
.option('-c, --configFile [configFile]', 'JSON configuration file for mermaid.')
.option('-C, --cssFile [cssFile]', 'CSS file for the page.')
.option('-I, --svgId [svgId]', 'The id attribute for the SVG element to be rendered.')
.addOption(new Option('-s, --scale [scale]', 'Puppeteer scale factor').argParser(parseCommanderInt).default(1))
.option('-f, --pdfFit', 'Scale PDF to fit chart')
.option('-q, --quiet', 'Suppress log output')
Expand All @@ -117,7 +118,7 @@ async function cli () {

const options = commander.opts()

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

// check input file
if (!input) {
Expand Down Expand Up @@ -187,7 +188,7 @@ async function cli () {
quiet,
outputFormat,
parseMMDOptions: {
mermaidConfig, backgroundColor, myCSS, pdfFit, viewport: { width, height, deviceScaleFactor: scale }
mermaidConfig, backgroundColor, myCSS, pdfFit, viewport: { width, height, deviceScaleFactor: scale }, svgId
}
}
)
Expand All @@ -200,6 +201,7 @@ async function cli () {
* @property {Parameters<import("mermaid")["default"]["initialize"]>[0]} [mermaidConfig] - Mermaid config.
* @property {CSSStyleDeclaration["cssText"]} [myCSS] - Optional CSS text.
* @property {boolean} [pdfFit] - If set, scale PDF to fit chart.
* @property {string} [svgId] - The id attribute for the SVG element to be rendered.
*/

/**
Expand Down Expand Up @@ -229,7 +231,7 @@ async function parseMMD (browser, definition, outputFormat, opt) {
* @returns {Promise<{title: string | null, desc: string | null, data: Buffer}>} The output file in bytes,
* with optional metadata.
*/
async function renderMermaid (browser, definition, outputFormat, { viewport, backgroundColor = 'white', mermaidConfig = {}, myCSS, pdfFit } = {}) {
async function renderMermaid (browser, definition, outputFormat, { viewport, backgroundColor = 'white', mermaidConfig = {}, myCSS, pdfFit, svgId } = {}) {
const page = await browser.newPage()
page.on('console', (msg) => {
console.log(msg.text())
Expand All @@ -243,7 +245,7 @@ async function renderMermaid (browser, definition, outputFormat, { viewport, bac
await page.$eval('body', (body, backgroundColor) => {
body.style.background = backgroundColor
}, backgroundColor)
const metadata = await page.$eval('#container', async (container, definition, mermaidConfig, myCSS, backgroundColor) => {
const metadata = await page.$eval('#container', async (container, definition, mermaidConfig, myCSS, backgroundColor, svgId) => {
/**
* @typedef {Object} GlobalThisWithMermaid
* We've already imported these modules in our `index.html` file, so that they
Expand All @@ -256,7 +258,7 @@ async function renderMermaid (browser, definition, outputFormat, { viewport, bac
await mermaid.registerExternalDiagrams([zenuml])
mermaid.initialize({ startOnLoad: false, ...mermaidConfig })
// should throw an error if mmd diagram is invalid
const { svg: svgText } = await mermaid.render('my-svg', definition, container)
const { svg: svgText } = await mermaid.render(svgId || 'my-svg', definition, container)
container.innerHTML = svgText

const svg = container.getElementsByTagName?.('svg')?.[0]
Expand Down Expand Up @@ -292,7 +294,7 @@ async function renderMermaid (browser, definition, outputFormat, { viewport, bac
return {
title, desc
}
}, definition, mermaidConfig, myCSS, backgroundColor)
}, definition, mermaidConfig, myCSS, backgroundColor, svgId)

if (outputFormat === 'svg') {
const svgXML = await page.$eval('svg', (svg) => {
Expand Down
Loading