refactor: use shared getDate and ValidDateType from community packages

This commit is contained in:
saberzero1 2026-04-03 15:45:45 +02:00
parent 1399fdc2d1
commit 57e1401d4c
No known key found for this signature in database
7 changed files with 29 additions and 43 deletions

View file

@ -1,5 +1,6 @@
import { createRequire } from 'module';
import { formatDate } from '@quartz-community/utils/date';
import { getDate } from '@quartz-community/utils/sort';
import { jsx } from 'preact/jsx-runtime';
const require$1 = createRequire(import.meta.url);
@ -190,16 +191,6 @@ var locales = {
function i18n(locale) {
return locales[locale] || en_US_default;
}
function getDate(cfg, data) {
const dateType = data.defaultDateType ?? cfg.defaultDateType;
if (!dateType) {
throw new Error(
`Field 'defaultDateType' was not set. Either configure it in the CreatedModifiedDate plugin options or set it in quartz.config.ts. See https://quartz.jzhao.xyz/configuration#general-configuration for more details.`
);
}
const dates = data.dates;
return dates?.[dateType];
}
function DateComponent({ date, locale }) {
return /* @__PURE__ */ jsx("time", { datetime: date.toISOString(), children: formatDate(date, locale) });
}
@ -218,7 +209,11 @@ var ContentMeta_default = ((opts) => {
const segments = [];
if (fileData.dates) {
const locale = cfg.locale || "en-US";
segments.push(/* @__PURE__ */ jsx(DateComponent, { date: getDate(cfg, fileData), locale }));
const dataWithDefaultDateType = {
...fileData,
defaultDateType: cfg.defaultDateType
};
segments.push(/* @__PURE__ */ jsx(DateComponent, { date: getDate(dataWithDefaultDateType), locale }));
}
if (options.showReadingTime) {
const { minutes, words: _words } = (0, import_reading_time.default)(text);

File diff suppressed because one or more lines are too long

17
dist/index.js vendored
View file

@ -1,5 +1,6 @@
import { createRequire } from 'module';
import { formatDate } from '@quartz-community/utils/date';
import { getDate } from '@quartz-community/utils/sort';
import { jsx } from 'preact/jsx-runtime';
const require$1 = createRequire(import.meta.url);
@ -190,16 +191,6 @@ var locales = {
function i18n(locale) {
return locales[locale] || en_US_default;
}
function getDate(cfg, data) {
const dateType = data.defaultDateType ?? cfg.defaultDateType;
if (!dateType) {
throw new Error(
`Field 'defaultDateType' was not set. Either configure it in the CreatedModifiedDate plugin options or set it in quartz.config.ts. See https://quartz.jzhao.xyz/configuration#general-configuration for more details.`
);
}
const dates = data.dates;
return dates?.[dateType];
}
function DateComponent({ date, locale }) {
return /* @__PURE__ */ jsx("time", { datetime: date.toISOString(), children: formatDate(date, locale) });
}
@ -218,7 +209,11 @@ var ContentMeta_default = ((opts) => {
const segments = [];
if (fileData.dates) {
const locale = cfg.locale || "en-US";
segments.push(/* @__PURE__ */ jsx(DateComponent, { date: getDate(cfg, fileData), locale }));
const dataWithDefaultDateType = {
...fileData,
defaultDateType: cfg.defaultDateType
};
segments.push(/* @__PURE__ */ jsx(DateComponent, { date: getDate(dataWithDefaultDateType), locale }));
}
if (options.showReadingTime) {
const { minutes, words: _words } = (0, import_reading_time.default)(text);

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

4
package-lock.json generated
View file

@ -1039,7 +1039,7 @@
},
"node_modules/@quartz-community/types": {
"version": "0.2.1",
"resolved": "git+ssh://git@github.com/quartz-community/types.git#653aece4c90759fb911d62d4af89bf300865f85d",
"resolved": "git+ssh://git@github.com/quartz-community/types.git#d413569b02ff0cd9a7e8d74dee125d20a5e5ec72",
"license": "MIT",
"engines": {
"node": ">=22",
@ -1060,7 +1060,7 @@
},
"node_modules/@quartz-community/utils": {
"version": "0.1.0",
"resolved": "git+ssh://git@github.com/quartz-community/utils.git#8b6e32ba07b62500dce10f0c79ca43bb7d7e55bc",
"resolved": "git+ssh://git@github.com/quartz-community/utils.git#20b1ac1844ea477b4e32f206a515a5be8d01c56e",
"license": "MIT",
"dependencies": {
"@quartz-community/types": "github:quartz-community/types"

View file

@ -1,4 +1,9 @@
import type { QuartzComponentConstructor, QuartzComponentProps } from "@quartz-community/types";
import type {
QuartzComponentConstructor,
QuartzComponentProps,
QuartzPluginData,
ValidDateType,
} from "@quartz-community/types";
import readingTime from "reading-time";
import { classNames } from "../util/lang";
import { i18n } from "../i18n";
@ -31,7 +36,11 @@ export default ((opts?: Partial<ContentMetaOptions>) => {
if (fileData.dates) {
const locale = cfg.locale || "en-US";
segments.push(<DateComponent date={getDate(cfg, fileData)!} locale={locale} />);
const dataWithDefaultDateType: QuartzPluginData = {
...(fileData as QuartzPluginData),
defaultDateType: cfg.defaultDateType as ValidDateType,
};
segments.push(<DateComponent date={getDate(dataWithDefaultDateType)!} locale={locale} />);
}
// Display reading time if enabled

View file

@ -1,25 +1,12 @@
import { formatDate } from "@quartz-community/utils/date";
export type ValidDateType = "created" | "modified" | "published";
import { getDate } from "@quartz-community/utils/sort";
interface Props {
date: Date;
locale?: string;
}
export function getDate(
cfg: { defaultDateType?: string },
data: Record<string, unknown>,
): Date | undefined {
const dateType = (data.defaultDateType as string) ?? cfg.defaultDateType;
if (!dateType) {
throw new Error(
`Field 'defaultDateType' was not set. Either configure it in the CreatedModifiedDate plugin options or set it in quartz.config.ts. See https://quartz.jzhao.xyz/configuration#general-configuration for more details.`,
);
}
const dates = data.dates as Record<string, Date> | undefined;
return dates?.[dateType];
}
export { getDate };
export function DateComponent({ date, locale }: Props) {
return <time datetime={date.toISOString()}>{formatDate(date, locale)}</time>;