diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/index.ts | 23 | ||||
| -rw-r--r-- | src/scraper.ts | 77 |
2 files changed, 99 insertions, 1 deletions
diff --git a/src/index.ts b/src/index.ts index df0a116..a546f13 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import { Hono } from "hono"; import { serve } from "@hono/node-server"; import { serveStatic } from "@hono/node-server/serve-static"; import { Eta } from "eta"; -import { getGitHubTree } from "./scraper.js"; +import { getGitHubTree, getGitHubBlob } from "./scraper.js"; const app = new Hono(); const eta = new Eta({ views: "./views" }); @@ -30,6 +30,27 @@ app.get("/:owner/:repo/tree/:branch/:path{.*}?", async (c) => { } }); +app.get("/:owner/:repo/blob/:branch/:path{.*}", async (c) => { + try { + const owner = c.req.param("owner"); + const repo = c.req.param("repo"); + const branch = c.req.param("branch"); + const path = c.req.param("path"); + + const data = await getGitHubBlob(owner, repo, branch, path); + const html = await eta.renderAsync("blob.eta", data); + + return c.html(html); + } catch (error) { + const errorMessage = + error instanceof Error ? error.message : "Unknown error"; + return c.html( + `<html><body><h1>Error</h1><p>${errorMessage}</p></body></html>`, + 500, + ); + } +}); + const port = parseInt(process.env.PORT || "3000", 10); serve({ fetch: app.fetch, port }, (info) => { console.log(`Server running on http://localhost:${info.port}`); diff --git a/src/scraper.ts b/src/scraper.ts index add03b2..fa8d655 100644 --- a/src/scraper.ts +++ b/src/scraper.ts @@ -15,6 +15,20 @@ export interface GitHubTree { }; } +export interface GitHubBlob { + path: string; + branch: string; + content: string; + rawContent: string; + language: string | null; + size: number; + repo: { + name: string; + owner: string; + isPublic: boolean; + }; +} + export async function getGitHubTree( owner: string, repo: string, @@ -89,3 +103,66 @@ export async function getGitHubTree( }, }; } + +export async function getGitHubBlob( + owner: string, + repo: string, + branch: string, + path: string, +): Promise<GitHubBlob> { + const url = `https://github.com/${owner}/${repo}/blob/${branch}/${path}`; + + const response = await fetch(url, { + headers: { + "User-Agent": + "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36", + Accept: + "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Accept-Language": "en-US,en;q=0.5", + }, + }); + const html = await response.text(); + + // GitHub embeds file data in a JSON script tag + let payload; + + const match = html.match( + /<script type="application\/json" data-target="react-app\.embeddedData">([^<]+)<\/script>/, + ); + if (match) { + try { + const data = JSON.parse(match[1]); + if (data.payload?.blob) { + payload = data.payload; + } + } catch (e) { + // Skip invalid JSON + } + } + + if (!payload) { + throw new Error("Could not find blob data in embedded JSON"); + } + + const blob = payload.blob; + const rawLines = blob.rawLines || []; + const rawContent = rawLines.join("\n"); + const highlightedLines = blob.highlightedLines || []; + const content = highlightedLines + .map((line: any) => line.text || "") + .join("\n"); + + return { + path: payload.path, + branch: payload.refInfo.name, + content: content || rawContent, + rawContent, + language: blob.language || null, + size: blob.size, + repo: { + name: payload.repo.name, + owner: payload.repo.ownerLogin, + isPublic: payload.repo.public, + }, + }; +} |
