Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Phantom 2023-09-27 21:17:56 -05:00
commit 67825ae135
19 changed files with 1535 additions and 5272 deletions

View file

@ -0,0 +1,67 @@
import { moment } from 'obsidian';
export class DateFormatter {
toFormat: string;
locale: string;
constructor() {
this.toFormat = 'YYYY-MM-DD';
// get locale of this machine (e.g. en, en-gb, de, fr, etc.)
this.locale = new Intl.DateTimeFormat().resolvedOptions().locale;
}
setFormat(format: string): void {
this.toFormat = format;
}
getPreview(format?: string): string {
const today = moment();
if (!format) {
format = this.toFormat;
}
return today.locale(this.locale).format(format);
}
/**
* Tries to format a given date string with the currently set date format.
* You can set a date format by calling `setFormat()`.
*
* @param dateString the date string to be formatted
* @param dateFormat the current format of `dateString`. When this is `null` and the actual format of the
* given date string is not `C2822` or `ISO` format, this function will try to guess the format by using the native `Date` module.
* @param locale the locale of `dateString`. This is needed when `dateString` includes a month or day name and its locale format differs
* from the locale of this machine.
* @returns formatted date string or null if `dateString` is not a valid date
*/
format(dateString: string, dateFormat?: string, locale: string = 'en'): string | null {
if (!dateString) {
return null;
}
let date: moment.Moment;
if (!dateFormat) {
// reading date formats other then C2822 or ISO with moment is deprecated
// see https://momentjs.com/docs/#/parsing/string/
if (this.hasMomentFormat(dateString)) {
// expect C2822 or ISO format
date = moment(dateString);
} else {
// try to read date string with native Date
date = moment(new Date(dateString));
}
} else {
date = moment(dateString, dateFormat, locale);
}
// format date (if it is valid)
return date.isValid() ? date.locale(this.locale).format(this.toFormat) : null;
}
private hasMomentFormat(dateString: string): boolean {
const date = moment(dateString, true); // strict mode
return date.isValid();
}
}

View file

@ -6,4 +6,5 @@ export enum MediaType {
MusicRelease = 'musicRelease',
Wiki = 'wiki',
BoardGame = 'boardgame',
Book = 'book',
}

View file

@ -10,8 +10,9 @@ import { GameModel } from '../models/GameModel';
import { WikiModel } from '../models/WikiModel';
import { MusicReleaseModel } from '../models/MusicReleaseModel';
import { BoardGameModel } from '../models/BoardGameModel';
import { BookModel } from '../models/BookModel';
export const MEDIA_TYPES: MediaType[] = [MediaType.Movie, MediaType.Series, MediaType.Manga, MediaType.Game, MediaType.Wiki, MediaType.MusicRelease, MediaType.BoardGame];
export const MEDIA_TYPES: MediaType[] = [MediaType.Movie, MediaType.Series, MediaType.Manga, MediaType.Game, MediaType.Wiki, MediaType.MusicRelease, MediaType.BoardGame, MediaType.Book];
export class MediaTypeManager {
mediaFileNameTemplateMap: Map<MediaType, string>;
@ -29,6 +30,7 @@ export class MediaTypeManager {
this.mediaFileNameTemplateMap.set(MediaType.Wiki, settings.wikiFileNameTemplate);
this.mediaFileNameTemplateMap.set(MediaType.MusicRelease, settings.musicReleaseFileNameTemplate);
this.mediaFileNameTemplateMap.set(MediaType.BoardGame, settings.boardgameFileNameTemplate);
this.mediaFileNameTemplateMap.set(MediaType.Book, settings.bookFileNameTemplate);
this.mediaTemplateMap = new Map<MediaType, string>();
this.mediaTemplateMap.set(MediaType.Movie, settings.movieTemplate);
@ -38,6 +40,7 @@ export class MediaTypeManager {
this.mediaTemplateMap.set(MediaType.Wiki, settings.wikiTemplate);
this.mediaTemplateMap.set(MediaType.MusicRelease, settings.musicReleaseTemplate);
this.mediaTemplateMap.set(MediaType.BoardGame, settings.boardgameTemplate);
this.mediaTemplateMap.set(MediaType.Book, settings.bookTemplate);
}
updateFolders(settings: MediaDbPluginSettings): void {
@ -49,6 +52,7 @@ export class MediaTypeManager {
this.mediaFolderMap.set(MediaType.Wiki, settings.wikiFolder);
this.mediaFolderMap.set(MediaType.MusicRelease, settings.musicReleaseFolder);
this.mediaFolderMap.set(MediaType.BoardGame, settings.boardgameFolder);
this.mediaFolderMap.set(MediaType.Book, settings.bookFolder);
}
getFileName(mediaTypeModel: MediaTypeModel): string {
@ -117,6 +121,8 @@ export class MediaTypeManager {
return new MusicReleaseModel(obj);
} else if (mediaType === MediaType.BoardGame) {
return new BoardGameModel(obj);
} else if (mediaType === MediaType.Book) {
return new BookModel(obj);
}
return undefined;

View file

@ -135,6 +135,8 @@ export function markdownTable(content: string[][]): string {
return table;
}
export const fragWithHTML = (html: string) => createFragment(frag => (frag.createDiv().innerHTML = html));
export function dateToString(date: Date): string {
return `${date.getMonth() + 1}-${date.getDate()}-${date.getFullYear()}`;
}

View file

@ -19,7 +19,7 @@ export class YAMLConverter {
} else if (typeof value === 'number') {
return value.toString();
} else if (typeof value === 'string') {
return '"' + value.replace("\"", "\\\"") + '"';
return '"' + value.replace('"', '\\"') + '"';
} else if (typeof value === 'object') {
let output = '';