Skip to content

Commit

Permalink
Don't add copy button where code block has language-console class
Browse files Browse the repository at this point in the history
Additional changes
-Added comments to explain whole code where necessary
  • Loading branch information
thisisommore committed Aug 4, 2021
1 parent f24c6fb commit 7f6eb05
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions docs/themes/porter/static/js/custom/copy_code.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
document.querySelectorAll("pre").forEach(ele => {
var codeElement = ele.querySelector("code")
document.querySelectorAll("pre").forEach(preElement => {
var codeElement = preElement.querySelector("code")

if (codeElement != null) {
/* There should be code element inside pre element and
code element should not have language-console class */
if (codeElement != null && !codeElement.classList.contains("language-console")) {
var copyButtonContainer = document.createElement('div')
copyButtonContainer.classList.add("copy-button-container")
var copyButton = document.createElement('button');
Expand All @@ -16,10 +18,13 @@ document.querySelectorAll("pre").forEach(ele => {
}).catch(e => {
copyButton.innerText = "error"
})

// Restore original "copy" text on button after 900 milliseconds
setTimeout(() => innerText = "copy", 900)
});

ele.parentElement.insertBefore(copyButtonContainer, ele)
// Insert before pre element so that it can appear in top of code block
preElement.parentElement.insertBefore(copyButtonContainer, preElement)
}
})

0 comments on commit 7f6eb05

Please sign in to comment.