mirror of
https://github.com/jackyzha0/quartz
synced 2026-11-27 22:14:40 +01:00
feat(content-index): dateFromFrontmatter marks dates set in frontmatter
This commit is contained in:
4 files changed
+88
-4
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.
|
||||
@@ -38,7 +38,7 @@ Options:
|
||||
|
||||
## `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)`.
|
||||
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
|
||||
|
||||
|
||||
@@ -567,6 +567,44 @@ describe("content index built-in", () => {
|
||||
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",
|
||||
])
|
||||
|
||||
@@ -162,6 +163,38 @@ function buildTimeDate(): 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,
|
||||
@@ -188,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]) ?? buildTimeDate(),
|
||||
date: entryDate(data),
|
||||
description: (data.description as string | undefined) ?? "",
|
||||
}
|
||||
index.set(slug, applyContentIndexExtensions(ctx, extensions, entry, data))
|
||||
@@ -255,13 +288,21 @@ function generateRSSFeed(cfg: GlobalConfiguration, idx: ContentIndexMap, limit?:
|
||||
/**
|
||||
* 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.
|
||||
* 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, description: _description, ...rest }]) => {
|
||||
const hasDate = date instanceof Date && !isNaN(date.getTime()) && !buildTimeDates.has(date)
|
||||
return [slug, hasDate ? { ...rest, date: date.toISOString() } : rest]
|
||||
if (!hasDate) return [slug, rest]
|
||||
return [
|
||||
slug,
|
||||
frontmatterDates.has(date)
|
||||
? { ...rest, date: date.toISOString(), dateFromFrontmatter: true }
|
||||
: { ...rest, date: date.toISOString() },
|
||||
]
|
||||
}),
|
||||
)
|
||||
return JSON.stringify(simplified)
|
||||
|
||||
Reference in new issue
Block a user