-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·96 lines (89 loc) · 1.79 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
#!/usr/bin/env node
import { franc } from 'franc'
import getStdin from 'get-stdin'
import hljs from 'highlight.js'
import { iso6393To1 } from 'iso-639-3'
import pandoc from 'pandoc-filter'
const shortLang = iso6393To1
// programming languages roughly sorted by popularity
const codeLangs = [
'diff',
'markdown',
'json',
'yaml',
'javascript',
'java',
'python',
'css',
'html',
'php',
'ruby',
'makefile',
'cpp',
'c',
'bash',
'cs',
'objectivec',
'r',
'go',
'perl',
'coffee',
'latex',
'bibtex',
'swift',
'scala',
'haskell',
'lua',
'clojure',
'matlab',
'groovy',
'puppet',
'rust',
'kotlin',
'powershell',
'erlang',
'vb',
'typescript',
'xml',
'xslt',
'actionscript',
'asp',
'ocaml',
'd',
'scheme',
'dart',
'commonlisp',
'julia',
'fsharp',
'elixir',
'fortran',
'haxe'
]
function MetaInlines (...args) {
return { t: 'MetaInlines', c: args }
}
const words = []
function action (el) {
switch (el.t) {
case 'CodeBlock': return codeblock(...el.c)
case 'Space': words.push(' '); break
case 'Str': words.push(el.c); break
}
}
function codeblock ([id, classes, attrs], content) {
if (classes.join() === 'highlight') {
const { language, relevance } = hljs.highlightAuto(content, codeLangs)
classes = (relevance > 25) ? [language] : []
return pandoc.CodeBlock([id, classes, attrs], content)
}
}
getStdin().then(async str => {
const data = JSON.parse(str)
const format = (process.argv.length > 2) ? process.argv[2] : ''
const meta = data.meta || data[0].unMeta
const output = await pandoc.walk(data, action, format, meta)
let lang = franc(words.join(''))
if (shortLang[lang]) lang = shortLang[lang]
output.meta.lang = MetaInlines(pandoc.Str(lang))
process.stdout.write(JSON.stringify(output))
})