11import { RowDataType , TableColumn } from "cdm/FolderModel" ;
22import { LocalSettings } from "cdm/SettingsModel" ;
3- import { TFile } from "obsidian" ;
3+ import { requireApiVersion , TFile } from "obsidian" ;
44import { LOGGER } from "services/Logger" ;
55import { ParseService } from "services/ParseService" ;
66import { InputType , UpdateRowOptions } from "helpers/Constants" ;
@@ -10,6 +10,8 @@ import { EditionError, showDBError } from "errors/ErrorTypes";
1010import obtainRowDatabaseFields from "parsers/FileToRowDatabaseFields" ;
1111import { EditArguments } from "cdm/ServicesModel" ;
1212import NoteContentActionBuilder from "patterns/builders/NoteContentActionBuilder" ;
13+ import { parseFrontmatterFieldsToString } from "parsers/RowDatabaseFieldsToFile" ;
14+ import { hasFrontmatter } from "helpers/VaultManagement" ;
1315
1416class EditEngine {
1517 private static instance : EditEngine ;
@@ -109,6 +111,7 @@ class EditEngine {
109111 const content = await VaultManagerDB . obtainContentFromTfile ( file ) ;
110112 const frontmatterKeys = VaultManagerDB . obtainFrontmatterKeys ( content ) ;
111113 const rowFields = obtainRowDatabaseFields ( file , columns , frontmatterKeys ) ;
114+ const contentHasFrontmatter = hasFrontmatter ( content ) ;
112115 const column = columns . find (
113116 c => c . key === ( UpdateRowOptions . COLUMN_KEY === option ? newValue : columnId )
114117 ) ;
@@ -157,18 +160,30 @@ class EditEngine {
157160 }
158161
159162 async function persistFrontmatter ( deletedColumn ?: string ) : Promise < void > {
160- await app . fileManager . processFrontMatter ( file , ( frontmatter ) => {
161- frontmatter [ columnId ] = ParseService . parseLiteral (
162- rowFields . frontmatter [ columnId ] ,
163- InputType . MARKDOWN ,
164- ddbbConfig
165- ) ;
166-
167- if ( deletedColumn ) {
168- delete frontmatter [ deletedColumn ] ;
169- }
170- } ) ;
163+ if ( requireApiVersion ( "1.1.1" ) ) {
164+ await app . fileManager . processFrontMatter ( file , ( frontmatter ) => {
165+ frontmatter [ columnId ] = ParseService . parseLiteral (
166+ rowFields . frontmatter [ columnId ] ,
167+ InputType . MARKDOWN ,
168+ ddbbConfig
169+ ) ;
171170
171+ if ( deletedColumn ) {
172+ delete frontmatter [ deletedColumn ] ;
173+ }
174+ } ) ;
175+ } else {
176+ const frontmatterGroupRegex = contentHasFrontmatter ? / ^ - - - [ \s \S ] + ?- - - \n * / g : / ( ^ [ \s \S ] * $ ) / g;
177+ const frontmatterFieldsText = parseFrontmatterFieldsToString ( rowFields , ddbbConfig , deletedColumn ) ;
178+ const newContent = contentHasFrontmatter ? `${ frontmatterFieldsText } \n` : `${ frontmatterFieldsText ? frontmatterFieldsText . concat ( '\n' ) : frontmatterFieldsText } $1` ;
179+ const builder = new NoteContentActionBuilder ( )
180+ . setContent ( content )
181+ . setFile ( file )
182+ . addRegExp ( frontmatterGroupRegex )
183+ . addRegExpNewValue ( newContent )
184+ . build ( ) ;
185+ await VaultManagerDB . editNoteContent ( builder ) ;
186+ }
172187 }
173188
174189 /*******************************************************************************************
0 commit comments