2024-04-17 11:20:27 -04:00
|
|
|
import { parse } from 'node-html-parser';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Eleventy plugin to add copy buttons to code blocks.
|
|
|
|
|
*/
|
|
|
|
|
export function copyCodePlugin(options = {}) {
|
|
|
|
|
options = {
|
|
|
|
|
container: 'body',
|
2024-12-14 17:08:29 -05:00
|
|
|
...options,
|
2024-04-17 11:20:27 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return function (eleventyConfig) {
|
|
|
|
|
eleventyConfig.addTransform('copy-code', content => {
|
|
|
|
|
const doc = parse(content, { blockTextElements: { code: true } });
|
|
|
|
|
const container = doc.querySelector(options.container);
|
|
|
|
|
|
|
|
|
|
if (!container) {
|
|
|
|
|
return content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Look for code blocks
|
|
|
|
|
container.querySelectorAll('pre > code').forEach(code => {
|
|
|
|
|
const pre = code.closest('pre');
|
|
|
|
|
|
|
|
|
|
// Add a copy button (we set the copy data at runtime to reduce page bloat)
|
|
|
|
|
pre.innerHTML = `<wa-copy-button class="copy-button" hoist></wa-copy-button>` + pre.innerHTML;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return doc.toString();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|