Skip to content

Commit c97875a

Browse files
committed
Creating a javascript script for codeblocks
This script modifies the asciidoc codeblocks in the following ways: - Applies Patternfly formatting to the codeblocks - Adds a copy button to the the top right corner of the codeblock
1 parent 835dafb commit c97875a

File tree

3 files changed

+101
-4
lines changed

3 files changed

+101
-4
lines changed

layouts/_default/baseof.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
<!-- get all the things from the content files -->
99
{{- end }}
1010
</div>
11+
<script src="{{ "js/codeblock.js" | relURL }}"></script>
1112
</body>
1213
</html>

static/css/custom.css

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,22 @@ td > code {
7171

7272
/*Adds type of content to listingblock*/
7373

74-
.listingblock > .content {
74+
.listingblock {
75+
margin-bottom: 1rem;
76+
margin-top: 1rem;
77+
}
78+
79+
.listingblock > .pf-c-code-block {
7580
position: relative;
7681
}
7782

78-
.listingblock code[data-lang]::before {
83+
.listingblock div[data-lang]::before {
7984
content: attr(data-lang);
8085
position: absolute;
8186
font-size: 0.75em;
8287
top: 0.425rem;
83-
right: 0.5rem;
84-
line-height: 1;
88+
left: 0.5rem;
89+
line-height: 2;
8590
text-transform: uppercase;
8691
color: inherit;
8792
opacity: 0.5;

static/js/codeblock.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
function createElementAndClass(element, classes) {
2+
result = document.createElement(element);
3+
result.className = classes;
4+
return result;
5+
}
6+
7+
function createCopyButton(highlightDiv) {
8+
const codeBlockActionsCopyButton = createElementAndClass("button", "pf-c-button pf-m-plain");
9+
codeBlockActionsCopyButton.type = "button";
10+
codeBlockActionsCopyButton.addEventListener("click", () =>
11+
copyCodeToClipboard(codeBlockActionsCopyButton, highlightDiv)
12+
);
13+
const codeBlockActionsCopyButtonIcon = createElementAndClass("i", "fas fa-copy");
14+
codeBlockActionsCopyButton.appendChild(codeBlockActionsCopyButtonIcon);
15+
addCopyButtonToDom(codeBlockActionsCopyButton, highlightDiv);
16+
}
17+
18+
async function copyCodeToClipboard(button, highlightDiv) {
19+
const codeToCopy = highlightDiv.querySelector(":last-child > .highlight > code")
20+
.innerText;
21+
try {
22+
result = await navigator.permissions.query({ name: "clipboard-write" });
23+
if (result.state == "granted" || result.state == "prompt") {
24+
await navigator.clipboard.writeText(codeToCopy);
25+
} else {
26+
copyCodeBlockExecCommand(codeToCopy, highlightDiv);
27+
}
28+
} catch (_) {
29+
copyCodeBlockExecCommand(codeToCopy, highlightDiv);
30+
} finally {
31+
32+
codeWasCopied(button);
33+
}
34+
}
35+
36+
function copyCodeBlockExecCommand(codeToCopy, highlightDiv) {
37+
const textArea = document.createElement("textArea");
38+
textArea.contentEditable = "true";
39+
textArea.readOnly = "false";
40+
textArea.className = "copyable-text-area";
41+
textArea.value = codeToCopy;
42+
highlightDiv.insertBefore(textArea, highlightDiv.firstChild);
43+
const range = document.createRange();
44+
range.selectNodeContents(textArea);
45+
const sel = window.getSelection();
46+
sel.removeAllRanges();
47+
sel.addRange(range);
48+
textArea.setSelectionRange(0, 999999);
49+
document.execCommand("copy");
50+
highlightDiv.removeChild(textArea);
51+
}
52+
53+
function codeWasCopied(button) {
54+
button.blur();
55+
const icon = document.createElement("i");
56+
icon.className = "fas fa-check";
57+
button.replaceChild(icon, button.firstChild);
58+
setTimeout(function () {
59+
const icon = document.createElement("i");
60+
icon.className = "fas fa-copy";
61+
button.replaceChild(icon, button.firstChild);
62+
}, 2000);
63+
}
64+
65+
function addCopyButtonToDom(button, highlightDiv) {
66+
dataLang = highlightDiv.querySelector("pre > code[data-lang]").attributes["data-lang"].value;
67+
highlightDiv.classList.add("pf-c-code-block__content")
68+
pre = highlightDiv.getElementsByTagName("pre");
69+
pre[0].classList.add("pf-c-code-block__pre")
70+
code = pre[0].getElementsByTagName("code");
71+
code[0].classList.add("pf-c-code-block__code")
72+
console.log(highlightDiv);
73+
highlightDiv.insertBefore(button, highlightDiv.firstChild);
74+
const codeBlock = createElementAndClass("div", "pf-c-code-block");
75+
codeBlock.setAttribute("data-lang", dataLang)
76+
const codeBlockHeader = createElementAndClass("div", "pf-c-code-block__header");
77+
const codeBlockActions = createElementAndClass("div", "pf-c-code-block__actions");
78+
const codeBlockActionsCopy = createElementAndClass("div", "pf-c-code-block__actions-item");
79+
codeBlockActionsCopy.appendChild(button);
80+
codeBlockActions.appendChild(codeBlockActionsCopy);
81+
codeBlockHeader.appendChild(codeBlockActions);
82+
codeBlock.appendChild(codeBlockHeader);
83+
highlightDiv.parentNode.insertBefore(codeBlock, highlightDiv);
84+
codeBlock.appendChild(highlightDiv);
85+
// const wrapper = document.createElement("div");
86+
// wrapper.className = "highlight-wrapper";
87+
// highlightDiv.parentNode.insertBefore(wrapper, highlightDiv);
88+
// wrapper.appendChild(highlightDiv);
89+
}
90+
91+
document.querySelectorAll(".listingblock .content").forEach((highlightDiv) => createCopyButton(highlightDiv));

0 commit comments

Comments
 (0)