Jacky Zhao
2024-01-01 b33f13ccaf4ec14a94ee0ee467dda04cf4981d00
quartz/plugins/emitters/tagPage.tsx
@@ -6,26 +6,31 @@
import { ProcessedContent, defaultProcessedContent } from "../vfile"
import { FullPageLayout } from "../../cfg"
import {
  CanonicalSlug,
  FilePath,
  ServerSlug,
  FullSlug,
  getAllSegmentPrefixes,
  joinSegments,
} from "../../path"
  pathToRoot,
} from "../../util/path"
import { defaultListPageLayout, sharedPageComponents } from "../../../quartz.layout"
import { TagContent } from "../../components"
export const TagPage: QuartzEmitterPlugin<FullPageLayout> = (opts) => {
  if (!opts) {
    throw new Error("TagPage must be initialized with options specifiying the components to use")
export const TagPage: QuartzEmitterPlugin<FullPageLayout> = (userOpts) => {
  const opts: FullPageLayout = {
    ...sharedPageComponents,
    ...defaultListPageLayout,
    pageBody: TagContent(),
    ...userOpts,
  }
  const { head: Head, header, beforeBody, pageBody: Content, left, right, footer: Footer } = opts
  const { head: Head, header, beforeBody, pageBody, left, right, footer: Footer } = opts
  const Header = HeaderConstructor()
  const Body = BodyConstructor()
  return {
    name: "TagPage",
    getQuartzComponents() {
      return [Head, Header, Body, ...header, ...beforeBody, Content, ...left, ...right, Footer]
      return [Head, Header, Body, ...header, ...beforeBody, pageBody, ...left, ...right, Footer]
    },
    async emit(ctx, content, resources, emit): Promise<FilePath[]> {
      const fps: FilePath[] = []
@@ -35,16 +40,17 @@
      const tags: Set<string> = new Set(
        allFiles.flatMap((data) => data.frontmatter?.tags ?? []).flatMap(getAllSegmentPrefixes),
      )
      // add base tag
      tags.add("")
      tags.add("index")
      const tagDescriptions: Record<string, ProcessedContent> = Object.fromEntries(
        [...tags].map((tag) => {
          const title = tag === "" ? "Tag Index" : `Tag: #${tag}`
          const title = tag === "index" ? "Tag Index" : `Tag: #${tag}`
          return [
            tag,
            defaultProcessedContent({
              slug: joinSegments("tags", tag, "index") as ServerSlug,
              slug: joinSegments("tags", tag) as FullSlug,
              frontmatter: { title, tags: [] },
            }),
          ]
@@ -54,7 +60,7 @@
      for (const [tree, file] of content) {
        const slug = file.data.slug!
        if (slug.startsWith("tags/")) {
          const tag = joinSegments(slug.slice("tags/".length), "index")
          const tag = slug.slice("tags/".length)
          if (tags.has(tag)) {
            tagDescriptions[tag] = [tree, file]
          }
@@ -62,8 +68,8 @@
      }
      for (const tag of tags) {
        const slug = joinSegments("tags", tag) as CanonicalSlug
        const externalResources = pageResources(slug, resources)
        const slug = joinSegments("tags", tag) as FullSlug
        const externalResources = pageResources(pathToRoot(slug), resources)
        const [tree, file] = tagDescriptions[tag]
        const componentData: QuartzComponentProps = {
          fileData: file.data,
@@ -75,9 +81,7 @@
        }
        const content = renderPage(slug, componentData, opts, externalResources)
        const fp = (file.data.slug + ".html") as FilePath
        await emit({
        const fp = await emit({
          content,
          slug: file.data.slug!,
          ext: ".html",