Prefer scopes
This commit is contained in:
parent
71ed28b3f2
commit
78a621e788
|
@ -1 +0,0 @@
|
|||
**_test
|
23
README.md
23
README.md
|
@ -2,18 +2,27 @@
|
|||
|
||||
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
|
||||
|
||||
### `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.
|
||||
|
||||
#### Options
|
||||
|
||||
| Option | Description | Default |
|
||||
| ---------------- | ---------------------------------------------------------- | ------- |
|
||||
| `ignorePatterns` | An optional array of globs to ignore when discovering docs | `[]` |
|
||||
|
||||
### `vault.process(fn)`
|
||||
|
||||
Process all documents in the vault. Each document is passed to the provided function.
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@endeavorance/hammerstone",
|
||||
"version": "0.1.3",
|
||||
"name": "@foundry/hammerstone",
|
||||
"version": "0.2.0",
|
||||
"description": "Load and manipulate Obsidian vault data",
|
||||
"type": "module",
|
||||
"exports": "./bin/hammerstone.js",
|
||||
|
|
|
@ -80,13 +80,13 @@ Testing!
|
|||
|
||||
test("checking for tags", () => {
|
||||
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(doc.hasTag("tags"), false);
|
||||
});
|
||||
test("checking for taxonomy", () => {
|
||||
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(doc.hasTaxonomy(["Directory", "Subdirectory"]), false);
|
||||
});
|
||||
|
|
|
@ -4,7 +4,6 @@ import Vault from "../vault.js";
|
|||
import path from "node:path";
|
||||
|
||||
const DOCS_IN_TEST_VAULT = 5;
|
||||
const EXCLUDED_DOCS = 1;
|
||||
|
||||
describe("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", () => {
|
||||
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) {
|
||||
assert.fail("Expected file with tags");
|
||||
|
|
21
src/vault.ts
21
src/vault.ts
|
@ -8,13 +8,8 @@ import MarkdownDocument from "./document.js";
|
|||
* @param ignorePatterns A glob pattern to ignore files for
|
||||
* @returns A promise which resolves as an array of laoded Documents
|
||||
*/
|
||||
function loadVaultDocuments(
|
||||
vault: Vault,
|
||||
ignorePatterns: string[] = [],
|
||||
): MarkdownDocument[] {
|
||||
const discoveredMarkdownDocuments = globSync(`${vault.vaultPath}/**/*.md`, {
|
||||
ignore: ignorePatterns,
|
||||
});
|
||||
function loadVaultDocuments(vault: Vault): MarkdownDocument[] {
|
||||
const discoveredMarkdownDocuments = globSync(`${vault.vaultPath}/**/*.md`);
|
||||
|
||||
const markdownDocuments: MarkdownDocument[] = [];
|
||||
|
||||
|
@ -91,10 +86,6 @@ class VaultView {
|
|||
}
|
||||
}
|
||||
|
||||
interface VaultOptions {
|
||||
ignorePatterns?: string[];
|
||||
}
|
||||
|
||||
export default class Vault {
|
||||
/** An array of all discovered Markdown documents */
|
||||
documents: MarkdownDocument[] = [];
|
||||
|
@ -111,14 +102,10 @@ export default class Vault {
|
|||
/** The number of documents in this vault */
|
||||
readonly size: number = 0;
|
||||
|
||||
/** File patterns to ignore when discovering vault files */
|
||||
private ignorePatterns: string[] = [];
|
||||
|
||||
constructor(vaultRootPath: string, options?: VaultOptions) {
|
||||
constructor(vaultRootPath: string) {
|
||||
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);
|
||||
|
||||
this.documents = allFiles;
|
||||
|
|
Loading…
Reference in a new issue