forked from surmon-china/surmon.me
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marked.js
executable file
·98 lines (85 loc) · 2.84 KB
/
marked.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
/**
* @file 文档解析服务 / ES module
* @module plugins/marked
* @author Surmon <https://github.com/surmon-china>
*/
import marked from 'marked'
import Hljs from '~/plugins/highlight'
import buildTagLink from '~/utils/article-tag-releted-parse'
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
tables: true,
breaks: false,
pedantic: false,
sanitize: false,
smartLists: true,
smartypants: false,
highlight(code) {
return Hljs.highlightAuto(code).value
}
})
const renderer = new marked.Renderer()
// 段落解析
const paragraphParse = text => `<p>${text}</p>`
// 对连接进行权重防流和新窗处理
const linkParse = (href, title, text) => {
const isSelf = href.includes('surmon.me')
const textIsImage = text.includes('<img')
return `<a href="${href}"
target="_blank"
class="${textIsImage ? 'image-link' : 'link'}"
title="${title || (textIsImage ? href : text)}"
${isSelf ? '' : 'rel="external nofollow noopenter"'}>${text}</a>`.replace(/\s+/g, ' ').replace('\n', '')
}
// 对图片进行弹窗处理
const imageParse = (src, title, alt) => {
src = src.replace(/^http:\/\//ig, "/proxy/")
return `<img src="${src}"
title="${title || alt || 'surmon.me'}"
alt="${alt || title || src}"
onclick="if (window.utils) window.utils.openImgPopup('${src}')"/>`.replace(/\s+/g, ' ').replace('\n', '')
}
// 代码解析器(行号处理)
const codeParse = function(code, lang, escaped) {
if (this.options.highlight) {
const out = this.options.highlight(code, lang)
if (out != null && out !== code) {
escaped = true
code = out
}
}
const lineNums = code.split('\n').map((l, i) => {
return `<li class="code-line-number">${i + 1}</li>`.replace(/\s+/g, ' ')
}).join('')
if (!lang) {
return `<pre>
<ul class="code-lines">${lineNums}</ul>
<code>${(escaped ? code : escape(code, true))}\n</code>
</pre>`.replace('\n', '')
} else {
return `<pre data-lang="${lang}">
<ul class="code-lines">${lineNums}</ul>
<code class="${this.options.langPrefix}${escape(lang, true)}">${(escaped ? code : escape(code, true))}\n</code>
</pre>\n`.replace('\n', '')
}
}
renderer.link = linkParse
renderer.code = codeParse
renderer.image = imageParse
renderer.paragraph = paragraphParse
export default (content, tags, parseHtml = false) => {
// 所有非链接的关键字进行内链处理
if (tags && tags.length) {
renderer.text = text => buildTagLink(text, tags)
} else {
renderer.text = text => text
}
// 如果是解析评论,则不解析html内容
marked.setOptions({ sanitize: !parseHtml })
if (typeof content != 'string') {
return ''
}
// 返回解析内容
return marked(content, { renderer })
}