From ae2e3b463a91d94caa8bdf62e5c3a3d726b8b4e4 Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Sun, 23 Jul 2023 18:49:26 +0000
Subject: [PATCH] improve error handling while serving

---
 quartz/plugins/emitters/contentIndex.ts |   60 ++++++++++++++++++++++++++++++++----------------------------
 1 files changed, 32 insertions(+), 28 deletions(-)

diff --git a/quartz/plugins/emitters/contentIndex.ts b/quartz/plugins/emitters/contentIndex.ts
index cb2853b..0b1fec7 100644
--- a/quartz/plugins/emitters/contentIndex.ts
+++ b/quartz/plugins/emitters/contentIndex.ts
@@ -1,16 +1,16 @@
 import { GlobalConfiguration } from "../../cfg"
-import { CanonicalSlug, ClientSlug } from "../../path"
+import { CanonicalSlug, ClientSlug, FilePath, ServerSlug, canonicalizeServer } from "../../path"
 import { QuartzEmitterPlugin } from "../types"
 import path from "path"
 
 export type ContentIndex = Map<CanonicalSlug, ContentDetails>
 export type ContentDetails = {
-  title: string,
-  links: CanonicalSlug[],
-  tags: string[],
-  content: string,
-  date?: Date,
-  description?: string,
+  title: string
+  links: CanonicalSlug[]
+  tags: string[]
+  content: string
+  date?: Date
+  description?: string
 }
 
 interface Options {
@@ -31,7 +31,9 @@
     <loc>https://${base}/${slug}</loc>
     <lastmod>${content.date?.toISOString()}</lastmod>
   </url>`
-  const urls = Array.from(idx).map(([slug, content]) => createURLEntry(slug, content)).join("")
+  const urls = Array.from(idx)
+    .map(([slug, content]) => createURLEntry(slug, content))
+    .join("")
   return `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">${urls}</urlset>`
 }
 
@@ -47,7 +49,9 @@
     <pubDate>${content.date?.toUTCString()}</pubDate>
   </items>`
 
-  const items = Array.from(idx).map(([slug, content]) => createURLEntry(slug, content)).join("")
+  const items = Array.from(idx)
+    .map(([slug, content]) => createURLEntry(slug, content))
+    .join("")
   return `<rss xmlns:atom="http://www.w3.org/2005/atom" version="2.0">
     <channel>
       <title>${cfg.pageTitle}</title>
@@ -65,42 +69,42 @@
   return {
     name: "ContentIndex",
     async emit(_contentDir, cfg, content, _resources, emit) {
-      const emitted: string[] = []
+      const emitted: FilePath[] = []
       const linkIndex: ContentIndex = new Map()
       for (const [_tree, file] of content) {
-        const slug = file.data.slug!
+        const slug = canonicalizeServer(file.data.slug!)
         const date = file.data.dates?.modified ?? new Date()
         if (opts?.includeEmptyFiles || (file.data.text && file.data.text !== "")) {
-        linkIndex.set(slug, {
-          title: file.data.frontmatter?.title!,
-          links: file.data.links ?? [],
-          tags: file.data.frontmatter?.tags ?? [],
-          content: file.data.text ?? "",
-          date: date,
-          description: file.data.description ?? ""
-        })
+          linkIndex.set(slug, {
+            title: file.data.frontmatter?.title!,
+            links: file.data.links ?? [],
+            tags: file.data.frontmatter?.tags ?? [],
+            content: file.data.text ?? "",
+            date: date,
+            description: file.data.description ?? "",
+          })
         }
       }
 
       if (opts?.enableSiteMap) {
         await emit({
           content: generateSiteMap(cfg, linkIndex),
-          slug: "sitemap",
-          ext: ".xml"
+          slug: "sitemap" as ServerSlug,
+          ext: ".xml",
         })
-        emitted.push("sitemap.xml")
+        emitted.push("sitemap.xml" as FilePath)
       }
 
       if (opts?.enableRSS) {
         await emit({
           content: generateRSSFeed(cfg, linkIndex),
-          slug: "index",
-          ext: ".xml"
+          slug: "index" as ServerSlug,
+          ext: ".xml",
         })
-        emitted.push("index.xml")
+        emitted.push("index.xml" as FilePath)
       }
 
-      const fp = path.join("static", "contentIndex")
+      const fp = path.join("static", "contentIndex") as ServerSlug
       const simplifiedIndex = Object.fromEntries(
         Array.from(linkIndex).map(([slug, content]) => {
           // remove description and from content index as nothing downstream
@@ -109,7 +113,7 @@
           delete content.description
           delete content.date
           return [slug, content]
-        })
+        }),
       )
 
       await emit({
@@ -117,7 +121,7 @@
         slug: fp,
         ext: ".json",
       })
-      emitted.push(`${fp}.json`)
+      emitted.push(`${fp}.json` as FilePath)
 
       return emitted
     },

--
Gitblit v1.10.0