11#! /usr/bin/env node
2- var pandoc = require ( 'pandoc-filter' ) ;
2+ var pandoc = require ( 'pandoc-filter' ) ;
33var _ = require ( 'lodash' ) ;
44var tmp = require ( 'tmp' ) ;
55var fs = require ( 'fs' ) ;
@@ -8,7 +8,7 @@ var exec = require('child_process').execSync;
88var process = require ( 'process' )
99var sanfile = require ( 'sanitize-filename' )
1010
11- var prefix = "diagram" ;
11+ var prefix = "diagram" ;
1212var cmd = externalTool ( "mmdc" ) ;
1313var imgur = externalTool ( "imgur" ) ;
1414var 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
123123function 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 - Z 0 - 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
150161pandoc . 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