fix(dates): improve handling of missing dates

Make date handling more consistent such that file dates are optional
everywhere, i.e. dates are not rendered unless the CreatedModifiedDate
plugin sourced the configured date type.
This commit is contained in:
Bao Trinh 2024-12-04 08:39:27 -06:00
parent 9a967e6d0c
commit beec5e0cbb
No known key found for this signature in database
GPG Key ID: 66B50C2AD65984B2
4 changed files with 21 additions and 24 deletions

View File

@ -29,8 +29,9 @@ export default ((opts?: Partial<ContentMetaOptions>) => {
if (text) { if (text) {
const segments: (string | JSX.Element)[] = [] const segments: (string | JSX.Element)[] = []
if (fileData.dates) { const date = getDate(cfg, fileData)
segments.push(<Date date={getDate(cfg, fileData)!} locale={cfg.locale} />) if (date) {
segments.push(<Date date={date} locale={cfg.locale} />)
} }
// Display reading time if enabled // Display reading time if enabled

View File

@ -8,13 +8,15 @@ export type SortFn = (f1: QuartzPluginData, f2: QuartzPluginData) => number
export function byDateAndAlphabetical(cfg: GlobalConfiguration): SortFn { export function byDateAndAlphabetical(cfg: GlobalConfiguration): SortFn {
return (f1, f2) => { return (f1, f2) => {
if (f1.dates && f2.dates) { const f1Date = getDate(cfg, f1)
const f2Date = getDate(cfg, f2)
if (f1Date && f2Date) {
// sort descending // sort descending
return getDate(cfg, f2)!.toMillis() - getDate(cfg, f1)!.toMillis() return f2Date.toMillis() - f1Date.toMillis()
} else if (f1.dates && !f2.dates) { } else if (f1Date && !f2Date) {
// prioritize files with dates // prioritize files with dates
return -1 return -1
} else if (!f1.dates && f2.dates) { } else if (!f1Date && f2Date) {
return 1 return 1
} }
@ -32,23 +34,19 @@ type Props = {
export const PageList: QuartzComponent = ({ cfg, fileData, allFiles, limit, sort }: Props) => { export const PageList: QuartzComponent = ({ cfg, fileData, allFiles, limit, sort }: Props) => {
const sorter = sort ?? byDateAndAlphabetical(cfg) const sorter = sort ?? byDateAndAlphabetical(cfg)
let list = allFiles.sort(sorter) const list = allFiles.toSorted(sorter).slice(0, limit ?? allFiles.length)
if (limit) {
list = list.slice(0, limit)
}
return ( return (
<ul class="section-ul"> <ul class="section-ul">
{list.map((page) => { {list.map((page) => {
const title = page.frontmatter?.title const title = page.frontmatter?.title
const tags = page.frontmatter?.tags ?? [] const tags = page.frontmatter?.tags ?? []
const date = getDate(cfg, page)
return ( return (
<li class="section-li"> <li class="section-li">
<div class="section"> <div class="section">
<p class="meta"> <p class="meta">{date && <Date date={date} 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

@ -42,6 +42,7 @@ export default ((userOpts?: Partial<Options>) => {
{pages.slice(0, opts.limit).map((page) => { {pages.slice(0, opts.limit).map((page) => {
const title = page.frontmatter?.title ?? i18n(cfg.locale).propertyDefaults.title const title = page.frontmatter?.title ?? i18n(cfg.locale).propertyDefaults.title
const tags = page.frontmatter?.tags ?? [] const tags = page.frontmatter?.tags ?? []
const date = getDate(cfg, page)
return ( return (
<li class="recent-li"> <li class="recent-li">
@ -53,11 +54,7 @@ export default ((userOpts?: Partial<Options>) => {
</a> </a>
</h3> </h3>
</div> </div>
{page.dates && ( <p class="meta">{date && <Date date={date} locale={cfg.locale} />}</p>
<p class="meta">
<Date date={getDate(cfg, page)!} locale={cfg.locale} />
</p>
)}
{opts.showTags && ( {opts.showTags && (
<ul class="tags"> <ul class="tags">
{tags.map((tag) => ( {tags.map((tag) => (

View File

@ -74,7 +74,8 @@ export const CreatedModifiedDate: QuartzTransformerPlugin<Partial<Options>> = (u
for (const source of opts.priority) { for (const source of opts.priority) {
if (source === "filesystem") { if (source === "filesystem") {
const st = await fs.promises.stat(fullFp) const st = await fs.promises.stat(fullFp)
created ||= DateTime.fromMillis(st.birthtimeMs) // birthtime can be 0 on some filesystems, so default to the earlier of ctime/mtime
created ||= DateTime.fromMillis(st.birthtimeMs || Math.min(st.ctimeMs, st.mtimeMs))
modified ||= DateTime.fromMillis(st.mtimeMs) modified ||= DateTime.fromMillis(st.mtimeMs)
} else if (source === "frontmatter" && file.data.frontmatter) { } else if (source === "frontmatter" && file.data.frontmatter) {
created ||= parseDateString(fp, file.data.frontmatter.date, parseOpts) created ||= parseDateString(fp, file.data.frontmatter.date, parseOpts)
@ -106,9 +107,9 @@ export const CreatedModifiedDate: QuartzTransformerPlugin<Partial<Options>> = (u
} }
file.data.dates = { file.data.dates = {
created: created ?? DateTime.now(), created: created,
modified: modified ?? DateTime.now(), modified: modified,
published: published ?? DateTime.now(), published: published,
} }
} }
}, },
@ -119,10 +120,10 @@ export const CreatedModifiedDate: QuartzTransformerPlugin<Partial<Options>> = (u
declare module "vfile" { declare module "vfile" {
interface DataMap { interface DataMap {
dates: { dates: Partial<{
created: DateTime created: DateTime
modified: DateTime modified: DateTime
published: DateTime published: DateTime
} }>
} }
} }