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) => {
|
2019-05-25 10:16:35 +00:00
|
|
|
// Avoid duplicate button
|
2019-09-04 02:36:08 +00:00
|
|
|
if (data.includes('</button></td>')) return;
|
2019-05-25 10:16:35 +00:00
|
|
|
|
2019-09-04 02:36:08 +00:00
|
|
|
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
|
|
|
|
return data.replace(/<td class="code">(?!<\/td>).+?<\/td>/, (str) => str.replace('</td>', copyBtn + '</td>'))
|
2019-05-25 10:16:35 +00:00
|
|
|
})
|