Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dates): improve date string parsing #1646

Open
wants to merge 5 commits into
base: v4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ This part of the configuration concerns anything that can affect the whole site.
- Note that Quartz 4 will avoid using this as much as possible and use relative URLs whenever it can to make sure your site works no matter _where_ you end up actually deploying it.
- `ignorePatterns`: a list of [glob](<https://en.wikipedia.org/wiki/Glob_(programming)>) patterns that Quartz should ignore and not search through when looking for files inside the `content` folder. See [[private pages]] for more details.
- `defaultDateType`: whether to use created, modified, or published as the default date to display on pages and page listings.
- Can be a list (e.g. `["created", "modified"]`) to define fallbacks (highest priority first).
- `theme`: configure how the site looks.
- `cdnCaching`: If `true` (default), use Google CDN to cache the fonts. This will generally will be faster. Disable (`false`) this if you want Quartz to download the fonts to be self-contained.
- `typography`: what fonts to use. Any font available on [Google Fonts](https://fonts.google.com/) works here.
Expand Down
1 change: 1 addition & 0 deletions docs/plugins/CreatedModifiedDate.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This plugin determines the created, modified, and published dates for a document
This plugin accepts the following configuration options:

- `priority`: The data sources to consult for date information. Highest priority first. Possible values are `"frontmatter"`, `"git"`, and `"filesystem"`. Defaults to `["frontmatter", "git", "filesystem"]`.
- `defaultTimezone`: The timezone that is assumed (IANA format, e.g. `Africa/Algiers`) when the datetime frontmatter properties do not contain offsets/timezones. Defaults to `system`: the system's local timezone.

> [!warning]
> If you rely on `git` for dates, make sure `defaultDateType` is set to `modified` in `quartz.config.ts`.
Expand Down
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"is-absolute-url": "^4.0.1",
"js-yaml": "^4.1.0",
"lightningcss": "^1.28.2",
"luxon": "^3.5.0",
"mdast-util-find-and-replace": "^3.0.1",
"mdast-util-to-hast": "^13.2.0",
"mdast-util-to-string": "^4.0.0",
Expand Down Expand Up @@ -103,6 +104,7 @@
"@types/d3": "^7.4.3",
"@types/hast": "^3.0.4",
"@types/js-yaml": "^4.0.9",
"@types/luxon": "^3.4.2",
"@types/node": "^22.10.2",
"@types/pretty-time": "^1.1.5",
"@types/source-map-support": "^0.5.10",
Expand Down
3 changes: 3 additions & 0 deletions quartz/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { options } from "./util/sourcemap"
import { Mutex } from "async-mutex"
import DepGraph from "./depgraph"
import { getStaticResourcesFromPlugins } from "./plugins"
import { Settings as LuxonSettings } from "luxon"

type Dependencies = Record<string, DepGraph<FilePath> | null>

Expand Down Expand Up @@ -53,6 +54,8 @@ async function buildQuartz(argv: Argv, mut: Mutex, clientRefresh: () => void) {
const perf = new PerfTimer()
const output = argv.output

LuxonSettings.defaultLocale = cfg.configuration.locale

const pluginCount = Object.values(cfg.plugins).flat().length
const pluginNames = (key: "transformers" | "filters" | "emitters") =>
cfg.plugins[key].map((plugin) => plugin.name)
Expand Down
2 changes: 1 addition & 1 deletion quartz/cfg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface GlobalConfiguration {
/** Glob patterns to not search */
ignorePatterns: string[]
/** Whether to use created, modified, or published as the default type of date */
defaultDateType: ValidDateType
defaultDateType: ValidDateType | ValidDateType[]
/** 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
*/
Expand Down
5 changes: 3 additions & 2 deletions quartz/components/ContentMeta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export default ((opts?: Partial<ContentMetaOptions>) => {
if (text) {
const segments: (string | JSX.Element)[] = []

if (fileData.dates) {
segments.push(<Date date={getDate(cfg, fileData)!} locale={cfg.locale} />)
const date = getDate(cfg, fileData)
if (date) {
segments.push(<Date date={date} locale={cfg.locale} />)
}

// Display reading time if enabled
Expand Down
25 changes: 15 additions & 10 deletions quartz/components/Date.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
import { DateTime } from "luxon"
import { GlobalConfiguration } from "../cfg"
import { ValidLocale } from "../i18n"
import { QuartzPluginData } from "../plugins/vfile"

interface Props {
date: Date
date: DateTime
locale?: ValidLocale
}

export type ValidDateType = keyof Required<QuartzPluginData>["dates"]

export function getDate(cfg: GlobalConfiguration, data: QuartzPluginData): Date | undefined {
export function getDate(cfg: GlobalConfiguration, data: QuartzPluginData): DateTime | undefined {
if (!cfg.defaultDateType) {
throw new Error(
`Field 'defaultDateType' was not set in the configuration object of quartz.config.ts. See https://quartz.jzhao.xyz/configuration#general-configuration for more details.`,
)
}
return data.dates?.[cfg.defaultDateType]
const types = cfg.defaultDateType instanceof Array ? cfg.defaultDateType : [cfg.defaultDateType]
return types.map((p) => data.dates?.[p]).find((p) => p != null)
}

export function formatDate(d: Date, locale: ValidLocale = "en-US"): string {
return d.toLocaleDateString(locale, {
year: "numeric",
month: "short",
day: "2-digit",
})
export function formatDate(d: DateTime, locale: ValidLocale = "en-US"): string {
return d.toLocaleString(
{
year: "numeric",
month: "short",
day: "2-digit",
},
{ locale: locale },
)
}

export function Date({ date, locale }: Props) {
return <time datetime={date.toISOString()}>{formatDate(date, locale)}</time>
return <time datetime={date.toISO() || ""}>{formatDate(date, locale)}</time>
}
20 changes: 9 additions & 11 deletions quartz/components/PageList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ export type SortFn = (f1: QuartzPluginData, f2: QuartzPluginData) => number

export function byDateAndAlphabetical(cfg: GlobalConfiguration): SortFn {
return (f1, f2) => {
if (f1.dates && f2.dates) {
const f1Date = getDate(cfg, f1)
const f2Date = getDate(cfg, f2)
if (f1Date && f2Date) {
// sort descending
return getDate(cfg, f2)!.getTime() - getDate(cfg, f1)!.getTime()
} else if (f1.dates && !f2.dates) {
return f2Date.toMillis() - f1Date.toMillis()
} else if (f1Date && !f2Date) {
// prioritize files with dates
return -1
} else if (!f1.dates && f2.dates) {
} else if (!f1Date && f2Date) {
return 1
}

Expand All @@ -32,23 +34,19 @@ type Props = {

export const PageList: QuartzComponent = ({ cfg, fileData, allFiles, limit, sort }: Props) => {
const sorter = sort ?? byDateAndAlphabetical(cfg)
let list = allFiles.sort(sorter)
if (limit) {
list = list.slice(0, limit)
}
const list = allFiles.toSorted(sorter).slice(0, limit ?? allFiles.length)

return (
<ul class="section-ul">
{list.map((page) => {
const title = page.frontmatter?.title
const tags = page.frontmatter?.tags ?? []
const date = getDate(cfg, page)

return (
<li class="section-li">
<div class="section">
<p class="meta">
{page.dates && <Date date={getDate(cfg, page)!} locale={cfg.locale} />}
</p>
<p class="meta">{date && <Date date={date} locale={cfg.locale} />}</p>
<div class="desc">
<h3>
<a href={resolveRelative(fileData.slug!, page.slug!)} class="internal">
Expand Down
7 changes: 2 additions & 5 deletions quartz/components/RecentNotes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default ((userOpts?: Partial<Options>) => {
{pages.slice(0, opts.limit).map((page) => {
const title = page.frontmatter?.title ?? i18n(cfg.locale).propertyDefaults.title
const tags = page.frontmatter?.tags ?? []
const date = getDate(cfg, page)

return (
<li class="recent-li">
Expand All @@ -53,11 +54,7 @@ export default ((userOpts?: Partial<Options>) => {
</a>
</h3>
</div>
{page.dates && (
<p class="meta">
<Date date={getDate(cfg, page)!} locale={cfg.locale} />
</p>
)}
<p class="meta">{date && <Date date={date} locale={cfg.locale} />}</p>
{opts.showTags && (
<ul class="tags">
{tags.map((tag) => (
Expand Down
Loading
Loading