refactor: move defaultDateType to CreatedModifiedDate plugin

This commit is contained in:
saberzero1 2026-03-20 21:13:31 +01:00
parent 28414da76e
commit d9bfbf876f
No known key found for this signature in database
6 changed files with 11 additions and 23 deletions

View File

@ -1,4 +1,3 @@
import { ValidDateType } from "./components/Date"
import { QuartzComponent } from "./components/types" import { QuartzComponent } from "./components/types"
import { ValidLocale } from "./i18n" import { ValidLocale } from "./i18n"
import { PluginSpecifier } from "./plugins/loader/types" import { PluginSpecifier } from "./plugins/loader/types"
@ -68,8 +67,6 @@ export interface GlobalConfiguration {
analytics: Analytics analytics: Analytics
/** Glob patterns to not search */ /** Glob patterns to not search */
ignorePatterns: string[] ignorePatterns: string[]
/** Whether to use created, modified, or published as the default type of date */
defaultDateType: ValidDateType
/** Base URL to use for CNAME files, sitemaps, and RSS feeds that require an absolute URL. /** Base URL to use for CNAME files, sitemaps, and RSS feeds that require an absolute URL.
* Quartz will avoid using this as much as possible and use relative URLs most of the time * Quartz will avoid using this as much as possible and use relative URLs most of the time
*/ */

View File

@ -334,7 +334,6 @@ export function createConfigFromDefault() {
locale: "en-US", locale: "en-US",
baseUrl: "quartz.jzhao.xyz", baseUrl: "quartz.jzhao.xyz",
ignorePatterns: ["private", "templates", ".obsidian"], ignorePatterns: ["private", "templates", ".obsidian"],
defaultDateType: "created",
theme: { theme: {
cdnCaching: true, cdnCaching: true,
typography: { typography: {

View File

@ -1,4 +1,3 @@
import { GlobalConfiguration } from "../cfg"
import { ValidLocale } from "../i18n" import { ValidLocale } from "../i18n"
import { QuartzPluginData } from "../plugins/vfile" import { QuartzPluginData } from "../plugins/vfile"
@ -9,13 +8,13 @@ interface Props {
export type ValidDateType = keyof Required<QuartzPluginData>["dates"] export type ValidDateType = keyof Required<QuartzPluginData>["dates"]
export function getDate(cfg: GlobalConfiguration, data: QuartzPluginData): Date | undefined { export function getDate(data: QuartzPluginData): Date | undefined {
if (!cfg.defaultDateType) { if (!data.defaultDateType) {
throw new Error( throw new Error(
`Field 'defaultDateType' was not set in the configuration object of quartz.config.yaml. See https://quartz.jzhao.xyz/configuration#general-configuration for more details.`, `Field 'defaultDateType' was not set. Ensure the CreatedModifiedDate plugin is configured with a 'defaultDateType' option. See https://quartz.jzhao.xyz/plugins/CreatedModifiedDate for more details.`,
) )
} }
return data.dates?.[cfg.defaultDateType] return data.dates?.[data.defaultDateType]
} }
export function formatDate(d: Date, locale: ValidLocale = "en-US"): string { export function formatDate(d: Date, locale: ValidLocale = "en-US"): string {

View File

@ -2,16 +2,15 @@ import { FullSlug, isFolderPath, resolveRelative } from "../util/path"
import { QuartzPluginData } from "../plugins/vfile" import { QuartzPluginData } from "../plugins/vfile"
import { Date, getDate } from "./Date" import { Date, getDate } from "./Date"
import { QuartzComponent, QuartzComponentProps } from "./types" import { QuartzComponent, QuartzComponentProps } from "./types"
import { GlobalConfiguration } from "../cfg"
export type SortFn = (f1: QuartzPluginData, f2: QuartzPluginData) => number export type SortFn = (f1: QuartzPluginData, f2: QuartzPluginData) => number
export function byDateAndAlphabetical(cfg: GlobalConfiguration): SortFn { export function byDateAndAlphabetical(): SortFn {
return (f1, f2) => { return (f1, f2) => {
// Sort by date/alphabetical // Sort by date/alphabetical
if (f1.dates && f2.dates) { if (f1.dates && f2.dates) {
// sort descending // sort descending
return getDate(cfg, f2)!.getTime() - getDate(cfg, f1)!.getTime() return getDate(f2)!.getTime() - getDate(f1)!.getTime()
} else if (f1.dates && !f2.dates) { } else if (f1.dates && !f2.dates) {
// prioritize files with dates // prioritize files with dates
return -1 return -1
@ -26,7 +25,7 @@ export function byDateAndAlphabetical(cfg: GlobalConfiguration): SortFn {
} }
} }
export function byDateAndAlphabeticalFolderFirst(cfg: GlobalConfiguration): SortFn { export function byDateAndAlphabeticalFolderFirst(): SortFn {
return (f1, f2) => { return (f1, f2) => {
// Sort folders first // Sort folders first
const f1IsFolder = isFolderPath(f1.slug ?? "") const f1IsFolder = isFolderPath(f1.slug ?? "")
@ -37,7 +36,7 @@ export function byDateAndAlphabeticalFolderFirst(cfg: GlobalConfiguration): Sort
// If both are folders or both are files, sort by date/alphabetical // If both are folders or both are files, sort by date/alphabetical
if (f1.dates && f2.dates) { if (f1.dates && f2.dates) {
// sort descending // sort descending
return getDate(cfg, f2)!.getTime() - getDate(cfg, f1)!.getTime() return getDate(f2)!.getTime() - getDate(f1)!.getTime()
} else if (f1.dates && !f2.dates) { } else if (f1.dates && !f2.dates) {
// prioritize files with dates // prioritize files with dates
return -1 return -1
@ -58,7 +57,7 @@ type Props = {
} & QuartzComponentProps } & QuartzComponentProps
export const PageList: QuartzComponent = ({ cfg, fileData, allFiles, limit, sort }: Props) => { export const PageList: QuartzComponent = ({ cfg, fileData, allFiles, limit, sort }: Props) => {
const sorter = sort ?? byDateAndAlphabeticalFolderFirst(cfg) const sorter = sort ?? byDateAndAlphabeticalFolderFirst()
let list = allFiles.sort(sorter) let list = allFiles.sort(sorter)
if (limit) { if (limit) {
list = list.slice(0, limit) list = list.slice(0, limit)
@ -73,9 +72,7 @@ export const PageList: QuartzComponent = ({ cfg, fileData, allFiles, limit, sort
return ( return (
<li class="section-li"> <li class="section-li">
<div class="section"> <div class="section">
<p class="meta"> <p class="meta">{page.dates && <Date date={getDate(page)!} locale={cfg.locale} />}</p>
{page.dates && <Date date={getDate(cfg, page)!} locale={cfg.locale} />}
</p>
<div class="desc"> <div class="desc">
<h3> <h3>
<a href={resolveRelative(fileData.slug!, page.slug!)} class="internal"> <a href={resolveRelative(fileData.slug!, page.slug!)} class="internal">

View File

@ -95,5 +95,6 @@ declare module "vfile" {
modified: Date modified: Date
published: Date published: Date
} }
defaultDateType?: "created" | "modified" | "published"
} }
} }

View File

@ -174,11 +174,6 @@
"type": "string" "type": "string"
}, },
"description": "Patterns to ignore during processing" "description": "Patterns to ignore during processing"
},
"defaultDateType": {
"type": "string",
"enum": ["created", "modified", "published"],
"description": "Default date type for pages"
} }
} }
}, },