From e089616deb65210383bde59fb8c9092edbe628a5 Mon Sep 17 00:00:00 2001 From: Endeavorance Date: Wed, 8 May 2024 12:21:55 -0400 Subject: [PATCH] Fix circular reference in json --- package.json | 5 ++++- src/document.ts | 20 +++++++++++++++++++- src/vault.ts | 8 ++++---- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index d1a78e1..fad0e94 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@endeavorance/hammerstone", - "version": "0.0.1", + "version": "0.0.4", "description": "Load and manipulate Obsidian vault data", "type": "module", "exports": "./bin/hammerstone.js", @@ -26,6 +26,9 @@ "shx": "^0.3.4", "typescript": "^5.4.2" }, + "files": [ + "bin" + ], "prettier": { "bracketSpacing": true, "trailingComma": "all" diff --git a/src/document.ts b/src/document.ts index deac7b1..0e0aac3 100644 --- a/src/document.ts +++ b/src/document.ts @@ -2,6 +2,7 @@ import { existsSync, readFileSync, writeFileSync } from "fs"; import path from "node:path"; import slug from "slug"; import YAML from "yaml"; +import type Vault from "./vault.js"; export type FrontmatterShape = Record; @@ -112,6 +113,9 @@ ${markdown.trim()} } export default class MarkdownDocument { + /** A reference to the vault containing this document */ + readonly vault: Vault; + /** The original file path to this document */ readonly path: string; @@ -144,13 +148,14 @@ export default class MarkdownDocument { /** The markdown portion of the file (without frontmatter) */ private _markdown: string; - constructor(filePath: string, vaultPath: string) { + constructor(filePath: string, vault: Vault) { if (!existsSync(filePath)) { throw new Error(`File not found: ${filePath}`); } const rawFileContent = readFileSync(filePath, "utf-8"); const fileDirname = path.dirname(filePath); + const vaultPath = vault.vaultPath; this.path = filePath; this.dirname = path.relative(vaultPath, fileDirname); @@ -162,6 +167,7 @@ export default class MarkdownDocument { this.contentHistory = [rawFileContent]; this._frontmatter = processDocumentFrontmatter(rawFileContent); this._markdown = getDocumentMarkdown(rawFileContent); + this.vault = vault; } containsBlock(blockName: string): boolean { @@ -176,6 +182,18 @@ export default class MarkdownDocument { return writeDocumentBlock(this, blockName, newContent); } + toJSON() { + return { + path: this.path, + dirname: this.dirname, + taxonomy: this.taxonomy, + filename: this.filename, + slug: this.slug, + frontmatter: this._frontmatter, + markdown: this._markdown, + }; + } + set markdown(newValue: string) { this._markdown = newValue; this._content = combineMarkdownAndFrontmatter(newValue, this.frontmatter); diff --git a/src/vault.ts b/src/vault.ts index c016cf2..f756743 100644 --- a/src/vault.ts +++ b/src/vault.ts @@ -8,17 +8,17 @@ import MarkdownDocument from "./document.js"; * @returns A promise which resolves as an array of laoded Documents */ function loadVaultDocuments( - vaultPath: string, + vault: Vault, ignorePatterns: string[] = [], ): MarkdownDocument[] { - const discoveredMarkdownDocuments = globSync(`${vaultPath}/**/*.md`, { + const discoveredMarkdownDocuments = globSync(`${vault.vaultPath}/**/*.md`, { ignore: ignorePatterns, }); const markdownDocuments: MarkdownDocument[] = []; for (const filePath of discoveredMarkdownDocuments) { - const file = new MarkdownDocument(filePath, vaultPath); + const file = new MarkdownDocument(filePath, vault); if (markdownDocuments.some((check) => check.slug === file.slug)) { throw new Error("Duplicate slug: " + file.slug); @@ -65,7 +65,7 @@ export default class Vault { this.vaultPath = vaultRootPath; this.ignorePatterns = options?.ignorePatterns ?? []; - const allFiles = loadVaultDocuments(this.vaultPath, this.ignorePatterns); + const allFiles = loadVaultDocuments(this, this.ignorePatterns); const allSlugs = allFiles.map((doc) => doc.slug); this.documents = allFiles;