Files
chuan 70d6712d00 feat: add CSS styles for theme preview and implement template overrides
- Introduced a new CSS file for styling the theme preview.
- Updated build script to copy template files to the output directory.
- Created a new script for serving the preview of the theme.
- Enhanced theme verification to check for required template overrides.
- Added README documentation for template overrides.
- Implemented footer and navbar template overrides to customize Gitea's UI.
2026-05-15 21:29:04 +08:00

60 lines
1.6 KiB
TypeScript

import { join, resolve } from "node:path";
const root = resolve(import.meta.dir, "..");
const previewDir = join(root, "preview");
const assetsDir = join(root, ".gitea-data", "gitea", "public", "assets");
const port = Number(Bun.env.PORT ?? 4173);
const contentTypes: Record<string, string> = {
".css": "text/css; charset=utf-8",
".html": "text/html; charset=utf-8",
".js": "text/javascript; charset=utf-8",
".svg": "image/svg+xml",
};
function contentType(pathname: string): string {
for (const [extension, type] of Object.entries(contentTypes)) {
if (pathname.endsWith(extension)) return type;
}
return "application/octet-stream";
}
async function serveFile(pathname: string, baseDir: string): Promise<Response> {
const filePath = join(baseDir, pathname);
const file = Bun.file(filePath);
if (!(await file.exists())) {
return new Response("Not found", { status: 404 });
}
return new Response(file, {
headers: {
"content-type": contentType(pathname),
"cache-control": "no-store",
},
});
}
Bun.serve({
port,
async fetch(request) {
const url = new URL(request.url);
if (url.pathname === "/" || url.pathname === "/index.html") {
return serveFile("index.html", previewDir);
}
if (url.pathname.startsWith("/preview/")) {
return serveFile(url.pathname.replace("/preview/", ""), previewDir);
}
if (url.pathname.startsWith("/assets/")) {
return serveFile(url.pathname.replace("/assets/", ""), assetsDir);
}
return new Response("Not found", { status: 404 });
},
});
console.log(`Preview running at http://localhost:${port}`);