From c6f10b44f6878e76a416332f14e3681de8df40db Mon Sep 17 00:00:00 2001
From: Emile Bangma <github@emilebangma.com>
Date: Wed, 05 Mar 2025 23:54:11 +0000
Subject: [PATCH] feat(rss): configurable RSS feed URL (#1806)
---
quartz/plugins/emitters/contentIndex.ts | 52 +++++++++++++++++++++++++++++++++++++++-------------
1 files changed, 39 insertions(+), 13 deletions(-)
diff --git a/quartz/plugins/emitters/contentIndex.ts b/quartz/plugins/emitters/contentIndex.ts
index bc4c6c3..f4a1a91 100644
--- a/quartz/plugins/emitters/contentIndex.ts
+++ b/quartz/plugins/emitters/contentIndex.ts
@@ -5,7 +5,9 @@
import { FilePath, FullSlug, SimpleSlug, joinSegments, simplifySlug } from "../../util/path"
import { QuartzEmitterPlugin } from "../types"
import { toHtml } from "hast-util-to-html"
-import path from "path"
+import { write } from "./helpers"
+import { i18n } from "../../i18n"
+import DepGraph from "../../depgraph"
export type ContentIndex = Map<FullSlug, ContentDetails>
export type ContentDetails = {
@@ -23,6 +25,7 @@
enableRSS: boolean
rssLimit?: number
rssFullHtml: boolean
+ rssSlug: string
includeEmptyFiles: boolean
}
@@ -31,6 +34,7 @@
enableRSS: true,
rssLimit: 10,
rssFullHtml: false,
+ rssSlug: "index",
includeEmptyFiles: true,
}
@@ -38,7 +42,7 @@
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))
@@ -48,12 +52,11 @@
function generateRSSFeed(cfg: GlobalConfiguration, idx: ContentIndex, limit?: number): string {
const base = cfg.baseUrl ?? ""
- const root = `https://${base}`
const createURLEntry = (slug: SimpleSlug, content: ContentDetails): string => `<item>
<title>${escapeHTML(content.title)}</title>
- <link>${joinSegments(root, encodeURI(slug))}</link>
- <guid>${joinSegments(root, encodeURI(slug))}</guid>
+ <link>https://${joinSegments(base, encodeURI(slug))}</link>
+ <guid>https://${joinSegments(base, encodeURI(slug))}</guid>
<description>${content.richContent ?? content.description}</description>
<pubDate>${content.date?.toUTCString()}</pubDate>
</item>`
@@ -78,8 +81,8 @@
<rss version="2.0">
<channel>
<title>${escapeHTML(cfg.pageTitle)}</title>
- <link>${root}</link>
- <description>${!!limit ? `Last ${limit} notes` : "Recent notes"} on ${escapeHTML(
+ <link>https://${base}</link>
+ <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>
@@ -92,7 +95,27 @@
opts = { ...defaultOptions, ...opts }
return {
name: "ContentIndex",
- async emit(ctx, content, _resources, emit) {
+ 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()
@@ -116,7 +139,8 @@
if (opts?.enableSiteMap) {
emitted.push(
- await emit({
+ await write({
+ ctx,
content: generateSiteMap(cfg, linkIndex),
slug: "sitemap" as FullSlug,
ext: ".xml",
@@ -126,15 +150,16 @@
if (opts?.enableRSS) {
emitted.push(
- await emit({
+ await write({
+ ctx,
content: generateRSSFeed(cfg, linkIndex, opts.rssLimit),
- slug: "index" as FullSlug,
+ slug: (opts?.rssSlug ?? "index") as FullSlug,
ext: ".xml",
}),
)
}
- const fp = path.join("static", "contentIndex") as FullSlug
+ const fp = joinSegments("static", "contentIndex") as FullSlug
const simplifiedIndex = Object.fromEntries(
Array.from(linkIndex).map(([slug, content]) => {
// remove description and from content index as nothing downstream
@@ -147,7 +172,8 @@
)
emitted.push(
- await emit({
+ await write({
+ ctx,
content: JSON.stringify(simplifiedIndex),
slug: fp,
ext: ".json",
--
Gitblit v1.10.0