feat(content-index): write the page date to contentIndex.json (quartz-community/explorer#5)

This commit is contained in:
saberzero1 committed 2026-11-26 15:54:27 +01:00
1 parent 773e2c7fe1
commit d5dad620d8
5 files changed
+48 -7

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@jackyzha0/quartz": minor
---
`contentIndex.json` entries now include the page's `date` (the one `defaultDateType` selects) as an ISO 8601 string, so client-side components such as the explorer can sort by date. Pages without a date have no `date` field.
+4
View File
@@ -36,6 +36,10 @@ Options:
> [!note]
> An existing `@quartz-community/content-index` entry is applied to `configuration.contentIndex` automatically. Run `npx quartz plugin doctor --fix` to move it.
## `contentIndex.json`
Each entry of `static/contentIndex.json` is keyed by slug and holds `slug`, `filePath`, `title`, `links`, `tags` and `content` (the page's text), plus `date` when the page has one: the date selected by `defaultDateType` of [[CreatedModifiedDate]] (the same date [[ContentMeta]] shows and the RSS feed uses), as an ISO 8601 string such as `"2024-01-02T03:04:05.000Z"`. Client-side components can use it to sort by date, e.g. `new Date(entry.date)`.
## Adding fields
Plugins can add their own fields to each entry of `contentIndex.json` with `extendContentIndex` — see [[making plugins#Extending the content index]].
+13
View File
@@ -554,6 +554,19 @@ describe("content index built-in", () => {
])
})
test("writes the page date to contentIndex.json as an ISO string (explorer#5)", () => {
const created = new Date("2024-01-02T03:04:05.000Z")
const modified = new Date("2025-06-07T08:09:10.000Z")
const dated = page("dated", { defaultDateType: "created", dates: { created, modified } })
const idx = buildContentIndex(ctxWith([]), [dated, page("undated")])
const json = JSON.parse(serializeContentIndex(idx))
assert.strictEqual(json.dated.date, "2024-01-02T03:04:05.000Z")
assert.ok(!("description" in json.dated))
// pages without a date keep the build-time stand-in for RSS, but not in the JSON
assert.ok(idx.get("undated" as FullSlug)!.date instanceof Date)
assert.ok(!("date" in json.undated))
})
test("plugins add fields through extendContentIndex", () => {
const ctx = ctxWith([
{
+22 -6
View File
@@ -150,6 +150,18 @@ export function prepareContentIndex(ctx: BuildCtx, content: ProcessedContent[]):
return index
}
/**
* Pages without a date get the build time, so RSS and the sitemap keep working as before.
* Those stand-in dates are remembered here and not written to contentIndex.json, where a
* build-time date would look like a real one to client-side sorting (explorer, recent notes).
*/
const buildTimeDates = new WeakSet<Date>()
function buildTimeDate(): Date {
const date = new Date()
buildTimeDates.add(date)
return date
}
/** Build the index from processed content (also used by tests and potentially other emitters). */
export function buildContentIndex(
ctx: BuildCtx,
@@ -176,7 +188,7 @@ export function buildContentIndex(
options.rssFullHtml && data.encrypted !== true
? escapeHTML(toHtml(tree as Root, { allowDangerousHtml: true }))
: undefined,
date: getDate(data as Parameters<typeof getDate>[0]) ?? new Date(),
date: getDate(data as Parameters<typeof getDate>[0]) ?? buildTimeDate(),
description: (data.description as string | undefined) ?? "",
}
index.set(slug, applyContentIndexExtensions(ctx, extensions, entry, data))
@@ -240,13 +252,17 @@ function generateRSSFeed(cfg: GlobalConfiguration, idx: ContentIndexMap, limit?:
</rss>`
}
/** Entries as written to contentIndex.json (date and description are RSS/sitemap-only). */
/**
* Entries as written to contentIndex.json. `date` is the page's date (the one
* `defaultDateType` selects, as shown by ContentMeta and used by RSS) as an ISO 8601 string,
* left out for pages without a date. `description` is RSS-only.
*/
export function serializeContentIndex(idx: ContentIndexMap): string {
const simplified = Object.fromEntries(
Array.from(idx).map(([slug, { date: _date, description: _description, ...rest }]) => [
slug,
rest,
]),
Array.from(idx).map(([slug, { date, description: _description, ...rest }]) => {
const hasDate = date instanceof Date && !isNaN(date.getTime()) && !buildTimeDates.has(date)
return [slug, hasDate ? { ...rest, date: date.toISOString() } : rest]
}),
)
return JSON.stringify(simplified)
}
+4 -1
View File
@@ -31,7 +31,10 @@ export type ContentDetails = {
tags: string[]
content: string
richContent?: string
/** RSS / sitemap only — not written to contentIndex.json. */
/**
* The page's date (see `defaultDateType`). Written to contentIndex.json as an ISO 8601
* string, and only for pages that have a date.
*/
date?: Date
/** RSS / sitemap only — not written to contentIndex.json. */
description?: string