From d1f2a203bff3b525545f3607d2da44a1f87b3bdc Mon Sep 17 00:00:00 2001 From: Endeavorance Date: Thu, 9 May 2024 09:48:47 -0400 Subject: [PATCH] Improved chaining options --- src/document.ts | 16 +++++++++++----- src/vault.ts | 10 ++++++---- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/document.ts b/src/document.ts index 1f3f7c9..c8ccbc4 100644 --- a/src/document.ts +++ b/src/document.ts @@ -92,31 +92,34 @@ export default class MarkdownDocument { * Update the markdown content for the document * @param newValue The new markdown content for the document */ - setMarkdown(newValue: string) { + setMarkdown(newValue: string): MarkdownDocument { this._markdown = newValue; this._content = combineMarkdownAndFrontmatter(newValue, this.frontmatter); this.contentHistory.push(this._content); + return this; } /** * Update the frontmatter of the document * @param newValue The new frontmatter shape for this document */ - setFrontmatter(newValue: FrontmatterShape) { + setFrontmatter(newValue: FrontmatterShape): MarkdownDocument { this._frontmatter = newValue; this._content = combineMarkdownAndFrontmatter(this._markdown, newValue); this.contentHistory.push(this._content); + return this; } /** * Update the full content of the doc with a combination of markdown and yaml frontmatter * @param newValue The full content of the document to set */ - setContent(newValue: string) { + setContent(newValue: string): MarkdownDocument { this._content = newValue; this._frontmatter = extractFrontmatter(newValue); this._markdown = extractMarkdown(newValue); this.contentHistory.push(this._content); + return this; } /** @@ -150,7 +153,7 @@ export default class MarkdownDocument { /** * Revert this document to its original loaded content */ - revert() { + revert(): MarkdownDocument { const originalCopy = this.contentHistory[0]; if (originalCopy === undefined) { @@ -158,17 +161,20 @@ export default class MarkdownDocument { } this.setContent(originalCopy); + return this; } /** * Write this document back to disk */ - write() { + write(): MarkdownDocument { writeFileSync( this.path, combineMarkdownAndFrontmatter(this._markdown, this.frontmatter), "utf-8", ); + + return this; } /** The markdown portion of the file (without frontmatter) */ diff --git a/src/vault.ts b/src/vault.ts index 16f25cb..85bb2ac 100644 --- a/src/vault.ts +++ b/src/vault.ts @@ -77,7 +77,7 @@ class VaultView { * @param fn A function to map over every document in this view * @returns A reference to this view to chain additional calls */ - process(fn: (document: MarkdownDocument) => void) { + process(fn: (document: MarkdownDocument) => void): VaultView { this.documents.forEach(fn); return this; } @@ -127,7 +127,7 @@ export default class Vault { this.index = buildVaultIndex(allFiles); } - scope(fn: (document: MarkdownDocument) => boolean) { + scope(fn: (document: MarkdownDocument) => boolean): VaultView { const matchingDocs = this.documents.filter(fn); return new VaultView(matchingDocs, this); } @@ -137,14 +137,16 @@ export default class Vault { * @param fn A function to map over every document in this vault * @returns A reference to this vault to chain additional calls */ - process(fn: (document: MarkdownDocument) => void) { + process(fn: (document: MarkdownDocument) => void): Vault { this.documents.forEach(fn); return this; } - write() { + write(): Vault { this.documents.forEach((doc) => { doc.write(); }); + + return this; } }