Skip to content

Commit

Permalink
feat: add dateFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
bingryan committed Jul 31, 2023
1 parent a118e83 commit f30210f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"dependencies": {
"handlebars": "^4.7.7",
"handlebars-group-by": "^1.0.1",
"handlebars-helpers": "^0.10.0"
"handlebars-helpers": "^0.10.0",
"moment": "^2.29.4"
}
}
11 changes: 11 additions & 0 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IbookPluginSettings } from '@/config';
import { RendererData } from '@/types';
import helpers from 'handlebars-helpers';
import groupBy from 'handlebars-group-by';
import moment from 'moment';

export class Renderer {
private settings: IbookPluginSettings;
Expand All @@ -15,6 +16,16 @@ export class Renderer {
const source = this.settings.template;
handlebars.registerHelper(groupBy(handlebars));
handlebars.registerHelper(helpers(handlebars));
handlebars.registerHelper("dateFormat", (date, format, utc) => {
// issue: https://github.com/bingryan/obsidian-ibook-plugin/issues/53
const cocoaOffset = 978307200;
if (typeof date === 'string') {
date = parseInt(date);
}
const dateTime = moment.unix(date + cocoaOffset);
return (utc === true) ? dateTime.utc().format(format) : dateTime.format(format);
});

const template = handlebars.compile(source);
return template(data);
}
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2638,7 +2638,7 @@ module-alias@^2.2.2:
resolved "https://registry.npmmirror.com/module-alias/-/module-alias-2.2.2.tgz#151cdcecc24e25739ff0aa6e51e1c5716974c0e0"
integrity sha512-A/78XjoX2EmNvppVWEhM2oGk3x4lLxnkEA4jTbaK97QKSDjkIoOsKQlfylt/d3kKKi596Qy3NP5XrXJ6fZIC9Q==

[email protected], moment@^2.18.1:
[email protected], moment@^2.18.1, moment@^2.29.4:
version "2.29.4"
resolved "https://registry.npmmirror.com/moment/-/moment-2.29.4.tgz#3dbe052889fe7c1b2ed966fcb3a77328964ef108"
integrity sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==
Expand Down

0 comments on commit f30210f

Please sign in to comment.