Skip to content

Commit

Permalink
new plugin 'date'
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Jan 14, 2021
1 parent 0f31c52 commit f842163
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added
- New filter `njk` registered by the `nunjucks` plugin
- Moved `url` filter to a plugin that also created an additional filter `htmlUrl` to search and fix urls in html code
- New plugin `date` to manage date and time values using the library [date-fns](https://date-fns.org)

### Fixed
- Url filter with `null` values
Expand Down
3 changes: 0 additions & 3 deletions deps/date.js
@@ -1,4 +1 @@
export { default as format } from "https://deno.land/x/date_fns@v2.15.0/format/index.js";
import gl from "https://deno.land/x/date_fns/locale/gl/index.js";

export const locales = { gl };
36 changes: 36 additions & 0 deletions plugins/date.js
@@ -0,0 +1,36 @@
import { format } from "../deps/date.js";

const formats = new Map([
["ATOM", "yyyy-MM-dd'T'HH:mm:ssxxx"],
["DATE", "yyyy-MM-dd"],
["DATETIME", "yyyy-MM-dd HH:mm:ss"],
["TIME", "HH:mm:ss"],
["HUMAN_DATE", "PPP"],
["HUMAN_DATETIME", "PPPppp"],
]);

const defaults = {
locales: {},
formats: {},
};

export default function (options = {}) {
const userOptions = { ...defaults, ...options };
const defaultLocale = Object.keys(userOptions.locales).shift();

return (site) => {
site.filter("date", filter);

function filter(date, pattern = "DATE", lang = defaultLocale) {
if (!date) {
return;
}

const patt = userOptions.formats[pattern] || formats.get(pattern) ||
pattern;
const locale = userOptions.locales[lang];

return format(date, patt, { locale });
}
};
}

0 comments on commit f842163

Please sign in to comment.