2019-08-09 13:02:51 +00:00
|
|
|
'use strict'
|
|
|
|
/* global hexo */
|
|
|
|
|
2019-05-25 12:50:54 +00:00
|
|
|
/*
|
|
|
|
* Add "Copy" button to code snippet
|
|
|
|
*/
|
2019-05-25 10:16:35 +00:00
|
|
|
|
2019-09-04 02:36:08 +00:00
|
|
|
hexo.extend.filter.register('after_render:html', (data) => {
|
|
|
|
const copyBtn = '<button class="copy-button">Copy</button>'
|
2019-05-25 10:16:35 +00:00
|
|
|
|
2019-09-04 02:36:08 +00:00
|
|
|
// Regex is based on https://github.com/hexojs/hexo/pull/3697
|
2019-10-19 03:12:15 +00:00
|
|
|
return data.replace(/<pre>(?!<\/pre>).+?<\/pre>/gs, (str) => {
|
2019-10-19 03:07:59 +00:00
|
|
|
if (!str.includes(copyBtn)) return str.replace('</pre>', copyBtn + '</pre>')
|
2019-09-06 07:02:44 +00:00
|
|
|
return str
|
|
|
|
})
|
2019-05-25 10:16:35 +00:00
|
|
|
})
|