mirror of
https://github.com/jackyzha0/quartz
synced 2026-11-27 22:14:40 +01:00
Merge pull request #2703 from jackyzha0/dev-mode/content-index-date
feat(content-index): write the page date to contentIndex.json (quartz-community/explorer#5)
This commit is contained in:
6 files changed
+132
-7
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@jackyzha0/quartz": minor
|
||||
---
|
||||
|
||||
`contentIndex.json` entries have `dateFromFrontmatter: true` when their `date` is the one set in the page's frontmatter for the configured date type (not from git or the file system), so client-side components can tell explicit dates apart, e.g. for folder dates in the explorer.
|
||||
@@ -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.
|
||||
@@ -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)`. When that date is the one the page's frontmatter sets for the configured date type (e.g. `modified:` / `lastmod:` with `defaultDateType: modified`), rather than one from git or the file system, the entry also has `dateFromFrontmatter: true`; otherwise the field is absent.
|
||||
|
||||
## Adding fields
|
||||
|
||||
Plugins can add their own fields to each entry of `contentIndex.json` with `extendContentIndex` — see [[making plugins#Extending the content index]].
|
||||
|
||||
@@ -592,6 +592,57 @@ 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("dateFromFrontmatter marks dates set in frontmatter for the configured type", () => {
|
||||
const fmModified = new Date("2024-03-04T00:00:00") // local midnight, like the date plugin
|
||||
const gitModified = new Date("2025-01-01T10:00:00.000Z")
|
||||
const created = new Date("2020-01-01T00:00:00.000Z")
|
||||
const fromFm = page("fm", {
|
||||
defaultDateType: "modified",
|
||||
frontmatter: { title: "FM", modified: "2024-03-04" },
|
||||
dates: { created, modified: fmModified },
|
||||
})
|
||||
const fromGit = page("git", {
|
||||
defaultDateType: "modified",
|
||||
frontmatter: { title: "GIT", created: "2020-01-01T00:00:00.000Z" },
|
||||
dates: { created, modified: gitModified },
|
||||
})
|
||||
// frontmatter has a date, but for another type than the configured one
|
||||
const otherType = page("other", {
|
||||
defaultDateType: "created",
|
||||
frontmatter: { title: "O", modified: "2024-03-04" },
|
||||
dates: { created: gitModified, modified: fmModified },
|
||||
})
|
||||
// frontmatter value lost to a higher-priority source (date differs)
|
||||
const overridden = page("over", {
|
||||
defaultDateType: "modified",
|
||||
frontmatter: { title: "OV", modified: "2023-01-01" },
|
||||
dates: { created, modified: gitModified },
|
||||
})
|
||||
const json = JSON.parse(
|
||||
serializeContentIndex(
|
||||
buildContentIndex(ctxWith([]), [fromFm, fromGit, otherType, overridden, page("undated")]),
|
||||
),
|
||||
)
|
||||
assert.strictEqual(json.fm.dateFromFrontmatter, true)
|
||||
assert.strictEqual(json.fm.date, fmModified.toISOString())
|
||||
for (const slug of ["git", "other", "over", "undated"]) {
|
||||
assert.ok(!("dateFromFrontmatter" in json[slug]), slug)
|
||||
}
|
||||
})
|
||||
|
||||
test("plugins add fields through extendContentIndex", () => {
|
||||
const ctx = ctxWith([
|
||||
{
|
||||
|
||||
@@ -53,6 +53,7 @@ export const CORE_CONTENT_INDEX_FIELDS = new Set([
|
||||
"content",
|
||||
"richContent",
|
||||
"date",
|
||||
"dateFromFrontmatter",
|
||||
"description",
|
||||
])
|
||||
|
||||
@@ -150,6 +151,50 @@ 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
|
||||
}
|
||||
|
||||
function entryDate(data: QuartzPluginData): Date {
|
||||
const date = getDate(data as Parameters<typeof getDate>[0])
|
||||
if (!date) return buildTimeDate()
|
||||
if (dateIsFromFrontmatter(data, date)) frontmatterDates.add(date)
|
||||
return date
|
||||
}
|
||||
|
||||
const DATE_ONLY = /^\d{4}-\d{2}-\d{2}$/
|
||||
|
||||
/**
|
||||
* Whether the page's `date` is the one its frontmatter sets for the configured date type
|
||||
* (`defaultDateType`: created / modified / published, after the frontmatter built-in's
|
||||
* aliases such as `date` or `lastmod`), rather than one from git or the file system. Compared
|
||||
* by value, so it holds whatever `priority` the date plugin uses. Date-only values
|
||||
* (`2024-01-02`) are local midnight, as the date plugin reads them.
|
||||
*/
|
||||
export function dateIsFromFrontmatter(data: QuartzPluginData, date: Date | undefined): boolean {
|
||||
if (!(date instanceof Date) || isNaN(date.getTime())) return false
|
||||
const type = data.defaultDateType as string | undefined
|
||||
const frontmatter = data.frontmatter as Record<string, unknown> | undefined
|
||||
if (!type || !frontmatter) return false
|
||||
let raw = frontmatter[type]
|
||||
if (raw === undefined || raw === null || raw === "") return false
|
||||
if (typeof raw === "string" && DATE_ONLY.test(raw)) raw = `${raw}T00:00:00`
|
||||
if (!(raw instanceof Date) && typeof raw !== "string" && typeof raw !== "number") return false
|
||||
const fromFrontmatter = new Date(raw)
|
||||
return !isNaN(fromFrontmatter.getTime()) && fromFrontmatter.getTime() === date.getTime()
|
||||
}
|
||||
|
||||
/** Pages whose date came from frontmatter (see dateIsFromFrontmatter). */
|
||||
const frontmatterDates = new WeakSet<Date>()
|
||||
|
||||
/** Build the index from processed content (also used by tests and potentially other emitters). */
|
||||
export function buildContentIndex(
|
||||
ctx: BuildCtx,
|
||||
@@ -176,7 +221,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: entryDate(data),
|
||||
description: (data.description as string | undefined) ?? "",
|
||||
}
|
||||
index.set(slug, applyContentIndexExtensions(ctx, extensions, entry, data))
|
||||
@@ -240,13 +285,25 @@ 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. `dateFromFrontmatter: true` is added when that date is the
|
||||
* one the page's frontmatter sets for the date type (absent otherwise). `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)
|
||||
if (!hasDate) return [slug, rest]
|
||||
return [
|
||||
slug,
|
||||
frontmatterDates.has(date)
|
||||
? { ...rest, date: date.toISOString(), dateFromFrontmatter: true }
|
||||
: { ...rest, date: date.toISOString() },
|
||||
]
|
||||
}),
|
||||
)
|
||||
return JSON.stringify(simplified)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user