Skip to content

Commit 9dde7bf

Browse files
committed
Add copy-to-clipboard icon to the <code> blocks within the documentation.
the particular use-case I have in mind is quickstarts, but this also has general utility across the documentation. Signed-off-by: Keith Wall <[email protected]>
1 parent f64582c commit 9dde7bf

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
<script src="/assets/scripts/clipboard.min.js"></script>
3+
4+
<script>
5+
let atLeastOne = false;
6+
document.querySelectorAll("code").forEach((codeElem) => {
7+
if (codeElem.textContent && codeElem.closest('.listingblock')) {
8+
let selectableCode = codeElem.textContent;
9+
if (codeElem.getAttribute("data-lang") === "terminal") {
10+
let codeLines = codeElem.textContent.split(/\n/);
11+
for (let idx = 0; idx < codeLines.length; idx++) {
12+
// line is a shell command, remove the console prompt
13+
codeLines[idx] = codeLines[idx].replace("$ ", "")
14+
}
15+
selectableCode = codeLines.join("\n");
16+
}
17+
let clipboardBtn = document.createElement("button");
18+
clipboardBtn.setAttribute('data-clipboard-text', selectableCode);
19+
clipboardBtn.className = "clipboardBtn";
20+
codeElem.parentNode.insertBefore(clipboardBtn, codeElem.nextSibling);
21+
atLeastOne = true;
22+
}
23+
});
24+
25+
if (atLeastOne) {
26+
new Clipboard('.clipboardBtn');
27+
}
28+
</script>
29+

_layouts/guide.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,11 @@ <h1 class="card-title fs-3">
3535
</div>
3636
</div>
3737
</div>
38+
39+
40+
41+
{% include asciidoctor-scripts/clipboard.html %}
3842
{% include asciidoctor-scripts/icons.html %}
3943
{% include asciidoctor-scripts/sectanchors.html %}
44+
45+

_sass/kroxylicious.scss

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,14 @@ b.conum * {
334334
.kroxylicious-guide .sect1+.sect1 {
335335
border-top: 1px solid $kroxy-light;
336336
}
337+
338+
.kroxylicious-guide .clipboardBtn {
339+
background:url(/assets/theme/images/clippy.svg) no-repeat;
340+
float: right;
341+
vertical-align: top;
342+
cursor:pointer;
343+
width: 13px;
344+
height: 15px;
345+
background-size:contain;
346+
border: none;
347+
}

assets/scripts/clipboard.min.js

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/theme/images/clippy.svg

Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)