chore(copyright): 2024
[curben-blog.git] / themes / chameleon / scripts / copy-button.js
blobe2e48bc642a51aefa9f4f65746ded0c416d4dce7
1 'use strict'
2 /* global hexo */
4 /*
5 * Add "Copy" button to code snippet
6 */
8 hexo.extend.filter.register('after_render:html', (data) => {
9   const copyBtn = '<button class="copy-button">Copy</button>'
11   // Regex is based on https://github.com/hexojs/hexo/pull/3697
12   return data.replace(/<pre>(?!<\/pre>).+?<\/pre>/gs, (str) => {
13     if (!str.includes(copyBtn)) return str.replace('</pre>', copyBtn + '</pre>')
14     return str
15   })