-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
162 lines (133 loc) · 4.15 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const path = require('path')
const marked = require('marked')
const traverse = require('traverse')
const MetadocPlugin = require('@author.io/metadoc-plugin')
// NOTICE: Mermaid doesn't support server side SVG Rendering yet
// due to lack of dependencies on JSDOM and deprecation of PhantomJS.
// See https://github.com/knsv/mermaid/issues/559.
// let mermaidId = 0
// const mermaid = require('mermaid')
// mermaid.initialize()
// const renderMermaidSvg = function (code) {
// return new Promise(resolve => {
// mermaid.render(`mermaid${mermaidId++}`, code, svg => {
// resolve(svg)
// })
// })
// }
class Parser extends MetadocPlugin {
constructor () {
super(...arguments)
this.NAME = require(path.join(__dirname, 'package.json')).name
this.md = {
pedantic: false,
gfm: true,
tables: true,
breaks: false,
smartLists: true,
smartypants: false,
xhtml: false,
svg: true
}
}
get pedantic () {
return this.md.pedantic
}
get gfm () {
return this.md.gfm
}
get tables () {
return this.md.tables
}
get breaks () {
return this.md.breaks
}
get smartLists () {
return this.md.get
}
get smartypants () {
return this.md.get
}
get xhtml () {
return this.md.xhtml
}
set pedantic (value) {
this.md.pedantic = value
}
set gfm (value) {
this.md.gfm = value
}
set tables (value) {
this.md.tables = value
}
set breaks (value) {
this.md.breaks = value
}
set smartLists (value) {
this.md.smartLists = value
}
set smartypants (value) {
this.md.smartypants = value
}
set xhtml (value) {
this.md.xhtml = value
}
process () {
let mermaidId = 0
let mathId = 0
let renderer = new marked.Renderer()
// Add mermaid SVG support
// const me = this
// renderer.code = async function (code, language) {
// if (code.match(/^sequenceDiagram/)||code.match(/^graph/)||code.match(/^gantt/) || language === 'sequenceDiagram' || language === 'graph' || language === 'gantt' && code !== '' || language === 'mermaid') {
// return await renderMermaidSvg(code)
// }
// }
const originalRenderer = renderer.code
renderer.code = function (code, language, escaped) {
// Mermaid Support
if (code.match(/^sequenceDiagram/) || code.match(/^classDiagram/) || code.match(/^gitGraph/) || code.match(/^graph/) || code.match(/^gantt/) || language === 'sequenceDiagram' || language === 'classDiagram' || language === 'graph' || language === 'gitGraph' || (language === 'gantt' && code !== '') || language === 'mermaid') {
return `<div id="mermaid${mermaidId}" class="mermaid ${language}">${code}</div>`
}
if (language) {
// MathJAX Support
if (language.startsWith('math') || language.startsWith('mathjax')) {
if (language.toLowerCase() === 'mathml') {
language = 'MathML'
} else if (language.toLowerCase() === 'tex' || language.toLowerCase() === 'latex') {
language = 'TeX'
} else if (language.toLowerCase() === 'inlinetex') {
language = 'inline-TeX'
} else if (language.toLowerCase() === 'asciimath') {
language = 'AsciiMath'
} else if (language.indexOf('-') > 0) {
language = language.splt('-').pop()
}
return `<div id="math${mathId}" class="math ${['tex', 'inline-tex', 'asciimath', 'mathml'].indexOf(language.trim().toLowerCase()) >= 0 ? language : 'tex'}">${code}</div>`
}
}
return originalRenderer.call(renderer, ...arguments)
}
marked.setOptions({
renderer,
pedantic: this.md.pedantic,
gfm: this.md.gfm,
tables: this.md.tables,
breaks: this.md.breaks,
smartLists: this.md.smartlists,
smartypants: this.md.smartypants,
xhtml: this.md.xhtml
})
traverse(this.SOURCE || {}).forEach(function () {
try {
if (this.isLeaf && this.key === 'description' && this.node) {
this.update(marked(this.node))
}
} catch (e) {
console.error(e)
}
})
this.emit('complete', this.SOURCE)
}
}
module.exports = Parser