| | |
| | | import { QuartzEmitterPlugin } from "../types" |
| | | import { toHtml } from "hast-util-to-html" |
| | | import { write } from "./helpers" |
| | | import { i18n } from "../../i18n" |
| | | import DepGraph from "../../depgraph" |
| | | |
| | | export type ContentIndex = Map<FullSlug, ContentDetails> |
| | | export type ContentIndexMap = Map<FullSlug, ContentDetails> |
| | | export type ContentDetails = { |
| | | title: string |
| | | links: SimpleSlug[] |
| | |
| | | enableRSS: boolean |
| | | rssLimit?: number |
| | | rssFullHtml: boolean |
| | | rssSlug: string |
| | | includeEmptyFiles: boolean |
| | | } |
| | | |
| | |
| | | enableRSS: true, |
| | | rssLimit: 10, |
| | | rssFullHtml: false, |
| | | rssSlug: "index", |
| | | includeEmptyFiles: true, |
| | | } |
| | | |
| | | function generateSiteMap(cfg: GlobalConfiguration, idx: ContentIndex): string { |
| | | function generateSiteMap(cfg: GlobalConfiguration, idx: ContentIndexMap): string { |
| | | const base = cfg.baseUrl ?? "" |
| | | const createURLEntry = (slug: SimpleSlug, content: ContentDetails): string => `<url> |
| | | <loc>https://${joinSegments(base, encodeURI(slug))}</loc> |
| | | <lastmod>${content.date?.toISOString()}</lastmod> |
| | | ${content.date && `<lastmod>${content.date.toISOString()}</lastmod>`} |
| | | </url>` |
| | | const urls = Array.from(idx) |
| | | .map(([slug, content]) => createURLEntry(simplifySlug(slug), content)) |
| | |
| | | return `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">${urls}</urlset>` |
| | | } |
| | | |
| | | function generateRSSFeed(cfg: GlobalConfiguration, idx: ContentIndex, limit?: number): string { |
| | | function generateRSSFeed(cfg: GlobalConfiguration, idx: ContentIndexMap, limit?: number): string { |
| | | const base = cfg.baseUrl ?? "" |
| | | |
| | | const createURLEntry = (slug: SimpleSlug, content: ContentDetails): string => `<item> |
| | |
| | | <channel> |
| | | <title>${escapeHTML(cfg.pageTitle)}</title> |
| | | <link>https://${base}</link> |
| | | <description>${!!limit ? `Last ${limit} notes` : "Recent notes"} on ${escapeHTML( |
| | | <description>${!!limit ? i18n(cfg.locale).pages.rss.lastFewNotes({ count: limit }) : i18n(cfg.locale).pages.rss.recentNotes} on ${escapeHTML( |
| | | cfg.pageTitle, |
| | | )}</description> |
| | | <generator>Quartz -- quartz.jzhao.xyz</generator> |
| | |
| | | opts = { ...defaultOptions, ...opts } |
| | | return { |
| | | name: "ContentIndex", |
| | | async getDependencyGraph(ctx, content, _resources) { |
| | | const graph = new DepGraph<FilePath>() |
| | | |
| | | for (const [_tree, file] of content) { |
| | | const sourcePath = file.data.filePath! |
| | | |
| | | graph.addEdge( |
| | | sourcePath, |
| | | joinSegments(ctx.argv.output, "static/contentIndex.json") as FilePath, |
| | | ) |
| | | if (opts?.enableSiteMap) { |
| | | graph.addEdge(sourcePath, joinSegments(ctx.argv.output, "sitemap.xml") as FilePath) |
| | | } |
| | | if (opts?.enableRSS) { |
| | | graph.addEdge(sourcePath, joinSegments(ctx.argv.output, "index.xml") as FilePath) |
| | | } |
| | | } |
| | | |
| | | return graph |
| | | }, |
| | | async emit(ctx, content, _resources) { |
| | | const cfg = ctx.cfg.configuration |
| | | const emitted: FilePath[] = [] |
| | | const linkIndex: ContentIndex = new Map() |
| | | const linkIndex: ContentIndexMap = new Map() |
| | | for (const [tree, file] of content) { |
| | | const slug = file.data.slug! |
| | | const date = getDate(ctx.cfg.configuration, file.data) ?? new Date() |
| | |
| | | await write({ |
| | | ctx, |
| | | content: generateRSSFeed(cfg, linkIndex, opts.rssLimit), |
| | | slug: "index" as FullSlug, |
| | | slug: (opts?.rssSlug ?? "index") as FullSlug, |
| | | ext: ".xml", |
| | | }), |
| | | ) |