fix(copy-button): add button to every codeblock

This commit is contained in:
curben 2019-09-06 08:02:44 +01:00
parent 586adc63cd
commit a429e0fd81
No known key found for this signature in database
GPG Key ID: 5D9DB57A25D34EE3
1 changed files with 4 additions and 4 deletions

View File

@ -6,11 +6,11 @@
*/ */
hexo.extend.filter.register('after_render:html', (data) => { hexo.extend.filter.register('after_render:html', (data) => {
// Avoid duplicate button
if (data.includes('</button></td>')) return;
const copyBtn = '<button class="copy-button">Copy</button>' const copyBtn = '<button class="copy-button">Copy</button>'
// Regex is based on https://github.com/hexojs/hexo/pull/3697 // Regex is based on https://github.com/hexojs/hexo/pull/3697
return data.replace(/<td class="code">(?!<\/td>).+?<\/td>/, (str) => str.replace('</td>', copyBtn + '</td>')) return data.replace(/<td class="code">(?!<\/td>).+?<\/td>/ig, (str) => {
if (!str.includes(copyBtn)) return str.replace('</td>', copyBtn + '</td>')
return str
})
}) })