forked from b3log/markdown-http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
136 lines (117 loc) · 2.79 KB
/
index.js
File metadata and controls
136 lines (117 loc) · 2.79 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
/**
* @fileoverview Markdown HTTP server.
*
* @author <a href="http://vanessa.b3log.org">Liyuan Li</a>
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.3.1.0, Apr 2, 2019
*/
const http = require('http')
const hljs = require('highlight.js')
const PORT = 8250
const RENDER = 'markdown-it' // support 'markdown-it', 'marked'
const TASKLICLASS = 'vditor-task'
const task = require('./markdown-it-task')
class MD {
init () {
switch (RENDER) {
case 'marked':
this.mdRender = this.initMarked()
break
case 'markdown-it':
this.mdRender = this.initMarkdownIt()
break
default:
break
}
}
render (md) {
let html
switch (RENDER) {
case 'marked':
html = this.mdRender(md)
break
case 'markdown-it':
html = this.mdRender.render(md)
break
default:
html = md
break
}
return html
}
highlight (str, lang) {
if (lang === 'mermaid' || lang === 'echarts' || lang === 'abc') {
return str
}
if (lang && hljs.getLanguage(lang)) {
return `<pre><code class="language-${lang} hljs">${hljs.highlight(lang, str,
true).value}</code></pre>`
}
return `<pre><code class="hljs">${hljs.highlightAuto(
str).value}</code></pre>`
}
initMarked () {
const marked = require('marked')
const renderer = new marked.Renderer()
renderer.listitem = (text) => {
if (text.indexOf('<input') > -1) {
return `<li class="${TASKLICLASS}">${text}</li>`
}
return `<li>${text}</li>`
}
marked.setOptions({
renderer: renderer,
gfm: true,
tables: true,
breaks: true,
smartLists: true,
highlight: this.highlight,
})
return marked
}
initMarkdownIt () {
const MarkdownIt = require('markdown-it')
return new MarkdownIt({
html: true,
linkify: true,
highlight: this.highlight,
breaks: true,
}).use(task, TASKLICLASS)
}
}
const md = new MD()
md.init()
process.on('uncaughtException', (e) => {
console.log(err)
})
process.on('exit', () => {
console.log('exit')
})
process.on('SIGTERM', () => {
console.log('on signal [SIGTERM]')
process.exit(0)
})
process.on('SIGINT', () => {
console.log('on signal [SIGINT]')
process.exit(0)
})
process.on('SIGUSR1', () => {
console.log('on signal [SIGUSR1]')
process.exit(0)
})
process.on('SIGUSR2', () => {
console.log('on signal [SIGUSR2]')
process.exit(0)
})
const server = http.createServer((request, response) => {
let mdContent = ''
request.on('data', (data) => {
mdContent += data
})
request.on('end', () => {
response.write(md.render(mdContent))
response.end()
})
})
server.listen(PORT, '127.0.0.1')
console.log(`${RENDER} engine is running at port: ${PORT}`)