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

Commit 5d1d19d

Browse files
committed
differ with booleans and numbers quotes
1 parent 43231f7 commit 5d1d19d

File tree

5 files changed

+32
-28
lines changed

5 files changed

+32
-28
lines changed

src/components/reducers/DatabaseDispatch.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ export function databaseReducer(state: TableDataType, action: ActionType) {
191191
...row,
192192
[action.columnId]: DataviewService.parseLiteral(
193193
row[action.columnId],
194-
action.dataType // Destination type to parse
194+
action.dataType, // Destination type to parse
195+
state.view.diskConfig.yaml.config
195196
),
196197
}));
197198
// Update state

src/helpers/VaultManagement.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export async function adapterTFilesToRows(folderPath: string, columns: TableColu
7878
}
7979
folderFiles.map((page) => {
8080
const noteInfo = new NoteInfo(page, ++id);
81-
rows.push(noteInfo.getRowDataType(columns));
81+
rows.push(noteInfo.getRowDataType(columns, dbYaml.config));
8282
});
8383

8484
LOGGER.debug(`<= adapterTFilesToRows. number of rows:${rows.length}`);
@@ -199,7 +199,7 @@ export async function updateRowFile(file: TFile, columnId: string, newValue: Lit
199199

200200
// Check if the column is already in the frontmatter
201201
// assign an empty value to the new key
202-
rowFields.frontmatter[DataviewService.parseLiteral(newValue, DataTypes.TEXT) as string] = rowFields.frontmatter[columnId] ?? "";
202+
rowFields.frontmatter[DataviewService.parseLiteral(newValue, DataTypes.TEXT, state.view.diskConfig.yaml.config) as string] = rowFields.frontmatter[columnId] ?? "";
203203
delete rowFields.frontmatter[columnId];
204204
await persistFrontmatter(columnId);
205205
}
@@ -243,7 +243,7 @@ export async function updateRowFile(file: TFile, columnId: string, newValue: Lit
243243
action: 'replace',
244244
file: file,
245245
regexp: inlineFieldRegex,
246-
newValue: `$1 ${DataviewService.parseLiteral(newValue, DataTypes.MARKDOWN) as string, true}`
246+
newValue: `$1 ${DataviewService.parseLiteral(newValue, DataTypes.MARKDOWN, state.view.diskConfig.yaml.config) as string, true}`
247247
};
248248
await VaultManagerDB.editNoteContent(noteObject);
249249
await persistFrontmatter();

src/parsers/RowDatabaseFieldsToFile.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,9 @@ function parseLiteralToString(literal: Literal, level: number, localSettings: Lo
3535
});
3636
}
3737
else if (key) {
38-
literalBlock.push(`${" ".repeat(level)}${key}: ${wrapWithQuotes(DataviewService.parseLiteral(literal, DataTypes.MARKDOWN), localSettings.frontmatter_quote_wrap)}`);
38+
literalBlock.push(`${" ".repeat(level)}${key}: ${DataviewService.parseLiteral(literal, DataTypes.MARKDOWN, localSettings)}`);
3939
} else {
40-
literalBlock.push(`${" ".repeat(level)}- ${wrapWithQuotes(DataviewService.parseLiteral(literal, DataTypes.MARKDOWN), localSettings.frontmatter_quote_wrap)}`);
40+
literalBlock.push(`${" ".repeat(level)}- ${DataviewService.parseLiteral(literal, DataTypes.MARKDOWN, localSettings)}`);
4141
}
4242
return literalBlock;
43-
}
44-
45-
function wrapWithQuotes(literal: Literal, isWrapActivated: boolean): string {
46-
if (!literal.toString().startsWith('"') && !literal.toString().endsWith('"') && isWrapActivated) {
47-
return `\"${literal.toString()}\"`;
48-
}
49-
return literal.toString();
5043
}

