Improved chaining options
This commit is contained in:
parent
d964f1149e
commit
d1f2a203bf
|
@ -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) */
|
||||
|
|
10
src/vault.ts
10
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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue