Early return if index.md is not present

This commit is contained in:
bfahrenfort 2024-02-14 13:19:28 -06:00
parent a8c0ffe721
commit 9fe604ba18

View File

@ -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<FullSlug, ContentDetails>
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<Partial<Options>> = (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<FilePath>[] = []
const feedIndices: Map<string, ContentIndex> = new Map()
for (const feed of opts?.feedDirectories!) {
@ -149,7 +162,7 @@ export const ContentIndex: QuartzEmitterPlugin<Partial<Options>> = (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<Partial<Options>> = (opts) => {
}
if (opts?.enableRSS) {
var feedPromises: Promise<FilePath>[] = []
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<Partial<Options>> = (opts) => {
)
emitted.push(
await write({
write({
ctx,
content: JSON.stringify(simplifiedIndex),
slug: fp,
@ -197,7 +206,7 @@ export const ContentIndex: QuartzEmitterPlugin<Partial<Options>> = (opts) => {
}),
)
return emitted
return await Promise.all(emitted)
},
getQuartzComponents: () => [],
}