1
1
import { RowDataType , TableColumn } from "cdm/FolderModel" ;
2
2
import { LocalSettings } from "cdm/SettingsModel" ;
3
- import { TFile } from "obsidian" ;
3
+ import { requireApiVersion , TFile } from "obsidian" ;
4
4
import { LOGGER } from "services/Logger" ;
5
5
import { ParseService } from "services/ParseService" ;
6
6
import { InputType , UpdateRowOptions } from "helpers/Constants" ;
@@ -10,6 +10,8 @@ import { EditionError, showDBError } from "errors/ErrorTypes";
10
10
import obtainRowDatabaseFields from "parsers/FileToRowDatabaseFields" ;
11
11
import { EditArguments } from "cdm/ServicesModel" ;
12
12
import NoteContentActionBuilder from "patterns/builders/NoteContentActionBuilder" ;
13
+ import { parseFrontmatterFieldsToString } from "parsers/RowDatabaseFieldsToFile" ;
14
+ import { hasFrontmatter } from "helpers/VaultManagement" ;
13
15
14
16
class EditEngine {
15
17
private static instance : EditEngine ;
@@ -109,6 +111,7 @@ class EditEngine {
109
111
const content = await VaultManagerDB . obtainContentFromTfile ( file ) ;
110
112
const frontmatterKeys = VaultManagerDB . obtainFrontmatterKeys ( content ) ;
111
113
const rowFields = obtainRowDatabaseFields ( file , columns , frontmatterKeys ) ;
114
+ const contentHasFrontmatter = hasFrontmatter ( content ) ;
112
115
const column = columns . find (
113
116
c => c . key === ( UpdateRowOptions . COLUMN_KEY === option ? newValue : columnId )
114
117
) ;
@@ -157,18 +160,30 @@ class EditEngine {
157
160
}
158
161
159
162
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
+ ) ;
171
170
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
+ }
172
187
}
173
188
174
189
/*******************************************************************************************
0 commit comments