@@ -98,40 +98,38 @@ async function updateArticle(req, res, next) {
98
98
linkify : true ,
99
99
typographer : true ,
100
100
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>'
101
109
if ( lang && hljs . getLanguage ( lang ) ) {
102
110
try {
103
111
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 ) {
110
114
html += '<b class="name">' + lang + '</b>'
111
115
}
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
+ }
116
120
}
117
121
118
122
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>`
127
125
}
128
126
} )
129
127
let articleContentHtml = md . use ( markdownItTocAndAnchor , {
130
128
tocCallback : function ( tocMarkdown , tocArray , tocHtml ) {
131
129
titleArray = tocArray
132
130
}
133
131
} ) . 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 )
135
133
let updateData = await mysql . query ( 'UPDATE vue_blog SET articleTitle = ?, articleSubTitle = ?, articleNature = ?, articleKey = ?, articleContentMarkdown = ?, articleContentHtml = ?, articleUpdateTime = ? WHERE articleId = ?' ,
136
134
[ 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 ] )
137
135
0 commit comments