Add frontmatter number
This commit is contained in:
parent
fcbaa1bd52
commit
b29c09a339
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@foundry/hammerstone",
|
"name": "@foundry/hammerstone",
|
||||||
"version": "0.3.1",
|
"version": "0.3.2",
|
||||||
"description": "Load and manipulate Obsidian vault data",
|
"description": "Load and manipulate Obsidian vault data",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"exports": "./bin/hammerstone.js",
|
"exports": "./bin/hammerstone.js",
|
||||||
|
|
|
@ -194,11 +194,7 @@ export default class MarkdownDocument {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof val !== "string") {
|
return `${val}`;
|
||||||
throw new Error(`Frontmatter key is not a string: ${key}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return val;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -225,6 +221,30 @@ export default class MarkdownDocument {
|
||||||
return val;
|
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) */
|
/** The markdown portion of the file (without frontmatter) */
|
||||||
get markdown() {
|
get markdown() {
|
||||||
return this._markdown;
|
return this._markdown;
|
||||||
|
|
Loading…
Reference in a new issue