From de9ed5311703c80a9ffce13028544e1ce2bb2af9 Mon Sep 17 00:00:00 2001 From: Concedo Date: Tue, 9 Jul 2024 20:15:17 +0800 Subject: [PATCH] code block copy --- index.html | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index 52c2a75..22b09a8 100644 --- a/index.html +++ b/index.html @@ -83,6 +83,15 @@ filter: invert(1); } + .unselectable { + -webkit-touch-callout: none !important; + -webkit-user-select: none !important; + -khtml-user-select: none !important; + -moz-user-select: none !important; + -ms-user-select: none !important; + user-select: none !important; + } + .maincontainer { padding-right: 4px; padding-left: 4px; @@ -3621,9 +3630,17 @@ return words.length; } + function copyMarkdownCode(btn) + { + const codeContainer = btn.parentElement.querySelector('pre code'); + //selectElementContents(codeContainer); + navigator.clipboard.writeText(codeContainer.innerText); + } + function simpleMarkdown(text) { const escapeHTML = (str) => str.replace(//g, ">"); const highlightCode = (code) => { + let cpybtn = ``; code = escapeHTML(code); code = code.replace(//g, ">"); code = code.replace(/\t/g, " "); @@ -3632,7 +3649,7 @@ code = code.replace(/\s\/\/(.*)/gm, " //$1"); code = code.replace(/(\s?)(function|procedure|return|exit|if|then|else|end|loop|while|or|and|case|when)(\s)/gim, "$1$2$3"); code = code.replace(/(\s?)(var|let|const|=>|for|next|do|while|loop|continue|break|switch|try|catch|finally)(\s)/gim, "$1$2$3"); - return `
${code}
`; + return `
${cpybtn}${code}
`; }; const convertMarkdownTableToHtml = (t) => { let hsep = /^[\s]*\|(?:[\s]*[-:]+[-:|\s]*)+\|[\s]*$/gm;let l=/^[\s]*\|(.*)\|[\s]*$/gm,r=t.split(/\r?\n|\r/),e="";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+=``}}return e+"
${i.join("")}
"