mirror of https://github.com/theNewDynamic/gohugo-theme-ananke.git

Patrick Kollitsch
07.44.2026 c31ff8582907a3d06dcd524980895c7860befb99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{{- /*
  Copy-to-clipboard behaviour for code blocks rendered by
  layouts/_markup/render-codeblock.html.
 
  Progressive enhancement: the buttons are hidden in the markup and only
  revealed here, so a site with JavaScript disabled never shows an inert
  button. A single delegated listener handles every code block on the page.
 
  Override this partial in your own project to add or replace site scripts;
  disable the copy buttons entirely with `[ananke] copy_code = false`.
*/ -}}
{{- if ne false site.Params.ananke.copy_code -}}
<script>
  (function () {
    "use strict";
 
    // Reveal the copy buttons once the DOM is ready. This script is included in
    // the <head>, so the code blocks may not exist yet when it first runs.
    function revealButtons() {
      var buttons = document.querySelectorAll(".code-block .code-copy[hidden]");
      for (var i = 0; i < buttons.length; i++) {
        buttons[i].hidden = false;
      }
    }
    if (document.readyState === "loading") {
      document.addEventListener("DOMContentLoaded", revealButtons);
    } else {
      revealButtons();
    }
 
    function flash(button) {
      var label = button.querySelector(".code-copy-label");
      var previous = label ? label.textContent : null;
      button.classList.add("is-copied");
      if (label) label.textContent = "Copied";
      window.setTimeout(function () {
        button.classList.remove("is-copied");
        if (label && previous !== null) label.textContent = previous;
      }, 2000);
    }
 
    function legacyCopy(text) {
      var area = document.createElement("textarea");
      area.value = text;
      area.setAttribute("readonly", "");
      area.style.position = "absolute";
      area.style.left = "-9999px";
      document.body.appendChild(area);
      area.select();
      var ok = false;
      try {
        ok = document.execCommand("copy");
      } catch (e) {
        ok = false;
      }
      document.body.removeChild(area);
      return ok;
    }
 
    function copyFrom(button) {
      var container = button.closest(".code-block");
      if (!container) return;
      var pre = container.querySelector("pre");
      if (!pre) return;
      var source = pre.querySelector("code") || pre;
      var text = source.innerText;
 
      if (navigator.clipboard && navigator.clipboard.writeText) {
        navigator.clipboard.writeText(text).then(
          function () { flash(button); },
          function () { if (legacyCopy(text)) flash(button); }
        );
      } else if (legacyCopy(text)) {
        flash(button);
      }
    }
 
    document.addEventListener("click", function (event) {
      var button = event.target.closest(".code-copy");
      if (button) copyFrom(button);
    });
  })();
</script>
{{- end -}}