Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a248cc3

Browse files
committed
修复第二次添加文章失败的问题
1 parent e4c97fc commit a248cc3

File tree

2 files changed

+66
-66
lines changed

2 files changed

+66
-66
lines changed

‎routes/addArticle.js

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,6 @@ const mysql = require('./../mysql/db')
55
const MarkdownIt = require('markdown-it')
66
const markdownItTocAndAnchor = require('markdown-it-toc-and-anchor').default
77
const hljs = require('highlight.js')
8-
const md = new MarkdownIt({
9-
html: true,
10-
linkify: true,
11-
typographer: true,
12-
highlight: function (str, lang) {
13-
if (lang && hljs.getLanguage(lang)) {
14-
try {
15-
const preCode = hljs.highlight(lang, str, true).value
16-
const lines = preCode.split(/\n/).slice(0, -1)
17-
let html = lines.map((item, index) => {
18-
return '<li><span class="line-num" data-line="' + (index + 1) + '"></span>' + item + '</li>'
19-
}).join('')
20-
html = '<ol>' + html + '</ol>'
21-
if (lines.length) {
22-
html += '<b class="name">' + lang + '</b>'
23-
}
24-
return '<pre class="hljs"><code>' +
25-
html +
26-
'</code></pre>'
27-
} catch (__) {}
28-
}
29-
30-
const preCode = md.utils.escapeHtml(str)
31-
const lines = preCode.split(/\n/).slice(0, -1)
32-
let html = lines.map((item, index) => {
33-
return '<li><span class="line-num" data-line="' + (index + 1) + '"></span>' + item + '</li>'
34-
}).join('')
35-
html = '<ol>' + html + '</ol>'
36-
return '<pre class="hljs"><code>' +
37-
html +
38-
'</code></pre>'
39-
}
40-
})
418

429
router.post('/', addArticle)
4310

@@ -46,6 +13,39 @@ async function addArticle(req, res, next) {
4613
let subTitle = req.body.content.slice(0, 200)
4714
let titleArray = []
4815
let titleObject = {}
16+
const md = new MarkdownIt({
17+
html: true,
18+
linkify: true,
19+
typographer: true,
20+
highlight: function (str, lang) {
21+
if (lang && hljs.getLanguage(lang)) {
22+
try {
23+
const preCode = hljs.highlight(lang, str, true).value
24+
const lines = preCode.split(/\n/).slice(0, -1)
25+
let html = lines.map((item, index) => {
26+
return '<li><span class="line-num" data-line="' + (index + 1) + '"></span>' + item + '</li>'
27+
}).join('')
28+
html = '<ol>' + html + '</ol>'
29+
if (lines.length) {
30+
html += '<b class="name">' + lang + '</b>'
31+
}
32+
return '<pre class="hljs"><code>' +
33+
html +
34+
'</code></pre>'
35+
} catch (__) {}
36+
}
37+
38+
const preCode = md.utils.escapeHtml(str)
39+
const lines = preCode.split(/\n/).slice(0, -1)
40+
let html = lines.map((item, index) => {
41+
return '<li><span class="line-num" data-line="' + (index + 1) + '"></span>' + item + '</li>'
42+
}).join('')
43+
html = '<ol>' + html + '</ol>'
44+
return '<pre class="hljs"><code>' +
45+
html +
46+
'</code></pre>'
47+
}
48+
})
4949
let articleContentHtml = md.use(markdownItTocAndAnchor, {
5050
tocCallback: function (tocMarkdown, tocArray, tocHtml) {
5151
titleArray = tocArray

‎routes/updateArticle.js

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,6 @@ const mysql = require('./../mysql/db')
55
const MarkdownIt = require('markdown-it')
66
const markdownItTocAndAnchor = require('markdown-it-toc-and-anchor').default
77
const hljs = require('highlight.js')
8-
const md = new MarkdownIt({
9-
html: true,
10-
linkify: true,
11-
typographer: true,
12-
highlight: function (str, lang) {
13-
if (lang && hljs.getLanguage(lang)) {
14-
try {
15-
const preCode = hljs.highlight(lang, str, true).value
16-
const lines = preCode.split(/\n/).slice(0, -1)
17-
let html = lines.map((item, index) => {
18-
return '<li><span class="line-num" data-line="' + (index + 1) + '"></span>' + item + '</li>'
19-
}).join('')
20-
html = '<ol>' + html + '</ol>'
21-
if (lines.length) {
22-
html += '<b class="name">' + lang + '</b>'
23-
}
24-
return '<pre class="hljs"><code>' +
25-
html +
26-
'</code></pre>'
27-
} catch (__) {}
28-
}
29-
30-
const preCode = md.utils.escapeHtml(str)
31-
const lines = preCode.split(/\n/).slice(0, -1)
32-
let html = lines.map((item, index) => {
33-
return '<li><span class="line-num" data-line="' + (index + 1) + '"></span>' + item + '</li>'
34-
}).join('')
35-
html = '<ol>' + html + '</ol>'
36-
return '<pre class="hljs"><code>' +
37-
html +
38-
'</code></pre>'
39-
}
40-
})
418

429
router.post('/', updateArticle)
4310

@@ -46,6 +13,39 @@ async function updateArticle(req, res, next) {
4613
let subTitle = req.body.content.slice(0, 200)
4714
let titleArray = []
4815
let titleObject = {}
16+
const md = new MarkdownIt({
17+
html: true,
18+
linkify: true,
19+
typographer: true,
20+
highlight: function (str, lang) {
21+
if (lang && hljs.getLanguage(lang)) {
22+
try {
23+
const preCode = hljs.highlight(lang, str, true).value
24+
const lines = preCode.split(/\n/).slice(0, -1)
25+
let html = lines.map((item, index) => {
26+
return '<li><span class="line-num" data-line="' + (index + 1) + '"></span>' + item + '</li>'
27+
}).join('')
28+
html = '<ol>' + html + '</ol>'
29+
if (lines.length) {
30+
html += '<b class="name">' + lang + '</b>'
31+
}
32+
return '<pre class="hljs"><code>' +
33+
html +
34+
'</code></pre>'
35+
} catch (__) {}
36+
}
37+
38+
const preCode = md.utils.escapeHtml(str)
39+
const lines = preCode.split(/\n/).slice(0, -1)
40+
let html = lines.map((item, index) => {
41+
return '<li><span class="line-num" data-line="' + (index + 1) + '"></span>' + item + '</li>'
42+
}).join('')
43+
html = '<ol>' + html + '</ol>'
44+
return '<pre class="hljs"><code>' +
45+
html +
46+
'</code></pre>'
47+
}
48+
})
4949
let articleContentHtml = md.use(markdownItTocAndAnchor, {
5050
tocCallback: function (tocMarkdown, tocArray, tocHtml) {
5151
titleArray = tocArray

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /