diff options
| author | Joe Mou <dev@mou.fo> | 2026-01-28 15:10:00 -0500 |
|---|---|---|
| committer | Joe Mou <dev@mou.fo> | 2026-01-28 15:32:53 -0500 |
| commit | 631bc70d94cd12d0432cca1a220a3642eea6976a (patch) | |
| tree | 184c444585702ef8d8e58e95cf7946b778f71426 | |
| parent | 71442a856d2180c68a51a1db480514d611137bd0 (diff) | |
Image support
| -rw-r--r-- | cgithub/src/scraper.test.ts | 24 | ||||
| -rw-r--r-- | cgithub/src/scraper.ts | 18 | ||||
| -rw-r--r-- | cgithub/views/blob.eta | 4 |
3 files changed, 40 insertions, 6 deletions
diff --git a/cgithub/src/scraper.test.ts b/cgithub/src/scraper.test.ts index 9326747..ae38c44 100644 --- a/cgithub/src/scraper.test.ts +++ b/cgithub/src/scraper.test.ts @@ -69,6 +69,8 @@ describe("GitHub scraper", () => { assert.strictEqual(data.size, "1.04 KB / 21 lines / 17 loc"); assert.strictEqual(data.language, "Text"); + assert.strictEqual(data.image, false); + assert.strictEqual(data.textLines?.length, 21); assert.strictEqual(data.textLines[0], "MIT License"); assert.strictEqual(data.htmlLines?.length, 21); @@ -86,6 +88,8 @@ describe("GitHub scraper", () => { assert.strictEqual(data.size, "8.95 KB / 133 lines / 94 loc"); assert.strictEqual(data.language, "Markdown"); + assert.strictEqual(data.image, false); + assert.strictEqual(data.textLines, null); assert.strictEqual(data.htmlLines, null); const firstLine = @@ -103,6 +107,8 @@ describe("GitHub scraper", () => { assert.strictEqual(data.size, "39 Bytes / 1 lines / 1 loc"); assert.strictEqual(data.language, "Git Attributes"); + assert.strictEqual(data.image, false); + assert.deepStrictEqual(data.textLines, ["dist/** -diff linguist-generated=true "]); assert.deepStrictEqual(data.htmlLines, [ '\u003cspan class="pl-e"\u003edist\u003c/span\u003e/\u003cspan class="pl-k"\u003e**\u003c/span\u003e \u003cspan class="pl-k"\u003e-\u003c/span\u003e\u003cspan class="pl-v"\u003ediff\u003c/span\u003e \u003cspan class="pl-v"\u003elinguist-generated\u003c/span\u003e=\u003cspan class="pl-c1"\u003etrue\u003c/span\u003e ', @@ -124,6 +130,24 @@ describe("GitHub scraper", () => { }); }); + describe("rich file types", () => { + it("should support PNG", async () => { + const data = await getGitHubBlob("github", "docs", "main", "assets/images/site/logo.png"); + + assert.strictEqual(data.repo.owner, "github"); + assert.strictEqual(data.repo.name, "docs"); + assert.strictEqual(data.branch, "main"); + assert.strictEqual(data.path, "assets/images/site/logo.png"); + + assert.strictEqual(data.size, "1.26 KB"); + assert.strictEqual(data.language, null); + assert.strictEqual(data.image, true); + assert.strictEqual(data.textLines, null); + assert.strictEqual(data.htmlLines, null); + assert.strictEqual(data.htmlContent, null); + }); + }); + describe("error handling", () => { it("should throw for non-existent repository", async () => { await assert.rejects(getGitHubRepo("nosuchowner", "nosuchrepo"), (err) => { diff --git a/cgithub/src/scraper.ts b/cgithub/src/scraper.ts index 05afc33..e58402a 100644 --- a/cgithub/src/scraper.ts +++ b/cgithub/src/scraper.ts @@ -68,11 +68,12 @@ interface RawPayload { headerInfo: { blobSize: string; lineInfo: { - truncatedLoc: string; - truncatedSloc: string; + truncatedLoc: string | null; + truncatedSloc: string | null; }; }; - language?: string; + language: string | null; + image: boolean; rawLines: string[] | null; colorizedLines: string[] | null; richText: string | null; @@ -139,6 +140,7 @@ export interface GitHubRepo extends GitHubTree { export interface GitHubBlob extends GitHubNav { language: string | null; size: string; + image: boolean; textLines: string[] | null; htmlLines: string[] | null; htmlContent: string | null; @@ -302,12 +304,18 @@ export async function getGitHubBlob( htmlLines = blob.rawLines.map((line, i) => applyStyling(line, stylingDirectives[i])); } - // TODO fine tune - const size = `${headerInfo.blobSize} / ${headerInfo.lineInfo.truncatedLoc} lines / ${headerInfo.lineInfo.truncatedSloc} loc`; + let size = headerInfo.blobSize; + if (headerInfo.lineInfo.truncatedLoc) { + size += ` / ${headerInfo.lineInfo.truncatedLoc} lines`; + } + if (headerInfo.lineInfo.truncatedSloc) { + size += ` / ${headerInfo.lineInfo.truncatedSloc} loc`; + } return extractGitHub(payload, { language: blob.language || null, size, + image: blob.image, textLines: blob.rawLines, htmlLines, htmlContent: blob.richText, diff --git a/cgithub/views/blob.eta b/cgithub/views/blob.eta index 4972209..ea07059 100644 --- a/cgithub/views/blob.eta +++ b/cgithub/views/blob.eta @@ -12,7 +12,9 @@ </div> </div> -<% if (it.htmlContent) { %> +<% if (it.image) { %> + <div class="box"><img src="/<%= it.repo.owner %>/<%= it.repo.name %>/raw/<%= it.branch %>/<%= it.path %>"></div> +<% } else if (it.htmlContent) { %> <div class="box"><%~ it.htmlContent %></div> <% } else if (it.htmlLines) { %> <pre class="box"><code><%~ it.htmlLines.join("\n") %></code></pre> |
