mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-27 14:54:05 -06:00
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:
parent
9a967e6d0c
commit
beec5e0cbb
@ -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
|
||||
|
||||
@ -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)!.toMillis() - getDate(cfg, f1)!.toMillis()
|
||||
} 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
|
||||
}
|
||||
|
||||
@ -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">
|
||||
|
||||
@ -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">
|
||||
@ -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) => (
|
||||
|
||||
@ -74,7 +74,8 @@ export const CreatedModifiedDate: QuartzTransformerPlugin<Partial<Options>> = (u
|
||||
for (const source of opts.priority) {
|
||||
if (source === "filesystem") {
|
||||
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)
|
||||
} else if (source === "frontmatter" && file.data.frontmatter) {
|
||||
created ||= parseDateString(fp, file.data.frontmatter.date, parseOpts)
|
||||
@ -106,9 +107,9 @@ export const CreatedModifiedDate: QuartzTransformerPlugin<Partial<Options>> = (u
|
||||
}
|
||||
|
||||
file.data.dates = {
|
||||
created: created ?? DateTime.now(),
|
||||
modified: modified ?? DateTime.now(),
|
||||
published: published ?? DateTime.now(),
|
||||
created: created,
|
||||
modified: modified,
|
||||
published: published,
|
||||
}
|
||||
}
|
||||
},
|
||||
@ -119,10 +120,10 @@ export const CreatedModifiedDate: QuartzTransformerPlugin<Partial<Options>> = (u
|
||||
|
||||
declare module "vfile" {
|
||||
interface DataMap {
|
||||
dates: {
|
||||
dates: Partial<{
|
||||
created: DateTime
|
||||
modified: DateTime
|
||||
published: DateTime
|
||||
}
|
||||
}>
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user