refactor: use shared getDate, byDateAndAlphabetical, ValidDateType from community packages

This commit is contained in:
saberzero1 2026-04-03 15:44:30 +02:00
parent 48c141feee
commit 96b256f2f7
No known key found for this signature in database
3 changed files with 43 additions and 53 deletions

View file

@ -1,17 +1,22 @@
import type { QuartzComponent, QuartzComponentProps } from "@quartz-community/types";
import type {
QuartzComponent,
QuartzComponentProps,
QuartzPluginData,
SortFn,
ValidDateType,
} from "@quartz-community/types";
import { formatDate } from "@quartz-community/utils/date";
import { byDateAndAlphabetical, getDate } from "@quartz-community/utils/sort";
import { classNames } from "../util/lang";
import { i18n } from "../i18n";
import { resolveRelative } from "../util/path";
import { getDate } from "../util/date";
import { byDateAndAlphabetical } from "../util/sort";
import style from "./styles/recentNotes.scss";
type QuartzComponentConstructor<Options extends object | undefined = undefined> = (
opts: Options,
) => QuartzComponent;
interface QuartzPluginData {
type RecentNotesPluginData = QuartzPluginData & {
slug?: string;
filePath?: string;
dates?: Record<string, Date>;
@ -21,11 +26,11 @@ interface QuartzPluginData {
[key: string]: unknown;
};
[key: string]: unknown;
}
};
interface GlobalConfiguration {
locale: string;
defaultDateType: string;
defaultDateType: ValidDateType;
[key: string]: unknown;
}
@ -34,16 +39,33 @@ export interface RecentNotesOptions {
limit: number;
linkToMore: string | false;
showTags: boolean;
filter: (f: QuartzPluginData) => boolean;
sort: (f1: QuartzPluginData, f2: QuartzPluginData) => number;
filter: (f: RecentNotesPluginData) => boolean;
sort: SortFn;
}
const withDefaultDateType = (
data: RecentNotesPluginData,
defaultDateType: ValidDateType,
): QuartzPluginData => ({
...data,
defaultDateType,
});
const byDateAndAlphabeticalWithConfig = (cfg: GlobalConfiguration): SortFn => {
const sortFn = byDateAndAlphabetical();
return (f1, f2) =>
sortFn(
withDefaultDateType(f1 as RecentNotesPluginData, cfg.defaultDateType),
withDefaultDateType(f2 as RecentNotesPluginData, cfg.defaultDateType),
);
};
const defaultOptions = (cfg: GlobalConfiguration): RecentNotesOptions => ({
limit: 3,
linkToMore: false,
showTags: true,
filter: () => true,
sort: byDateAndAlphabetical(cfg),
sort: byDateAndAlphabeticalWithConfig(cfg),
});
export default ((userOpts?: Partial<RecentNotesOptions>) => {
@ -55,7 +77,7 @@ export default ((userOpts?: Partial<RecentNotesOptions>) => {
}: QuartzComponentProps & { displayClass?: string }) => {
const globalCfg = cfg as unknown as GlobalConfiguration;
const opts = { ...defaultOptions(globalCfg), ...userOpts };
const pages = (allFiles as QuartzPluginData[]).filter(opts.filter).sort(opts.sort);
const pages = (allFiles as RecentNotesPluginData[]).filter(opts.filter).sort(opts.sort);
const remaining = Math.max(0, pages.length - opts.limit);
const slug = fileData.slug as string | undefined;
const locale = cfg.locale ?? "en-US";
@ -79,8 +101,15 @@ export default ((userOpts?: Partial<RecentNotesOptions>) => {
</div>
{page.dates && (
<p class="meta">
<time datetime={getDate(globalCfg, page)?.toISOString()}>
{formatDate(getDate(globalCfg, page)!, locale)}
<time
datetime={getDate(
withDefaultDateType(page, globalCfg.defaultDateType),
)?.toISOString()}
>
{formatDate(
getDate(withDefaultDateType(page, globalCfg.defaultDateType))!,
locale,
)}
</time>
</p>
)}

View file

@ -1,18 +1 @@
interface GlobalConfiguration {
defaultDateType: string;
[key: string]: unknown;
}
interface QuartzPluginData {
dates?: Record<string, Date>;
[key: string]: unknown;
}
export function getDate(cfg: GlobalConfiguration, data: QuartzPluginData): Date | undefined {
if (!cfg.defaultDateType) {
throw new Error(
"Field 'defaultDateType' was not set in the configuration object of quartz.config.ts.",
);
}
return data.dates?.[cfg.defaultDateType];
}
export { getDate } from "@quartz-community/utils/sort";

View file

@ -1,23 +1 @@
import type { SortFn } from "@quartz-community/types";
import { getDate } from "./date";
interface GlobalConfiguration {
defaultDateType: string;
[key: string]: unknown;
}
export function byDateAndAlphabetical(cfg: GlobalConfiguration): SortFn {
return (f1, f2) => {
if (f1.dates && f2.dates) {
return getDate(cfg, f2)!.getTime() - getDate(cfg, f1)!.getTime();
} else if (f1.dates && !f2.dates) {
return -1;
} else if (!f1.dates && f2.dates) {
return 1;
}
const f1Title = (f1.frontmatter as { title?: string } | undefined)?.title?.toLowerCase() ?? "";
const f2Title = (f2.frontmatter as { title?: string } | undefined)?.title?.toLowerCase() ?? "";
return f1Title.localeCompare(f2Title);
};
}
export { byDateAndAlphabetical } from "@quartz-community/utils/sort";