mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-30 16:24:06 -06:00
chore: rebase Add per-folder RSS feeds
This commit is contained in:
parent
b87a701ff7
commit
4c50cf9338
@ -70,6 +70,7 @@ const config: QuartzConfig = {
|
|||||||
Plugin.ContentIndex({
|
Plugin.ContentIndex({
|
||||||
enableSiteMap: true,
|
enableSiteMap: true,
|
||||||
enableRSS: true,
|
enableRSS: true,
|
||||||
|
feedDirectories: ["index"], // For a feed for only pages in content/Folder/, add "Folder" to the array
|
||||||
}),
|
}),
|
||||||
Plugin.Assets(),
|
Plugin.Assets(),
|
||||||
Plugin.Static(),
|
Plugin.Static(),
|
||||||
|
|||||||
@ -26,6 +26,7 @@ interface Options {
|
|||||||
rssLimit?: number
|
rssLimit?: number
|
||||||
rssFullHtml: boolean
|
rssFullHtml: boolean
|
||||||
includeEmptyFiles: boolean
|
includeEmptyFiles: boolean
|
||||||
|
feedDirectories: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultOptions: Options = {
|
const defaultOptions: Options = {
|
||||||
@ -34,6 +35,7 @@ const defaultOptions: Options = {
|
|||||||
rssLimit: 10,
|
rssLimit: 10,
|
||||||
rssFullHtml: false,
|
rssFullHtml: false,
|
||||||
includeEmptyFiles: true,
|
includeEmptyFiles: true,
|
||||||
|
feedDirectories: ["index"],
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateSiteMap(cfg: GlobalConfiguration, idx: ContentIndex): string {
|
function generateSiteMap(cfg: GlobalConfiguration, idx: ContentIndex): string {
|
||||||
@ -48,7 +50,10 @@ function generateSiteMap(cfg: GlobalConfiguration, idx: ContentIndex): string {
|
|||||||
return `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">${urls}</urlset>`
|
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?: ContentIndex, limit?: number): string {
|
||||||
|
if (idx == undefined) {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
const base = cfg.baseUrl ?? ""
|
const base = cfg.baseUrl ?? ""
|
||||||
|
|
||||||
const createURLEntry = (slug: SimpleSlug, content: ContentDetails): string => `<item>
|
const createURLEntry = (slug: SimpleSlug, content: ContentDetails): string => `<item>
|
||||||
@ -116,30 +121,35 @@ export const ContentIndex: QuartzEmitterPlugin<Partial<Options>> = (opts) => {
|
|||||||
async emit(ctx, content, _resources) {
|
async emit(ctx, content, _resources) {
|
||||||
const cfg = ctx.cfg.configuration
|
const cfg = ctx.cfg.configuration
|
||||||
const emitted: FilePath[] = []
|
const emitted: FilePath[] = []
|
||||||
const linkIndex: ContentIndex = new Map()
|
const feedIndices: Map<String, ContentIndex> = new Map()
|
||||||
for (const [tree, file] of content) {
|
for (const feed of opts?.feedDirectories) {
|
||||||
const slug = file.data.slug!
|
const linkIndex: ContentIndex = new Map()
|
||||||
const date = getDate(ctx.cfg.configuration, file.data) ?? new Date()
|
for (const [tree, file] of content) {
|
||||||
if (opts?.includeEmptyFiles || (file.data.text && file.data.text !== "")) {
|
const slug = file.data.slug!
|
||||||
linkIndex.set(slug, {
|
|
||||||
title: file.data.frontmatter?.title!,
|
const date = getDate(ctx.cfg.configuration, file.data) ?? new Date()
|
||||||
links: file.data.links ?? [],
|
if ((opts?.includeEmptyFiles || (file.data.text && file.data.text !== "")) && (slug.startsWith(feed) || feed == "index")) {
|
||||||
tags: file.data.frontmatter?.tags ?? [],
|
linkIndex.set(slug, {
|
||||||
content: file.data.text ?? "",
|
title: file.data.frontmatter?.title!,
|
||||||
richContent: opts?.rssFullHtml
|
links: file.data.links ?? [],
|
||||||
? escapeHTML(toHtml(tree as Root, { allowDangerousHtml: true }))
|
tags: file.data.frontmatter?.tags ?? [],
|
||||||
: undefined,
|
content: file.data.text ?? "",
|
||||||
date: date,
|
richContent: opts?.rssFullHtml
|
||||||
description: file.data.description ?? "",
|
? escapeHTML(toHtml(tree as Root, { allowDangerousHtml: true }))
|
||||||
})
|
: undefined,
|
||||||
|
date: date,
|
||||||
|
description: file.data.description ?? "",
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
feedIndices.set(feed, linkIndex)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts?.enableSiteMap) {
|
if (opts?.enableSiteMap) {
|
||||||
emitted.push(
|
emitted.push(
|
||||||
await write({
|
await write({
|
||||||
ctx,
|
ctx,
|
||||||
content: generateSiteMap(cfg, linkIndex),
|
content: generateSiteMap(cfg, feedIndices.get("index")),
|
||||||
slug: "sitemap" as FullSlug,
|
slug: "sitemap" as FullSlug,
|
||||||
ext: ".xml",
|
ext: ".xml",
|
||||||
}),
|
}),
|
||||||
@ -147,19 +157,20 @@ export const ContentIndex: QuartzEmitterPlugin<Partial<Options>> = (opts) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (opts?.enableRSS) {
|
if (opts?.enableRSS) {
|
||||||
emitted.push(
|
opts?.feedDirectories?.map(async (feed) => {
|
||||||
await write({
|
const emittedFeed = await write({
|
||||||
ctx,
|
ctx,
|
||||||
content: generateRSSFeed(cfg, linkIndex, opts.rssLimit),
|
content: generateRSSFeed(cfg, feedIndices.get(feed), opts?.rssLimit),
|
||||||
slug: "index" as FullSlug,
|
slug: feed as FullSlug,
|
||||||
ext: ".xml",
|
ext: ".xml",
|
||||||
}),
|
})
|
||||||
)
|
emitted.push(emittedFeed)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const fp = joinSegments("static", "contentIndex") as FullSlug
|
const fp = joinSegments("static", "contentIndex") as FullSlug
|
||||||
const simplifiedIndex = Object.fromEntries(
|
const simplifiedIndex = Object.fromEntries(
|
||||||
Array.from(linkIndex).map(([slug, content]) => {
|
Array.from(feedIndices.get("index") ?? [] ).map(([slug, content]) => {
|
||||||
// remove description and from content index as nothing downstream
|
// remove description and from content index as nothing downstream
|
||||||
// actually uses it. we only keep it in the index as we need it
|
// actually uses it. we only keep it in the index as we need it
|
||||||
// for the RSS feed
|
// for the RSS feed
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user