@@ -78,13 +78,26 @@ export default class MediaDbPlugin extends Plugin {
78
78
// register command to update the open note
79
79
this . addCommand ( {
80
80
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) ' ,
82
82
checkCallback : ( checking : boolean ) => {
83
83
if ( ! this . app . workspace . getActiveFile ( ) ) {
84
84
return false ;
85
85
}
86
86
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 ) ;
88
101
}
89
102
return true ;
90
103
} ,
@@ -175,12 +188,12 @@ export default class MediaDbPlugin extends Plugin {
175
188
return ;
176
189
}
177
190
178
- await this . createMediaDbNoteFromModel ( idSearchResult ) ;
191
+ await this . createMediaDbNoteFromModel ( idSearchResult , { attachTemplate : true , openNote : true } ) ;
179
192
}
180
193
181
194
async createMediaDbNotes ( models : MediaTypeModel [ ] , attachFile ?: TFile ) : Promise < void > {
182
195
for ( const model of models ) {
183
- await this . createMediaDbNoteFromModel ( model , attachFile ) ;
196
+ await this . createMediaDbNoteFromModel ( model , { attachTemplate : true , attachFile : attachFile } ) ;
184
197
}
185
198
}
186
199
@@ -197,27 +210,27 @@ export default class MediaDbPlugin extends Plugin {
197
210
return detailModels ;
198
211
}
199
212
200
- async createMediaDbNoteFromModel ( mediaTypeModel : MediaTypeModel , attachFile ?: TFile ) : Promise < void > {
213
+ async createMediaDbNoteFromModel ( mediaTypeModel : MediaTypeModel , options : { attachTemplate ?: boolean , attachFile ?: TFile , openNote ?: boolean } ) : Promise < void > {
201
214
try {
202
215
console . debug ( 'MDB | creating new note' ) ;
203
216
204
- let fileContent = await this . generateMediaDbNoteContents ( mediaTypeModel , attachFile ) ;
217
+ let fileContent = await this . generateMediaDbNoteContents ( mediaTypeModel , { attachTemplate : options . attachTemplate , attachFile : options . attachFile } ) ;
205
218
206
- await this . createNote ( this . mediaTypeManager . getFileName ( mediaTypeModel ) , fileContent ) ;
219
+ await this . createNote ( this . mediaTypeManager . getFileName ( mediaTypeModel ) , fileContent , options . openNote ) ;
207
220
} catch ( e ) {
208
221
console . warn ( e ) ;
209
222
new Notice ( e . toString ( ) ) ;
210
223
}
211
224
}
212
225
213
- private async generateMediaDbNoteContents ( mediaTypeModel : MediaTypeModel , attachFile : TFile ) {
226
+ private async generateMediaDbNoteContents ( mediaTypeModel : MediaTypeModel , options : { attachTemplate ?: boolean , attachFile ? : TFile } ) {
214
227
let fileMetadata = this . modelPropertyMapper . convertObject ( mediaTypeModel . toMetaDataObject ( ) ) ;
215
228
let fileContent = '' ;
216
229
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 ) : '' ) ) ;
219
232
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 ;
221
234
return fileContent ;
222
235
}
223
236
@@ -230,8 +243,9 @@ export default class MediaDbPlugin extends Plugin {
230
243
fileMetadata = Object . assign ( attachFileMetadata , fileMetadata ) ;
231
244
232
245
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 ) ;
234
247
attachFileContent = attachFileContent . replace ( regExp , '' ) ;
248
+ attachFileContent = attachFileContent . startsWith ( '\n' ) ? attachFileContent . substring ( 1 ) : attachFileContent ;
235
249
fileContent += attachFileContent ;
236
250
237
251
return { fileMetadata : fileMetadata , fileContent : fileContent } ;
@@ -331,7 +345,7 @@ export default class MediaDbPlugin extends Plugin {
331
345
* Update the active note by querying the API again.
332
346
* 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.
333
347
*/
334
- async updateActiveNote ( ) {
348
+ async updateActiveNote ( onlyMetadata : boolean = false ) {
335
349
const activeFile : TFile = this . app . workspace . getActiveFile ( ) ;
336
350
if ( ! activeFile ) {
337
351
throw new Error ( 'MDB | there is no active note' ) ;
@@ -357,7 +371,12 @@ export default class MediaDbPlugin extends Plugin {
357
371
358
372
// deletion not happening anymore why is this log statement still here
359
373
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
+
361
380
}
362
381
363
382
async createEntriesFromFolder ( folder : TFolder ) {
0 commit comments