refactor: abstract front matter generating functions

This commit is contained in:
Kelvin John Falk Szolnoky 2024-01-22 22:47:22 +01:00
parent d499071d23
commit f4fcc3df88
No known key found for this signature in database
GPG key ID: 02BA666D8C0E7E1B

View file

@ -313,6 +313,13 @@ export default class MediaDbPlugin extends Plugin {
let template = await this.mediaTypeManager.getTemplate(mediaTypeModel, this.app);
if (this.settings.useDefaultFrontMatter || !template) {
return this.generateContentWithDefaultFrontMatter(mediaTypeModel, options, template);
} else {
return this.generateContentWithCustomFrontMatter(mediaTypeModel, options, template);
}
}
async generateContentWithDefaultFrontMatter(mediaTypeModel: MediaTypeModel, options: CreateNoteOptions, template?: string): Promise<string> {
let fileMetadata = this.modelPropertyMapper.convertObject(mediaTypeModel.toMetaDataObject());
let fileContent = '';
template = options.attachTemplate ? template : '';
@ -322,7 +329,9 @@ export default class MediaDbPlugin extends Plugin {
fileContent = `---\n${this.settings.useCustomYamlStringifier ? YAMLConverter.toYaml(fileMetadata) : stringifyYaml(fileMetadata)}---\n` + fileContent;
return fileContent;
} else {
}
async generateContentWithCustomFrontMatter(mediaTypeModel: MediaTypeModel, options: CreateNoteOptions, template: string): Promise<string> {
const frontMatterRegex = /^---*\n([\s\S]*?)\n---\h*/;
const match = template.match(frontMatterRegex);
@ -374,7 +383,6 @@ export default class MediaDbPlugin extends Plugin {
return fileContent;
}
}
async attachFile(fileMetadata: any, fileContent: string, fileToAttach?: TFile): Promise<{ fileMetadata: any; fileContent: string }> {
if (!fileToAttach) {