Skip to content

Commit

Permalink
Merge pull request #50 from AB1908/master
Browse files Browse the repository at this point in the history
Fix MediaDB note updates so that they preserve content
  • Loading branch information
mProjectsCode committed Sep 23, 2022
2 parents 2f0c15c + 3a73df6 commit 72b7eb8
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/main.ts
Expand Up @@ -185,23 +185,29 @@ export default class MediaDbPlugin extends Plugin {
async createMediaDbNoteFromModel(mediaTypeModel: MediaTypeModel, attachFile?: TFile): Promise<void> {
try {
console.log('MDB | Creating new note...');
// console.log(mediaTypeModel);

let fileMetadata = this.modelPropertyMapper.convertObject(mediaTypeModel.toMetaDataObject());
let fileContent = '';

({fileMetadata, fileContent} = await this.attachFile(fileMetadata, fileContent, attachFile));
({fileMetadata, fileContent} = await this.attachTemplate(fileMetadata, fileContent, await this.mediaTypeManager.getTemplate(mediaTypeModel, this.app)));

fileContent = `---\n${this.settings.useCustomYamlStringifier ? YAMLConverter.toYaml(fileMetadata) : stringifyYaml(fileMetadata)}---\n` + fileContent;
let fileContent = await this.generateMediaDbNoteContents(mediaTypeModel, attachFile);

console.log("Inside createMediaDbNote", fileContent)
await this.createNote(this.mediaTypeManager.getFileName(mediaTypeModel), fileContent);
} catch (e) {
console.warn(e);
new Notice(e.toString());
}
}

private async generateMediaDbNoteContents(mediaTypeModel: MediaTypeModel, attachFile: TFile) {
let fileMetadata = this.modelPropertyMapper.convertObject(mediaTypeModel.toMetaDataObject());
let fileContent = '';

({ fileMetadata, fileContent } = await this.attachFile(fileMetadata, fileContent, attachFile));
console.log("Inside createYAML", attachFile);
({ fileMetadata, fileContent } = await this.attachTemplate(fileMetadata, fileContent, await this.mediaTypeManager.getTemplate(mediaTypeModel, this.app)));

fileContent = `---\n${this.settings.useCustomYamlStringifier ? YAMLConverter.toYaml(fileMetadata) : stringifyYaml(fileMetadata)}---` + fileContent;
return fileContent;
}

async attachFile(fileMetadata: any, fileContent: string, fileToAttach?: TFile): Promise<{ fileMetadata: any, fileContent: string }> {
if (!fileToAttach) {
return {fileMetadata: fileMetadata, fileContent: fileContent};
Expand All @@ -219,7 +225,8 @@ export default class MediaDbPlugin extends Plugin {
let attachFileContent: string = await this.app.vault.read(fileToAttach);
const regExp = new RegExp('^(---)\\n[\\s\\S]*\\n---');
attachFileContent = attachFileContent.replace(regExp, '');
fileContent += '\n' + attachFileContent;
fileContent += attachFileContent;
console.log("Inside attachfile", fileContent)

return {fileMetadata: fileMetadata, fileContent: fileContent};
}
Expand All @@ -234,7 +241,7 @@ export default class MediaDbPlugin extends Plugin {

const regExp = new RegExp('^(---)\\n[\\s\\S]*\\n---');
const attachFileContent = template.replace(regExp, '');
fileContent += '\n' + attachFileContent;
fileContent += attachFileContent;

return {fileMetadata: fileMetadata, fileContent: fileContent};
}
Expand Down Expand Up @@ -331,8 +338,7 @@ export default class MediaDbPlugin extends Plugin {
newMediaTypeModel = Object.assign(oldMediaTypeModel, newMediaTypeModel.getWithOutUserData());

console.log('MDB | deleting old entry');
await this.app.vault.delete(activeFile);
await this.createMediaDbNoteFromModel(newMediaTypeModel);
await this.createMediaDbNoteFromModel(newMediaTypeModel, activeFile);
}

async createEntriesFromFolder(folder: TFolder) {
Expand All @@ -347,7 +353,7 @@ export default class MediaDbPlugin extends Plugin {

for (const child of folder.children) {
if (child instanceof TFile) {
const file = child as TFile;
const file: TFile = child;
if (canceled) {
erroredFiles.push({filePath: file.path, error: 'user canceled'});
continue;
Expand Down Expand Up @@ -426,7 +432,7 @@ export default class MediaDbPlugin extends Plugin {
const filePath = `${this.settings.folder.replace(/\/$/, '')}/${title}.md`;

const table = [['file', 'error']].concat(erroredFiles.map(x => [x.filePath, x.error]));
// console.log(table)

let fileContent = `# ${title}\n\n${markdownTable(table)}`;

const targetFile = await this.app.vault.create(filePath, fileContent);
Expand Down

0 comments on commit 72b7eb8

Please sign in to comment.