@okfit/core 0.1.0 → 0.3.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.
@@ -0,0 +1,205 @@
1
+ import { Diagnostic, DiagnosticRange } from "../Diagnostic.js";
2
+ import { IndexDocument, IndexEntry, IndexSection } from "../IndexDocument.js";
3
+ import { LogDocument, LogGroup, LogItem } from "../LogDocument.js";
4
+ import { Option } from "effect";
5
+ import { MarkdownParseOptions } from "@effected/markdown";
6
+
7
+ //#region src/internal/reserved.ts
8
+ /**
9
+ * The one parse configuration every OKF file uses: GFM with frontmatter capture
10
+ * (effected-markdown.md section 1; `Markdown.ts:60-62`).
11
+ *
12
+ * @public
13
+ */
14
+ const OPTIONS = MarkdownParseOptions.make({ frontmatter: true });
15
+ const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
16
+ const DESCRIPTION_LEAD = /^[-–—:]\s*/;
17
+ const rangeOf = (text, position) => DiagnosticRange.fromOffset(text, position.start.offset, position.end.offset - position.start.offset);
18
+ const diagnostic = (file, code, severity, message, range) => Diagnostic.make({
19
+ file,
20
+ code,
21
+ severity,
22
+ message,
23
+ range
24
+ });
25
+ /**
26
+ * Plain text of phrasing nodes: text and code values, recursing into containers
27
+ * (the derivation `DocumentHeading.text` uses, MarkdownDocument.ts:248-279).
28
+ *
29
+ * @public
30
+ */
31
+ const phrasingText = (nodes) => nodes.map((node) => {
32
+ if (node.type === "text" || node.type === "inlineCode") return node.value;
33
+ if ("children" in node) return phrasingText(node.children);
34
+ return "";
35
+ }).join("");
36
+ const isLink = (node) => node.type === "link";
37
+ const indexEntry = (text, item) => {
38
+ const paragraph = item.children[0];
39
+ if (paragraph === void 0 || paragraph.type !== "paragraph") return void 0;
40
+ const at = paragraph.children.findIndex(isLink);
41
+ const link = paragraph.children[at];
42
+ if (link === void 0 || !isLink(link)) return void 0;
43
+ const description = phrasingText(paragraph.children.slice(at + 1)).trim().replace(DESCRIPTION_LEAD, "").trim();
44
+ const fields = {
45
+ title: phrasingText(link.children).trim(),
46
+ target: link.url,
47
+ range: rangeOf(text, item.position)
48
+ };
49
+ return IndexEntry.make(description === "" ? fields : {
50
+ ...fields,
51
+ description
52
+ });
53
+ };
54
+ /**
55
+ * Parse an `index.md` (D-21; okf-spec-rules-and-sample-bundles.md section 2.1).
56
+ * `frontmatter` is the decoded YAML value when the file had a closed, parseable block.
57
+ *
58
+ * @public
59
+ */
60
+ const parseIndex = (input) => {
61
+ const { file, dir, document } = input;
62
+ const text = document.source;
63
+ const diagnostics = [];
64
+ let okfVersion;
65
+ const node = document.frontmatter;
66
+ if (node !== void 0 && Option.isSome(input.frontmatter)) {
67
+ const raw = input.frontmatter.value;
68
+ const record = raw !== null && typeof raw === "object" && !Array.isArray(raw) ? raw : void 0;
69
+ const keys = record === void 0 ? [] : Object.keys(record);
70
+ if (dir === "" && record !== void 0 && keys.length === 1 && typeof record.okf_version === "string") okfVersion = record.okf_version;
71
+ else {
72
+ const message = dir === "" ? "root index.md frontmatter may only carry a string \"okf_version\"" : "index.md below the bundle root must not carry frontmatter";
73
+ diagnostics.push(diagnostic(file, "index-frontmatter", "error", message, rangeOf(text, node.position)));
74
+ }
75
+ }
76
+ const sections = [];
77
+ let current;
78
+ const flush = (end) => {
79
+ if (current === void 0) return;
80
+ const start = current.heading.position.start.offset;
81
+ sections.push(IndexSection.make({
82
+ heading: phrasingText(current.heading.children).trim(),
83
+ entries: current.entries,
84
+ range: DiagnosticRange.fromOffset(text, start, end - start)
85
+ }));
86
+ };
87
+ for (const block of document.root.children) {
88
+ if (block.type === "frontmatter" || block.type === "mdxjsEsm") continue;
89
+ if (block.type === "heading" && block.depth === 1) {
90
+ flush(block.position.start.offset);
91
+ current = {
92
+ heading: block,
93
+ entries: []
94
+ };
95
+ continue;
96
+ }
97
+ if (current === void 0) {
98
+ if (block.type === "list") diagnostics.push(diagnostic(file, "index-malformed", "error", "index entries appear before any section heading", rangeOf(text, block.position)));
99
+ continue;
100
+ }
101
+ if (block.type !== "list") {
102
+ diagnostics.push(diagnostic(file, "index-malformed", "error", `expected a list under "# ${phrasingText(current.heading.children).trim()}", found ${block.type}`, rangeOf(text, block.position)));
103
+ continue;
104
+ }
105
+ for (const item of block.children) {
106
+ const entry = indexEntry(text, item);
107
+ if (entry === void 0) diagnostics.push(diagnostic(file, "index-malformed", "error", "index entry has no link", rangeOf(text, item.position)));
108
+ else current.entries.push(entry);
109
+ }
110
+ }
111
+ flush(text.length);
112
+ const fields = {
113
+ path: file,
114
+ dir,
115
+ sections
116
+ };
117
+ return {
118
+ document: IndexDocument.make(okfVersion === void 0 ? fields : {
119
+ ...fields,
120
+ okfVersion
121
+ }),
122
+ diagnostics
123
+ };
124
+ };
125
+ /**
126
+ * Parse a `log.md` (D-21; okf-spec-rules-and-sample-bundles.md section 2.2). Any
127
+ * frontmatter is `log-frontmatter` (warning) and is never decoded.
128
+ *
129
+ * @public
130
+ */
131
+ const parseLog = (input) => {
132
+ const { file, dir, document } = input;
133
+ const text = document.source;
134
+ const diagnostics = [];
135
+ if (document.frontmatter !== void 0) diagnostics.push(diagnostic(file, "log-frontmatter", "warning", "log.md carries frontmatter; the spec defines none", rangeOf(text, document.frontmatter.position)));
136
+ let title;
137
+ const groups = [];
138
+ let current;
139
+ let skipping = false;
140
+ const flush = (end) => {
141
+ if (current === void 0) return;
142
+ const start = current.heading.position.start.offset;
143
+ groups.push(LogGroup.make({
144
+ date: current.date,
145
+ items: current.items,
146
+ range: DiagnosticRange.fromOffset(text, start, end - start)
147
+ }));
148
+ current = void 0;
149
+ };
150
+ for (const block of document.root.children) {
151
+ if (block.type === "frontmatter" || block.type === "mdxjsEsm") continue;
152
+ if (block.type === "heading" && block.depth === 1) {
153
+ title ??= phrasingText(block.children).trim();
154
+ continue;
155
+ }
156
+ if (block.type === "heading" && block.depth === 2) {
157
+ flush(block.position.start.offset);
158
+ const date = phrasingText(block.children).trim();
159
+ skipping = !DATE_RE.test(date);
160
+ if (skipping) diagnostics.push(diagnostic(file, "log-heading-invalid", "error", `log heading "${date}" is not a YYYY-MM-DD date`, rangeOf(text, block.position)));
161
+ else current = {
162
+ heading: block,
163
+ date,
164
+ items: []
165
+ };
166
+ continue;
167
+ }
168
+ if (skipping) continue;
169
+ if (current === void 0) {
170
+ if (block.type === "list") diagnostics.push(diagnostic(file, "log-malformed", "error", "log entries appear before any date heading", rangeOf(text, block.position)));
171
+ continue;
172
+ }
173
+ if (block.type !== "list") {
174
+ diagnostics.push(diagnostic(file, "log-malformed", "error", `expected a list under "## ${current.date}", found ${block.type}`, rangeOf(text, block.position)));
175
+ continue;
176
+ }
177
+ for (const item of block.children) {
178
+ const paragraph = item.children[0];
179
+ if (paragraph === void 0 || paragraph.type !== "paragraph") {
180
+ diagnostics.push(diagnostic(file, "log-malformed", "error", "log item does not start with text", rangeOf(text, item.position)));
181
+ continue;
182
+ }
183
+ current.items.push(LogItem.make({
184
+ text: text.slice(paragraph.position.start.offset, paragraph.position.end.offset),
185
+ range: rangeOf(text, item.position)
186
+ }));
187
+ }
188
+ }
189
+ flush(text.length);
190
+ const fields = {
191
+ path: file,
192
+ dir,
193
+ groups
194
+ };
195
+ return {
196
+ document: LogDocument.make(title === void 0 ? fields : {
197
+ ...fields,
198
+ title
199
+ }),
200
+ diagnostics
201
+ };
202
+ };
203
+
204
+ //#endregion
205
+ export { OPTIONS, parseIndex, parseLog, phrasingText };
@@ -0,0 +1,16 @@
1
+ //#region src/internal/templates.ts
2
+ /** `* [Title](target) - description`, or without the tail when there is no description (D-36; spec §8). */
3
+ const indexEntry = (entry) => entry.description === void 0 || entry.description === "" ? `* [${entry.title}](${entry.target})` : `* [${entry.title}](${entry.target}) - ${entry.description}`;
4
+ /** An H1 section: heading, blank line, entries, trailing newline. */
5
+ const indexSection = (heading, entries) => `# ${heading}\n\n${entries.join("\n")}\n`;
6
+ /** Root-index frontmatter carrying only `okf_version` (D-21). */
7
+ const indexFrontmatter = (okfVersion) => `---\nokf_version: "${okfVersion}"\n---\n\n`;
8
+ /** `## <date>`, a blank line, then one `* item` line per item (spec §9; S-32, MD022/MD032). */
9
+ const logEntry = (date, items) => `${[
10
+ `## ${date}`,
11
+ "",
12
+ ...items.map((item) => `* ${item}`)
13
+ ].join("\n")}\n`;
14
+
15
+ //#endregion
16
+ export { indexEntry, indexFrontmatter, indexSection, logEntry };
@@ -0,0 +1,45 @@
1
+ import { Effect, FileSystem } from "effect";
2
+ import { descend } from "@effected/walker";
3
+ import { GlobPattern, GlobPatternOptions } from "@effected/glob";
4
+
5
+ //#region src/internal/walk.ts
6
+ /**
7
+ * Walker's default prune list (`walker/src/Descend.ts:64`), kept for D-8.
8
+ *
9
+ * @public
10
+ */
11
+ const DEFAULT_PRUNE = ["node_modules", ".git"];
12
+ /**
13
+ * Core's adapter over `@effected/walker`'s `descend` (api-contract.md section 5): the
14
+ * kit's `onUnreadable: "record"` mode records unreadable subdirectories instead of
15
+ * failing or hiding them (D-9); only depth exhaustion and an unreadable root fail typed
16
+ * (W-1, W-2). `includeHidden` governs the compiled glob's `dot` option (W-3).
17
+ *
18
+ * @public
19
+ */
20
+ const walk = (options) => Effect.gen(function* () {
21
+ if (!Number.isInteger(options.maxDepth) || options.maxDepth < 1) return yield* Effect.die(/* @__PURE__ */ new Error(`walk: maxDepth must be a positive integer, received ${options.maxDepth}`));
22
+ const pattern = yield* GlobPattern.compile("**/*", GlobPatternOptions.make({ dot: options.includeHidden })).pipe(Effect.orDie);
23
+ const result = yield* descend(pattern, {
24
+ cwd: options.root,
25
+ maxDepth: options.maxDepth,
26
+ prune: [...options.prune],
27
+ onUnreadable: "record"
28
+ });
29
+ if (result.unreadable.includes("")) {
30
+ yield* (yield* FileSystem.FileSystem).readDirectory(options.root);
31
+ const unreadable = result.unreadable.filter((entry) => entry !== "").sort();
32
+ return {
33
+ files: result.matches,
34
+ unreadable
35
+ };
36
+ }
37
+ const unreadable = [...result.unreadable].sort();
38
+ return {
39
+ files: result.matches,
40
+ unreadable
41
+ };
42
+ });
43
+
44
+ //#endregion
45
+ export { DEFAULT_PRUNE, walk };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@okfit/core",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "private": false,
5
5
  "description": "Spec-level Open Knowledge Format (OKF) v0.2 schemas, bundle loading, graph, derivation, and validation for Effect.",
6
6
  "keywords": [
@@ -36,8 +36,15 @@
36
36
  },
37
37
  "./package.json": "./package.json"
38
38
  },
39
- "dependencies": {
40
- "effect": "4.0.0-rc.109"
39
+ "peerDependencies": {
40
+ "@effected/config-file": "^0.7.0",
41
+ "@effected/glob": "^0.5.0",
42
+ "@effected/jsonc": "^0.9.0",
43
+ "@effected/markdown": "^0.9.0",
44
+ "@effected/toml": "^0.6.0",
45
+ "@effected/walker": "^0.7.0",
46
+ "@effected/yaml": "^0.14.0",
47
+ "effect": "4.0.0-rc.112"
41
48
  },
42
49
  "engines": {
43
50
  "node": ">=24.11.0"