summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Mou <dev@mou.fo>2026-08-12 01:38:24 -0400
committerJoe Mou <dev@mou.fo>2026-08-12 01:42:59 -0400
commite8461b57674b9c4e6ed93151d06dbc6149703178 (patch)
treebd34e2e1b00fc60f3ead8f8c4c290bbbf8a55f9d
parent679a2b71c3e73dc1b8cf11806b3f3df49018b535 (diff)
Handle blobs GitHub cannot preview
Binary files with no preview (a .ttf, say) have no rawLines, no colorizedLines and no richText, so the blob view's final branch threw on a null textLines and the page 500ed. Offer the raw file instead. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
-rw-r--r--cgithub/src/app.test.ts22
-rw-r--r--cgithub/src/scraper.test.ts18
-rw-r--r--cgithub/views/blob.eta6
3 files changed, 44 insertions, 2 deletions
diff --git a/cgithub/src/app.test.ts b/cgithub/src/app.test.ts
index 59ed352..94bf205 100644
--- a/cgithub/src/app.test.ts
+++ b/cgithub/src/app.test.ts
@@ -92,6 +92,28 @@ describe("sidebar fragment", () => {
});
});
+describe("blob view", () => {
+ const blob = {
+ repo: { owner: "justrajdeep", name: "fonts" },
+ branch: "master",
+ path: "Times New Roman.ttf",
+ language: null,
+ size: "815 KB",
+ image: false,
+ textLines: null,
+ htmlLines: null,
+ htmlContent: null,
+ };
+
+ // GitHub previews neither the text nor the bytes of most binary files.
+ it("should offer the raw file when there is nothing to display", () => {
+ const html = eta.render("blob.eta", blob);
+
+ assert.match(html, /This file cannot be displayed\./);
+ assert.match(html, /href="\/justrajdeep\/fonts\/raw\/master\/Times New Roman.ttf">View raw/);
+ });
+});
+
describe("redirects to GitHub", () => {
// An HTTP redirect would be caught by the extension's declarativeNetRequest
// rules and bounced straight back here.
diff --git a/cgithub/src/scraper.test.ts b/cgithub/src/scraper.test.ts
index 24fdfc0..836235e 100644
--- a/cgithub/src/scraper.test.ts
+++ b/cgithub/src/scraper.test.ts
@@ -332,6 +332,24 @@ describe("GitHub scraper", () => {
assert.strictEqual(data.htmlLines, null);
assert.strictEqual(data.htmlContent, null);
});
+
+ // A binary file GitHub has no preview for: no content of any kind, and the
+ // path exercises escaping of a space.
+ it("should support an unpreviewable binary file", async () => {
+ const data = await getGitHubBlob("justrajdeep", "fonts", "master", "Times New Roman.ttf");
+
+ assert.strictEqual(data.repo.owner, "justrajdeep");
+ assert.strictEqual(data.repo.name, "fonts");
+ assert.strictEqual(data.branch, "master");
+ assert.strictEqual(data.path, "Times New Roman.ttf");
+
+ assert.strictEqual(data.size, "815 KB");
+ assert.strictEqual(data.language, null);
+ assert.strictEqual(data.image, false);
+ assert.strictEqual(data.textLines, null);
+ assert.strictEqual(data.htmlLines, null);
+ assert.strictEqual(data.htmlContent, null);
+ });
});
describe("owner pages", () => {
diff --git a/cgithub/views/blob.eta b/cgithub/views/blob.eta
index ed1e84b..c7315f6 100644
--- a/cgithub/views/blob.eta
+++ b/cgithub/views/blob.eta
@@ -8,7 +8,7 @@
<% } %>
<div><%= it.size %></div>
<div>
- <a href="/<%= it.repo.owner %>/<%= it.repo.name %>/raw/<%= it.branch %>/<%= it.path %>">Raw</a>
+ <a href="/<%= it.repo.owner %>/<%= it.repo.name %>/raw/<%= it.branch %>/<%= it.path %>">View raw</a>
</div>
</div>
@@ -18,6 +18,8 @@
<div class="box"><%~ it.htmlContent %></div>
<% } else if (it.htmlLines) { %>
<pre class="box"><code><%~ it.htmlLines.join("\n") %></code></pre>
-<% } else { %>
+<% } else if (it.textLines) { %>
<pre class="box"><code><%= it.textLines.join("\n") %></code></pre>
+<% } else { %>
+ <div class="box">This file cannot be displayed</div>
<% } %>