Jacky Zhao
2023-06-20 fd5c8d17d3b3cd0a13b0a21a2b78d85010e02e12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { QuartzEmitterPlugin } from "../types"
import path from "path"
 
export type ContentIndex = Map<string, ContentDetails>
export type ContentDetails = {
  title: string,
  links?: string[],
  tags?: string[],
  content: string,
}
 
export const ContentIndex: QuartzEmitterPlugin = () => {
  return {
    name: "ContentIndex",
    async emit(_contentDir, _cfg, content, _resources, emit) {
      const fp = path.join("static", "contentIndex")
      const linkIndex: ContentIndex = new Map()
      for (const [_tree, file] of content) {
        let slug = file.data.slug!
        linkIndex.set(slug, {
          title: file.data.frontmatter?.title!,
          links: file.data.links ?? [],
          tags: file.data.frontmatter?.tags,
          content: file.data.text ?? ""
        })
      }
 
      await emit({
        content: JSON.stringify(Object.fromEntries(linkIndex)),
        slug: fp,
        ext: ".json",
      })
 
      return [`${fp}.json`]
    },
    getQuartzComponents: () => [],
  }
}