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

Commit a1d6b08

Browse files
committed
Merge branch '94-fr-insert-tag-in-new-note-when-source-is-tag'
2 parents 4bb6cff + b767b28 commit a1d6b08

File tree

2 files changed

+42
-90
lines changed

2 files changed

+42
-90
lines changed
Lines changed: 32 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { YamlHandlerResponse } from 'cdm/MashallModel';
2-
import { SourceDataTypes, CellSizeOptions } from 'helpers/Constants';
2+
import { LocalSettings } from 'cdm/SettingsModel';
3+
import { SourceDataTypes, CellSizeOptions, DEFAULT_SETTINGS } from 'helpers/Constants';
34
import { Literal } from 'obsidian-dataview';
45
import { AbstractYamlHandler } from 'parsers/handlers/marshall/AbstractYamlPropertyHandler';
56

@@ -8,99 +9,40 @@ export class MarshallConfigHandler extends AbstractYamlHandler {
89

910
public handle(handlerResponse: YamlHandlerResponse): YamlHandlerResponse {
1011
const { yaml } = handlerResponse;
11-
// check if config is defined. If not, load default
12-
if (yaml.config) {
13-
// if enable_show_state is not defined, load default
14-
if (checkNullable(yaml.config.enable_show_state)) {
15-
this.addError(`There was not enable_debug_mode key in yaml. Default will be loaded`);
16-
yaml.config.enable_show_state = false;
17-
}
18-
yaml.config.enable_show_state = parseBoolean(yaml.config.enable_show_state);
19-
20-
// if group_folder_column is not defined, load empty (optional)
21-
if (checkNullable(yaml.config.group_folder_column)) {
22-
yaml.config.group_folder_column = '';
23-
}
24-
25-
// if remove_field_when_delete_column is not defined, load default
26-
if (checkNullable(yaml.config.remove_field_when_delete_column)) {
27-
this.addError(`There was not remove_field_when_delete_column key in yaml. Default will be loaded`);
28-
yaml.config.remove_field_when_delete_column = false;
29-
}
30-
yaml.config.remove_field_when_delete_column = parseBoolean(yaml.config.remove_field_when_delete_column);
31-
32-
// if cell_size is not defined, load default
33-
if (checkNullable(yaml.config.cell_size)) {
34-
this.addError(`There was not cell_size key in yaml. Default will be loaded`);
35-
yaml.config.cell_size = CellSizeOptions.NORMAL;
36-
}
37-
38-
// if sticky_first_column is not defined, load default
39-
if (checkNullable(yaml.config.sticky_first_column)) {
40-
this.addError(`There was not sticky_first_column key in yaml. Default will be loaded`);
41-
yaml.config.sticky_first_column = false;
42-
}
43-
yaml.config.sticky_first_column = parseBoolean(yaml.config.sticky_first_column);
44-
45-
// if show_metadata_created is not defined, load default
46-
if (checkNullable(yaml.config.show_metadata_created)) {
47-
this.addError(`There was not show_metadata_created key in yaml. Default will be loaded`);
48-
yaml.config.show_metadata_created = false;
49-
}
50-
yaml.config.show_metadata_created = parseBoolean(yaml.config.show_metadata_created);
51-
52-
// if show_metadata_modified is not defined, load default
53-
if (checkNullable(yaml.config.show_metadata_modified)) {
54-
this.addError(`There was not show_metadata_modified key in yaml. Default will be loaded`);
55-
yaml.config.show_metadata_modified = false;
56-
}
57-
yaml.config.show_metadata_modified = parseBoolean(yaml.config.show_metadata_modified);
58-
59-
// if show_metadata_modified is not defined, load default
60-
if (checkNullable(yaml.config.show_metadata_tasks)) {
61-
this.addError(`There was not show_metadata_tasks key in yaml. Default will be loaded`);
62-
yaml.config.show_metadata_tasks = false;
63-
}
64-
yaml.config.show_metadata_tasks = parseBoolean(yaml.config.show_metadata_tasks);
65-
66-
// if source_data is not defined, load default
67-
if (checkNullable(yaml.config.source_data)) {
68-
this.addError(`There was not source_data key in yaml. Default will be loaded`);
69-
yaml.config.source_data = SourceDataTypes.CURRENT_FOLDER;
70-
}
71-
72-
// if source_form_result is not defined, load empty (optional)
73-
if (checkNullable(yaml.config.source_form_result)) {
74-
yaml.config.source_form_result = '';
75-
}
76-
77-
// if frontmatter_quote_wrap is not defined, load default
78-
if (checkNullable(yaml.config.frontmatter_quote_wrap)) {
79-
this.addError(`There was not frontmatter_quote_wrap key in yaml. Default will be loaded`);
80-
yaml.config.frontmatter_quote_wrap = false;
81-
}
82-
yaml.config.frontmatter_quote_wrap = parseBoolean(yaml.config.frontmatter_quote_wrap);
83-
84-
// if row_templates_folder is not defined, load root folder as default
85-
if (checkNullable(yaml.config.row_templates_folder)) {
86-
this.addError(`There was not templates_folder key in yaml. Default will be loaded`);
87-
yaml.config.row_templates_folder = '/';
88-
}
89-
90-
// if current_row_template is not defined, load empty (optional)
91-
if (checkNullable(yaml.config.current_row_template)) {
92-
yaml.config.current_row_template = '';
93-
}
12+
if (this.checkNullable(yaml.config)) {
13+
yaml.config = DEFAULT_SETTINGS.local_settings;
14+
this.addError(`configuration was null or undefined, using default configuration instead`);
15+
} else {
16+
Object.entries(DEFAULT_SETTINGS.local_settings).forEach(([key, value]) => {
17+
if (this.checkNullable(yaml.config[key as keyof LocalSettings])) {
18+
yaml.config = this.loadDefaultConfig(key, value, yaml.config);
19+
if (value !== "") {
20+
this.addError(`There was not "${key}" key in yaml. Default value "${value}" will be loaded`);
21+
}
22+
}
23+
// Check type of default value
24+
if (typeof value === "boolean") {
25+
yaml.config = this.parseBoolean(key, yaml.config);
26+
}
27+
});
9428
}
9529
handlerResponse.yaml = yaml;
9630
return this.goNext(handlerResponse);
9731
}
98-
}
9932

100-
function checkNullable<T>(value: T): boolean {
101-
return value === null || value === undefined;
33+
loadDefaultConfig<K extends keyof LocalSettings>(key: K, value: Literal, localSettings: LocalSettings): LocalSettings {
34+
localSettings[key] = value as any;
35+
return localSettings;
36+
}
37+
38+
checkNullable<T>(value: T): boolean {
39+
return value === null || value === undefined;
40+
}
41+
42+
parseBoolean<K extends keyof LocalSettings>(key: K, localSettings: LocalSettings): LocalSettings {
43+
const parsedValue = localSettings[key].toString().toLowerCase() === 'true';
44+
localSettings[key] = parsedValue as any;
45+
return localSettings;
46+
}
10247
}
10348

104-
function parseBoolean(value: Literal): boolean {
105-
return value.toString().toLowerCase() === 'true';
106-
}

src/services/FileManagerService.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Notice, parseYaml, TFile, TFolder } from "obsidian";
77
import { parseFrontmatterFieldsToString, parseInlineFieldsToString } from "parsers/RowDatabaseFieldsToFile";
88
import { LOGGER } from "services/Logger";
99
import { DataviewService } from "services/DataviewService";
10+
import { SourceDataTypes } from "helpers/Constants";
1011
class VaultManager {
1112
private static instance: VaultManager;
1213

@@ -23,6 +24,15 @@ class VaultManager {
2324
filename ?? "Untitled"
2425
);
2526
let content = parseFrontmatterFieldsToString(databasefields, localSettings).concat("\n").concat(parseInlineFieldsToString(databasefields));
27+
28+
// Custom content by source
29+
switch (localSettings.source_data) {
30+
case SourceDataTypes.TAG:
31+
content = content.concat(`#${localSettings.source_form_result}\n`);
32+
break;
33+
default:
34+
}
35+
2636
// Obtain content from current row template
2737
try {
2838
if (DataviewService.isTruthy(localSettings.current_row_template) && localSettings.current_row_template.endsWith(".md")) {

0 commit comments

Comments
 (0)