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-09-06 07:02:44 +00:00
|
|
|
return data.replace(/<td class="code">(?!<\/td>).+?<\/td>/ig, (str) => {
|
|
|
|
if (!str.includes(copyBtn)) return str.replace('</td>', copyBtn + '</td>')
|
|
|
|
return str
|
|
|
|
})
|
2019-05-25 10:16:35 +00:00
|
|
|
})
|