blog/scripts/copy-button.js

17 lines
440 B
JavaScript
Raw Normal View History

2019-08-09 13:02:51 +00:00
'use strict'
/* global hexo */
/*
* Add "Copy" button to code snippet
*/
hexo.extend.filter.register('after_render:html', (data) => {
const copyBtn = '<button class="copy-button">Copy</button>'
// Regex is based on https://github.com/hexojs/hexo/pull/3697
return data.replace(/<td class="code">(?!<\/td>).+?<\/td>/ig, (str) => {
if (!str.includes(copyBtn)) return str.replace('</td>', copyBtn + '</td>')
return str
})
})