@okforest/okf 0.6.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -34,6 +34,21 @@ const tree = parseTree([
|
|
|
34
34
|
])
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
+
A tree too large to hold parsed at once can be read one file at a time.
|
|
38
|
+
`outlineTree` works out the tree from its paths alone (and the settings
|
|
39
|
+
file's text); `read` then parses one of its markdown files, with its links
|
|
40
|
+
resolved against the whole tree, as `parseTree` would:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { outlineTree } from "@okforest/okf"
|
|
44
|
+
|
|
45
|
+
const outline = outlineTree(paths, settingsText) // .okforest/settings.yaml, if any
|
|
46
|
+
for (const path of outline.markdown) {
|
|
47
|
+
const file = outline.read(path, await textOf(path)) // a Concept or ReservedFile
|
|
48
|
+
// ...use it, and let it go
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
37
52
|
Parsing never throws on malformed content: problems are recorded on each file
|
|
38
53
|
(`frontmatterError`) for [`@okforest/validate`](https://www.npmjs.com/package/@okforest/validate)
|
|
39
54
|
to report.
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as KeyPath, c as Position, d as Status, f as Tree, g as
|
|
1
|
+
import { _ as Verification, a as KeyPath, c as Position, d as Status, f as Tree, g as TrustTier, h as TreeSettings, i as FootnoteReference, l as ReservedFile, m as TreeOutline, n as Concept, o as Link, p as TreeFile, r as Diagnostic, s as MarkdownFile, t as CodeBlock, u as Source } from "./types-BF5G8id_.mjs";
|
|
2
2
|
import { Nodes, Root } from "mdast";
|
|
3
3
|
//#region src/markdown.d.ts
|
|
4
4
|
/** Depth-first, document-order walk over an mdast tree. */
|
|
@@ -75,6 +75,13 @@ export declare const SETTINGS_PATH = ".okforest/settings.yaml";
|
|
|
75
75
|
* `.git/`, as OKF says; settings `ignore:` globs leave out others.
|
|
76
76
|
*/
|
|
77
77
|
export declare function parseTree(input: Iterable<TreeFile>): Tree;
|
|
78
|
+
/**
|
|
79
|
+
* Outline a tree from its file paths and the text of its settings file
|
|
80
|
+
* (`.okforest/settings.yaml`, when it has one), without reading its markdown:
|
|
81
|
+
* a tree too large to hold parsed at once is then read one file at a time
|
|
82
|
+
* with {@link TreeOutline.read}, each file as {@link parseTree} reads it.
|
|
83
|
+
*/
|
|
84
|
+
export declare function outlineTree(paths: Iterable<string>, settingsText?: string): TreeOutline;
|
|
78
85
|
//#endregion
|
|
79
86
|
//#region src/trust.d.ts
|
|
80
87
|
/** Lifecycle status (spec §5.4): absent, or not a recognized value, means `stable`. */
|
|
@@ -95,4 +102,4 @@ export declare function normalizeSources(value: unknown): Source[];
|
|
|
95
102
|
//#region src/yaml.d.ts
|
|
96
103
|
export declare function isRecord(value: unknown): value is Record<string, unknown>;
|
|
97
104
|
//#endregion
|
|
98
|
-
export type { CodeBlock, Concept, Count, Diagnostic, FootnoteReference, Inventory, KeyPath, Link, MarkdownFile, NearDuplicates, Position, ReservedFile, Source, Status, Tree, TreeFile, TreeSettings, TrustTier, Verification };
|
|
105
|
+
export type { CodeBlock, Concept, Count, Diagnostic, FootnoteReference, Inventory, KeyPath, Link, MarkdownFile, NearDuplicates, Position, ReservedFile, Source, Status, Tree, TreeFile, TreeOutline, TreeSettings, TrustTier, Verification };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { c as isStaleAt, d as normalizeVerified, f as globToRegExp, h as visit, i as parseTree, l as isStatus, m as isRecord, o as deriveStatus, p as normalizeText, r as outlineTree, s as deriveTrustTier, t as SETTINGS_PATH, u as normalizeSources } from "./tree-CRV8aRK6.mjs";
|
|
2
2
|
//#region src/inventory.ts
|
|
3
3
|
/** Build the metadata inventory of a tree. Deterministic: no model calls. */
|
|
4
4
|
function buildInventory(tree, options = {}) {
|
|
@@ -137,4 +137,4 @@ function editDistance(a, b, limit) {
|
|
|
137
137
|
return previous[b.length];
|
|
138
138
|
}
|
|
139
139
|
//#endregion
|
|
140
|
-
export { SETTINGS_PATH, buildInventory, deriveStatus, deriveTrustTier, editDistance, findNearDuplicates, globToRegExp, isRecord, isStaleAt, isStatus, normalizeSources, normalizeText, normalizeVerified, parseTree, visit };
|
|
140
|
+
export { SETTINGS_PATH, buildInventory, deriveStatus, deriveTrustTier, editDistance, findNearDuplicates, globToRegExp, isRecord, isStaleAt, isStatus, normalizeSources, normalizeText, normalizeVerified, outlineTree, parseTree, visit };
|
package/dist/node.d.mts
CHANGED
package/dist/node.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as readSettings, i as parseTree, n as ignoreMatcher, t as SETTINGS_PATH } from "./tree-CRV8aRK6.mjs";
|
|
2
2
|
import { readFile, readdir } from "node:fs/promises";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
//#region src/node.ts
|
|
@@ -326,43 +326,13 @@ function parseTree(input) {
|
|
|
326
326
|
const path = normalize(file.path.replaceAll("\\", "/"));
|
|
327
327
|
if (path) texts.set(path, file.text);
|
|
328
328
|
}
|
|
329
|
-
const
|
|
330
|
-
const fileSet = new Set(files);
|
|
331
|
-
const directories = /* @__PURE__ */ new Set([""]);
|
|
332
|
-
for (const path of files) for (let dir = dirname(path); dir !== ""; dir = dirname(dir)) directories.add(dir);
|
|
333
|
-
const settings = readSettings(texts.get(SETTINGS_PATH));
|
|
334
|
-
const isIgnored = ignoreMatcher(settings?.ignore ?? []);
|
|
335
|
-
const ignored = [];
|
|
336
|
-
const parsed = [];
|
|
337
|
-
for (const path of files) {
|
|
338
|
-
if (!path.endsWith(".md")) continue;
|
|
339
|
-
if (path.split("/").some((s) => SKIPPED_FOLDERS.has(s))) continue;
|
|
340
|
-
if (isIgnored(path)) {
|
|
341
|
-
ignored.push(path);
|
|
342
|
-
continue;
|
|
343
|
-
}
|
|
344
|
-
parsed.push({
|
|
345
|
-
file: readMarkdownFile(path, texts.get(path) ?? ""),
|
|
346
|
-
reserved: RESERVED.has(basename(path))
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
const conceptIds = new Set(parsed.filter((p) => !p.reserved).map((p) => p.file.path.slice(0, -3)));
|
|
350
|
-
const resolve = (raw, from) => resolveLink(raw, from, fileSet, directories, conceptIds);
|
|
329
|
+
const outline = outlineTree(texts.keys(), texts.get(SETTINGS_PATH));
|
|
351
330
|
const concepts = [];
|
|
352
331
|
const reserved = [];
|
|
353
|
-
for (const
|
|
354
|
-
const
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
...rest,
|
|
358
|
-
links,
|
|
359
|
-
kind: basename(file.path) === "index.md" ? "index" : "log",
|
|
360
|
-
directory: dirname(file.path)
|
|
361
|
-
});
|
|
362
|
-
else concepts.push(buildConcept({
|
|
363
|
-
...rest,
|
|
364
|
-
links
|
|
365
|
-
}));
|
|
332
|
+
for (const path of outline.markdown) {
|
|
333
|
+
const file = outline.read(path, texts.get(path) ?? "");
|
|
334
|
+
if (file.kind === "concept") concepts.push(file);
|
|
335
|
+
else reserved.push(file);
|
|
366
336
|
}
|
|
367
337
|
const byId = new Map(concepts.map((c) => [c.id, c]));
|
|
368
338
|
const backlinks = /* @__PURE__ */ new Map();
|
|
@@ -374,17 +344,68 @@ function parseTree(input) {
|
|
|
374
344
|
}
|
|
375
345
|
const rootIndex = reserved.find((r) => r.path === "index.md");
|
|
376
346
|
return {
|
|
377
|
-
files,
|
|
347
|
+
files: outline.files,
|
|
378
348
|
concepts,
|
|
379
349
|
reserved,
|
|
380
|
-
ignored,
|
|
381
|
-
...settings ? { settings } : {},
|
|
350
|
+
ignored: outline.ignored,
|
|
351
|
+
...outline.settings ? { settings: outline.settings } : {},
|
|
382
352
|
...rootIndex?.frontmatter && "okf_version" in rootIndex.frontmatter ? { okfVersion: rootIndex.frontmatter.okf_version } : {},
|
|
383
353
|
links: [...concepts, ...reserved].sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0).flatMap((doc) => doc.links),
|
|
384
354
|
concept: (id) => byId.get(id),
|
|
385
355
|
backlinks: (id) => backlinks.get(id) ?? []
|
|
386
356
|
};
|
|
387
357
|
}
|
|
358
|
+
/**
|
|
359
|
+
* Outline a tree from its file paths and the text of its settings file
|
|
360
|
+
* (`.okforest/settings.yaml`, when it has one), without reading its markdown:
|
|
361
|
+
* a tree too large to hold parsed at once is then read one file at a time
|
|
362
|
+
* with {@link TreeOutline.read}, each file as {@link parseTree} reads it.
|
|
363
|
+
*/
|
|
364
|
+
function outlineTree(paths, settingsText) {
|
|
365
|
+
const unique = /* @__PURE__ */ new Set();
|
|
366
|
+
for (const raw of paths) {
|
|
367
|
+
const path = normalize(raw.replaceAll("\\", "/"));
|
|
368
|
+
if (path) unique.add(path);
|
|
369
|
+
}
|
|
370
|
+
const files = [...unique].sort();
|
|
371
|
+
const fileSet = new Set(files);
|
|
372
|
+
const directories = /* @__PURE__ */ new Set([""]);
|
|
373
|
+
for (const path of files) for (let dir = dirname(path); dir !== ""; dir = dirname(dir)) directories.add(dir);
|
|
374
|
+
const settings = readSettings(settingsText);
|
|
375
|
+
const isIgnored = ignoreMatcher(settings?.ignore ?? []);
|
|
376
|
+
const ignored = [];
|
|
377
|
+
const markdown = [];
|
|
378
|
+
for (const path of files) {
|
|
379
|
+
if (!path.endsWith(".md")) continue;
|
|
380
|
+
if (path.split("/").some((s) => SKIPPED_FOLDERS.has(s))) continue;
|
|
381
|
+
if (isIgnored(path)) ignored.push(path);
|
|
382
|
+
else markdown.push(path);
|
|
383
|
+
}
|
|
384
|
+
const inTree = new Set(markdown);
|
|
385
|
+
const conceptIds = new Set(markdown.filter((path) => !RESERVED.has(basename(path))).map((path) => path.slice(0, -3)));
|
|
386
|
+
return {
|
|
387
|
+
files,
|
|
388
|
+
markdown,
|
|
389
|
+
ignored,
|
|
390
|
+
...settings ? { settings } : {},
|
|
391
|
+
read(path, text) {
|
|
392
|
+
if (!inTree.has(path)) throw new Error(`${path} is not one of the tree's markdown files.`);
|
|
393
|
+
const { rawLinks, ...rest } = readMarkdownFile(path, text);
|
|
394
|
+
const links = rawLinks.map((raw) => resolveLink(raw, path, fileSet, directories, conceptIds));
|
|
395
|
+
const name = basename(path);
|
|
396
|
+
if (!RESERVED.has(name)) return buildConcept({
|
|
397
|
+
...rest,
|
|
398
|
+
links
|
|
399
|
+
});
|
|
400
|
+
return {
|
|
401
|
+
...rest,
|
|
402
|
+
links,
|
|
403
|
+
kind: name === "index.md" ? "index" : "log",
|
|
404
|
+
directory: dirname(path)
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
}
|
|
388
409
|
function buildConcept(file) {
|
|
389
410
|
const fm = file.frontmatter ?? {};
|
|
390
411
|
const str = (key) => {
|
|
@@ -503,4 +524,4 @@ function readSettings(text) {
|
|
|
503
524
|
};
|
|
504
525
|
}
|
|
505
526
|
//#endregion
|
|
506
|
-
export {
|
|
527
|
+
export { readSettings as a, isStaleAt as c, normalizeVerified as d, globToRegExp as f, visit as h, parseTree as i, isStatus as l, isRecord as m, ignoreMatcher as n, deriveStatus as o, normalizeText as p, outlineTree as r, deriveTrustTier as s, SETTINGS_PATH as t, normalizeSources as u };
|
|
@@ -150,6 +150,25 @@ interface TreeSettings {
|
|
|
150
150
|
description?: string;
|
|
151
151
|
locate(path: KeyPath): Position | undefined;
|
|
152
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* A tree without its documents: every path in it, the markdown files that are
|
|
155
|
+
* part of it, and its settings, worked out from paths alone (and the settings
|
|
156
|
+
* file's text). A tree too large to hold parsed at once is read one file at a
|
|
157
|
+
* time with {@link TreeOutline.read}; {@link Tree} is the same, read whole.
|
|
158
|
+
*/
|
|
159
|
+
interface TreeOutline {
|
|
160
|
+
/** Every file path in the tree, sorted. */
|
|
161
|
+
files: string[];
|
|
162
|
+
/** The markdown files that are part of the tree, sorted: its concepts,
|
|
163
|
+
* `index.md`, and `log.md` files, without those the settings ignore. */
|
|
164
|
+
markdown: string[];
|
|
165
|
+
/** Markdown files left out by the settings' `ignore:` globs. */
|
|
166
|
+
ignored: string[];
|
|
167
|
+
settings?: TreeSettings;
|
|
168
|
+
/** Read one of {@link markdown}, with its links resolved against the whole
|
|
169
|
+
* tree, as {@link Tree} holds it. Throws for any other path. */
|
|
170
|
+
read(path: string, text: string): Concept | ReservedFile;
|
|
171
|
+
}
|
|
153
172
|
/** A tree (an OKF bundle) read into a model. */
|
|
154
173
|
interface Tree {
|
|
155
174
|
/** Every file path in the tree, sorted. */
|
|
@@ -171,4 +190,4 @@ interface Tree {
|
|
|
171
190
|
backlinks(id: string): Link[];
|
|
172
191
|
}
|
|
173
192
|
//#endregion
|
|
174
|
-
export { KeyPath as a, Position as c, Status as d, Tree as f,
|
|
193
|
+
export { Verification as _, KeyPath as a, Position as c, Status as d, Tree as f, TrustTier as g, TreeSettings as h, FootnoteReference as i, ReservedFile as l, TreeOutline as m, Concept as n, Link as o, TreeFile as p, Diagnostic as r, MarkdownFile as s, CodeBlock as t, Source as u };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@okforest/okf",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Read an Open Knowledge Format (OKF) v0.2 tree into a model: concepts, frontmatter, links, backlinks, trust tiers, staleness, and a metadata inventory. No SDKs or frameworks; runs in Node, browsers, and Workers.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"okf",
|