Skip to content

Commit de9ed53

Browse files
ConcedoConcedo
Concedo
authored and
Concedo
committed
code block copy
1 parent 1e28ac1 commit de9ed53

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

index.html

+18-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@
8383
filter: invert(1);
8484
}
8585

86+
.unselectable {
87+
-webkit-touch-callout: none !important;
88+
-webkit-user-select: none !important;
89+
-khtml-user-select: none !important;
90+
-moz-user-select: none !important;
91+
-ms-user-select: none !important;
92+
user-select: none !important;
93+
}
94+
8695
.maincontainer {
8796
padding-right: 4px;
8897
padding-left: 4px;
@@ -3621,9 +3630,17 @@
36213630
return words.length;
36223631
}
36233632

3633+
function copyMarkdownCode(btn)
3634+
{
3635+
const codeContainer = btn.parentElement.querySelector('pre code');
3636+
//selectElementContents(codeContainer);
3637+
navigator.clipboard.writeText(codeContainer.innerText);
3638+
}
3639+
36243640
function simpleMarkdown(text) {
36253641
const escapeHTML = (str) => str.replace(/</g, "&lt;").replace(/>/g, "&gt;");
36263642
const highlightCode = (code) => {
3643+
let cpybtn = `<button class="unselectable" onclick="return copyMarkdownCode(this)" style="float:right;">Copy</button>`;
36273644
code = escapeHTML(code);
36283645
code = code.replace(/</g, "&lt;").replace(/>/g, "&gt;");
36293646
code = code.replace(/\t/g, " ");
@@ -3632,7 +3649,7 @@
36323649
code = code.replace(/\s\/\/(.*)/gm, " <rem>//$1</rem>");
36333650
code = code.replace(/(\s?)(function|procedure|return|exit|if|then|else|end|loop|while|or|and|case|when)(\s)/gim, "$1<b>$2</b>$3");
36343651
code = code.replace(/(\s?)(var|let|const|=>|for|next|do|while|loop|continue|break|switch|try|catch|finally)(\s)/gim, "$1<b>$2</b>$3");
3635-
return `<pre><code>${code}</code></pre>`;
3652+
return `<pre>${cpybtn}<code>${code}</code></pre>`;
36363653
};
36373654
const convertMarkdownTableToHtml = (t) => {
36383655
let hsep = /^[\s]*\|(?:[\s]*[-:]+[-:|\s]*)+\|[\s]*$/gm;let l=/^[\s]*\|(.*)\|[\s]*$/gm,r=t.split(/\r?\n|\r/),e="<table class='tablelines'>";for(let o of r){let hs=o.match(hsep);if(hs){continue;}let d=o.match(l);if(d){let i=d[0].split("|").map(t=>t.trim());e+=`<tr class='tablelines'><td class='tablelines'>${i.join("</td><td class='tablelines'>")}</td></tr>`}}return e+"</table>"

0 commit comments

Comments
 (0)