From f30210f1672782904a693d066a19a083019733c7 Mon Sep 17 00:00:00 2001 From: bingryan Date: Mon, 31 Jul 2023 10:40:02 +0800 Subject: [PATCH] feat: add dateFormat --- package.json | 3 ++- src/renderer.ts | 11 +++++++++++ yarn.lock | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 9a73662..9e4a87a 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/renderer.ts b/src/renderer.ts index d739741..3e4e9c5 100644 --- a/src/renderer.ts +++ b/src/renderer.ts @@ -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; @@ -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); } diff --git a/yarn.lock b/yarn.lock index 1eec81b..ec8930e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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== -moment@2.29.4, moment@^2.18.1: +moment@2.29.4, 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==