Skip to content

Commit

Permalink
refactor: abstract front matter generating functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kelszo committed Jan 24, 2024
1 parent d499071 commit f4fcc3d
Showing 1 changed file with 54 additions and 46 deletions.
100 changes: 54 additions & 46 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,67 +313,75 @@ export default class MediaDbPlugin extends Plugin {
let template = await this.mediaTypeManager.getTemplate(mediaTypeModel, this.app);

if (this.settings.useDefaultFrontMatter || !template) {
let fileMetadata = this.modelPropertyMapper.convertObject(mediaTypeModel.toMetaDataObject());
let fileContent = '';
template = options.attachTemplate ? template : '';

({ fileMetadata, fileContent } = await this.attachFile(fileMetadata, fileContent, options.attachFile));
({ fileMetadata, fileContent } = await this.attachTemplate(fileMetadata, fileContent, template));

fileContent = `---\n${this.settings.useCustomYamlStringifier ? YAMLConverter.toYaml(fileMetadata) : stringifyYaml(fileMetadata)}---\n` + fileContent;
return fileContent;
return this.generateContentWithDefaultFrontMatter(mediaTypeModel, options, template);
} else {
const frontMatterRegex = /^---*\n([\s\S]*?)\n---\h*/;
return this.generateContentWithCustomFrontMatter(mediaTypeModel, options, template);
}
}

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

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

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

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

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

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

if (!frontMatter[key] && value) {
frontMatter[key] = value;
}
});
}
let frontMatter = parseYaml(match[1]);
let fileContent: string = template.replace(frontMatterRegex, '');

// Ensure that id, type, and dataSource are defined
if (!frontMatter.id) {
frontMatter.id = mediaTypeModel.id;
}
// Updating a previous file
if (options.attachFile) {
const previousMetadata = this.app.metadataCache.getFileCache(options.attachFile).frontmatter;

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

if (!frontMatter.dataSource) {
frontMatter.dataSource = mediaTypeModel.dataSource;
}
// Update updated front matter with entries from the old front matter, if it isn't defined in the new front matter
Object.keys(previousMetadata).forEach(key => {
const value = previousMetadata[key];

if (!frontMatter[key] && value) {
frontMatter[key] = value;
}
});
}

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

fileContent = executeInlineScriptsTemplates(mediaTypeModel, fileContent);
if (!frontMatter.type) {
frontMatter.type = mediaTypeModel.type;
}

return fileContent;
if (!frontMatter.dataSource) {
frontMatter.dataSource = mediaTypeModel.dataSource;
}

// Only support stringifyYaml for templater plugin
fileContent = `---\n${stringifyYaml(frontMatter)}---\n${fileContent}`;

fileContent = executeInlineScriptsTemplates(mediaTypeModel, fileContent);

return fileContent;
}

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

0 comments on commit f4fcc3d

Please sign in to comment.