refactor: abstract front matter generating functions
This commit is contained in:
parent
d499071d23
commit
f4fcc3df88
1 changed files with 67 additions and 59 deletions
12
src/main.ts
12
src/main.ts
|
|
@ -313,6 +313,13 @@ export default class MediaDbPlugin extends Plugin {
|
||||||
let template = await this.mediaTypeManager.getTemplate(mediaTypeModel, this.app);
|
let template = await this.mediaTypeManager.getTemplate(mediaTypeModel, this.app);
|
||||||
|
|
||||||
if (this.settings.useDefaultFrontMatter || !template) {
|
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 fileMetadata = this.modelPropertyMapper.convertObject(mediaTypeModel.toMetaDataObject());
|
||||||
let fileContent = '';
|
let fileContent = '';
|
||||||
template = options.attachTemplate ? template : '';
|
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;
|
fileContent = `---\n${this.settings.useCustomYamlStringifier ? YAMLConverter.toYaml(fileMetadata) : stringifyYaml(fileMetadata)}---\n` + fileContent;
|
||||||
return fileContent;
|
return fileContent;
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
async generateContentWithCustomFrontMatter(mediaTypeModel: MediaTypeModel, options: CreateNoteOptions, template: string): Promise<string> {
|
||||||
const frontMatterRegex = /^---*\n([\s\S]*?)\n---\h*/;
|
const frontMatterRegex = /^---*\n([\s\S]*?)\n---\h*/;
|
||||||
|
|
||||||
const match = template.match(frontMatterRegex);
|
const match = template.match(frontMatterRegex);
|
||||||
|
|
@ -374,7 +383,6 @@ export default class MediaDbPlugin extends Plugin {
|
||||||
|
|
||||||
return fileContent;
|
return fileContent;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
async attachFile(fileMetadata: any, fileContent: string, fileToAttach?: TFile): Promise<{ fileMetadata: any; fileContent: string }> {
|
async attachFile(fileMetadata: any, fileContent: string, fileToAttach?: TFile): Promise<{ fileMetadata: any; fileContent: string }> {
|
||||||
if (!fileToAttach) {
|
if (!fileToAttach) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue