-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
151 lines (124 loc) · 3.76 KB
/
main.js
File metadata and controls
151 lines (124 loc) · 3.76 KB
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
145
146
147
148
149
150
151
#! /usr/bin/env node
const pandoc = require('pandoc-filter')
const fs = require('fs')
const path = require('path')
const process = require('process')
const yaml = require('yaml')
const mermaidAction = require('./mermaid-action.js')
const chartjsAction = require('./chartjs-action.js')
/*
utils
*/
function createLogger() {
// it's necessary to redirect any standard output into a file
// otherwise pandoc will break
const loggerFilename = path.join(process.cwd(), 'pandoc-mermaid-chartjs-filter.log')
const loggerStream = fs.createWriteStream(loggerFilename)
const { log } = console
console.log = function (message) {
loggerStream.write(message + '\n')
log.apply(console, arguments)
}
process.stderr.write = loggerStream.write.bind(loggerStream)
return console
}
function createDataURI(data, format) {
const formats = {
'svg': 'image/svg+xml',
'png': 'image/png'
}
if (!formats.hasOwnProperty(format)) return null
let encodedData = new Buffer.from(data).toString('base64')
return `data:${formats[format]};base64,${encodedData}`
}
function createDataFile(data, format, options) {
fs.writeFileSync(options.filename, data)
return fs.existsSync(options.filename)
}
function loadOptions(userOptions = []) {
const options = {
background: 'white',
caption: '',
filename: false,
format: 'png',
inline: true,
scale: 2,
skip: false,
theme: 'default',
width: '800'
}
userOptions.forEach(option => {
if (option.length == 2 && options.hasOwnProperty(option[0].toLowerCase()))
options[option[0].toLowerCase()] = option[1]
})
options.format = options.format.toLowerCase().trim()
return options
}
/*
main
*/
function action({ t: type, c: value }, format, meta) {
if (type != 'CodeBlock') return null
let data = null
let options = loadOptions(value[0][2])
const classes = value[0][1]
const content = value[1]
const id = value[0][0]
const title = options.caption
const alt = options.caption
if (!options.filename)
options.filename = `figure-${figureIndex}.${options.format}`
else
if (options.filename.match(/\.(svg|png)$/i))
options.format = options.filename.match(/\.(svg|png)$/i)[1]
if (options.skip) return null
// mermaid
if (classes.includes('mermaid'))
data = mermaidAction(content, options)
// chartjs
if (classes.includes('chartjs'))
data = chartjsAction(content, options)
if (!data) return null
figureIndex++
// console.log(process.cwd())
let src = null
if (options.inline && options.format.match(/svg|png/i))
src = createDataURI(data, options.format)
else {
if (createDataFile(data, options.format, { filename: options.filename } ))
src = options.filename
}
if (src) {
if (title !== '') {
return pandoc.Figure(
[id, classes, []],
[
[pandoc.Str(title)],
[pandoc.Para([pandoc.Str(title)])]
],
[pandoc.Para([
pandoc.Image(
[id, classes, []],
[pandoc.Str(alt)],
[src, title]
)
])]
)
}
else {
return pandoc.Para([
pandoc.Image(
[id, classes, []],
[pandoc.Str(alt)],
[src, title]
)
])
}
}
else {
return null
}
}
createLogger()
var figureIndex = 1
pandoc.stdio(action)