From a8c0ffe7215974053c14c84ad1808a56f65023d1 Mon Sep 17 00:00:00 2001 From: bfahrenfort Date: Wed, 14 Feb 2024 10:34:11 -0600 Subject: [PATCH 1/4] refactor: incorporate suggestions --- quartz/plugins/emitters/contentIndex.ts | 35 +++++++++++-------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/quartz/plugins/emitters/contentIndex.ts b/quartz/plugins/emitters/contentIndex.ts index 81e91542a..4c5649b7b 100644 --- a/quartz/plugins/emitters/contentIndex.ts +++ b/quartz/plugins/emitters/contentIndex.ts @@ -51,9 +51,6 @@ function generateSiteMap(cfg: GlobalConfiguration, idx: ContentIndex): string { } function generateRSSFeed(cfg: GlobalConfiguration, idx: ContentIndex, limit?: number): string { - if (idx == undefined) { - return "" - } const base = cfg.baseUrl ?? "" const createURLEntry = (slug: SimpleSlug, content: ContentDetails): string => ` @@ -121,13 +118,9 @@ export const ContentIndex: QuartzEmitterPlugin> = (opts) => { async emit(ctx, content, _resources) { const cfg = ctx.cfg.configuration const emitted: FilePath[] = [] - const feedIndices: Map = new Map() + const feedIndices: Map = new Map() - // bfahrenfort: ts can't see the expansion of opts above that guarantees a non-null feedDirectories - const directories = - opts?.feedDirectories == null ? defaultOptions.feedDirectories : opts.feedDirectories - - for (const feed of directories) { + for (const feed of opts?.feedDirectories!) { const linkIndex: ContentIndex = new Map() for (const [tree, file] of content) { const slug = file.data.slug! @@ -153,13 +146,14 @@ export const ContentIndex: QuartzEmitterPlugin> = (opts) => { feedIndices.set(feed, linkIndex) } + const siteFeed = feedIndices.get("index")! if (opts?.enableSiteMap) { emitted.push( await write({ ctx, // bfahrenfort: "index" is guaranteed non-null // see directories instantiation and feedIndices.set iterating over directories - content: generateSiteMap(cfg, feedIndices.get("index")!), + content: generateSiteMap(cfg, siteFeed), slug: "sitemap" as FullSlug, ext: ".xml", }), @@ -167,16 +161,19 @@ export const ContentIndex: QuartzEmitterPlugin> = (opts) => { } if (opts?.enableRSS) { - directories.map(async (feed) => { - const emittedFeed = await write({ - ctx, - // bfahrenfort: we just generated a feedIndices entry for every directories entry, guaranteed non-null - content: generateRSSFeed(cfg, feedIndices.get(feed)!, opts?.rssLimit), - slug: feed as FullSlug, - ext: ".xml", - }) - emitted.push(emittedFeed) + var feedPromises: Promise[] = [] + opts.feedDirectories!.map((feed) => { + feedPromises.push( + write({ + ctx, + // bfahrenfort: we just generated a feedIndices entry for every directories entry, guaranteed non-null + content: generateRSSFeed(cfg, feedIndices.get(feed)!, opts?.rssLimit), + slug: feed as FullSlug, + ext: ".xml", + }), + ) }) + emitted.push(...(await Promise.all(feedPromises))) } const fp = joinSegments("static", "contentIndex") as FullSlug From 9fe604ba182e04851bd2a09ba8ae46a9b759ae84 Mon Sep 17 00:00:00 2001 From: bfahrenfort Date: Wed, 14 Feb 2024 13:19:28 -0600 Subject: [PATCH 2/4] Early return if index.md is not present --- quartz/plugins/emitters/contentIndex.ts | 41 +++++++++++++++---------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/quartz/plugins/emitters/contentIndex.ts b/quartz/plugins/emitters/contentIndex.ts index 4c5649b7b..f17834009 100644 --- a/quartz/plugins/emitters/contentIndex.ts +++ b/quartz/plugins/emitters/contentIndex.ts @@ -8,6 +8,7 @@ import { toHtml } from "hast-util-to-html" import { write } from "./helpers" import { i18n } from "../../i18n" import DepGraph from "../../depgraph" +import chalk from "chalk" export type ContentIndex = Map export type ContentDetails = { @@ -23,6 +24,7 @@ export type ContentDetails = { interface Options { enableSiteMap: boolean enableRSS: boolean + bypassIndexCheck: boolean rssLimit?: number rssFullHtml: boolean includeEmptyFiles: boolean @@ -30,6 +32,7 @@ interface Options { } const defaultOptions: Options = { + bypassIndexCheck: false, enableSiteMap: true, enableRSS: true, rssLimit: 10, @@ -116,8 +119,18 @@ export const ContentIndex: QuartzEmitterPlugin> = (opts) => { return graph }, async emit(ctx, content, _resources) { + // If we're missing an index file, don't bother with sitemap/RSS gen + if (!(opts?.bypassIndexCheck || "index" in content.filter(c => c[1].data.slug!))) { + console.warn(chalk.yellow(`Warning: contentIndex: + content/ folder is missing an index.md. RSS feeds and sitemap will not be generated. + If you still wish to generate these files, add: + bypassIndexCheck: true, + to your configuration for Plugin.ContentIndex({...}) in quartz.config.ts`)) + return [] + } + const cfg = ctx.cfg.configuration - const emitted: FilePath[] = [] + const emitted: Promise[] = [] const feedIndices: Map = new Map() for (const feed of opts?.feedDirectories!) { @@ -149,7 +162,7 @@ export const ContentIndex: QuartzEmitterPlugin> = (opts) => { const siteFeed = feedIndices.get("index")! if (opts?.enableSiteMap) { emitted.push( - await write({ + write({ ctx, // bfahrenfort: "index" is guaranteed non-null // see directories instantiation and feedIndices.set iterating over directories @@ -161,24 +174,20 @@ export const ContentIndex: QuartzEmitterPlugin> = (opts) => { } if (opts?.enableRSS) { - var feedPromises: Promise[] = [] opts.feedDirectories!.map((feed) => { - feedPromises.push( - write({ - ctx, - // bfahrenfort: we just generated a feedIndices entry for every directories entry, guaranteed non-null - content: generateRSSFeed(cfg, feedIndices.get(feed)!, opts?.rssLimit), - slug: feed as FullSlug, - ext: ".xml", - }), - ) + emitted.push(write({ + ctx, + // bfahrenfort: we just generated a feedIndices entry for every directories entry, guaranteed non-null + content: generateRSSFeed(cfg, feedIndices.get(feed)!, opts?.rssLimit), + slug: feed as FullSlug, + ext: ".xml", + })) }) - emitted.push(...(await Promise.all(feedPromises))) } const fp = joinSegments("static", "contentIndex") as FullSlug const simplifiedIndex = Object.fromEntries( - Array.from(feedIndices.get("index") ?? []).map(([slug, content]) => { + Array.from(feedIndices.get("index")!).map(([slug, content]) => { // remove description and from content index as nothing downstream // actually uses it. we only keep it in the index as we need it // for the RSS feed @@ -189,7 +198,7 @@ export const ContentIndex: QuartzEmitterPlugin> = (opts) => { ) emitted.push( - await write({ + write({ ctx, content: JSON.stringify(simplifiedIndex), slug: fp, @@ -197,7 +206,7 @@ export const ContentIndex: QuartzEmitterPlugin> = (opts) => { }), ) - return emitted + return await Promise.all(emitted) }, getQuartzComponents: () => [], } From a674a6e567bd5b77cd74402d6412958e93336fdd Mon Sep 17 00:00:00 2001 From: bfahrenfort Date: Wed, 14 Feb 2024 13:47:09 -0600 Subject: [PATCH 3/4] fix: map vs filter --- quartz/plugins/emitters/contentIndex.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/quartz/plugins/emitters/contentIndex.ts b/quartz/plugins/emitters/contentIndex.ts index f17834009..03e95c52e 100644 --- a/quartz/plugins/emitters/contentIndex.ts +++ b/quartz/plugins/emitters/contentIndex.ts @@ -120,12 +120,13 @@ export const ContentIndex: QuartzEmitterPlugin> = (opts) => { }, async emit(ctx, content, _resources) { // If we're missing an index file, don't bother with sitemap/RSS gen - if (!(opts?.bypassIndexCheck || "index" in content.filter(c => c[1].data.slug!))) { + if (!(opts?.bypassIndexCheck || "index" in content.map(c => c[1].data.slug!))) { console.warn(chalk.yellow(`Warning: contentIndex: content/ folder is missing an index.md. RSS feeds and sitemap will not be generated. If you still wish to generate these files, add: bypassIndexCheck: true, - to your configuration for Plugin.ContentIndex({...}) in quartz.config.ts`)) + to your configuration for Plugin.ContentIndex({...}) in quartz.config.ts. + Don't do this unless you know what you're doing!`)) return [] } From edc51f75abf7306efa3487eebb07ef0209ad0179 Mon Sep 17 00:00:00 2001 From: bfahrenfort Date: Wed, 14 Feb 2024 13:50:14 -0600 Subject: [PATCH 4/4] lint: format --- quartz/plugins/emitters/contentIndex.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/quartz/plugins/emitters/contentIndex.ts b/quartz/plugins/emitters/contentIndex.ts index 03e95c52e..be6f82fa6 100644 --- a/quartz/plugins/emitters/contentIndex.ts +++ b/quartz/plugins/emitters/contentIndex.ts @@ -120,13 +120,15 @@ export const ContentIndex: QuartzEmitterPlugin> = (opts) => { }, async emit(ctx, content, _resources) { // If we're missing an index file, don't bother with sitemap/RSS gen - if (!(opts?.bypassIndexCheck || "index" in content.map(c => c[1].data.slug!))) { - console.warn(chalk.yellow(`Warning: contentIndex: + if (!(opts?.bypassIndexCheck || "index" in content.map((c) => c[1].data.slug!))) { + console.warn( + chalk.yellow(`Warning: contentIndex: content/ folder is missing an index.md. RSS feeds and sitemap will not be generated. If you still wish to generate these files, add: bypassIndexCheck: true, to your configuration for Plugin.ContentIndex({...}) in quartz.config.ts. - Don't do this unless you know what you're doing!`)) + Don't do this unless you know what you're doing!`), + ) return [] } @@ -176,13 +178,15 @@ export const ContentIndex: QuartzEmitterPlugin> = (opts) => { if (opts?.enableRSS) { opts.feedDirectories!.map((feed) => { - emitted.push(write({ - ctx, - // bfahrenfort: we just generated a feedIndices entry for every directories entry, guaranteed non-null - content: generateRSSFeed(cfg, feedIndices.get(feed)!, opts?.rssLimit), - slug: feed as FullSlug, - ext: ".xml", - })) + emitted.push( + write({ + ctx, + // bfahrenfort: we just generated a feedIndices entry for every directories entry, guaranteed non-null + content: generateRSSFeed(cfg, feedIndices.get(feed)!, opts?.rssLimit), + slug: feed as FullSlug, + ext: ".xml", + }), + ) }) }