Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit d500ce5

Browse files
committed
working on less noisy regex
1 parent 044962b commit d500ce5

File tree

4 files changed

+32
-34
lines changed

4 files changed

+32
-34
lines changed

src/helpers/FileContent.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,22 @@ export class FileContent {
99
}
1010

1111
replaceAll(pattern_to_replace: RegExp, input: string): FileContent {
12-
if (input !== '') {
13-
if (Array.isArray(pattern_to_replace)) {
14-
pattern_to_replace.forEach(
15-
(regex, index) => {
16-
this.value = this.value.replaceAll(
17-
regex,
18-
input[index]
19-
);
20-
}
21-
);
22-
} else {
23-
this.value = this.value.replaceAll(
24-
pattern_to_replace,
25-
input
26-
);
27-
}
12+
if (Array.isArray(pattern_to_replace)) {
13+
pattern_to_replace.forEach(
14+
(regex, index) => {
15+
this.value = this.value.replaceAll(
16+
regex,
17+
input[index]
18+
);
19+
}
20+
);
21+
} else {
22+
this.value = this.value.replaceAll(
23+
pattern_to_replace,
24+
input
25+
);
2826
}
27+
2928
return this;
3029
}
3130

src/helpers/FileManagement.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ export function inline_regex_target_in_function_of(position: string, columnId: s
7474
let regex_target = "";
7575
switch (position) {
7676
case INLINE_POSITION.BOTTOM:
77-
regex_target = contentHasFrontmatter ? `$1$2\n${columnId}:: ${newValue}` : `$1\n${columnId}:: ${newValue}`;
77+
regex_target = contentHasFrontmatter ? `$1$2${columnId}:: ${newValue}` : `$1${columnId}:: ${newValue}`;
7878
break;
7979
default:
80-
regex_target = contentHasFrontmatter ? `$1\n${columnId}:: ${newValue}$2` : `${columnId}:: ${newValue}\n$1`;
80+
regex_target = contentHasFrontmatter ? `$1${columnId}:: ${newValue}\n$2` : `${columnId}:: ${newValue}\n$1`;
8181
}
8282
return regex_target;
8383
}

src/helpers/VaultManagement.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -231,18 +231,16 @@ export async function updateRowFile(file: TFile, columnId: string, newValue: Lit
231231
}
232232

233233
async function persistFrontmatter(deletedColumn?: string): Promise<void> {
234-
// If the frontmatter is empty, do not persist it
235-
if (Object.keys(rowFields.frontmatter).length > 0 || deletedColumn !== undefined) {
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-
}
234+
console.log('persistFrontmatter');
235+
const frontmatterGroupRegex = contentHasFrontmatter ? /^---[\s\S]+?---\n/g : /(^[\s\S]*$)/g;
236+
const frontmatterFieldsText = parseFrontmatterFieldsToString(rowFields, ddbbConfig, deletedColumn);
237+
const noteObject = {
238+
action: 'replace',
239+
file: file,
240+
regexp: frontmatterGroupRegex,
241+
newValue: contentHasFrontmatter ? `${frontmatterFieldsText}` : `${frontmatterFieldsText}\n$1`,
242+
};
243+
await VaultManagerDB.editNoteContent(noteObject);
246244
}
247245

248246
/*******************************************************************************************
@@ -288,7 +286,7 @@ export async function updateRowFile(file: TFile, columnId: string, newValue: Lit
288286
}
289287

290288
async function inlineAddColumn(): Promise<void> {
291-
const inlineAddRegex = contentHasFrontmatter ? new RegExp(`(^---[\\s\\S]+?---)+([\\s\\S]*$)`, 'g') : new RegExp(`(^[\\s\\S]*$)`, 'g');
289+
const inlineAddRegex = contentHasFrontmatter ? new RegExp(`(^---[\\s\\S]+?---\n)+([\\s\\S]*$)`, 'g') : new RegExp(`(^[\\s\\S]*$)`, 'g');
292290
const noteObject = {
293291
action: 'replace',
294292
file: file,

src/parsers/RowDatabaseFieldsToFile.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import { Literal } from "obsidian-dataview/lib/data-model/value";
55
import { DataviewService } from "services/DataviewService";
66
export const parseFrontmatterFieldsToString = (databaseFields: RowDatabaseFields, localSettings: LocalSettings, deletedColumn?: string): string => {
77
const frontmatterFields = databaseFields.frontmatter;
8-
const array: string[] = [];
9-
array.push(`---`);
8+
let array: string[] = [];
109
Object.keys(frontmatterFields).forEach(key => {
1110
if (key !== deletedColumn) {
1211
array.push(...parseLiteralToString(frontmatterFields[key], 0, localSettings, key));
1312
}
1413
});
15-
array.push(`---`);
14+
if (array.length > 0) {
15+
array = [`---`, ...array, `---`];
16+
}
1617
return array.join('\n');
1718
}
1819

0 commit comments

Comments
 (0)