Prefer scopes

This commit is contained in:
Endeavorance 2024-05-12 15:57:11 -04:00
parent 71ed28b3f2
commit 78a621e788
6 changed files with 25 additions and 42 deletions

View file

@ -1 +0,0 @@
**_test

View file

@ -2,18 +2,27 @@
Load and manipulate Obsidian vault data Load and manipulate Obsidian vault data
## Early Release
This project is in early development. It is not yet fully featured.
### Roadmap
Features that have yet to be implemented but are planned:
- Create / Delete documents
- Custom write lcoation
- Canvas support
- `.obsidian` folder support
- Improved file slug/id handling
- Plugin API
## API ## API
### `new Vault(vaultRootPath [, options])` ### `new Vault(vaultRootPath)`
Create a new `Vault` object. Searches for markdown files in the given directory, loads, and parses frontmatter for each document. Create a new `Vault` object. Searches for markdown files in the given directory, loads, and parses frontmatter for each document.
#### Options
| Option | Description | Default |
| ---------------- | ---------------------------------------------------------- | ------- |
| `ignorePatterns` | An optional array of globs to ignore when discovering docs | `[]` |
### `vault.process(fn)` ### `vault.process(fn)`
Process all documents in the vault. Each document is passed to the provided function. Process all documents in the vault. Each document is passed to the provided function.

View file

@ -1,6 +1,6 @@
{ {
"name": "@endeavorance/hammerstone", "name": "@foundry/hammerstone",
"version": "0.1.3", "version": "0.2.0",
"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",

View file

@ -80,13 +80,13 @@ Testing!
test("checking for tags", () => { test("checking for tags", () => {
const doc = getTestDocument(); const doc = getTestDocument();
const taggedDoc = doc.vault.index["directory-subdirectory-i-have-tags"]!; const taggedDoc = doc.vault.index["i-have-tags"]!;
assert.equal(taggedDoc.hasTag("tags"), true); assert.equal(taggedDoc.hasTag("tags"), true);
assert.equal(doc.hasTag("tags"), false); assert.equal(doc.hasTag("tags"), false);
}); });
test("checking for taxonomy", () => { test("checking for taxonomy", () => {
const doc = getTestDocument(); const doc = getTestDocument();
const taggedDoc = doc.vault.index["directory-subdirectory-i-have-tags"]!; const taggedDoc = doc.vault.index["i-have-tags"]!;
assert.equal(taggedDoc.hasTaxonomy(["Directory", "Subdirectory"]), true); assert.equal(taggedDoc.hasTaxonomy(["Directory", "Subdirectory"]), true);
assert.equal(doc.hasTaxonomy(["Directory", "Subdirectory"]), false); assert.equal(doc.hasTaxonomy(["Directory", "Subdirectory"]), false);
}); });

View file

@ -4,7 +4,6 @@ import Vault from "../vault.js";
import path from "node:path"; import path from "node:path";
const DOCS_IN_TEST_VAULT = 5; const DOCS_IN_TEST_VAULT = 5;
const EXCLUDED_DOCS = 1;
describe("Vault", () => { describe("Vault", () => {
test("load a vault", () => { test("load a vault", () => {
@ -25,20 +24,9 @@ describe("Vault", () => {
); );
}); });
test("ignored paths are not loaded into the vault", () => {
const vault = new Vault("./bin/_test/test-vault", {
ignorePatterns: ["**/Ignoreme/**"],
});
assert.equal(
vault.size,
DOCS_IN_TEST_VAULT - EXCLUDED_DOCS,
"Unexpected number of documents in vault",
);
});
test("document tags are properly parsed", () => { test("document tags are properly parsed", () => {
const vault = new Vault("./bin/_test/test-vault"); const vault = new Vault("./bin/_test/test-vault");
const taggedFile = vault.index["directory-subdirectory-i-have-tags"]; const taggedFile = vault.index["i-have-tags"];
if (taggedFile === undefined) { if (taggedFile === undefined) {
assert.fail("Expected file with tags"); assert.fail("Expected file with tags");

View file

@ -8,13 +8,8 @@ import MarkdownDocument from "./document.js";
* @param ignorePatterns A glob pattern to ignore files for * @param ignorePatterns A glob pattern to ignore files for
* @returns A promise which resolves as an array of laoded Documents * @returns A promise which resolves as an array of laoded Documents
*/ */
function loadVaultDocuments( function loadVaultDocuments(vault: Vault): MarkdownDocument[] {
vault: Vault, const discoveredMarkdownDocuments = globSync(`${vault.vaultPath}/**/*.md`);
ignorePatterns: string[] = [],
): MarkdownDocument[] {
const discoveredMarkdownDocuments = globSync(`${vault.vaultPath}/**/*.md`, {
ignore: ignorePatterns,
});
const markdownDocuments: MarkdownDocument[] = []; const markdownDocuments: MarkdownDocument[] = [];
@ -91,10 +86,6 @@ class VaultView {
} }
} }
interface VaultOptions {
ignorePatterns?: string[];
}
export default class Vault { export default class Vault {
/** An array of all discovered Markdown documents */ /** An array of all discovered Markdown documents */
documents: MarkdownDocument[] = []; documents: MarkdownDocument[] = [];
@ -111,14 +102,10 @@ export default class Vault {
/** The number of documents in this vault */ /** The number of documents in this vault */
readonly size: number = 0; readonly size: number = 0;
/** File patterns to ignore when discovering vault files */ constructor(vaultRootPath: string) {
private ignorePatterns: string[] = [];
constructor(vaultRootPath: string, options?: VaultOptions) {
this.vaultPath = path.resolve(vaultRootPath); this.vaultPath = path.resolve(vaultRootPath);
this.ignorePatterns = options?.ignorePatterns ?? [];
const allFiles = loadVaultDocuments(this, this.ignorePatterns); const allFiles = loadVaultDocuments(this);
const allSlugs = allFiles.map((doc) => doc.slug); const allSlugs = allFiles.map((doc) => doc.slug);
this.documents = allFiles; this.documents = allFiles;