Jacky Zhao
2023-07-16 3ac6b42e16dca5a44ed3fec2c0314f1dbbc2322b
quartz/plugins/emitters/contentIndex.ts
@@ -1,11 +1,12 @@
import { GlobalConfiguration } from "../../cfg"
import { CanonicalSlug, ClientSlug, FilePath, ServerSlug, canonicalizeServer } from "../../path"
import { QuartzEmitterPlugin } from "../types"
import path from "path"
export type ContentIndex = Map<string, ContentDetails>
export type ContentIndex = Map<CanonicalSlug, ContentDetails>
export type ContentDetails = {
  title: string,
  links: string[],
  links: CanonicalSlug[],
  tags: string[],
  content: string,
  date?: Date,
@@ -15,16 +16,18 @@
interface Options {
  enableSiteMap: boolean
  enableRSS: boolean
  includeEmptyFiles: boolean
}
const defaultOptions: Options = {
  enableSiteMap: true,
  enableRSS: true,
  includeEmptyFiles: false,
}
function generateSiteMap(cfg: GlobalConfiguration, idx: ContentIndex): string {
  const base = cfg.canonicalUrl ?? ""
  const createURLEntry = (slug: string, content: ContentDetails): string => `<url>
  const base = cfg.baseUrl ?? ""
  const createURLEntry = (slug: CanonicalSlug, content: ContentDetails): string => `<url>
    <loc>https://${base}/${slug}</loc>
    <lastmod>${content.date?.toISOString()}</lastmod>
  </url>`
@@ -33,10 +36,10 @@
}
function generateRSSFeed(cfg: GlobalConfiguration, idx: ContentIndex): string {
  const base = cfg.canonicalUrl ?? ""
  const root = `https://${base}`
  const base = cfg.baseUrl ?? ""
  const root = `https://${base}` as ClientSlug
  const createURLEntry = (slug: string, content: ContentDetails): string => `<items>
  const createURLEntry = (slug: CanonicalSlug, content: ContentDetails): string => `<items>
    <title>${content.title}</title>
    <link>${root}/${slug}</link>
    <guid>${root}/${slug}</guid>
@@ -57,16 +60,17 @@
  </rss>`
}
export const ContentIndex: QuartzEmitterPlugin<Options> = (opts) => {
export const ContentIndex: QuartzEmitterPlugin<Partial<Options>> = (opts) => {
  opts = { ...defaultOptions, ...opts }
  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 ?? [],
@@ -75,27 +79,28 @@
          date: date,
          description: file.data.description ?? ""
        })
        }
      }
      if (opts?.enableSiteMap) {
        await emit({
          content: generateSiteMap(cfg, linkIndex),
          slug: "sitemap",
          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",
          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
@@ -106,12 +111,13 @@
          return [slug, content]
        })
      )
      await emit({
        content: JSON.stringify(simplifiedIndex),
        slug: fp,
        ext: ".json",
      })
      emitted.push(`${fp}.json`)
      emitted.push(`${fp}.json` as FilePath)
      return emitted
    },