mProjectsCode / obsidian-media-db-plugin

A plugin that can query multiple APIs for movies, series, anime, games, music and wiki articles, and import them into your vault.
GNU General Public License v3.0
255 stars 31 forks source link

add feature to customize date formatting #100

Closed spozer closed 1 year ago

spozer commented 1 year ago

This adds the option to set a custom date format, like requested in #78 by @iznaut.

I added a DateFormatter class, which gets used in the APIModels to format date strings. I also looked up what type of date formats are used by the various APIs, and saved them as variables called apiDateFormat. This makes the date formatting more robust, because you don't have to guess the actual date formats.

The DateFormatter holds the currently set custom date format. This gets updated in the main - by calling the setFormat() function of DateFormatter - when the user changes the custom date format in the settings tab.

For the settings tab, I added a text field where the user can input a custom date format string. The settings also shows a preview of what the current format will look like. image

For the actual date formatting the DateFormatter uses moment.js. Which is also used and exported by obsidian itself. But I had some trouble importing it. With esModuleInterop set in the compiler config, the type script compiler doesn't like that obsidian imports moment with a namespace-style import (which is also not compliant with ES6 module spec) like this:

// obsidian.ts
import * as Moment from 'moment';
export const moment: typeof Moment;

So when running npm run build the following always results in compiler errors complaining about the namespace-style import of moment in obsidian.ts:

// DateFormatter.ts
import { 'moment' } from 'obsidian';

I eventually got it to work with:

// DateFormatter.ts
const obsidian = require('obsidian');
const moment = obsidian.moment;

I don't know if this is a good way to import it though.

mProjectsCode commented 1 year ago

Thanks for the PR. I am currently quite busy, but I will try to review the PR before the weekend.

spozer commented 1 year ago

I just noticed that there is an error when using other locale formats other then 'en' or 'gb'. I will try to fix this.