Skip to content

Commit 0514ce5

Browse files
author
SvenPf
committed
add feature to customize date formatting
1 parent 2d2e380 commit 0514ce5

File tree

8 files changed

+110
-9
lines changed

8 files changed

+110
-9
lines changed

src/api/apis/MALAPI.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { MediaType } from '../../utils/MediaType';
88
export class MALAPI extends APIModel {
99
plugin: MediaDbPlugin;
1010
typeMappings: Map<string, string>;
11+
apiDateFormat: string = 'YYYY-MM-DDTHH:mm:ssZ'; // ISO
1112

1213
constructor(plugin: MediaDbPlugin) {
1314
super();
@@ -115,7 +116,7 @@ export class MALAPI extends APIModel {
115116
image: result.images?.jpg?.image_url ?? '',
116117

117118
released: true,
118-
premiere: new Date(result.aired?.from).toLocaleDateString() ?? 'unknown',
119+
premiere: this.plugin.dateFormatter.format(result.aired?.from, this.apiDateFormat) ?? 'unknown',
119120
streamingServices: result.streaming?.map((x: any) => x.name) ?? [],
120121

121122
userData: {
@@ -146,7 +147,7 @@ export class MALAPI extends APIModel {
146147
image: result.images?.jpg?.image_url ?? '',
147148

148149
released: true,
149-
premiere: new Date(result.aired?.from).toLocaleDateString() ?? 'unknown',
150+
premiere: this.plugin.dateFormatter.format(result.aired?.from, this.apiDateFormat) ?? 'unknown',
150151
streamingServices: result.streaming?.map((x: any) => x.name) ?? [],
151152

152153
userData: {
@@ -176,8 +177,8 @@ export class MALAPI extends APIModel {
176177
image: result.images?.jpg?.image_url ?? '',
177178

178179
released: true,
179-
airedFrom: new Date(result.aired?.from).toLocaleDateString() ?? 'unknown',
180-
airedTo: new Date(result.aired?.to).toLocaleDateString() ?? 'unknown',
180+
airedFrom: this.plugin.dateFormatter.format(result.aired?.from, this.apiDateFormat) ?? 'unknown',
181+
airedTo: this.plugin.dateFormatter.format(result.aired?.to, this.apiDateFormat) ?? 'unknown',
181182
airing: result.airing,
182183

183184
userData: {

src/api/apis/OMDbAPI.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { MediaType } from '../../utils/MediaType';
99
export class OMDbAPI extends APIModel {
1010
plugin: MediaDbPlugin;
1111
typeMappings: Map<string, string>;
12+
apiDateFormat: string = 'DD MMM YYYY';
1213

1314
constructor(plugin: MediaDbPlugin) {
1415
super();
@@ -142,7 +143,7 @@ export class OMDbAPI extends APIModel {
142143

143144
released: true,
144145
streamingServices: [],
145-
premiere: new Date(result.Released).toLocaleDateString() ?? 'unknown',
146+
premiere: this.plugin.dateFormatter.format(result.Released, this.apiDateFormat) ?? 'unknown',
146147

147148
userData: {
148149
watched: false,
@@ -173,7 +174,7 @@ export class OMDbAPI extends APIModel {
173174
released: true,
174175
streamingServices: [],
175176
airing: false,
176-
airedFrom: new Date(result.Released).toLocaleDateString() ?? 'unknown',
177+
airedFrom: this.plugin.dateFormatter.format(result.Released, this.apiDateFormat) ?? 'unknown',
177178
airedTo: 'unknown',
178179

179180
userData: {
@@ -199,7 +200,7 @@ export class OMDbAPI extends APIModel {
199200
image: result.Poster ?? '',
200201

201202
released: true,
202-
releaseDate: new Date(result.Released).toLocaleDateString() ?? 'unknown',
203+
releaseDate: this.plugin.dateFormatter.format(result.Released, this.apiDateFormat) ?? 'unknown',
203204

204205
userData: {
205206
played: false,

src/api/apis/SteamAPI.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { MediaType } from '../../utils/MediaType';
88
export class SteamAPI extends APIModel {
99
plugin: MediaDbPlugin;
1010
typeMappings: Map<string, string>;
11+
apiDateFormat: string = 'DD MMM, YYYY';
1112

1213
constructor(plugin: MediaDbPlugin) {
1314
super();
@@ -109,7 +110,7 @@ export class SteamAPI extends APIModel {
109110
image: result.header_image ?? '',
110111

111112
released: !result.release_date?.comming_soon,
112-
releaseDate: new Date(result.release_date?.date).toLocaleDateString() ?? 'unknown',
113+
releaseDate: this.plugin.dateFormatter.format(result.release_date?.date, this.apiDateFormat) ?? 'unknown',
113114

114115
userData: {
115116
played: false,

src/api/apis/WikipediaAPI.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { MediaType } from '../../utils/MediaType';
66

77
export class WikipediaAPI extends APIModel {
88
plugin: MediaDbPlugin;
9+
apiDateFormat: string = 'YYYY-MM-DDTHH:mm:ssZ'; // ISO
910

1011
constructor(plugin: MediaDbPlugin) {
1112
super();
@@ -72,7 +73,7 @@ export class WikipediaAPI extends APIModel {
7273
id: result.pageid,
7374

7475
wikiUrl: result.fullurl,
75-
lastUpdated: new Date(result.touched).toLocaleDateString() ?? 'unknown',
76+
lastUpdated: this.plugin.dateFormatter.format(result.touched, this.apiDateFormat),
7677
length: result.length,
7778

7879
userData: {},

src/main.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ import { YAMLConverter } from './utils/YAMLConverter';
1515
import { MediaDbFolderImportModal } from './modals/MediaDbFolderImportModal';
1616
import { PropertyMapping, PropertyMappingModel } from './settings/PropertyMapping';
1717
import { ModalHelper, ModalResultCode, SearchModalOptions } from './utils/ModalHelper';
18+
import { DateFormatter } from './utils/DateFormatter';
1819

1920
export default class MediaDbPlugin extends Plugin {
2021
settings: MediaDbPluginSettings;
2122
apiManager: APIManager;
2223
mediaTypeManager: MediaTypeManager;
2324
modelPropertyMapper: PropertyMapper;
2425
modalHelper: ModalHelper;
26+
dateFormatter: DateFormatter;
2527

2628
frontMatterRexExpPattern: string = '^(---)\\n[\\s\\S]*?\\n---';
2729

@@ -39,13 +41,15 @@ export default class MediaDbPlugin extends Plugin {
3941
this.mediaTypeManager = new MediaTypeManager();
4042
this.modelPropertyMapper = new PropertyMapper(this);
4143
this.modalHelper = new ModalHelper(this);
44+
this.dateFormatter = new DateFormatter();
4245

4346
await this.loadSettings();
4447
// register the settings tab
4548
this.addSettingTab(new MediaDbSettingTab(this.app, this));
4649

4750
this.mediaTypeManager.updateTemplates(this.settings);
4851
this.mediaTypeManager.updateFolders(this.settings);
52+
this.dateFormatter.setFormat(this.settings.customDateFormat);
4953

5054
// add icon to the left ribbon
5155
const ribbonIconEl = this.addRibbonIcon('database', 'Add new Media DB entry', () => this.createEntryWithAdvancedSearchModal());
@@ -563,6 +567,7 @@ export default class MediaDbPlugin extends Plugin {
563567
async saveSettings(): Promise<void> {
564568
this.mediaTypeManager.updateTemplates(this.settings);
565569
this.mediaTypeManager.updateFolders(this.settings);
570+
this.dateFormatter.setFormat(this.settings.customDateFormat);
566571

567572
await this.saveData(this.settings);
568573
}

src/settings/Settings.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import PropertyMappingModelsComponent from './PropertyMappingModelsComponent.sve
77
import { PropertyMapping, PropertyMappingModel, PropertyMappingOption } from './PropertyMapping';
88
import { MEDIA_TYPES } from '../utils/MediaTypeManager';
99
import { MediaTypeModel } from '../models/MediaTypeModel';
10+
import { fragWithHTML } from '../utils/Utils';
1011

1112
export interface MediaDbPluginSettings {
1213
OMDbKey: string;
1314
sfwFilter: boolean;
1415
useCustomYamlStringifier: boolean;
1516
templates: boolean;
17+
customDateFormat: string;
1618

1719
movieTemplate: string;
1820
seriesTemplate: string;
@@ -50,6 +52,7 @@ const DEFAULT_SETTINGS: MediaDbPluginSettings = {
5052
sfwFilter: true,
5153
useCustomYamlStringifier: true,
5254
templates: true,
55+
customDateFormat: 'L',
5356

5457
movieTemplate: '',
5558
seriesTemplate: '',
@@ -165,6 +168,23 @@ export class MediaDbSettingTab extends PluginSettingTab {
165168
});
166169
});
167170

171+
new Setting(containerEl)
172+
.setName('Date format')
173+
.setDesc(fragWithHTML("Your custom date format. Use <em>\'YYYY-MM-DD\'</em> for example.<br>" +
174+
"For more syntax, refer to <a href='https://momentjs.com/docs/#/displaying/format/'>format reference</a>.<br>" +
175+
"Your current syntax looks like this: <b><a id='dateformat-preview' style='pointer-events: none; cursor: default; text-decoration: none;'>" +
176+
this.plugin.dateFormatter.getPreview() + "</a></b>"))
177+
.addText(cb => {
178+
cb.setPlaceholder(DEFAULT_SETTINGS.customDateFormat)
179+
.setValue(this.plugin.settings.customDateFormat === DEFAULT_SETTINGS.customDateFormat ? '' : this.plugin.settings.customDateFormat)
180+
.onChange(data => {
181+
const newDateFormat = data ? data : DEFAULT_SETTINGS.customDateFormat;
182+
this.plugin.settings.customDateFormat = newDateFormat;
183+
document.getElementById('dateformat-preview').innerHTML = this.plugin.dateFormatter.getPreview(newDateFormat); // update preview
184+
this.plugin.saveSettings();
185+
});
186+
});
187+
168188
containerEl.createEl('h3', { text: 'New File Location' });
169189
// region new file location
170190
new Setting(containerEl)

src/utils/DateFormatter.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// obsidian already uses moment, so no need to package it twice!
2+
// import { moment } from 'obsidian'; // doesn't work for release build
3+
// obsidian uses a namespace-style import for moment, which ES6 doesn't allow anymore
4+
const obsidian = require('obsidian');
5+
const moment = obsidian.moment;
6+
7+
export class DateFormatter {
8+
toFormat: string;
9+
localeString: string;
10+
11+
constructor() {
12+
this.toFormat = 'YYYY-MM-DD';
13+
// get locale string (e.g. en, en-gb, de, fr, etc.)
14+
this.localeString = new Intl.DateTimeFormat().resolvedOptions().locale;
15+
}
16+
17+
setFormat(format: string): void {
18+
this.toFormat = format;
19+
}
20+
21+
getPreview(format?: string): string {
22+
const today = moment();
23+
today.locale(this.localeString);
24+
25+
if (!format) {
26+
format = this.toFormat;
27+
}
28+
29+
return today.format(format);
30+
}
31+
32+
/**
33+
* Tries to format a given date string with the currently set date format.
34+
* You can set a date format by calling `setFormat()`.
35+
*
36+
* @param dateString the date string to be formatted
37+
* @param dateFormat the current format of `dateString`. When this is `null` and the actual format of the
38+
* given date string is not `C2822` or `ISO` format, this function will try to guess the format by using the native `Date` module.
39+
* @returns formatted date string or null if `dateString` is not a valid date
40+
*/
41+
format(dateString: string, dateFormat?: string): string | null {
42+
if (!dateString) {
43+
return null;
44+
}
45+
46+
let date: moment.Moment;
47+
48+
if (!dateFormat) {
49+
// reading date formats other then C2822 or ISO with moment is deprecated
50+
if (this.hasMomentFormat(dateString)) {
51+
// expect C2822 or ISO format
52+
date = moment(dateString);
53+
} else {
54+
// try to read date string with native Date
55+
date = moment(new Date(dateString));
56+
}
57+
date.locale(this.localeString); // set local locale definition for moment
58+
} else {
59+
date = moment(dateString, dateFormat, this.localeString);
60+
}
61+
62+
// format date (if it is valid)
63+
return date.isValid() ? date.format(this.toFormat) : null;
64+
}
65+
66+
private hasMomentFormat(dateString: string): boolean {
67+
const date = moment(dateString, true); // strict mode
68+
return date.isValid();
69+
}
70+
}

src/utils/Utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ export function markdownTable(content: string[][]): string {
135135
return table;
136136
}
137137

138+
export const fragWithHTML = (html: string) => createFragment(frag => (frag.createDiv().innerHTML = html));
139+
138140
export function dateToString(date: Date): string {
139141
return `${date.getMonth() + 1}-${date.getDate()}-${date.getFullYear()}`;
140142
}

0 commit comments

Comments
 (0)