diff --git a/index.js b/index.js new file mode 100644 index 0000000..27ddd6c --- /dev/null +++ b/index.js @@ -0,0 +1,19 @@ +'use strict'; + +/* global hexo */ + +const mermaid = require('./mermaid.js'); + +hexo.config.mermaid = Object.assign({ + mmdcPath: 'mmdc', + timeout: 5000, + theme: 'default', + width: 800, + height: 600, + format: 'svg', + background: 'white', + scale: 1 +}, +hexo.config.mermaid); + +hexo.extend.tag.register('mermaid', mermaid(hexo.config.mermaid), { ends = true }); \ No newline at end of file diff --git a/lib/mermaid.js b/lib/mermaid.js new file mode 100644 index 0000000..436a997 --- /dev/null +++ b/lib/mermaid.js @@ -0,0 +1,57 @@ +'use strict'; + +const { spawnSync } = require('node:child_process'); + +function mermaid(config) { + return function(args, content) { + let options = Object.assign({}, config); + let caption = ''; + for (const arg in args) { + const arr = arg.split(':'); + if (arr.length == 1) { + caption = arr; + } else { + const { key, val } = arr; + options[key] = val; + } + } + + const args = [ + '-t', options.theme || 'default', + '-w', options.width || 800, + '-H', options.height || 600, + '-e', options.format || 'svg', + '-b', options.background || 'white', + '-s', options.scale || 1, + '-i', '-', + '-o', '-' + ]; + + const result = spawnSync(options.mmdcPath, args, { + cwd: process.cwd(), + env: process.env, + input: content, + encoding: 'utf8', + timeout: options.timeout + }); + + if (result.status === 0) { + if (result.stderr) { + console.warn(result.stderr); + } + const path = 'data:image/svg+xml;base64,' + Buffer.from(result.stdout).toString('base64'); + + if (caption != '') { + return `
${ caption }
${ caption }
` + } else { + return `

`; + } + } + if (result.error) { + throw result.error; + } + throw Error('mmdc exited with code ' + result.status + (result.stderr ? ': ' + result.stderr : '.')); + }; +} + +module.exports = mermaid; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..f3dca33 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "hexo-tag-mermaid", + "version": "0.0.0", + "description": "add mermaid diagrams support for hexo.", + "homepage": "https://git.gzezfisher.top/FISHER_/hexo-tag-mermaid", + "bugs": { + "url": "https://git.gzezfisher.top/FISHER_/hexo-tag-mermaid/issues" + }, + "repository": { + "type": "git", + "url": "https://git.gzezfisher.top/FISHER_/hexo-tag-mermaid.git" + }, + "main": "index.js", + "files": [ + "lib" + ], + "contributers": [], + "tags": [ + "hexo", + "plugin", + "mermaid" + ], + "dependencies": {} +}