diff --git a/package.json b/package.json index ed81f97..3fad268 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@foundry/hammerstone", - "version": "0.3.1", + "version": "0.3.2", "description": "Load and manipulate Obsidian vault data", "type": "module", "exports": "./bin/hammerstone.js", diff --git a/src/document.ts b/src/document.ts index 890e146..fffe2dd 100644 --- a/src/document.ts +++ b/src/document.ts @@ -194,11 +194,7 @@ export default class MarkdownDocument { return defaultValue; } - if (typeof val !== "string") { - throw new Error(`Frontmatter key is not a string: ${key}`); - } - - return val; + return `${val}`; } /** @@ -225,6 +221,30 @@ export default class MarkdownDocument { return val; } + /** + * Retrieves a number value from the frontmatter using the specified key. + * If the key is not found in the frontmatter, the defaultValue is returned. + * If the value associated with the key is not a number, an error is thrown. + * + * @param key - The key to retrieve the number value from the frontmatter. + * @param defaultValue - The default value to return if the key is not found in the frontmatter. + * @returns The number value associated with the key in the frontmatter, or the defaultValue if the key is not found. + * @throws Error if the value associated with the key is not a number. + */ + getFrontmatterNumber(key: string, defaultValue: number): number { + const val = this._frontmatter[key]; + + if (val === undefined) { + return defaultValue; + } + + if (typeof val !== "number") { + throw new Error(`Frontmatter key is not a number: ${key}`); + } + + return val; + } + /** The markdown portion of the file (without frontmatter) */ get markdown() { return this._markdown;