Skip to content

Commit f4fcc3d

Browse files
committed
refactor: abstract front matter generating functions
1 parent d499071 commit f4fcc3d

File tree

1 file changed

+54
-46
lines changed

1 file changed

+54
-46
lines changed

src/main.ts

Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -313,67 +313,75 @@ export default class MediaDbPlugin extends Plugin {
313313
let template = await this.mediaTypeManager.getTemplate(mediaTypeModel, this.app);
314314

315315
if (this.settings.useDefaultFrontMatter || !template) {
316-
let fileMetadata = this.modelPropertyMapper.convertObject(mediaTypeModel.toMetaDataObject());
317-
let fileContent = '';
318-
template = options.attachTemplate ? template : '';
319-
320-
({ fileMetadata, fileContent } = await this.attachFile(fileMetadata, fileContent, options.attachFile));
321-
({ fileMetadata, fileContent } = await this.attachTemplate(fileMetadata, fileContent, template));
322-
323-
fileContent = `---\n${this.settings.useCustomYamlStringifier ? YAMLConverter.toYaml(fileMetadata) : stringifyYaml(fileMetadata)}---\n` + fileContent;
324-
return fileContent;
316+
return this.generateContentWithDefaultFrontMatter(mediaTypeModel, options, template);
325317
} else {
326-
const frontMatterRegex = /^---*\n([\s\S]*?)\n---\h*/;
318+
return this.generateContentWithCustomFrontMatter(mediaTypeModel, options, template);
319+
}
320+
}
327321

328-
const match = template.match(frontMatterRegex);
322+
async generateContentWithDefaultFrontMatter(mediaTypeModel: MediaTypeModel, options: CreateNoteOptions, template?: string): Promise<string> {
323+
let fileMetadata = this.modelPropertyMapper.convertObject(mediaTypeModel.toMetaDataObject());
324+
let fileContent = '';
325+
template = options.attachTemplate ? template : '';
329326

330-
if (!match || match.length !== 2) {
331-
throw new Error('Cannot find YAML front matter for template.');
332-
}
327+
({ fileMetadata, fileContent } = await this.attachFile(fileMetadata, fileContent, options.attachFile));
328+
({ fileMetadata, fileContent } = await this.attachTemplate(fileMetadata, fileContent, template));
333329

334-
let frontMatter = parseYaml(match[1]);
335-
let fileContent: string = template.replace(frontMatterRegex, '');
330+
fileContent = `---\n${this.settings.useCustomYamlStringifier ? YAMLConverter.toYaml(fileMetadata) : stringifyYaml(fileMetadata)}---\n` + fileContent;
331+
return fileContent;
332+
}
336333

337-
// Updating a previous file
338-
if (options.attachFile) {
339-
const previousMetadata = this.app.metadataCache.getFileCache(options.attachFile).frontmatter;
334+
async generateContentWithCustomFrontMatter(mediaTypeModel: MediaTypeModel, options: CreateNoteOptions, template: string): Promise<string> {
335+
const frontMatterRegex = /^---*\n([\s\S]*?)\n---\h*/;
340336

341-
// Use contents (below front matter) from previous file
342-
fileContent = await this.app.vault.read(options.attachFile);
343-
const regExp = new RegExp(this.frontMatterRexExpPattern);
344-
fileContent = fileContent.replace(regExp, '');
345-
fileContent = fileContent.startsWith('\n') ? fileContent.substring(1) : fileContent;
337+
const match = template.match(frontMatterRegex);
346338

347-
// Update updated front matter with entries from the old front matter, if it isn't defined in the new front matter
348-
Object.keys(previousMetadata).forEach(key => {
349-
const value = previousMetadata[key];
339+
if (!match || match.length !== 2) {
340+
throw new Error('Cannot find YAML front matter for template.');
341+
}
350342

351-
if (!frontMatter[key] && value) {
352-
frontMatter[key] = value;
353-
}
354-
});
355-
}
343+
let frontMatter = parseYaml(match[1]);
344+
let fileContent: string = template.replace(frontMatterRegex, '');
356345

357-
// Ensure that id, type, and dataSource are defined
358-
if (!frontMatter.id) {
359-
frontMatter.id = mediaTypeModel.id;
360-
}
346+
// Updating a previous file
347+
if (options.attachFile) {
348+
const previousMetadata = this.app.metadataCache.getFileCache(options.attachFile).frontmatter;
361349

362-
if (!frontMatter.type) {
363-
frontMatter.type = mediaTypeModel.type;
364-
}
350+
// Use contents (below front matter) from previous file
351+
fileContent = await this.app.vault.read(options.attachFile);
352+
const regExp = new RegExp(this.frontMatterRexExpPattern);
353+
fileContent = fileContent.replace(regExp, '');
354+
fileContent = fileContent.startsWith('\n') ? fileContent.substring(1) : fileContent;
365355

366-
if (!frontMatter.dataSource) {
367-
frontMatter.dataSource = mediaTypeModel.dataSource;
368-
}
356+
// Update updated front matter with entries from the old front matter, if it isn't defined in the new front matter
357+
Object.keys(previousMetadata).forEach(key => {
358+
const value = previousMetadata[key];
359+
360+
if (!frontMatter[key] && value) {
361+
frontMatter[key] = value;
362+
}
363+
});
364+
}
369365

370-
// Only support stringifyYaml for templater plugin
371-
fileContent = `---\n${stringifyYaml(frontMatter)}---\n${fileContent}`;
366+
// Ensure that id, type, and dataSource are defined
367+
if (!frontMatter.id) {
368+
frontMatter.id = mediaTypeModel.id;
369+
}
372370

373-
fileContent = executeInlineScriptsTemplates(mediaTypeModel, fileContent);
371+
if (!frontMatter.type) {
372+
frontMatter.type = mediaTypeModel.type;
373+
}
374374

375-
return fileContent;
375+
if (!frontMatter.dataSource) {
376+
frontMatter.dataSource = mediaTypeModel.dataSource;
376377
}
378+
379+
// Only support stringifyYaml for templater plugin
380+
fileContent = `---\n${stringifyYaml(frontMatter)}---\n${fileContent}`;
381+
382+
fileContent = executeInlineScriptsTemplates(mediaTypeModel, fileContent);
383+
384+
return fileContent;
377385
}
378386

379387
async attachFile(fileMetadata: any, fileContent: string, fileToAttach?: TFile): Promise<{ fileMetadata: any; fileContent: string }> {

0 commit comments

Comments
 (0)