forked from raghur/mermaid-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
144 lines (131 loc) · 4.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#! /usr/bin/env node
var pandoc=require('pandoc-filter');
var _ = require('lodash');
var tmp = require('tmp');
var fs = require('fs');
var path = require('path');
var exec = require('child_process').execSync;
var process = require('process')
var sanfile = require('sanitize-filename')
var prefix="diagram";
var cmd = externalTool("mmdc");
var imgur = externalTool("imgur");
var counter = 0;
var folder = process.cwd()
// console.log(folder)
function mermaid(type, value, format, meta) {
if (type != "CodeBlock") return null;
var attrs = value[0],
content = value[1];
var id = attrs[0],
classes = attrs[1];
var options = {
width: process.env.MERMAID_FILTER_WIDTH || 800,
format: process.env.MERMAID_FILTER_FORMAT || 'png',
loc: process.env.MERMAID_FILTER_LOC || 'inline',
theme: process.env.MERMAID_FILTER_THEME || 'default',
caption: process.env.MERMAID_FILTER_CAPTION || '',
filename: process.env.MERMAID_FILTER_FILENAME || ''
};
var configFile = path.join(folder, ".mermaid-config.json")
var confFileOpts = ""
if (fs.existsSync(configFile)) {
confFileOpts += " -c " + configFile
}
var puppeteerConfig = path.join(folder, ".puppeteer.json")
var puppeteerOpts = ""
if (fs.existsSync(puppeteerConfig)) {
puppeteerOpts += " -p " + puppeteerConfig
}
var cssFile = path.join(folder, ".mermaid.css")
if (fs.existsSync(cssFile)) {
confFileOpts += " -C " + cssFile
}
// console.log(classes)
if (classes.indexOf('mermaid') < 0) return null;
// console.log(attrs, content);
attrs[2].map(item => {
if (item.length === 1) options[item[0]] = true;
else options[item[0]] = item[1];
});
// console.log(options);
// if (options.loc === 'inline') options.format = 'svg'
counter++;
//console.log(content);
var tmpfileObj = tmp.fileSync();
// console.log(tmpfileObj.name);
fs.writeFileSync(tmpfileObj.name, content);
var outdir = options.loc !== 'imgur' ? options.loc : path.dirname(tmpfileObj.name);
// console.log(outdir);
if (options.caption !== "" && options.filename === ""){
options.filename = sanfile(options.caption, {replacement: ''}).replace(/[#$~%+;()\[\]{}&=_\-\s]/g, '');
}
if (options.filename === ""){
options.filename = `${prefix}-${counter}`;
}
var savePath = tmpfileObj.name + "." + options.format
var newPath = path.join(outdir, `${options.filename}.${options.format}`);
var fullCmd = `${cmd} ${confFileOpts} ${puppeteerOpts} -w ${options.width} -i ${tmpfileObj.name} -t ${options.theme} -o ${savePath}`
// console.log(fullCmd, savePath)
exec(fullCmd);
//console.log(oldPath, newPath);
if (options.loc == 'inline') {
if (options.format === 'svg') {
var data = fs.readFileSync(savePath, 'utf8')
newPath = "data:image/svg+xml;base64," + new Buffer(data).toString('base64');
} else {
var data = fs.readFileSync(savePath)
newPath = 'data:image/png;base64,' + new Buffer(data).toString('base64');
}
} else if (options.loc === 'imgur')
newPath = exec(`${imgur} ${savePath}`)
.toString()
.trim()
.replace("http://", "https://");
else {
mv(savePath, newPath);
}
var fig = "";
if (options.caption != "") {
fig = "fig:";
}
return pandoc.Para(
[
pandoc.Image(
[id, [], []],
[pandoc.Str(options.caption)],
[newPath, fig]
)
]);
}
function externalTool(command) {
return firstExisting([
path.resolve(__dirname, "node_modules", ".bin", command),
path.resolve(__dirname, "..", ".bin", command)],
function() {
console.error("External tool not found: " + command);
process.exit(1);
});
}
function mv(from, to) {
var readStream = fs.createReadStream(from)
var writeStream = fs.createWriteStream(to);
readStream.on('close', () => {
fs.unlinkSync(from);
});
readStream.pipe(writeStream);
}
function firstExisting(paths, error) {
for (var i = 0; i < paths.length; i++) {
if (fs.existsSync(paths[i])) return `"${paths[i]}"`;
}
error();
}
pandoc.toJSONFilter(function(type, value, format, meta) {
// redirect stderr to file - if it logs to stdout, then pandoc hangs due to improper json
errFile = path.join(folder, "mermaid-filter.err");
errorLog = fs.createWriteStream(errFile);
var origStdErr = process.stderr.write;
process.stderr.write = errorLog.write.bind(errorLog);
return mermaid(type, value, format, meta);
});