Skip to content

Commit

Permalink
docs: code block copying
Browse files Browse the repository at this point in the history
Enable code blocks copying

Ref: https://aaronluna.dev/blog/add-copy-button-to-code-blocks-hugo-chroma/

Supersedes #5235

Signed-off-by: Noel Georgi <git@frezbo.dev>
  • Loading branch information
frezbo committed Mar 29, 2022
1 parent c41f2b2 commit 6665e0f
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 12 deletions.
10 changes: 5 additions & 5 deletions website/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -207,19 +207,19 @@ url = "https://twitter.com/SideroLabs"
desc = "Development takes place here!"
icon = "fab fa-github"
name = "GitHub"
url = "https://github.com/talos-systems/talos"
url = "https://github.com/siderolabs/talos"

[[menu.main]]
name = "Sidero Labs"
url = "https://www.siderolabs.com"
weight = 100
weight = 98

[[menu.main]]
name = "GitHub"
url = "https://github.com/talos-systems/talos"
weight = 99
url = "https://github.com/siderolabs/talos"
weight = 100

[[menu.main]]
name = "Sidero Metal"
url = "https://sidero.dev"
weight = 98
weight = 99
41 changes: 34 additions & 7 deletions website/content/v1.0/guides/nvidia-gpu.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ These are the steps to enabling NVIDIA support in Talos.
- Building NVIDIA container toolkit system extension which allows to register a custom runtime with containerd
- Upgrading Talos with the custom installer and enabling NVIDIA modules and the system extension

Both these components require that the user build and maintain their own Talos installer image and the NVIDIA container toolkit [Talos System Extension](../system-extensions).
Both these components require that the user build and maintain their own Talos installer image and the NVIDIA container toolkit [Talos System Extension]({{< relref "system-extensions">}}).

## Prerequisites

Expand Down Expand Up @@ -123,21 +123,36 @@ Once the node reboots, the NVIDIA modules should be loaded and the system extens
This can be confirmed by running:

```bash
❯ talosctl read /proc/modules
talosctl read /proc/modules
```

which should produce an output similar to below:

```text
nvidia_uvm 1146880 - - Live 0xffffffffc2733000 (PO)
nvidia_drm 69632 - - Live 0xffffffffc2721000 (PO)
nvidia_modeset 1142784 - - Live 0xffffffffc25ea000 (PO)
nvidia 39047168 - - Live 0xffffffffc00ac000 (PO)
```

```bash
❯ talosctl get extensions
talosctl get extensions
```

which should produce an output similar to below:

```text
NODE NAMESPACE TYPE ID VERSION NAME VERSION
172.31.41.27 runtime ExtensionStatus 000.ghcr.io-frezbo-nvidia-container-toolkit-510.60.02-v1.9.0 1 nvidia-container-toolkit 510.60.02-v1.9.0
```

```bash
❯ talosctl read /proc/driver/nvidia/version
talosctl read /proc/driver/nvidia/version
```

which should produce an output similar to below:

```text
NVRM version: NVIDIA UNIX x86_64 Kernel Module 510.60.02 Wed Mar 16 11:24:05 UTC 2022
GCC version: gcc version 11.2.0 (GCC)
```
Expand Down Expand Up @@ -167,7 +182,8 @@ helm install nvidia-device-plugin nvdp/nvidia-device-plugin --version=0.11.0 --s

Apply the following manifest to run CUDA pod via nvidia runtime:

```yaml
```bash
cat <<EOF | kubectl apply -f -
---
apiVersion: v1
kind: Pod
Expand All @@ -182,18 +198,29 @@ spec:
resources:
limits:
nvidia.com/gpu: 1
<<EOF
```
The status can be viewed by running:
```bash
❯ kubectl get pods
kubectl get pods
```
which should produce an output similar to below:
```text
NAME READY STATUS RESTARTS AGE
gpu-operator-test 0/1 Completed 0 13s
```
```bash
❯ kubectl logs gpu-operator-test
kubectl logs gpu-operator-test
```
which should produce an output similar to below:
```text
[Vector addition of 50000 elements]
Copy input data from the host memory to the CUDA device
CUDA kernel launch with 196 blocks of 256 threads
Expand Down
5 changes: 5 additions & 0 deletions website/layouts/partials/hooks/body-end.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<!--- https://aaronluna.dev/blog/add-copy-button-to-code-blocks-hugo-chroma/ -->
{{ if (findRE "<pre" .Content 1) }}
<script src="/js/clipboard.js"></script>
{{ end }}

