roadmap/quartz/plugins/emitters/contentIndex.ts

39 lines
1006 B
TypeScript
Raw Normal View History

2023-06-17 02:41:59 +00:00
import { QuartzEmitterPlugin } from "../types"
import path from "path"
2023-06-20 03:37:45 +00:00
export type ContentIndex = Map<string, ContentDetails>
2023-06-18 17:47:07 +00:00
export type ContentDetails = {
2023-06-17 02:41:59 +00:00
title: string,
links?: string[],
tags?: string[],
content: string,
2023-06-18 17:47:07 +00:00
}
2023-06-17 02:41:59 +00:00
2023-06-20 03:37:45 +00:00
export const ContentIndex: QuartzEmitterPlugin = () => {
2023-06-17 02:41:59 +00:00
return {
name: "ContentIndex",
async emit(_contentDir, _cfg, content, _resources, emit) {
2023-06-18 17:47:07 +00:00
const fp = path.join("static", "contentIndex")
2023-06-17 02:41:59 +00:00
const linkIndex: ContentIndex = new Map()
2023-06-20 03:37:45 +00:00
for (const [_tree, file] of content) {
let slug = file.data.slug!
2023-06-17 02:41:59 +00:00
linkIndex.set(slug, {
title: file.data.frontmatter?.title!,
2023-06-20 03:37:45 +00:00
links: file.data.links ?? [],
2023-06-17 02:41:59 +00:00
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: () => [],
}
}