-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd.js
53 lines (48 loc) · 1.46 KB
/
md.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
'use strict'
const hljs = require('highlight.js')
const marked = require('marked')
exports.md = md
function md(content) {
return marked.parse(content, markedOptions)
.replace(/<pre><code class="(.*)">|<pre><code>/g, '<pre class="overflow-x-scroll"><code class="hljs $1">')
.replace(/<!--\s*:((?:[^:]|:(?!\s*-->))*):\s*-->/g, '$1')
}
class MarkedRenderer extends marked.Renderer {
// Adds ID anchors
heading(text, level, raw) {
const id = this.options.headerPrefix + raw.toLowerCase().replace(/[^\w]+/g, '-')
return (
`<h${level}>` +
`<a class="heading-anchor decorate-link" href="#${id}" id="${id}">#</a> ${text}` +
`</h${level}>\n`
)
}
// Adds target="_blank" to external links. Mostly copied from marked's source.
link(href, title, text) {
if (this.options.sanitize) {
let prot = ''
try {
prot = decodeURIComponent(unescape(href))
.replace(/[^\w:]/g, '')
.toLowerCase()
}
catch (__) {
return ''
}
if (/^(javascript|vbscript):/.test(prot)) return ''
}
let out = '<a href="' + href + '"'
if (title) out += ' title="' + title + '"'
if (/^[a-z]+:\/\//.test(href)) out += ' target="_blank"'
out += '>' + text + '</a>'
return out
}
}
const markedOptions = {
renderer: new MarkedRenderer(),
smartypants: true,
highlight(code, lang) {
const {value} = lang ? hljs.highlight(lang, code) : hljs.highlightAuto(code)
return value
},
}