Skip to content

Commit

Permalink
Merge pull request #589 from UppaJung/main
Browse files Browse the repository at this point in the history
add date format HUMAN_SINCE
  • Loading branch information
oscarotero committed Mar 26, 2024
2 parents 8aea32c + 124ccc1 commit 9cb4606
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Go to the `v1` branch to see the changelog of Lume 1.
## [2.1.3] - Unreleased
### Added
- PostCSS plugin: new option `name` with the default value `postcss` [#582].
- date plugin: new formats `HUMAN_SINCE` and `HUMAN_SINCE_STRICT` expose [`formatDistanceToNow`](https://date-fns.org/v3.6.0/docs/formatDistanceToNow) and [`formatDistanceToNowStrict`](https://date-fns.org/v3.6.0/docs/formatDistanceToNowStrict) in the [date-fns](https://date-fns.org/) package, so you can refer to the amount of time that has passed since the an article was last written/modified, rather than just the date it was written.

### Changed
- Do not ignore the `/.well-known` folder by default [#585].
Expand Down
2 changes: 2 additions & 0 deletions deps/date.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export { format } from "npm:date-fns@3.6.0/format";
export { formatDistanceToNow } from "npm:date-fns@3.6.0/formatDistanceToNow";
export { formatDistanceToNowStrict } from "npm:date-fns@3.6.0/formatDistanceToNowStrict";
export type { Locale } from "npm:date-fns@3.6.0/locale";
14 changes: 12 additions & 2 deletions plugins/date.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { format } from "../deps/date.ts";
import {
format,
formatDistanceToNow,
formatDistanceToNowStrict,
} from "../deps/date.ts";
import { merge } from "../core/utils/object.ts";

import type Site from "../core/site.ts";
Expand Down Expand Up @@ -56,7 +60,13 @@ export default function (userOptions?: Options) {
const patt = options.formats[pattern] || pattern;
const locale = lang ? options.locales[lang] : undefined;

return format(date, patt, { locale });
if (pattern === "HUMAN_SINCE") {
return formatDistanceToNow(date, { locale });
} else if (pattern === "HUMAN_SINCE_STRICT") {
return formatDistanceToNowStrict(date, { locale });
} else {
return format(date, patt, { locale });
}
}
};
}
Expand Down
17 changes: 17 additions & 0 deletions tests/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ Deno.test("date plugin", () => {
equals(format(date0, "CUSTOM"), "1970_01");
});

Deno.test("date plugin formats: HUMAN_SINCE and HUMAN_SINCE_STRICT", () => {
const site = lume();
site.use(date());

const { helpers } = site.renderer;
assert(helpers.has("date"));
const [format] = helpers.get("date")!;

const aBitMoreThanAYearFromNow = new Date(
Date.now() + 367 * 24 * 60 * 60 * 1000,
);
// See https://date-fns.org/v3.6.0/docs/formatDistanceToNow
equals(format(aBitMoreThanAYearFromNow, "HUMAN_SINCE"), "about 1 year");
// See https://date-fns.org/v3.6.0/docs/formatDistanceToNowStrict
equals(format(aBitMoreThanAYearFromNow, "HUMAN_SINCE_STRICT"), "1 year");
});

Deno.test("date plugin with custom locale", async () => {
const site = lume();
site.use(date({
Expand Down

0 comments on commit 9cb4606

Please sign in to comment.