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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
<script>
(function () {
onReady(() => {
fixCgitMarkdownImages();
linkifyCgitSubtitles();
enhanceBlobPreview();
});
/**
* Runs a callback once the DOM is ready.
* @param {() => void} fn
*/
function onReady(fn) {
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", fn, { once: true });
} else {
fn();
}
}
/**
* Rewrites relative image URLs in cgit README ("about") pages
* to use /<repo>/plain/<path>, so images render correctly.
*/
function fixCgitMarkdownImages() {
const parts = location.pathname.split("/").filter(Boolean);
// Expected path: /<repo>/about/
if (parts[1] !== "about") return;
const baseUrl = `${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 (!isRelativeUrl(src)) return;
img.src = baseUrl + src.replace(/^\.?\//, "");
});
}
/**
* Converts plain-text URLs in repository subtitle cells
* (<td class="sub">) into clickable links.
*/
function linkifyCgitSubtitles() {
const urlRegex = /\bhttps?:\/\/[^\s<]+/g;
document.querySelectorAll("td.sub").forEach(sub => {
if (sub.querySelector("a")) return;
const text = sub.textContent;
if (!text || !urlRegex.test(text)) return;
urlRegex.lastIndex = 0;
sub.innerHTML = text.replace(urlRegex, url =>
`<a href="${url}" target="_blank" rel="noopener noreferrer">${url}</a>`
);
});
}
/**
* Replaces binary image hex dumps with rendered image previews.
* The hex dump is moved into a <details> block below the image.
*/
function enhanceBlobPreview() {
const content = document.querySelector("div.content");
const binBlob = document.querySelector("table.bin-blob");
const plainLink = document.querySelector('a[href*="/plain/"]');
if (!content || !binBlob || !plainLink) return;
const url = plainLink.getAttribute("href");
const ext = getFileExtension(url);
if (!isSupportedImage(ext)) {
binBlob.style.display = "table";
return;
}
const img = new Image();
img.src = url;
img.alt = "Rendered image";
img.onload = () => {
// Image preview
const preview = document.createElement("div");
preview.className = "cgit-image-preview";
preview.appendChild(img);
// Insert preview before hex dump
content.insertBefore(preview, binBlob);
// Wrap hex dump in <details>
const details = document.createElement("details");
details.className = "cgit-hexdump";
const summary = document.createElement("summary");
summary.textContent = "Show hex dump";
binBlob.style.display = "table";
details.appendChild(summary);
details.appendChild(binBlob);
content.appendChild(details);
};
}
/**
* Returns true if a URL is relative and safe to rewrite.
* @param {string|null} url
* @returns {boolean}
*/
function isRelativeUrl(url) {
return Boolean(
url &&
!url.startsWith("http://") &&
!url.startsWith("https://") &&
!url.startsWith("//") &&
!url.startsWith("data:") &&
!url.startsWith("#")
);
}
/**
* Extracts the lowercase file extension from a URL.
* @param {string} url
* @returns {string}
*/
function getFileExtension(url) {
return url.split(".").pop().toLowerCase();
}
/**
* Checks whether a file extension is a browser-renderable image.
* @param {string} ext
* @returns {boolean}
*/
function isSupportedImage(ext) {
return new Set([
"png", "jpg", "jpeg", "gif", "webp",
"bmp", "svg", "ico", "avif"
]).has(ext);
}
})();
</script>
|