{{ $currentVersion := index (split .Page.File.Dir "/" ) 0 }}
{{ $currentVersionDir := $currentVersion | printf "/%s"}}

Expand Down
5 changes: 5 additions & 0 deletions website/layouts/partials/hooks/head-end.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@docsearch/css@3"/>
<link rel="stylesheet" type="text/css" href="/asciinema/asciinema-player.css" />

<!--- https://aaronluna.dev/blog/add-copy-button-to-code-blocks-hugo-chroma/ -->
{{ if (findRE "<pre" .Content 1) }}
<link rel="stylesheet" href="/css/clipboard.css">
{{ end }}
67 changes: 67 additions & 0 deletions website/static/css/clipboard.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
.highlight-wrapper {
display: block;
}

.highlight {
position: relative;
z-index: 0;
padding: 0;
margin: 0;
border-radius: 4px;
}

code[data-lang]:before {
color: #d0d0d0;
background-color: #002b36;
position: absolute;
z-index: 0;
top: -22px;
left: 0px;
content: attr(data-lang);
border-top-left-radius: 4px;
border-top-right-radius: 4px;
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
padding: 5px 10px 7px;
}

.copy-code-button {
position: absolute;
z-index: 2;
right: 0;
top: 0;
font-size: 13px;
font-weight: 700;
line-height: 14px;
letter-spacing: 0.5px;
width: 65px;
color: #232326;
background-color: #7f7f7f;
border: 1.25px solid #232326;
border-top-left-radius: 0;
border-top-right-radius: 4px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 4px;
white-space: nowrap;
padding: 4px 4px 5px 4px;
margin: 0 0 0 1px;
cursor: pointer;
opacity: 0.6;
}

.copy-code-button:hover,
.copy-code-button:focus,
.copy-code-button:active,
.copy-code-button:active:hover {
color: #222225;
background-color: #b3b3b3;
opacity: 0.8;
}

.copyable-text-area {
position: absolute;
height: 0;
z-index: -1;
opacity: .01;
}

62 changes: 62 additions & 0 deletions website/static/js/clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
function createCopyButton(highlightDiv) {
const button = document.createElement("button");
button.className = "copy-code-button";
button.type = "button";
button.innerText = "Copy";
button.addEventListener("click", () => copyCodeToClipboard(button, highlightDiv));
addCopyButtonToDom(button, highlightDiv);
}

async function copyCodeToClipboard(button, highlightDiv) {
const codeToCopy = highlightDiv.querySelector(":last-child > code").innerText;
try {
result = await navigator.permissions.query({ name: "clipboard-write" });
if (result.state == "granted" || result.state == "prompt") {
await navigator.clipboard.writeText(codeToCopy);
} else {
copyCodeBlockExecCommand(codeToCopy, highlightDiv);
}
} catch (_) {
copyCodeBlockExecCommand(codeToCopy, highlightDiv);
}
finally {
codeWasCopied(button);
}
}

function copyCodeBlockExecCommand(codeToCopy, highlightDiv) {
const textArea = document.createElement("textArea");
textArea.contentEditable = 'true'
textArea.readOnly = 'false'
textArea.className = "copyable-text-area";
textArea.value = codeToCopy;
highlightDiv.insertBefore(textArea, highlightDiv.firstChild);
const range = document.createRange()
range.selectNodeContents(textArea)
const sel = window.getSelection()
sel.removeAllRanges()
sel.addRange(range)
textArea.setSelectionRange(0, 999999)
document.execCommand("copy");
highlightDiv.removeChild(textArea);
}

function codeWasCopied(button) {
button.blur();
button.innerText = "Copied!";
setTimeout(function() {
button.innerText = "Copy";
}, 2000);
}

function addCopyButtonToDom(button, highlightDiv) {
highlightDiv.insertBefore(button, highlightDiv.firstChild);
const wrapper = document.createElement("div");
wrapper.className = "highlight-wrapper";
highlightDiv.parentNode.insertBefore(wrapper, highlightDiv);
wrapper.appendChild(highlightDiv);
}

document.querySelectorAll(".highlight")
.forEach(highlightDiv => createCopyButton(highlightDiv));

0 comments on commit 6665e0f

Please sign in to comment.