forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpandoc.js
More file actions
51 lines (42 loc) · 1.19 KB
/
pandoc.js
File metadata and controls
51 lines (42 loc) · 1.19 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
// Copy from https://github.com/utatti/simple-pandoc/blob/master/index.js
import { spawn } from 'child_process'
import commandExists from 'command-exists'
import { isFile2 } from 'common/filesystem'
const pandocCommand = 'pandoc'
const getCommand = () => {
if (envPathExists()) {
return process.env.MARKTEXT_PANDOC
}
return pandocCommand
}
const pandoc = (from, to, ...args) => {
const command = getCommand()
const option = ['-s', from, '-t', to].concat(args)
const converter = () => new Promise((resolve, reject) => {
const proc = spawn(command, option)
proc.on('error', reject)
let data = ''
proc.stdout.on('data', chunk => {
data += chunk.toString()
})
proc.stdout.on('end', () => resolve(data))
proc.stdout.on('error', reject)
proc.stdin.end()
})
converter.stream = srcStream => {
const proc = spawn(command, option)
srcStream.pipe(proc.stdin)
return proc.stdout
}
return converter
}
pandoc.exists = () => {
if (envPathExists()) {
return true
}
return commandExists.sync(pandocCommand)
}
const envPathExists = () => {
return !!process.env.MARKTEXT_PANDOC && isFile2(process.env.MARKTEXT_PANDOC)
}
export default pandoc