Improved chaining options

This commit is contained in:
Endeavorance 2024-05-09 09:48:47 -04:00
parent d964f1149e
commit d1f2a203bf
2 changed files with 17 additions and 9 deletions

View file

@ -92,31 +92,34 @@ export default class MarkdownDocument {
* Update the markdown content for the document * Update the markdown content for the document
* @param newValue The new 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._markdown = newValue;
this._content = combineMarkdownAndFrontmatter(newValue, this.frontmatter); this._content = combineMarkdownAndFrontmatter(newValue, this.frontmatter);
this.contentHistory.push(this._content); this.contentHistory.push(this._content);
return this;
} }
/** /**
* Update the frontmatter of the document * Update the frontmatter of the document
* @param newValue The new frontmatter shape for this document * @param newValue The new frontmatter shape for this document
*/ */
setFrontmatter(newValue: FrontmatterShape) { setFrontmatter(newValue: FrontmatterShape): MarkdownDocument {
this._frontmatter = newValue; this._frontmatter = newValue;
this._content = combineMarkdownAndFrontmatter(this._markdown, newValue); this._content = combineMarkdownAndFrontmatter(this._markdown, newValue);
this.contentHistory.push(this._content); this.contentHistory.push(this._content);
return this;
} }
/** /**
* Update the full content of the doc with a combination of markdown and yaml frontmatter * 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 * @param newValue The full content of the document to set
*/ */
setContent(newValue: string) { setContent(newValue: string): MarkdownDocument {
this._content = newValue; this._content = newValue;
this._frontmatter = extractFrontmatter(newValue); this._frontmatter = extractFrontmatter(newValue);
this._markdown = extractMarkdown(newValue); this._markdown = extractMarkdown(newValue);
this.contentHistory.push(this._content); this.contentHistory.push(this._content);
return this;
} }
/** /**
@ -150,7 +153,7 @@ export default class MarkdownDocument {
/** /**
* Revert this document to its original loaded content * Revert this document to its original loaded content
*/ */
revert() { revert(): MarkdownDocument {
const originalCopy = this.contentHistory[0]; const originalCopy = this.contentHistory[0];
if (originalCopy === undefined) { if (originalCopy === undefined) {
@ -158,17 +161,20 @@ export default class MarkdownDocument {
} }
this.setContent(originalCopy); this.setContent(originalCopy);
return this;
} }
/** /**
* Write this document back to disk * Write this document back to disk
*/ */
write() { write(): MarkdownDocument {
writeFileSync( writeFileSync(
this.path, this.path,
combineMarkdownAndFrontmatter(this._markdown, this.frontmatter), combineMarkdownAndFrontmatter(this._markdown, this.frontmatter),
"utf-8", "utf-8",
); );
return this;
} }
/** The markdown portion of the file (without frontmatter) */ /** The markdown portion of the file (without frontmatter) */

View file

@ -77,7 +77,7 @@ class VaultView {
* @param fn A function to map over every document in this view * @param fn A function to map over every document in this view
* @returns A reference to this view to chain additional calls * @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); this.documents.forEach(fn);
return this; return this;
} }
@ -127,7 +127,7 @@ export default class Vault {
this.index = buildVaultIndex(allFiles); this.index = buildVaultIndex(allFiles);
} }
scope(fn: (document: MarkdownDocument) => boolean) { scope(fn: (document: MarkdownDocument) => boolean): VaultView {
const matchingDocs = this.documents.filter(fn); const matchingDocs = this.documents.filter(fn);
return new VaultView(matchingDocs, this); 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 * @param fn A function to map over every document in this vault
* @returns A reference to this vault to chain additional calls * @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); this.documents.forEach(fn);
return this; return this;
} }
write() { write(): Vault {
this.documents.forEach((doc) => { this.documents.forEach((doc) => {
doc.write(); doc.write();
}); });
return this;
} }
} }