Skip to content

Commit 90c9138

Browse files
authored
Add ability to replace configs and cmds with env vars (#67)
1 parent 4007fbc commit 90c9138

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

index.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#! /usr/bin/env node
2-
var pandoc=require('pandoc-filter');
2+
var pandoc = require('pandoc-filter');
33
var _ = require('lodash');
44
var tmp = require('tmp');
55
var fs = require('fs');
@@ -8,7 +8,7 @@ var exec = require('child_process').execSync;
88
var process = require('process')
99
var sanfile = require('sanitize-filename')
1010

11-
var prefix="diagram";
11+
var prefix = "diagram";
1212
var cmd = externalTool("mmdc");
1313
var imgur = externalTool("imgur");
1414
var counter = 0;
@@ -36,17 +36,17 @@ function mermaid(type, value, format, meta) {
3636
scale: process.env.MERMAID_FILTER_SCALE || 1,
3737
imageClass: process.env.MERMAID_FILTER_IMAGE_CLASS || ''
3838
};
39-
var configFile = path.join(folder, ".mermaid-config.json")
39+
var configFile = process.env.MERMAID_FILTER_MERMAID_CONFIG || path.join(folder, ".mermaid-config.json");
4040
var confFileOpts = ""
4141
if (fs.existsSync(configFile)) {
4242
confFileOpts += ` -c "${configFile}"`
4343
}
44-
var puppeteerConfig = path.join(folder, ".puppeteer.json")
44+
var puppeteerConfig = process.env.MERMAID_FILTER_PUPPETEER_CONFIG || path.join(folder, ".puppeteer.json");
4545
var puppeteerOpts = ""
4646
if (fs.existsSync(puppeteerConfig)) {
4747
puppeteerOpts += ` -p "${puppeteerConfig}"`
4848
}
49-
var cssFile = path.join(folder, ".mermaid.css")
49+
var cssFile = process.env.MERMAID_FILTER_MERMAID_CSS || path.join(folder, ".mermaid.css");
5050
if (fs.existsSync(cssFile)) {
5151
confFileOpts += ` -C "${cssFile}"`
5252
}
@@ -121,9 +121,20 @@ function mermaid(type, value, format, meta) {
121121
}
122122

123123
function externalTool(command) {
124-
return firstExisting([
125-
path.resolve(__dirname, "node_modules", ".bin", command),
126-
path.resolve(__dirname, "..", ".bin", command)],
124+
var paths = [
125+
path.resolve(__dirname, "node_modules", ".bin", command),
126+
path.resolve(__dirname, "..", ".bin", command)
127+
];
128+
// Ability to replace path of external tool by environment variable
129+
// to replace `mmdc` use `MERMAID_FILTER_CMD_MMDC`
130+
// to replace `imgur` use `MERMAID_FILTER_CMD_IMGUR`
131+
var envCmdName = "MERMAID_FILTER_CMD_" + (command || "").toUpperCase().replace(/[^A-Z0-9-]/g, "_");
132+
var envCmd = process.env[envCmdName];
133+
if (envCmd) {
134+
paths = [envCmd];
135+
command = "env: " + envCmdName; // for error message
136+
}
137+
return firstExisting(paths,
127138
function() {
128139
console.error("External tool not found: " + command);
129140
process.exit(1);
@@ -148,7 +159,7 @@ function firstExisting(paths, error) {
148159
}
149160

150161
pandoc.toJSONFilter(function(type, value, format, meta) {
151-
// Redirect stderr to a globally created writeable stream
162+
// Redirect stderr to a globally created writeable stream
152163
process.stderr.write = errorLog.write.bind(errorLog);
153164
return mermaid(type, value, format, meta);
154165
});

0 commit comments

Comments
 (0)