mirror of
https://github.com/jackyzha0/quartz
synced 2026-11-27 22:14:40 +01:00
fix: leave the date plugin's stand-in (build-time) dates out of contentIndex.json and article meta
This commit is contained in:
6 files changed
+159
-25
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@jackyzha0/quartz": patch
|
||||
---
|
||||
|
||||
Build-time stand-in dates are no longer written as real dates. A page whose date no source gives (for example `defaultDateType: published` and no `published:`, or an invalid one) gets the build time from the date plugin; `contentIndex.json` now leaves its `date` out, and `article:published_time` / `article:modified_time` are only set for real dates (an invalid `published:` gave the build time). RSS and the sitemap keep the build time. created-modified-date 1.0.5 lists its stand-ins in `file.data.fallbackDates`; with older versions a `published` date only counts when the frontmatter sets it.
|
||||
@@ -4,6 +4,7 @@ import { CSSResourceToStyleElement, JSResourceToScriptElement } from "../util/re
|
||||
import { googleFontHref, googleFontSubsetHref } from "../util/theme"
|
||||
import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "./types"
|
||||
import { unescapeHTML } from "../util/escape"
|
||||
import { realDate } from "../util/dates"
|
||||
|
||||
export default (() => {
|
||||
const Head: QuartzComponent = ({
|
||||
@@ -39,12 +40,10 @@ export default (() => {
|
||||
fileData.relativePath !== undefined &&
|
||||
!fileData.slug!.endsWith("index") &&
|
||||
!fileData.slug!.startsWith("tags/")
|
||||
const isoDate = (d: unknown) =>
|
||||
d instanceof Date && !isNaN(d.getTime()) ? d.toISOString() : undefined
|
||||
// Without a frontmatter date, the published date is only the build time.
|
||||
const published =
|
||||
isArticle && fileData.frontmatter?.published ? isoDate(fileData.dates?.published) : undefined
|
||||
const modified = isArticle ? isoDate(fileData.dates?.modified) : undefined
|
||||
// Real dates only: a date no source gave is the build time (see util/dates), and an
|
||||
// invalid `published:` gives none.
|
||||
const published = isArticle ? realDate(fileData, "published")?.toISOString() : undefined
|
||||
const modified = isArticle ? realDate(fileData, "modified")?.toISOString() : undefined
|
||||
|
||||
const usesCustomOgImage = ctx.cfg.plugins.emitters.some((e) => e.name === "CustomOgImages")
|
||||
const ogImageDefaultPath = `https://${cfg.baseUrl}/static/og-image.png`
|
||||
|
||||
@@ -643,6 +643,48 @@ describe("content index built-in", () => {
|
||||
}
|
||||
})
|
||||
|
||||
test("stand-in dates of the date plugin are not written to contentIndex.json", () => {
|
||||
const now = new Date()
|
||||
const published = new Date("2024-05-06T00:00:00")
|
||||
const json = JSON.parse(
|
||||
serializeContentIndex(
|
||||
buildContentIndex(ctxWith([]), [
|
||||
// created-modified-date >= 1.0.5 lists the dates it filled with the build time
|
||||
page("marked", {
|
||||
defaultDateType: "published",
|
||||
dates: { created: now, modified: now, published: now },
|
||||
fallbackDates: ["published"],
|
||||
}),
|
||||
// older versions don't: a published date only counts when frontmatter sets it
|
||||
page("old", {
|
||||
defaultDateType: "published",
|
||||
frontmatter: { title: "OLD" },
|
||||
dates: { created: now, modified: now, published: now },
|
||||
}),
|
||||
page("invalid", {
|
||||
defaultDateType: "published",
|
||||
frontmatter: { title: "INV", published: "not a date" },
|
||||
dates: { created: now, modified: now, published: now },
|
||||
}),
|
||||
page("real", {
|
||||
defaultDateType: "published",
|
||||
frontmatter: { title: "REAL", published: "2024-05-06" },
|
||||
dates: { created: now, modified: now, published },
|
||||
fallbackDates: [],
|
||||
}),
|
||||
page("oldReal", {
|
||||
defaultDateType: "published",
|
||||
frontmatter: { title: "OR", published: "2024-05-06" },
|
||||
dates: { created: now, modified: now, published },
|
||||
}),
|
||||
]),
|
||||
),
|
||||
)
|
||||
for (const slug of ["marked", "old", "invalid"]) assert.ok(!("date" in json[slug]), slug)
|
||||
assert.strictEqual(json.real.date, published.toISOString())
|
||||
assert.strictEqual(json.oldReal.date, published.toISOString())
|
||||
})
|
||||
|
||||
test("plugins add fields through extendContentIndex", () => {
|
||||
const ctx = ctxWith([
|
||||
{
|
||||
|
||||
@@ -3,7 +3,7 @@ import fs from "node:fs/promises"
|
||||
import { Root } from "hast"
|
||||
import { toHtml } from "hast-util-to-html"
|
||||
import { styleText } from "util"
|
||||
import { getDate } from "@quartz-community/utils"
|
||||
import { dateIsFromFrontmatter as dateIsFromFrontmatterOfType, realDate } from "../../util/dates"
|
||||
import { ContentIndexConfig, GlobalConfiguration } from "../../cfg"
|
||||
import { BuildCtx } from "../../util/ctx"
|
||||
import { escapeHTML } from "../../util/escape"
|
||||
@@ -152,7 +152,8 @@ export function prepareContentIndex(ctx: BuildCtx, content: ProcessedContent[]):
|
||||
}
|
||||
|
||||
/**
|
||||
* Pages without a date get the build time, so RSS and the sitemap keep working as before.
|
||||
* Pages without a date, or with one the date plugin filled in with the build time (its
|
||||
* `fallbackDates`), 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).
|
||||
*/
|
||||
@@ -164,32 +165,19 @@ function buildTimeDate(): Date {
|
||||
}
|
||||
|
||||
function entryDate(data: QuartzPluginData): Date {
|
||||
const date = getDate(data as Parameters<typeof getDate>[0])
|
||||
// a stand-in date of the date plugin (fallbackDates) is a build-time date as well
|
||||
const date = realDate(data, data.defaultDateType as string | undefined)
|
||||
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.
|
||||
* (`defaultDateType`). See util/dates.
|
||||
*/
|
||||
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()
|
||||
return dateIsFromFrontmatterOfType(data, date)
|
||||
}
|
||||
|
||||
/** Pages whose date came from frontmatter (see dateIsFromFrontmatter). */
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import test, { describe } from "node:test"
|
||||
import assert from "node:assert"
|
||||
import { realDate } from "./dates"
|
||||
import type { QuartzPluginData } from "../plugins/vfile"
|
||||
|
||||
const data = (d: Record<string, unknown>) => d as unknown as QuartzPluginData
|
||||
|
||||
describe("realDate", () => {
|
||||
const now = new Date()
|
||||
const fm = new Date("2024-05-06T00:00:00")
|
||||
|
||||
test("drops dates the date plugin lists in fallbackDates", () => {
|
||||
const d = data({ dates: { modified: now, published: now }, fallbackDates: ["published"] })
|
||||
assert.strictEqual(realDate(d, "published"), undefined)
|
||||
assert.strictEqual(realDate(d, "modified"), now)
|
||||
})
|
||||
|
||||
test("without fallbackDates (older plugin), published needs a matching frontmatter value", () => {
|
||||
assert.strictEqual(realDate(data({ dates: { published: now } }), "published"), undefined)
|
||||
assert.strictEqual(
|
||||
realDate(
|
||||
data({ frontmatter: { published: "nope" }, dates: { published: now } }),
|
||||
"published",
|
||||
),
|
||||
undefined,
|
||||
)
|
||||
assert.strictEqual(
|
||||
realDate(
|
||||
data({ frontmatter: { published: "2024-05-06" }, dates: { published: fm } }),
|
||||
"published",
|
||||
),
|
||||
fm,
|
||||
)
|
||||
assert.strictEqual(realDate(data({ dates: { modified: now } }), "modified"), now)
|
||||
})
|
||||
|
||||
test("missing and invalid dates", () => {
|
||||
assert.strictEqual(realDate(data({}), "modified"), undefined)
|
||||
assert.strictEqual(
|
||||
realDate(data({ dates: { modified: new Date(NaN) } }), "modified"),
|
||||
undefined,
|
||||
)
|
||||
assert.strictEqual(realDate(data({ dates: { modified: now } }), undefined), undefined)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,55 @@
|
||||
import { QuartzPluginData } from "../plugins/vfile"
|
||||
|
||||
export type DateType = "created" | "modified" | "published"
|
||||
|
||||
declare module "vfile" {
|
||||
interface DataMap {
|
||||
/**
|
||||
* Dates in `dates` that no source gave, so the date plugin filled them with the build time
|
||||
* (created-modified-date ≥ 1.0.5). Absent with older versions.
|
||||
*/
|
||||
fallbackDates: DateType[]
|
||||
}
|
||||
}
|
||||
|
||||
const DATE_ONLY = /^\d{4}-\d{2}-\d{2}$/
|
||||
|
||||
/**
|
||||
* Whether the page's `dates[type]` is the one its frontmatter sets for that type (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,
|
||||
type: string | undefined = data.defaultDateType as string | undefined,
|
||||
): boolean {
|
||||
if (!(date instanceof Date) || isNaN(date.getTime())) return false
|
||||
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()
|
||||
}
|
||||
|
||||
/**
|
||||
* The page's date of this type when it is a real one, `undefined` when it is missing, invalid
|
||||
* or a stand-in (the build time the date plugin fills in for a date no source gave).
|
||||
*
|
||||
* The date plugin lists stand-ins in `fallbackDates`. Older versions don't, so without that
|
||||
* list a `published` date only counts when it is the frontmatter's (frontmatter is its only
|
||||
* source); `created` and `modified` are taken as they are.
|
||||
*/
|
||||
export function realDate(data: QuartzPluginData, type: string | undefined): Date | undefined {
|
||||
if (!type) return undefined
|
||||
const date = (data.dates as Record<string, unknown> | undefined)?.[type]
|
||||
if (!(date instanceof Date) || isNaN(date.getTime())) return undefined
|
||||
const fallbacks = data.fallbackDates
|
||||
if (Array.isArray(fallbacks)) return fallbacks.includes(type as DateType) ? undefined : date
|
||||
if (type === "published" && !dateIsFromFrontmatter(data, date, "published")) return undefined
|
||||
return date
|
||||
}
|
||||
Reference in new issue
Block a user