Skip to content

Commit

Permalink
Did I break this?
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecole committed Jul 9, 2024
1 parent 717d750 commit 37eef6c
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,48 @@ <h1 class="display-4 fst-italic">{{ site.title }}</h1>
hljs.configure({ cssSelector: 'code', languages:[] });
hljs.highlightAll();

// var snippets = document.getElementsByTagName('pre');
// var numberOfSnippets = snippets.length;
// for (var i = 0; i < numberOfSnippets; i++) {
// var snippet = snippets[i];

// snippet.classList.add('hljs'); // append copy button to pre tag

// var language = snippet.getElementsByTagName('code')[0].classList.values().find(f => f.startsWith('language-'));
// if (language) {
// snippet.innerHTML = '<span class="badge text-bg-primary hljs-language">' + language.replace('language-', '') + '</span>' + snippet.innerHTML; //append language
// }

// snippet.innerHTML = '<button type="button" class="btn btn-primary btn-sm mt-1 mr-1 hljs-copy">Copy</button>' + snippet.innerHTML; // append copy button

// snippet.getElementsByClassName('hljs-copy')[0].addEventListener("click", function () {
// this.innerText = 'Copying...';
// var code = this.parentNode.getElementsByTagName('code')[0].innerText;
// navigator.clipboard.writeText(code);
// this.innerText = 'Copied!';
// button = this;
// setTimeout(function () {
// button.innerText = 'Copy';
// }, 1000)
// });
// }

var snippets = document.getElementsByTagName('pre');
var numberOfSnippets = snippets.length;
for (var i = 0; i < numberOfSnippets; i++) {
var snippet = snippets[i];

snippet.classList.add('hljs'); // append copy button to pre tag
code = snippets[i].getElementsByTagName('code')[0].innerText;

var language = snippet.getElementsByTagName('code')[0].classList.values().find(f => f.startsWith('language-'));
if (language) {
snippet.innerHTML = '<span class="badge text-bg-primary hljs-language">' + language.replace('language-', '') + '</span>' + snippet.innerHTML; //append language
}
snippets[i].classList.add('hljs'); // append copy button to pre tag

snippet.innerHTML = '<button type="button" class="btn btn-primary btn-sm mt-1 mr-1 hljs-copy">Copy</button>' + snippet.innerHTML; // append copy button
snippets[i].innerHTML = '<button class="hljs-copy">Copy</button>' + snippets[i].innerHTML; // append copy button

snippet.getElementsByClassName('hljs-copy')[0].addEventListener("click", function () {
this.innerText = 'Copying...';
var code = this.parentNode.getElementsByTagName('code')[0].innerText;
navigator.clipboard.writeText(code);
snippets[i].getElementsByClassName('hljs-copy')[0].addEventListener("click", function () {
this.innerText = 'Copying..';
if (!navigator.userAgent.toLowerCase().includes('safari')) {
navigator.clipboard.writeText(code);
} else {
prompt("Clipboard (Select: ⌘+a > Copy:⌘+c)", code);
}
this.innerText = 'Copied!';
button = this;
setTimeout(function () {
Expand Down

0 comments on commit 37eef6c

Please sign in to comment.