| | |
| | | import { GlobalConfiguration } from "../../cfg" |
| | | import { CanonicalSlug, ClientSlug } 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, |
| | |
| | | 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>` |
| | |
| | | } |
| | | |
| | | 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 |
| | | |
| | | // TODO: ogimage |
| | | 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> |
| | |
| | | </rss>` |
| | | } |
| | | |
| | | export const ContentIndex: QuartzEmitterPlugin<Options> = (opts) => { |
| | | export const ContentIndex: QuartzEmitterPlugin<Partial<Options>> = (opts) => { |
| | | opts = { ...defaultOptions, ...opts } |
| | | return { |
| | | name: "ContentIndex", |
| | |
| | | for (const [_tree, file] of content) { |
| | | const slug = 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 ?? [], |
| | |
| | | date: date, |
| | | description: file.data.description ?? "" |
| | | }) |
| | | } |
| | | } |
| | | |
| | | if (opts?.enableSiteMap) { |
| | |
| | | return [slug, content] |
| | | }) |
| | | ) |
| | | |
| | | await emit({ |
| | | content: JSON.stringify(simplifiedIndex), |
| | | slug: fp, |