diff --git a/src/components/RecentNotes.tsx b/src/components/RecentNotes.tsx index 67ad304..f998ff5 100644 --- a/src/components/RecentNotes.tsx +++ b/src/components/RecentNotes.tsx @@ -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 = ( opts: Options, ) => QuartzComponent; -interface QuartzPluginData { +type RecentNotesPluginData = QuartzPluginData & { slug?: string; filePath?: string; dates?: Record; @@ -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) => { @@ -55,7 +77,7 @@ export default ((userOpts?: Partial) => { }: 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) => { {page.dates && (

-

)} diff --git a/src/util/date.ts b/src/util/date.ts index bd4699a..3aba70c 100644 --- a/src/util/date.ts +++ b/src/util/date.ts @@ -1,18 +1 @@ -interface GlobalConfiguration { - defaultDateType: string; - [key: string]: unknown; -} - -interface QuartzPluginData { - dates?: Record; - [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"; diff --git a/src/util/sort.ts b/src/util/sort.ts index 0e4c898..6bfbbcf 100644 --- a/src/util/sort.ts +++ b/src/util/sort.ts @@ -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";