feat: add getDate and byDateAndAlphabetical sort utilities

This commit is contained in:
saberzero1 2026-04-03 15:39:35 +02:00
parent 8b6e32ba07
commit 20b1ac1844
No known key found for this signature in database
11 changed files with 866 additions and 86 deletions

1
dist/index.d.ts vendored
View file

@ -35,6 +35,7 @@ export { escapeHTML, unescapeHTML } from "./escape.js";
export { htmlToJsx } from "./jsx.js";
export { formatDate } from "./date.js";
export { getIconCode } from "./emoji.js";
export { byDateAndAlphabetical, getDate } from "./sort.js";
export { FilePath, FullSlug } from "@quartz-community/types";
import "hast-util-to-jsx-runtime";
import "hast";

30
dist/index.js vendored
View file

@ -383,7 +383,36 @@ function toCodePoint(unicodeSurrogates) {
return r.join("-");
}
// src/sort.ts
function getDate(data) {
const defaultDateType = data.defaultDateType;
if (!defaultDateType) {
throw new Error(
"Field 'defaultDateType' was not set. Ensure the CreatedModifiedDate plugin is configured with a 'defaultDateType' option.",
);
}
const dates = data.dates;
return dates?.[defaultDateType];
}
function byDateAndAlphabetical() {
return (f1, f2) => {
const f1Dates = f1.dates;
const f2Dates = f2.dates;
if (f1Dates && f2Dates) {
return getDate(f2).getTime() - getDate(f1).getTime();
} else if (f1Dates && !f2Dates) {
return -1;
} else if (!f1Dates && f2Dates) {
return 1;
}
const f1Title = (f1.frontmatter?.title ?? "").toLowerCase();
const f2Title = (f2.frontmatter?.title ?? "").toLowerCase();
return f1Title.localeCompare(f2Title);
};
}
export {
byDateAndAlphabetical,
capitalize,
classNames,
endsWith,
@ -391,6 +420,7 @@ export {
formatDate,
getAllSegmentPrefixes,
getBasePath,
getDate,
getFileExtension,
getFullSlug,
getFullSlugFromUrl,

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

6
dist/sort.d.ts vendored Normal file
View file

@ -0,0 +1,6 @@
import { SortFn, QuartzPluginData } from "@quartz-community/types";
declare function getDate(data: QuartzPluginData): Date | undefined;
declare function byDateAndAlphabetical(): SortFn;
export { byDateAndAlphabetical, getDate };

31
dist/sort.js vendored Normal file
View file

@ -0,0 +1,31 @@
// src/sort.ts
function getDate(data) {
const defaultDateType = data.defaultDateType;
if (!defaultDateType) {
throw new Error(
"Field 'defaultDateType' was not set. Ensure the CreatedModifiedDate plugin is configured with a 'defaultDateType' option.",
);
}
const dates = data.dates;
return dates?.[defaultDateType];
}
function byDateAndAlphabetical() {
return (f1, f2) => {
const f1Dates = f1.dates;
const f2Dates = f2.dates;
if (f1Dates && f2Dates) {
return getDate(f2).getTime() - getDate(f1).getTime();
} else if (f1Dates && !f2Dates) {
return -1;
} else if (!f1Dates && f2Dates) {
return 1;
}
const f1Title = (f1.frontmatter?.title ?? "").toLowerCase();
const f2Title = (f2.frontmatter?.title ?? "").toLowerCase();
return f1Title.localeCompare(f2Title);
};
}
export { byDateAndAlphabetical, getDate };
//# sourceMappingURL=sort.js.map
//# sourceMappingURL=sort.js.map

1
dist/sort.js.map vendored Normal file
View file

@ -0,0 +1 @@
{"version":3,"sources":["../src/sort.ts"],"names":[],"mappings":";AAEO,SAAS,QAAQ,IAAA,EAA0C;AAChE,EAAA,MAAM,kBAAkB,IAAA,CAAK,eAAA;AAC7B,EAAA,IAAI,CAAC,eAAA,EAAiB;AACpB,IAAA,MAAM,IAAI,KAAA;AAAA,MACR;AAAA,KACF;AAAA,EACF;AACA,EAAA,MAAM,QAAQ,IAAA,CAAK,KAAA;AACnB,EAAA,OAAO,QAAQ,eAAe,CAAA;AAChC;AAEO,SAAS,qBAAA,GAAgC;AAC9C,EAAA,OAAO,CAAC,IAAI,EAAA,KAAO;AACjB,IAAA,MAAM,UAAU,EAAA,CAAG,KAAA;AACnB,IAAA,MAAM,UAAU,EAAA,CAAG,KAAA;AACnB,IAAA,IAAI,WAAW,OAAA,EAAS;AACtB,MAAA,OAAO,OAAA,CAAQ,EAAE,CAAA,CAAG,OAAA,KAAY,OAAA,CAAQ,EAAE,EAAG,OAAA,EAAQ;AAAA,IACvD,CAAA,MAAA,IAAW,OAAA,IAAW,CAAC,OAAA,EAAS;AAC9B,MAAA,OAAO,EAAA;AAAA,IACT,CAAA,MAAA,IAAW,CAAC,OAAA,IAAW,OAAA,EAAS;AAC9B,MAAA,OAAO,CAAA;AAAA,IACT;AAEA,IAAA,MAAM,OAAA,GAAA,CACF,EAAA,CAAG,WAAA,EAAyC,KAAA,IAAoB,IAClE,WAAA,EAAY;AACd,IAAA,MAAM,OAAA,GAAA,CACF,EAAA,CAAG,WAAA,EAAyC,KAAA,IAAoB,IAClE,WAAA,EAAY;AACd,IAAA,OAAO,OAAA,CAAQ,cAAc,OAAO,CAAA;AAAA,EACtC,CAAA;AACF","file":"sort.js","sourcesContent":["import type { QuartzPluginData, ValidDateType, SortFn } from \"@quartz-community/types\";\n\nexport function getDate(data: QuartzPluginData): Date | undefined {\n const defaultDateType = data.defaultDateType as ValidDateType | undefined;\n if (!defaultDateType) {\n throw new Error(\n \"Field 'defaultDateType' was not set. Ensure the CreatedModifiedDate plugin is configured with a 'defaultDateType' option.\",\n );\n }\n const dates = data.dates as Record<ValidDateType, Date> | undefined;\n return dates?.[defaultDateType];\n}\n\nexport function byDateAndAlphabetical(): SortFn {\n return (f1, f2) => {\n const f1Dates = f1.dates as Record<string, Date> | undefined;\n const f2Dates = f2.dates as Record<string, Date> | undefined;\n if (f1Dates && f2Dates) {\n return getDate(f2)!.getTime() - getDate(f1)!.getTime();\n } else if (f1Dates && !f2Dates) {\n return -1;\n } else if (!f1Dates && f2Dates) {\n return 1;\n }\n\n const f1Title = (\n ((f1.frontmatter as Record<string, unknown>)?.title as string) ?? \"\"\n ).toLowerCase();\n const f2Title = (\n ((f2.frontmatter as Record<string, unknown>)?.title as string) ?? \"\"\n ).toLowerCase();\n return f1Title.localeCompare(f2Title);\n };\n}\n"]}

830
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -56,6 +56,10 @@
"types": "./dist/emoji.d.ts",
"import": "./dist/emoji.js"
},
"./sort": {
"types": "./dist/sort.d.ts",
"import": "./dist/sort.js"
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
@ -74,9 +78,9 @@
"check": "npm run typecheck && npm run lint && npm run format && npm run test"
},
"peerDependencies": {
"preact": "^10.0.0",
"github-slugger": "^2.0.0",
"hast-util-to-jsx-runtime": "^2.3.6",
"github-slugger": "^2.0.0"
"preact": "^10.0.0"
},
"devDependencies": {
"@types/node": "^24.10.0",
@ -84,10 +88,13 @@
"@typescript-eslint/parser": "^7.18.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"github-slugger": "^2.0.0",
"hast": "^0.0.2",
"hast-util-to-jsx-runtime": "^2.3.6",
"prettier": "^3.6.2",
"vitest": "^2.1.9",
"tsup": "^8.5.0",
"typescript": "^5.9.3"
"typescript": "^5.9.3",
"vitest": "^2.1.9"
},
"engines": {
"node": ">=22",

View file

@ -39,3 +39,4 @@ export { htmlToJsx } from "./jsx.js";
export { formatDate } from "./date.js";
export { getIconCode } from "./emoji.js";
export { getDate, byDateAndAlphabetical } from "./sort.js";

34
src/sort.ts Normal file
View file

@ -0,0 +1,34 @@
import type { QuartzPluginData, ValidDateType, SortFn } from "@quartz-community/types";
export function getDate(data: QuartzPluginData): Date | undefined {
const defaultDateType = data.defaultDateType as ValidDateType | undefined;
if (!defaultDateType) {
throw new Error(
"Field 'defaultDateType' was not set. Ensure the CreatedModifiedDate plugin is configured with a 'defaultDateType' option.",
);
}
const dates = data.dates as Record<ValidDateType, Date> | undefined;
return dates?.[defaultDateType];
}
export function byDateAndAlphabetical(): SortFn {
return (f1, f2) => {
const f1Dates = f1.dates as Record<string, Date> | undefined;
const f2Dates = f2.dates as Record<string, Date> | undefined;
if (f1Dates && f2Dates) {
return getDate(f2)!.getTime() - getDate(f1)!.getTime();
} else if (f1Dates && !f2Dates) {
return -1;
} else if (!f1Dates && f2Dates) {
return 1;
}
const f1Title = (
((f1.frontmatter as Record<string, unknown>)?.title as string) ?? ""
).toLowerCase();
const f2Title = (
((f2.frontmatter as Record<string, unknown>)?.title as string) ?? ""
).toLowerCase();
return f1Title.localeCompare(f2Title);
};
}

View file

@ -10,6 +10,7 @@ export default defineConfig({
date: "src/date.ts",
emoji: "src/emoji.ts",
jsx: "src/jsx.tsx",
sort: "src/sort.ts",
},
format: ["esm"],
dts: true,