diff --git a/lib/mermaid.js b/lib/mermaid.js index 6e6eecd..21008e1 100644 --- a/lib/mermaid.js +++ b/lib/mermaid.js @@ -1,11 +1,13 @@ 'use strict'; +const fs = require('fs'); +const tmp = require('tmp'); const { spawnSync } = require('node:child_process'); function mermaid(config) { return function(args, content) { let options = Object.assign({}, config); - let caption = ''; + let caption = '', configPath = ''; args.forEach(arg => { const arr = arg.split(':'); if (arr.length == 1) { @@ -16,7 +18,7 @@ function mermaid(config) { } }); - const mmdcArgs = [ + let mmdcArgs = [ '-t', options.theme, '-w', options.width, '-H', options.height, @@ -27,6 +29,19 @@ function mermaid(config) { '-o', '-' ]; + const arr = content.split(''); + if (arr.length == 2) { + const mmdConfig = arr[0]; + content = arr[1]; + + const tmpFile = tmp.fileSync(); + configPath = tmpFile.name; + fs.writeFileSync(configPath, mmdConfig); + } + if (configPath !== '') { + mmdcArgs.push('-c', configPath); + } + const result = spawnSync(options.mmdcPath, mmdcArgs, { cwd: process.cwd(), env: process.env,