@@ -180,29 +180,12 @@ export async function updateRowFileProxy(file: TFile, columnId: string, newValue
180180export async function updateRowFile ( file : TFile , columnId : string , newValue : Literal , columns : TableColumn [ ] , ddbbConfig : LocalSettings , option : string ) : Promise < void > {
181181 LOGGER . info ( `=>updateRowFile. file: ${ file . path } | columnId: ${ columnId } | newValue: ${ newValue } | option: ${ option } ` ) ;
182182 const content = await VaultManagerDB . obtainContentFromTfile ( file ) ;
183+ const contentHasFrontmatter = hasFrontmatter ( content ) ;
183184 const frontmatterKeys = VaultManagerDB . obtainFrontmatterKeys ( content ) ;
184185 const rowFields = obtainRowDatabaseFields ( file , columns , frontmatterKeys ) ;
185186 const column = columns . find (
186187 c => c . key === ( UpdateRowOptions . COLUMN_KEY === option ? newValue : columnId )
187188 ) ;
188-
189- // Adds an empty frontmatter at the beginning of the file
190- async function addFrontmatter ( ) : Promise < void > {
191- /* Regex explanation
192- * group 1 all content
193- */
194- const frontmatterRegex = / ( ^ [ \s \S ] * $ ) / g;
195-
196-
197- const noteObject = {
198- action : 'replace' ,
199- file : file ,
200- regexp : frontmatterRegex ,
201- newValue : `---\n---\n$1`
202- } ;
203- // update content on disk and in memory
204- await VaultManagerDB . editNoteContent ( noteObject ) ;
205- }
206189 /*******************************************************************************************
207190 * FRONTMATTER GROUP FUNCTIONS
208191 *******************************************************************************************/
@@ -248,15 +231,18 @@ export async function updateRowFile(file: TFile, columnId: string, newValue: Lit
248231 }
249232
250233 async function persistFrontmatter ( deletedColumn ?: string ) : Promise < void > {
251- const frontmatterGroupRegex = / ^ - - - [ \s \S ] + ?- - - / g;
252- const frontmatterFieldsText = parseFrontmatterFieldsToString ( rowFields , ddbbConfig , deletedColumn ) ;
253- const noteObject = {
254- action : 'replace' ,
255- file : file ,
256- regexp : frontmatterGroupRegex ,
257- newValue : `${ frontmatterFieldsText } `
258- } ;
259- await VaultManagerDB . editNoteContent ( noteObject ) ;
234+ // If the frontmatter is empty, do not persist it
235+ if ( Object . keys ( rowFields . frontmatter ) . length > 0 ) {
236+ const frontmatterGroupRegex = contentHasFrontmatter ? / ^ - - - [ \s \S ] + ?- - - / g : / ( ^ [ \s \S ] * $ ) / g;
237+ const frontmatterFieldsText = parseFrontmatterFieldsToString ( rowFields , ddbbConfig , deletedColumn ) ;
238+ const noteObject = {
239+ action : 'replace' ,
240+ file : file ,
241+ regexp : frontmatterGroupRegex ,
242+ newValue : contentHasFrontmatter ? `${ frontmatterFieldsText } ` : `${ frontmatterFieldsText } \n$1` ,
243+ } ;
244+ await VaultManagerDB . editNoteContent ( noteObject ) ;
245+ }
260246 }
261247
262248 /*******************************************************************************************
@@ -340,11 +326,6 @@ export async function updateRowFile(file: TFile, columnId: string, newValue: Lit
340326 updateOptions [ UpdateRowOptions . INLINE_VALUE ] = inlineColumnEdit ;
341327 // Execute action
342328 if ( updateOptions [ option ] ) {
343- // Check if file has frontmatter
344- if ( ! hasFrontmatter ( content ) ) {
345- // If not, add it
346- await addFrontmatter ( ) ;
347- }
348329 // Then execute the action
349330 await updateOptions [ option ] ( ) ;
350331 } else {
0 commit comments