aboutsummaryrefslogtreecommitdiff
path: root/head-include.html
blob: aaa8522e6ff797bb545b3e6fc5cd85e9c82ce413 (plain)
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
<script>
(function () {
  onReady(fixCgitMarkdownImages);
  onReady(linkifyCgitSubtitles);

  /**
   * Runs a callback when DOM is ready.
   * @param {Function} fn
   */
  function onReady(fn) {
    document.readyState === "loading"
      ? document.addEventListener("DOMContentLoaded", fn, { once: true })
      : fn();
  }

  /**
   * Rewrites relative image URLs in cgit README ("about") pages
   * to use /<repo>/plain/<path>.
   */
  function fixCgitMarkdownImages() {
    const parts = location.pathname.split("/").filter(Boolean);
    if (parts[1] !== "about") return;

    const base =
      location.origin + "/" + parts[0] + "/plain/";

    const container = document.querySelector(".markdown-body");
    if (!container) return;

    container.querySelectorAll("img[src]").forEach(img => {
      const src = img.getAttribute("src");
      if (
        !src ||
        src.startsWith("http://") ||
        src.startsWith("https://") ||
        src.startsWith("//") ||
        src.startsWith("data:") ||
        src.startsWith("#")
      ) {
        return;
      }

      img.src = base + src.replace(/^\.?\//, "");
    });
  }

  /**
   * Converts plain-text URLs in repository subtitles into clickable links.
   */
  function linkifyCgitSubtitles() {
    const urlRe = /\bhttps?:\/\/[^\s<]+/g;

    document.querySelectorAll("td.sub").forEach(sub => {
      if (sub.querySelector("a")) return;

      const text = sub.textContent;
      if (!text || !urlRe.test(text)) return;

      urlRe.lastIndex = 0;
      sub.innerHTML = text.replace(urlRe, url =>
        `<a href="${url}" target="_blank" rel="noopener noreferrer">${url}</a>`
      );
    });
  }
})();
</script>