@@ -78,13 +78,26 @@ export default class MediaDbPlugin extends Plugin {
7878 // register command to update the open note
7979 this . addCommand ( {
8080 id : 'update-media-db-note' ,
81- name : 'Update the open note, if it is a Media DB entry. ' ,
81+ name : 'Update open note (this will recreate the note) ' ,
8282 checkCallback : ( checking : boolean ) => {
8383 if ( ! this . app . workspace . getActiveFile ( ) ) {
8484 return false ;
8585 }
8686 if ( ! checking ) {
87- this . updateActiveNote ( ) ;
87+ this . updateActiveNote ( false ) ;
88+ }
89+ return true ;
90+ } ,
91+ } ) ;
92+ this . addCommand ( {
93+ id : 'update-media-db-note-metadata' ,
94+ name : 'Update metadata' ,
95+ checkCallback : ( checking : boolean ) => {
96+ if ( ! this . app . workspace . getActiveFile ( ) ) {
97+ return false ;
98+ }
99+ if ( ! checking ) {
100+ this . updateActiveNote ( true ) ;
88101 }
89102 return true ;
90103 } ,
@@ -175,12 +188,12 @@ export default class MediaDbPlugin extends Plugin {
175188 return ;
176189 }
177190
178- await this . createMediaDbNoteFromModel ( idSearchResult ) ;
191+ await this . createMediaDbNoteFromModel ( idSearchResult , { attachTemplate : true , openNote : true } ) ;
179192 }
180193
181194 async createMediaDbNotes ( models : MediaTypeModel [ ] , attachFile ?: TFile ) : Promise < void > {
182195 for ( const model of models ) {
183- await this . createMediaDbNoteFromModel ( model , attachFile ) ;
196+ await this . createMediaDbNoteFromModel ( model , { attachTemplate : true , attachFile : attachFile } ) ;
184197 }
185198 }
186199
@@ -197,27 +210,27 @@ export default class MediaDbPlugin extends Plugin {
197210 return detailModels ;
198211 }
199212
200- async createMediaDbNoteFromModel ( mediaTypeModel : MediaTypeModel , attachFile ?: TFile ) : Promise < void > {
213+ async createMediaDbNoteFromModel ( mediaTypeModel : MediaTypeModel , options : { attachTemplate ?: boolean , attachFile ?: TFile , openNote ?: boolean } ) : Promise < void > {
201214 try {
202215 console . debug ( 'MDB | creating new note' ) ;
203216
204- let fileContent = await this . generateMediaDbNoteContents ( mediaTypeModel , attachFile ) ;
217+ let fileContent = await this . generateMediaDbNoteContents ( mediaTypeModel , { attachTemplate : options . attachTemplate , attachFile : options . attachFile } ) ;
205218
206- await this . createNote ( this . mediaTypeManager . getFileName ( mediaTypeModel ) , fileContent ) ;
219+ await this . createNote ( this . mediaTypeManager . getFileName ( mediaTypeModel ) , fileContent , options . openNote ) ;
207220 } catch ( e ) {
208221 console . warn ( e ) ;
209222 new Notice ( e . toString ( ) ) ;
210223 }
211224 }
212225
213- private async generateMediaDbNoteContents ( mediaTypeModel : MediaTypeModel , attachFile : TFile ) {
226+ private async generateMediaDbNoteContents ( mediaTypeModel : MediaTypeModel , options : { attachTemplate ?: boolean , attachFile ? : TFile } ) {
214227 let fileMetadata = this . modelPropertyMapper . convertObject ( mediaTypeModel . toMetaDataObject ( ) ) ;
215228 let fileContent = '' ;
216229
217- ( { fileMetadata, fileContent} = await this . attachFile ( fileMetadata , fileContent , attachFile ) ) ;
218- ( { fileMetadata, fileContent} = await this . attachTemplate ( fileMetadata , fileContent , await this . mediaTypeManager . getTemplate ( mediaTypeModel , this . app ) ) ) ;
230+ ( { fileMetadata, fileContent} = await this . attachFile ( fileMetadata , fileContent , options . attachFile ) ) ;
231+ ( { fileMetadata, fileContent} = await this . attachTemplate ( fileMetadata , fileContent , options . attachTemplate ? await this . mediaTypeManager . getTemplate ( mediaTypeModel , this . app ) : '' ) ) ;
219232
220- fileContent = `---\n${ this . settings . useCustomYamlStringifier ? YAMLConverter . toYaml ( fileMetadata ) : stringifyYaml ( fileMetadata ) } ---` + fileContent ;
233+ fileContent = `---\n${ this . settings . useCustomYamlStringifier ? YAMLConverter . toYaml ( fileMetadata ) : stringifyYaml ( fileMetadata ) } ---\n ` + fileContent ;
221234 return fileContent ;
222235 }
223236
@@ -230,8 +243,9 @@ export default class MediaDbPlugin extends Plugin {
230243 fileMetadata = Object . assign ( attachFileMetadata , fileMetadata ) ;
231244
232245 let attachFileContent : string = await this . app . vault . read ( fileToAttach ) ;
233- const regExp = new RegExp ( '^(---)\\n[\\s\\S]*\\n---' ) ;
246+ const regExp = new RegExp ( this . frontMatterRexExpPattern ) ;
234247 attachFileContent = attachFileContent . replace ( regExp , '' ) ;
248+ attachFileContent = attachFileContent . startsWith ( '\n' ) ? attachFileContent . substring ( 1 ) : attachFileContent ;
235249 fileContent += attachFileContent ;
236250
237251 return { fileMetadata : fileMetadata , fileContent : fileContent } ;
@@ -331,7 +345,7 @@ export default class MediaDbPlugin extends Plugin {
331345 * Update the active note by querying the API again.
332346 * Tries to read the type, id and dataSource of the active note. If successful it will query the api, delete the old note and create a new one.
333347 */
334- async updateActiveNote ( ) {
348+ async updateActiveNote ( onlyMetadata : boolean = false ) {
335349 const activeFile : TFile = this . app . workspace . getActiveFile ( ) ;
336350 if ( ! activeFile ) {
337351 throw new Error ( 'MDB | there is no active note' ) ;
@@ -357,7 +371,12 @@ export default class MediaDbPlugin extends Plugin {
357371
358372 // deletion not happening anymore why is this log statement still here
359373 console . debug ( 'MDB | deleting old entry' ) ;
360- await this . createMediaDbNoteFromModel ( newMediaTypeModel , activeFile ) ;
374+ if ( onlyMetadata ) {
375+ await this . createMediaDbNoteFromModel ( newMediaTypeModel , { attachFile : activeFile , openNote : true } ) ;
376+ } else {
377+ await this . createMediaDbNoteFromModel ( newMediaTypeModel , { attachTemplate : true , openNote : true } ) ;
378+ }
379+
361380 }
362381
363382 async createEntriesFromFolder ( folder : TFolder ) {
0 commit comments