import { Hono } from "hono"; import { html, raw } from "hono/html"; import { layout } from "../views/layouts/main"; import { requireAuth, type Session } from "../lib/session"; import { csrfField } from "../lib/csrf"; import { isValidTID } from "../lib/validation"; import { createMarkdownContent, getDocumentContentText, } from "../lib/content-types"; import { marked } from "marked"; import type { AppVariables } from "../types"; export const documentRoutes = new Hono<{ Variables: AppVariables }>(); const DOCUMENT_COLLECTION = "site.standard.document"; const PUBLICATION_COLLECTION = "site.standard.publication"; // List all documents documentRoutes.get("/", async (c) => { let session: Session; try { session = requireAuth(c); } catch { return c.redirect("/auth/login"); } const filter = c.req.query("filter") || "all"; try { const response = await session.agent!.com.atproto.repo.listRecords({ repo: session.did!, collection: DOCUMENT_COLLECTION, limit: 100, }); let documents = response.data.records; // Filter by draft/published status if (filter === "drafts") { documents = documents.filter((doc: any) => { const tags = doc.value.tags || []; return tags.includes("draft"); }); } else if (filter === "published") { documents = documents.filter((doc: any) => { const tags = doc.value.tags || []; return !tags.includes("draft"); }); } // Sort by publishedAt or updatedAt documents.sort((a: any, b: any) => { const dateA = new Date( a.value.updatedAt || a.value.publishedAt, ).getTime(); const dateB = new Date( b.value.updatedAt || b.value.publishedAt, ).getTime(); return dateB - dateA; }); const content = html`
No documents yet. Create your first document.
` : html`Error loading documents. Please try again.
`; return c.html(layout(content, { title: "Documents - sitebase", session })); } }); // New document form documentRoutes.get("/new", async (c) => { let session: Session; try { session = requireAuth(c); } catch { return c.redirect("/auth/login"); } // Get publication to use as site reference let publicationUri = ""; try { const response = await session.agent!.com.atproto.repo.listRecords({ repo: session.did!, collection: PUBLICATION_COLLECTION, limit: 1, }); if (response.data.records[0]) { publicationUri = response.data.records[0].uri; } } catch (e) { // No publication yet, will need URL } const csrfToken = c.get("csrfToken") as string; const content = html`${doc.description}
` : "" }(No content)
`; const htmlContent = marked.parse(text) as string; return html`