blog/themes/chameleon/scripts/copy-button.js

17 lines
432 B
JavaScript
Raw Permalink Normal View History

2024-09-28 06:31:52 +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(/<pre>(?!<\/pre>).+?<\/pre>/gs, (str) => {
if (!str.includes(copyBtn)) return str.replace('</pre>', copyBtn + '</pre>')
return str
})
})