src/services/DataviewService.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { DataviewApi, getAPI, isPluginEnabled } from "obsidian-dataview";
55
import { Literal, WrappedLiteral } from "obsidian-dataview/lib/data-model/value";
66
import { DateTime } from "luxon";
77
import { LOGGER } from "services/Logger";
8+
import { LocalSettings } from "cdm/SettingsModel";
89
class DataviewProxy {
910

1011
private static instance: DataviewProxy;
@@ -64,7 +65,7 @@ class DataviewProxy {
6465
return this.getDataviewAPI().value.wrapValue(literal);
6566
}
6667

67-
parseLiteral(literal: Literal, dataTypeDst: string, isInline?: boolean): Literal {
68+
parseLiteral(literal: Literal, dataTypeDst: string, localSettings: LocalSettings, isInline?: boolean): Literal {
6869
let parsedLiteral: Literal = literal;
6970
if (!this.getDataviewAPI().value.isTruthy(literal.toString())) {
7071
return "";
@@ -79,7 +80,7 @@ class DataviewProxy {
7980
parsedLiteral = this.parseToString(wrapped);
8081
break;
8182
case DataTypes.MARKDOWN:
82-
parsedLiteral = this.parseToMarkdown(wrapped, isInline);
83+
parsedLiteral = this.parseToMarkdown(wrapped, localSettings, isInline);
8384
break;
8485
case DataTypes.TAGS:
8586
parsedLiteral = this.parseToOptionsArray(wrapped);
@@ -151,16 +152,23 @@ class DataviewProxy {
151152
return wrapped.type === 'number' ? wrapped.value : Number(adjustedValue);
152153
}
153154

154-
private parseToMarkdown(wrapped: WrappedLiteral, isInline: boolean): string {
155+
private parseToMarkdown(wrapped: WrappedLiteral, localSettings: LocalSettings, isInline: boolean): string {
155156
let auxMarkdown = '';
156-
if (wrapped.type === 'array') {
157-
auxMarkdown = wrapped.value
158-
.map(v => this.parseToMarkdown(this.getDataviewAPI().value.wrapValue(v), isInline))
159-
.join(', ');
160-
} else {
161-
auxMarkdown = this.parseToString(wrapped) as string;
162-
// Check possible markdown breakers
163-
auxMarkdown = this.handleMarkdownBreaker(auxMarkdown, isInline);
157+
switch (wrapped.type) {
158+
case 'boolean':
159+
case 'number':
160+
// Do nothing
161+
auxMarkdown = wrapped.value.toString();
162+
break;
163+
case 'array':
164+
auxMarkdown = wrapped.value
165+
.map(v => this.parseToMarkdown(this.getDataviewAPI().value.wrapValue(v), localSettings, isInline))
166+
.join(', ');
167+
break;
168+
default:
169+
auxMarkdown = this.parseToString(wrapped) as string;
170+
// Check possible markdown breakers
171+
auxMarkdown = this.handleMarkdownBreaker(auxMarkdown, localSettings, isInline);
164172
}
165173
return auxMarkdown;
166174
}
@@ -172,14 +180,15 @@ class DataviewProxy {
172180
return wrapped.value;
173181
}
174182

175-
private handleMarkdownBreaker(value: string, isInline?: boolean): string {
183+
private handleMarkdownBreaker(value: string, localSettings: LocalSettings, isInline?: boolean): string {
176184
if (isInline || (value.startsWith('"') && value.endsWith('"'))) {
177185
return value;
178186
}
179187
// Check possible markdown breakers of the yaml
180188
if (MarkdownBreakerRules.INIT_CHARS.some(c => value.startsWith(c)) ||
181189
MarkdownBreakerRules.BETWEEN_CHARS.some(rule => value.includes(rule)) ||
182-
MarkdownBreakerRules.UNIQUE_CHARS.some(c => value === c)) {
190+
MarkdownBreakerRules.UNIQUE_CHARS.some(c => value === c) ||
191+
localSettings.frontmatter_quote_wrap) {
183192
value = value.replaceAll(`"`, `\\"`);
184193
return `"${value}"`;
185194
}

src/services/NoteInfo.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { VaultManagerDB } from "services/FileManagerService";
55
import { DateTime } from "luxon";
66
import { DataviewService } from "./DataviewService";
77
import { Literal } from "obsidian-dataview/lib/data-model/value";
8+
import { LocalSettings } from "cdm/SettingsModel";
89
/**
910
* Keep info about a note and offer methods to manipulate it
1011
*/
@@ -18,7 +19,7 @@ export default class NoteInfo {
1819
this.id = id;
1920
}
2021

21-
getRowDataType(columns: TableColumn[]): RowDataType {
22+
getRowDataType(columns: TableColumn[], config: LocalSettings): RowDataType {
2223
/** Mandatory fields */
2324
const aFile: RowDataType = {
2425
id: this.id,
@@ -37,7 +38,7 @@ export default class NoteInfo {
3738
/** Parse data with the type of column */
3839
columns.forEach(column => {
3940
if (aFile[column.key] !== undefined) {
40-
aFile[column.key] = DataviewService.parseLiteral((aFile[column.key]) as Literal, column.dataType, column.config.isInline);
41+
aFile[column.key] = DataviewService.parseLiteral((aFile[column.key]) as Literal, column.dataType, config, column.config.isInline);
4142
}
4243
});
4344
return aFile;

0 commit comments

Comments
 (0)