Skip to content

Commit

Permalink
use stringifyYaml
Browse files Browse the repository at this point in the history
  • Loading branch information
muness committed Apr 8, 2024
1 parent f8c85ab commit b196afe
Showing 1 changed file with 2 additions and 18 deletions.
20 changes: 2 additions & 18 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Plugin,
normalizePath,
requestUrl,
stringifyYaml,
} from "obsidian";


Expand Down Expand Up @@ -407,29 +408,12 @@ export default class AmazingMarvinPlugin extends Plugin {
return details;
}

toYamlValue(value: any): string {
if (typeof value === 'string') {
return `"${value.replace(/"/g, '\\"')}"`;
} else if (Array.isArray(value)) {
// YAML array format
return `[${value.map(this.toYamlValue).join(', ')}]`;
} else if (typeof value === 'object' && value !== null) {
// YAML map format
const yamlMap = Object.entries(value).map(([k, v]) => ` ${k}: ${this.toYamlValue(v)}`);
return `\n${yamlMap.join('\n')}`;
} else {
return String(value);
}
}

async createContentForCategory(category: Category): Promise<string> {
let yamlFrontmatter = "---\n";


// Iterate over category properties and add non-null values to YAML frontmatter
for (const [key, value] of Object.entries(category)) {
if (value !== null && value !== undefined) {
yamlFrontmatter += `${key}: ${this.toYamlValue(value)}\n`;
yamlFrontmatter += `${key}: ${stringifyYaml(value)}\n`;
}
}

Expand Down

0 comments on commit b196afe

Please sign in to comment.