shoelace/docs/_utilities/copy-code-buttons.cjs

24 wiersze
608 B
JavaScript

2023-08-22 21:01:00 +00:00
let codeBlockId = 0;
2023-06-06 12:22:18 +00:00
/**
* Adds copy code buttons to code fields. The provided doc should be a document object provided by JSDOM. The same
* document will be returned with the appropriate DOM manipulations.
*/
module.exports = function (doc) {
doc.querySelectorAll('pre > code').forEach(code => {
const pre = code.closest('pre');
2023-08-22 21:01:00 +00:00
const button = doc.createElement('sl-copy-button');
2023-06-06 12:22:18 +00:00
2023-08-22 21:01:00 +00:00
if (!code.id) {
code.id = `code-block-${++codeBlockId}`;
}
button.classList.add('copy-code-button');
button.setAttribute('from', code.id);
2023-06-06 12:22:18 +00:00
pre.append(button);
});
return doc;
};