Skip to content

Commit

Permalink
Fix empty innerText
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed May 13, 2024
1 parent 783b6ff commit ddecbe3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion source/js/third-party/tags/mermaid.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ document.addEventListener('page:loaded', () => {
newElement.className = 'mermaid';
box.appendChild(newElement);
if (CONFIG.copycode.enable) {
NexT.utils.registerCopyButton(box, box, element.innerText);
NexT.utils.registerCopyButton(box, box, element.textContent);
}
const parent = element.parentNode;
parent.parentNode.replaceChild(box, parent);
Expand Down
9 changes: 6 additions & 3 deletions source/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ NexT.utils = {
});
},

registerCopyButton(target, element, code) {
registerCopyButton(target, element, code = '') {
// One-click copy code support.
target.insertAdjacentHTML('beforeend', '<div class="copy-btn"><i class="fa fa-copy fa-fw"></i></div>');
const button = target.querySelector('.copy-btn');
button.addEventListener('click', () => {
if (!code) {
const lines = element.querySelector('.code') || element.querySelector('code');
code = lines.innerText;
}
if (navigator.clipboard) {
// https://caniuse.com/mdn-api_clipboard_writetext
navigator.clipboard.writeText(code).then(() => {
Expand Down Expand Up @@ -131,8 +135,7 @@ NexT.utils = {
});
}
if (!inited && CONFIG.copycode.enable) {
const lines = element.querySelector('.code') || element.querySelector('code');
this.registerCopyButton(target, element, lines.innerText);
this.registerCopyButton(target, element);
}
});
},
Expand Down

0 comments on commit ddecbe3

Please sign in to comment.