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 aae2c34

Browse files
committed
代码段优化、添加复制功能
1 parent 5abbe5c commit aae2c34

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

‎routes/article.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -98,40 +98,38 @@ async function updateArticle(req, res, next) {
9898
linkify: true,
9999
typographer: true,
100100
highlight: function (str, lang) {
101+
const codeIndex = parseInt(Date.now())
102+
let html = `<button class="copy-btn" type="button" data-clipboard-action="copy" data-clipboard-target="#copy${codeIndex}">复制</button>`
103+
const linesLength = str.split(/\n/).length - 1
104+
let linesNum = '<span aria-hidden="true" class="line-numbers-rows">'
105+
for (let index = 0; index < linesLength; index++) {
106+
linesNum = linesNum + '<span></span>'
107+
}
108+
linesNum += '</span>'
101109
if (lang && hljs.getLanguage(lang)) {
102110
try {
103111
const preCode = hljs.highlight(lang, str, true).value
104-
const lines = preCode.split(/\n/).slice(0, -1)
105-
let html = lines.map((item, index) => {
106-
return '<li><span class="line-num" data-line="' + (index + 1) + '"></span>' + item + '</li>'
107-
}).join('')
108-
html = '<ol>' + html + '</ol>'
109-
if (lines.length) {
112+
html = html + preCode
113+
if (linesLength) {
110114
html += '<b class="name">' + lang + '</b>'
111115
}
112-
return '<pre ><code>'+
113-
html+
114-
'</code></pre>'
115-
}catch(__){}
116+
return `<pre class="hljs"><code>${html}</code>${linesNum}</pre><textarea style="position: absolute;top: -9999px;left: -9999px;z-index: -9999;" id="copy${codeIndex}">${str}</textarea>`
117+
}catch(error){
118+
console.log(error)
119+
}
116120
}
117121

118122
const preCode = md.utils.escapeHtml(str)
119-
const lines = preCode.split(/\n/).slice(0, -1)
120-
let html = lines.map((item, index) => {
121-
return '<li><span class="line-num" data-line="' + (index + 1) + '"></span>' + item + '</li>'
122-
}).join('')
123-
html = '<ol>' + html + '</ol>'
124-
return '<pre class="hljs"><code>' +
125-
html +
126-
'</code></pre>'
123+
html = html + preCode
124+
return `<pre class="hljs"><code>${html}</code>${linesNum}</pre><textarea style="position: absolute;top: -9999px;left: -9999px;z-index: -9999;" id="copy${codeIndex}">${str}</textarea>`
127125
}
128126
})
129127
let articleContentHtml = md.use(markdownItTocAndAnchor, {
130128
tocCallback: function (tocMarkdown, tocArray, tocHtml) {
131129
titleArray = tocArray
132130
}
133131
}).use(require('markdown-it-sub')).use(require('markdown-it-sup')).use(require('markdown-it-deflist')).use(require('markdown-it-abbr')).use(require('markdown-it-footnote')).use(require('markdown-it-ins')).use(require('markdown-it-mark')).render(`@[toc]\r${req.body.content}`)
134-
132+
console.log(articleContentHtml)
135133
let updateData = await mysql.query('UPDATE vue_blog SET articleTitle = ?, articleSubTitle = ?, articleNature = ?, articleKey = ?, articleContentMarkdown = ?, articleContentHtml = ?, articleUpdateTime = ? WHERE articleId = ?',
136134
[req.body.title, subTitle, req.body.nature, req.body.keyWords.join(), req.body.content, articleContentHtml, moment().format('YYYY-MM-DD HH:mm:ss'), req.body.articleId])
137135

‎routes/images.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async function addImage(req, res, next) {
2020
msg: '上传凭证获取成功',
2121
upToken: uploadToken
2222
});
23-
next();
23+
// next();
2424
}
2525

2626
module.exports = router;

0 commit comments

Comments
(0)

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