@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.
- package/Actor.js +31 -0
- package/AttestedComputation.js +40 -0
- package/Bundle.js +263 -0
- package/Concept.js +44 -0
- package/ConceptId.js +30 -0
- package/Derive.js +120 -0
- package/Diagnostic.js +81 -0
- package/Generated.js +28 -0
- package/Graph.js +158 -0
- package/IndexDocument.js +30 -0
- package/LogDocument.js +28 -0
- package/OkfitConfig.js +471 -0
- package/README.md +13 -2
- package/Source.js +29 -0
- package/Status.js +15 -0
- package/Timestamp.js +18 -0
- package/Validate.js +44 -0
- package/Verification.js +29 -0
- package/index.d.ts +918 -5
- package/index.js +19 -6
- package/internal/conceptDecode.js +122 -0
- package/internal/fence.js +18 -0
- package/internal/frontmatter.js +62 -0
- package/internal/links.js +129 -0
- package/internal/lintRules.js +94 -0
- package/internal/position.js +40 -0
- package/internal/posixPath.js +53 -0
- package/internal/reserved.js +205 -0
- package/internal/templates.js +16 -0
- package/internal/walk.js +45 -0
- package/package.json +10 -3
package/index.js
CHANGED
|
@@ -1,10 +1,23 @@
|
|
|
1
|
+
import { Actor } from "./Actor.js";
|
|
2
|
+
import { AttestedComputation, ComputationAttester, ComputationExecutor, ComputationParameter } from "./AttestedComputation.js";
|
|
3
|
+
import { Timestamp } from "./Timestamp.js";
|
|
4
|
+
import { BodySha256, Generated } from "./Generated.js";
|
|
5
|
+
import { Source, UsageWindow } from "./Source.js";
|
|
6
|
+
import { Status } from "./Status.js";
|
|
7
|
+
import { Verification } from "./Verification.js";
|
|
8
|
+
import { ATTESTED_COMPUTATION_TYPE, Concept } from "./Concept.js";
|
|
9
|
+
import { ConceptId } from "./ConceptId.js";
|
|
10
|
+
import { ConformanceCode, Diagnostic, DiagnosticCode, DiagnosticRange, DiagnosticSeverity, LintCode } from "./Diagnostic.js";
|
|
11
|
+
import { IndexDocument, IndexEntry, IndexSection } from "./IndexDocument.js";
|
|
12
|
+
import { LogDocument, LogGroup, LogItem } from "./LogDocument.js";
|
|
13
|
+
import { Bundle, BundleDepthExceededError, BundleReadError, BundleRootNotFoundError, LoadedBundle, LoadedConcept } from "./Bundle.js";
|
|
14
|
+
import { Derive, Staleness, TrustTier } from "./Derive.js";
|
|
15
|
+
import { Graph, GraphNodeKind, LinkGraph } from "./Graph.js";
|
|
16
|
+
import { FieldDeclaration, LintLevel, LintTable, OkfitConfig, OkfitConfigFile, StaleAfterDuration, TagDeclaration, TypeDeclaration, okfitConfigDocumentFields } from "./OkfitConfig.js";
|
|
17
|
+
import { Validate } from "./Validate.js";
|
|
18
|
+
|
|
1
19
|
//#region src/index.ts
|
|
2
20
|
/**
|
|
3
|
-
* Spec-level Open Knowledge Format (OKF) support for Effect.
|
|
4
|
-
*
|
|
5
|
-
* @packageDocumentation
|
|
6
|
-
*/
|
|
7
|
-
/**
|
|
8
21
|
* The OKF specification version this package implements.
|
|
9
22
|
*
|
|
10
23
|
* @public
|
|
@@ -12,4 +25,4 @@
|
|
|
12
25
|
const OKF_SPEC_VERSION = "0.2";
|
|
13
26
|
|
|
14
27
|
//#endregion
|
|
15
|
-
export { OKF_SPEC_VERSION };
|
|
28
|
+
export { ATTESTED_COMPUTATION_TYPE, Actor, AttestedComputation, BodySha256, Bundle, BundleDepthExceededError, BundleReadError, BundleRootNotFoundError, ComputationAttester, ComputationExecutor, ComputationParameter, Concept, ConceptId, ConformanceCode, Derive, Diagnostic, DiagnosticCode, DiagnosticRange, DiagnosticSeverity, FieldDeclaration, Generated, Graph, GraphNodeKind, IndexDocument, IndexEntry, IndexSection, LinkGraph, LintCode, LintLevel, LintTable, LoadedBundle, LoadedConcept, LogDocument, LogGroup, LogItem, OKF_SPEC_VERSION, OkfitConfig, OkfitConfigFile, Source, StaleAfterDuration, Staleness, Status, TagDeclaration, Timestamp, TrustTier, TypeDeclaration, UsageWindow, Validate, Verification, okfitConfigDocumentFields };
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { AttestedComputation } from "../AttestedComputation.js";
|
|
2
|
+
import { Timestamp } from "../Timestamp.js";
|
|
3
|
+
import { Generated } from "../Generated.js";
|
|
4
|
+
import { Source, UsageWindow } from "../Source.js";
|
|
5
|
+
import { Status } from "../Status.js";
|
|
6
|
+
import { Verification } from "../Verification.js";
|
|
7
|
+
import { ATTESTED_COMPUTATION_TYPE, Concept } from "../Concept.js";
|
|
8
|
+
import { Result, Schema, SchemaIssue } from "effect";
|
|
9
|
+
|
|
10
|
+
//#region src/internal/conceptDecode.ts
|
|
11
|
+
/** Top-level keys that form the computation family when `type` is `Attested Computation`.
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
const COMPUTATION_KEYS = [
|
|
15
|
+
"runtime",
|
|
16
|
+
"parameters",
|
|
17
|
+
"computation",
|
|
18
|
+
"executor",
|
|
19
|
+
"attester"
|
|
20
|
+
];
|
|
21
|
+
const FAMILY_SCHEMAS = {
|
|
22
|
+
title: Schema.String,
|
|
23
|
+
description: Schema.String,
|
|
24
|
+
resource: Schema.String,
|
|
25
|
+
tags: Schema.Array(Schema.String),
|
|
26
|
+
sources: Schema.Array(Source),
|
|
27
|
+
usage_window: UsageWindow,
|
|
28
|
+
generated: Generated,
|
|
29
|
+
verified: Verification.List,
|
|
30
|
+
status: Status,
|
|
31
|
+
stale_after: Timestamp
|
|
32
|
+
};
|
|
33
|
+
const FAMILY_KEYS = Object.keys(FAMILY_SCHEMAS);
|
|
34
|
+
const PARSE_OPTIONS = { errors: "all" };
|
|
35
|
+
const formatIssue = SchemaIssue.makeFormatterStandardSchemaV1();
|
|
36
|
+
const isMapping = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
37
|
+
const decodeFamily = (schema, family, prefix, value, issues) => {
|
|
38
|
+
const result = Schema.decodeUnknownResult(schema, PARSE_OPTIONS)(value);
|
|
39
|
+
if (Result.isSuccess(result)) return result.success;
|
|
40
|
+
for (const leaf of formatIssue(result.failure.issue).issues) {
|
|
41
|
+
const leafPath = leaf.path ?? [];
|
|
42
|
+
const path = [...prefix, ...leafPath.filter((segment) => typeof segment !== "symbol")];
|
|
43
|
+
issues.push({
|
|
44
|
+
code: "family-invalid",
|
|
45
|
+
family,
|
|
46
|
+
path,
|
|
47
|
+
message: leaf.message
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Decode a frontmatter value into a `Concept` without ever rejecting it for a
|
|
53
|
+
* bad family. `TypeMissing` is the only failure (conformance `type-missing`).
|
|
54
|
+
* @public
|
|
55
|
+
*/
|
|
56
|
+
const decodeConcept = (value) => {
|
|
57
|
+
if (!isMapping(value)) return {
|
|
58
|
+
_tag: "TypeMissing",
|
|
59
|
+
message: "frontmatter is not a mapping"
|
|
60
|
+
};
|
|
61
|
+
const raw = Object.fromEntries(Object.entries(value));
|
|
62
|
+
const type = raw.type;
|
|
63
|
+
if (typeof type !== "string") return {
|
|
64
|
+
_tag: "TypeMissing",
|
|
65
|
+
message: "`type` must be a non-empty string"
|
|
66
|
+
};
|
|
67
|
+
if (type.length === 0) return {
|
|
68
|
+
_tag: "TypeMissing",
|
|
69
|
+
message: "`type` must not be empty"
|
|
70
|
+
};
|
|
71
|
+
const issues = [];
|
|
72
|
+
const isComputation = type === ATTESTED_COMPUTATION_TYPE;
|
|
73
|
+
const known = /* @__PURE__ */ new Set([
|
|
74
|
+
"type",
|
|
75
|
+
...FAMILY_KEYS,
|
|
76
|
+
...isComputation ? COMPUTATION_KEYS : []
|
|
77
|
+
]);
|
|
78
|
+
const fields = {
|
|
79
|
+
type,
|
|
80
|
+
extensions: Object.fromEntries(Object.entries(raw).filter(([key]) => !known.has(key))),
|
|
81
|
+
raw
|
|
82
|
+
};
|
|
83
|
+
for (const key of FAMILY_KEYS) {
|
|
84
|
+
if (!(key in raw)) continue;
|
|
85
|
+
const decoded = decodeFamily(FAMILY_SCHEMAS[key], key, [key], raw[key], issues);
|
|
86
|
+
if (decoded !== void 0) fields[key] = decoded;
|
|
87
|
+
}
|
|
88
|
+
if (!("generated" in raw) && "timestamp" in raw) {
|
|
89
|
+
const at = decodeFamily(Timestamp, "timestamp", ["timestamp"], raw.timestamp, issues);
|
|
90
|
+
if (at !== void 0) {
|
|
91
|
+
fields.generated = Generated.make({
|
|
92
|
+
by: Generated.LEGACY_BY,
|
|
93
|
+
at
|
|
94
|
+
});
|
|
95
|
+
issues.push({
|
|
96
|
+
code: "legacy-timestamp",
|
|
97
|
+
family: "timestamp",
|
|
98
|
+
path: ["timestamp"],
|
|
99
|
+
message: "legacy `timestamp` used as `generated.at`"
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (isComputation) {
|
|
104
|
+
const picked = Object.fromEntries(COMPUTATION_KEYS.filter((key) => key in raw).map((key) => [key, raw[key]]));
|
|
105
|
+
const attested = decodeFamily(AttestedComputation, "attested", [], picked, issues);
|
|
106
|
+
if (attested !== void 0) fields.attested = attested;
|
|
107
|
+
if (raw.runtime === void 0) issues.push({
|
|
108
|
+
code: "computation-runtime-missing",
|
|
109
|
+
family: "attested",
|
|
110
|
+
path: [],
|
|
111
|
+
message: "`runtime` is required on an Attested Computation"
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
_tag: "Decoded",
|
|
116
|
+
concept: Concept.make(fields),
|
|
117
|
+
issues
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
//#endregion
|
|
122
|
+
export { COMPUTATION_KEYS, decodeConcept };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/internal/fence.ts
|
|
2
|
+
const LINE_BREAK = /\r\n|\r|\n/;
|
|
3
|
+
/**
|
|
4
|
+
* Core's own fence check, run before the markdown parser because the parser treats an
|
|
5
|
+
* unclosed fence as no frontmatter (effected-markdown.md section 2). Line 0 must be
|
|
6
|
+
* exactly `---`; the block closes at the first later line that is exactly `---`.
|
|
7
|
+
*
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
const detectFence = (text) => {
|
|
11
|
+
const lines = text.split(LINE_BREAK);
|
|
12
|
+
if (lines[0] !== "---") return "absent";
|
|
13
|
+
for (let index = 1; index < lines.length; index += 1) if (lines[index] === "---") return "closed";
|
|
14
|
+
return "unclosed";
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
export { detectFence };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { toFileOffset } from "./position.js";
|
|
2
|
+
import { Diagnostic, DiagnosticRange } from "../Diagnostic.js";
|
|
3
|
+
import { decodeConcept as decodeConcept$1 } from "./conceptDecode.js";
|
|
4
|
+
import { Effect, Option, Result } from "effect";
|
|
5
|
+
import { YamlDocument } from "@effected/yaml";
|
|
6
|
+
|
|
7
|
+
//#region src/internal/frontmatter.ts
|
|
8
|
+
/** D-34 default severities for a `FamilyIssue`'s lint code; `type-missing` (D-33) is always "error". */
|
|
9
|
+
const FAMILY_ISSUE_SEVERITY = {
|
|
10
|
+
"family-invalid": "error",
|
|
11
|
+
"legacy-timestamp": "info",
|
|
12
|
+
"computation-runtime-missing": "error"
|
|
13
|
+
};
|
|
14
|
+
const blockRange = (context) => DiagnosticRange.fromOffset(context.text, context.node.position.start.offset, context.node.position.end.offset - context.node.position.start.offset);
|
|
15
|
+
/**
|
|
16
|
+
* Lazily parses `node.value` as YAML only when a `FamilyIssue.path` needs a range (D-14, D-15;
|
|
17
|
+
* diagnostic-range-and-position-mapping.md section 5). Falls back to the whole frontmatter block
|
|
18
|
+
* when `path` is `[]`, the leaf can't be found, or the YAML re-parse itself is fatal (it already
|
|
19
|
+
* decoded once upstream, so a fatal re-parse should not happen; this is defense only).
|
|
20
|
+
*/
|
|
21
|
+
const rangeFor = (context, path) => {
|
|
22
|
+
if (path.length === 0) return blockRange(context);
|
|
23
|
+
const parsed = Effect.runSync(Effect.result(YamlDocument.parse(context.node.value)));
|
|
24
|
+
if (Result.isFailure(parsed) || parsed.success.contents === null) return blockRange(context);
|
|
25
|
+
const hit = parsed.success.contents.find(path);
|
|
26
|
+
if (Option.isNone(hit)) return blockRange(context);
|
|
27
|
+
return DiagnosticRange.fromOffset(context.text, toFileOffset(context.text, hit.value.offset), hit.value.length);
|
|
28
|
+
};
|
|
29
|
+
const toDiagnostic = (context, issue) => Diagnostic.make({
|
|
30
|
+
file: context.file,
|
|
31
|
+
code: issue.code,
|
|
32
|
+
severity: FAMILY_ISSUE_SEVERITY[issue.code],
|
|
33
|
+
message: issue.message,
|
|
34
|
+
range: rangeFor(context, issue.path)
|
|
35
|
+
});
|
|
36
|
+
/**
|
|
37
|
+
* Group C's seam onto group B's two-stage decoder (decision 2): adapts B3's
|
|
38
|
+
* `decodeConcept(value): ConceptDecodeResult` into the `Option<Concept>` / `Diagnostic[]`
|
|
39
|
+
* shape `Bundle.ts` consumes. Never fails; a `TypeMissing` envelope reports `type-missing`
|
|
40
|
+
* with no concept and no range.
|
|
41
|
+
*
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
const decodeConcept = (raw, context) => {
|
|
45
|
+
const result = decodeConcept$1(raw);
|
|
46
|
+
if (result._tag === "TypeMissing") return {
|
|
47
|
+
concept: Option.none(),
|
|
48
|
+
diagnostics: [Diagnostic.make({
|
|
49
|
+
file: context.file,
|
|
50
|
+
code: "type-missing",
|
|
51
|
+
severity: "error",
|
|
52
|
+
message: result.message
|
|
53
|
+
})]
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
concept: Option.some(result.concept),
|
|
57
|
+
diagnostics: result.issues.map((issue) => toDiagnostic(context, issue))
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
//#endregion
|
|
62
|
+
export { decodeConcept };
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { basename, dirname, join, normalize } from "./posixPath.js";
|
|
2
|
+
|
|
3
|
+
//#region src/internal/links.ts
|
|
4
|
+
const SCHEME_RE = /^[a-z][a-z0-9+.-]+:/i;
|
|
5
|
+
/** RFC 3986 scheme or `://` anywhere (D-24). @internal */
|
|
6
|
+
const isUrl = (raw) => raw.includes("://") || SCHEME_RE.test(raw);
|
|
7
|
+
const stripSuffixes = (raw) => {
|
|
8
|
+
const hash = raw.indexOf("#");
|
|
9
|
+
const noFragment = hash < 0 ? raw : raw.slice(0, hash);
|
|
10
|
+
const query = noFragment.indexOf("?");
|
|
11
|
+
return query < 0 ? noFragment : noFragment.slice(0, query);
|
|
12
|
+
};
|
|
13
|
+
const percentDecode = (raw) => {
|
|
14
|
+
try {
|
|
15
|
+
return decodeURIComponent(raw);
|
|
16
|
+
} catch {
|
|
17
|
+
return raw;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const isReserved = (path) => {
|
|
21
|
+
const name = basename(path);
|
|
22
|
+
return name === "index.md" || name === "log.md";
|
|
23
|
+
};
|
|
24
|
+
const escapes = (path) => path === ".." || path.startsWith("../");
|
|
25
|
+
const withIndex = (candidate) => candidate.endsWith("/") ? `${candidate}index.md` : candidate;
|
|
26
|
+
const existing = (path, index) => {
|
|
27
|
+
if (!index.files.has(path)) return void 0;
|
|
28
|
+
if (path.endsWith(".md") && !isReserved(path)) {
|
|
29
|
+
const id = path.slice(0, -3);
|
|
30
|
+
if (index.concepts.has(id)) return {
|
|
31
|
+
_tag: "node",
|
|
32
|
+
id,
|
|
33
|
+
kind: "concept"
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
_tag: "node",
|
|
38
|
+
id: path,
|
|
39
|
+
kind: "file"
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
const lookup = (candidate, index) => {
|
|
43
|
+
const path = withIndex(candidate);
|
|
44
|
+
if (path === "." || path === "") return void 0;
|
|
45
|
+
const direct = existing(path, index);
|
|
46
|
+
if (direct !== void 0) return direct;
|
|
47
|
+
return basename(path).includes(".") ? void 0 : existing(`${path}.md`, index);
|
|
48
|
+
};
|
|
49
|
+
const missing = (candidate) => ({
|
|
50
|
+
_tag: "node",
|
|
51
|
+
id: withIndex(candidate),
|
|
52
|
+
kind: "missing"
|
|
53
|
+
});
|
|
54
|
+
/** Resolve a body link or path field from `from` (the linking file's bundle-relative path), order A-D (D-23). @internal */
|
|
55
|
+
const resolveTarget = (from, raw, index) => {
|
|
56
|
+
const trimmed = raw.trim();
|
|
57
|
+
if (isUrl(trimmed)) return { _tag: "url" };
|
|
58
|
+
const target = percentDecode(stripSuffixes(trimmed));
|
|
59
|
+
if (target === "") return { _tag: "self" };
|
|
60
|
+
if (target.startsWith("/")) {
|
|
61
|
+
const rooted = target.slice(1);
|
|
62
|
+
const candidate = rooted === "" ? "index.md" : normalize(rooted);
|
|
63
|
+
if (escapes(candidate)) return { _tag: "external" };
|
|
64
|
+
return lookup(candidate, index) ?? missing(candidate);
|
|
65
|
+
}
|
|
66
|
+
const relative = target.startsWith("./") || target.startsWith("../");
|
|
67
|
+
const fileRelative = join(dirname(from), target);
|
|
68
|
+
if (escapes(fileRelative)) return { _tag: "external" };
|
|
69
|
+
const hitB = lookup(fileRelative, index);
|
|
70
|
+
if (hitB !== void 0) return hitB;
|
|
71
|
+
if (!relative) {
|
|
72
|
+
const rootRelative = normalize(target);
|
|
73
|
+
if (escapes(rootRelative)) return { _tag: "external" };
|
|
74
|
+
const hitC = lookup(rootRelative, index);
|
|
75
|
+
if (hitC !== void 0) return hitC;
|
|
76
|
+
if (target.includes("/")) return missing(rootRelative);
|
|
77
|
+
}
|
|
78
|
+
return missing(fileRelative);
|
|
79
|
+
};
|
|
80
|
+
/** Field-aware resolution (D-24): a `sources.resource` with whitespace, or that resolves to nothing, is a descriptor. @internal */
|
|
81
|
+
const resolvePathField = (from, field, raw, index) => {
|
|
82
|
+
if (field !== "sources.resource") return resolveTarget(from, raw, index);
|
|
83
|
+
if (isUrl(raw.trim())) return { _tag: "url" };
|
|
84
|
+
if (/\s/.test(raw.trim())) return { _tag: "descriptor" };
|
|
85
|
+
const resolved = resolveTarget(from, raw, index);
|
|
86
|
+
return resolved._tag === "node" && resolved.kind === "missing" ? { _tag: "descriptor" } : resolved;
|
|
87
|
+
};
|
|
88
|
+
/** The path-valued fields present on a concept, in frontmatter order. @internal */
|
|
89
|
+
const pathFieldsOf = (concept) => {
|
|
90
|
+
const out = [];
|
|
91
|
+
if (concept.resource !== void 0) out.push({
|
|
92
|
+
field: "resource",
|
|
93
|
+
raw: concept.resource
|
|
94
|
+
});
|
|
95
|
+
for (const source of concept.sources ?? []) out.push({
|
|
96
|
+
field: "sources.resource",
|
|
97
|
+
raw: source.resource
|
|
98
|
+
});
|
|
99
|
+
const attested = concept.attested;
|
|
100
|
+
if (attested?.computation !== void 0) out.push({
|
|
101
|
+
field: "computation",
|
|
102
|
+
raw: attested.computation
|
|
103
|
+
});
|
|
104
|
+
if (attested?.executor !== void 0) out.push({
|
|
105
|
+
field: "executor.resource",
|
|
106
|
+
raw: attested.executor.resource
|
|
107
|
+
});
|
|
108
|
+
if (attested?.attester !== void 0) out.push({
|
|
109
|
+
field: "attester.resource",
|
|
110
|
+
raw: attested.attester.resource
|
|
111
|
+
});
|
|
112
|
+
return out;
|
|
113
|
+
};
|
|
114
|
+
/** File span of the first occurrence of `raw` in the frontmatter block: `fenceLine.length + terminator.length + indexInValue`. @internal */
|
|
115
|
+
const frontmatterFieldSpan = (document, raw) => {
|
|
116
|
+
const node = document.frontmatter;
|
|
117
|
+
if (node === void 0) return void 0;
|
|
118
|
+
const at = node.value.indexOf(raw);
|
|
119
|
+
if (at < 0) return void 0;
|
|
120
|
+
const fence = node.format === "json" ? "---json" : node.format === "toml" ? "+++" : "---";
|
|
121
|
+
const fenceEnd = node.position.start.offset + fence.length;
|
|
122
|
+
return {
|
|
123
|
+
offset: fenceEnd + (document.source.charCodeAt(fenceEnd) === 13 && document.source.charCodeAt(fenceEnd + 1) === 10 ? 2 : 1) + at,
|
|
124
|
+
length: raw.length
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
//#endregion
|
|
129
|
+
export { frontmatterFieldSpan, isUrl, pathFieldsOf, resolvePathField, resolveTarget };
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Actor } from "../Actor.js";
|
|
2
|
+
import { Diagnostic, DiagnosticRange } from "../Diagnostic.js";
|
|
3
|
+
import { Derive } from "../Derive.js";
|
|
4
|
+
import { DateTime } from "effect";
|
|
5
|
+
|
|
6
|
+
//#region src/internal/lintRules.ts
|
|
7
|
+
const FOOTNOTE_RE = /\[\^([^\]\s]+)\]/g;
|
|
8
|
+
const frontmatterRange = (concept) => {
|
|
9
|
+
const node = concept.document.frontmatter;
|
|
10
|
+
if (node === void 0) return;
|
|
11
|
+
const start = node.position.start.offset;
|
|
12
|
+
return DiagnosticRange.fromOffset(concept.document.source, start, node.position.end.offset - start);
|
|
13
|
+
};
|
|
14
|
+
const present = (raw, key) => Object.hasOwn(raw, key) && raw[key] !== null;
|
|
15
|
+
const diagnostic = (file, code, severity, message, range) => Diagnostic.make({
|
|
16
|
+
file,
|
|
17
|
+
code,
|
|
18
|
+
severity,
|
|
19
|
+
message,
|
|
20
|
+
...range === void 0 ? {} : { range }
|
|
21
|
+
});
|
|
22
|
+
/** Resolves the rule's severity once; `[]` when the code is off. */
|
|
23
|
+
const rule = (code, body) => (context) => {
|
|
24
|
+
const severity = context.severity(code);
|
|
25
|
+
return severity === "off" ? [] : body(context, severity);
|
|
26
|
+
};
|
|
27
|
+
/** A rule that yields messages per concept, each carrying the frontmatter block range (decision 2). */
|
|
28
|
+
const perConcept = (code, body) => rule(code, (context, severity) => [...context.bundle.concepts.values()].flatMap((concept) => body(concept, context).map((message) => diagnostic(concept.path, code, severity, message, frontmatterRange(concept)))));
|
|
29
|
+
const configUnknownKey = rule("config-unknown-key", (context, severity) => Object.keys(context.config.extensions).map((key) => diagnostic("", "config-unknown-key", severity, `Unknown config key "${key}"`, void 0)));
|
|
30
|
+
const unknownType = perConcept("unknown-type", (concept, context) => Object.hasOwn(context.config.types ?? {}, concept.frontmatter.type) ? [] : [`Type "${concept.frontmatter.type}" is not declared in [types]`]);
|
|
31
|
+
const requiredKeyMissing = perConcept("required-key-missing", (concept, context) => {
|
|
32
|
+
const declared = context.config.types?.[concept.frontmatter.type]?.required ?? [];
|
|
33
|
+
return [.../* @__PURE__ */ new Set([...context.config.concepts?.required ?? [], ...declared])].filter((key) => !present(concept.frontmatter.raw, key)).map((key) => `Required key "${key}" is missing`);
|
|
34
|
+
});
|
|
35
|
+
const fieldValueUnknown = perConcept("field-value-unknown", (concept, context) => {
|
|
36
|
+
const fields = context.config.types?.[concept.frontmatter.type]?.fields ?? {};
|
|
37
|
+
const out = [];
|
|
38
|
+
for (const [key, declaration] of Object.entries(fields)) {
|
|
39
|
+
if (declaration.values === void 0 || !present(concept.frontmatter.raw, key)) continue;
|
|
40
|
+
const value = concept.frontmatter.raw[key];
|
|
41
|
+
if (typeof value !== "string" || !Object.hasOwn(declaration.values, value)) out.push(`Value ${JSON.stringify(value)} for "${key}" is not one of: ${Object.keys(declaration.values).join(", ")}`);
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
});
|
|
45
|
+
const requireVerifiedUnmet = perConcept("require-verified-unmet", (concept, context) => context.config.types?.[concept.frontmatter.type]?.require_verified === true && (concept.frontmatter.verified ?? []).length === 0 ? [`Type "${concept.frontmatter.type}" requires a verified entry`] : []);
|
|
46
|
+
const actorPrefixUnknown = perConcept("actor-prefix-unknown", (concept) => {
|
|
47
|
+
return [concept.frontmatter.generated?.by, ...(concept.frontmatter.verified ?? []).map((v) => v.by)].filter((by) => by !== void 0 && Actor.form(by) === "other").map((by) => `Actor "${by}" uses an unknown prefix; expected human:<id>, process:<id>, or <producer>/<version>`);
|
|
48
|
+
});
|
|
49
|
+
const stale = perConcept("stale", (concept, context) => context.now !== void 0 && concept.frontmatter.stale_after !== void 0 && Derive.isStale(concept.frontmatter, context.now) ? [`Concept is stale since ${DateTime.formatIso(concept.frontmatter.stale_after)}`] : []);
|
|
50
|
+
const footnoteSourceUnknown = rule("footnote-source-unknown", (context, severity) => {
|
|
51
|
+
const out = [];
|
|
52
|
+
for (const concept of context.bundle.concepts.values()) {
|
|
53
|
+
const ids = new Set((concept.frontmatter.sources ?? []).flatMap((s) => s.id === void 0 ? [] : [s.id]));
|
|
54
|
+
if (ids.size === 0) continue;
|
|
55
|
+
const source = concept.document.source;
|
|
56
|
+
const start = concept.document.frontmatter?.position.end.offset ?? 0;
|
|
57
|
+
const seen = /* @__PURE__ */ new Set();
|
|
58
|
+
for (const match of source.slice(start).matchAll(FOOTNOTE_RE)) {
|
|
59
|
+
const label = match[1];
|
|
60
|
+
if (label === void 0 || ids.has(label) || seen.has(label)) continue;
|
|
61
|
+
seen.add(label);
|
|
62
|
+
const range = DiagnosticRange.fromOffset(source, start + match.index, match[0].length);
|
|
63
|
+
out.push(diagnostic(concept.path, "footnote-source-unknown", severity, `Footnote label "${label}" matches no sources[].id`, range));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
});
|
|
68
|
+
const brokenLinks = rule("broken-links", (context, severity) => {
|
|
69
|
+
const pathOf = new Map([...context.bundle.concepts.values()].map((concept) => [concept.id, concept.path]));
|
|
70
|
+
return context.graph.dangling().map((link) => {
|
|
71
|
+
const message = `Link target "${link.data.raw}" does not exist in the bundle`;
|
|
72
|
+
return diagnostic(pathOf.get(link.from) ?? link.from, "broken-links", severity, message, link.data.position);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
const missingIndex = rule("missing-index", (context, severity) => context.bundle.directories.filter((dir) => !context.bundle.indexes.has(dir)).map((dir) => {
|
|
76
|
+
const message = `Directory "${dir === "" ? "." : dir}" has no index.md`;
|
|
77
|
+
return diagnostic(dir === "" ? "index.md" : `${dir}/index.md`, "missing-index", severity, message, void 0);
|
|
78
|
+
}));
|
|
79
|
+
/** Fixed rule order (task-group decision 9). */
|
|
80
|
+
const LINT_RULES = [
|
|
81
|
+
configUnknownKey,
|
|
82
|
+
unknownType,
|
|
83
|
+
requiredKeyMissing,
|
|
84
|
+
fieldValueUnknown,
|
|
85
|
+
requireVerifiedUnmet,
|
|
86
|
+
actorPrefixUnknown,
|
|
87
|
+
footnoteSourceUnknown,
|
|
88
|
+
brokenLinks,
|
|
89
|
+
missingIndex,
|
|
90
|
+
stale
|
|
91
|
+
];
|
|
92
|
+
|
|
93
|
+
//#endregion
|
|
94
|
+
export { LINT_RULES, actorPrefixUnknown, brokenLinks, configUnknownKey, fieldValueUnknown, footnoteSourceUnknown, missingIndex, requireVerifiedUnmet, requiredKeyMissing, stale, unknownType };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
//#region src/internal/position.ts
|
|
2
|
+
const LF = 10;
|
|
3
|
+
const CR = 13;
|
|
4
|
+
/** Line and character of `offset` within `text`; `\n`, `\r` and `\r\n` each count as one line break. @public */
|
|
5
|
+
const lineCharacter = (text, offset) => {
|
|
6
|
+
let line = 0;
|
|
7
|
+
let lineStart = 0;
|
|
8
|
+
const limit = Math.min(offset, text.length);
|
|
9
|
+
for (let i = 0; i < limit; i++) {
|
|
10
|
+
const code = text.charCodeAt(i);
|
|
11
|
+
if (code === LF) {
|
|
12
|
+
line++;
|
|
13
|
+
lineStart = i + 1;
|
|
14
|
+
} else if (code === CR) {
|
|
15
|
+
if (i + 1 < text.length && text.charCodeAt(i + 1) === LF) i++;
|
|
16
|
+
line++;
|
|
17
|
+
lineStart = i + 1;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
line,
|
|
22
|
+
character: offset - lineStart
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* File offset of the first frontmatter value character: the opening fence
|
|
27
|
+
* plus its terminator (`\r\n` counts two, `\n` or `\r` one). Only meaningful
|
|
28
|
+
* when `text` starts with the fence.
|
|
29
|
+
*
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
const frontmatterValueStart = (text) => {
|
|
33
|
+
const afterFence = 3;
|
|
34
|
+
return afterFence + (text.charCodeAt(afterFence) === CR && text.charCodeAt(4) === LF ? 2 : 1);
|
|
35
|
+
};
|
|
36
|
+
/** Map a `YamlDiagnostic.offset` or `YamlNode.offset` inside the frontmatter value to a whole-file offset. @public */
|
|
37
|
+
const toFileOffset = (text, yamlOffset) => frontmatterValueStart(text) + yamlOffset;
|
|
38
|
+
|
|
39
|
+
//#endregion
|
|
40
|
+
export { frontmatterValueStart, lineCharacter, toFileOffset };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
//#region src/internal/posixPath.ts
|
|
2
|
+
const SLASH = 47;
|
|
3
|
+
/**
|
|
4
|
+
* Collapse `.`/`..` and repeated slashes. Relative input keeps leading `..`
|
|
5
|
+
* (the caller classifies bundle escapes); absolute input clamps at the root.
|
|
6
|
+
* A trailing slash survives; `""` is `"."`.
|
|
7
|
+
*
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
const normalize = (path) => {
|
|
11
|
+
if (path.length === 0) return ".";
|
|
12
|
+
const isAbsolute = path.charCodeAt(0) === SLASH;
|
|
13
|
+
const trailingSeparator = path.charCodeAt(path.length - 1) === SLASH;
|
|
14
|
+
const segments = [];
|
|
15
|
+
for (const segment of path.split("/")) {
|
|
16
|
+
if (segment.length === 0 || segment === ".") continue;
|
|
17
|
+
if (segment === "..") {
|
|
18
|
+
if (segments.length > 0 && segments[segments.length - 1] !== "..") segments.pop();
|
|
19
|
+
else if (!isAbsolute) segments.push("..");
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
segments.push(segment);
|
|
23
|
+
}
|
|
24
|
+
let result = segments.join("/");
|
|
25
|
+
if (result.length === 0 && !isAbsolute) result = ".";
|
|
26
|
+
if (result.length > 0 && trailingSeparator) result += "/";
|
|
27
|
+
return isAbsolute ? `/${result}` : result;
|
|
28
|
+
};
|
|
29
|
+
/** Join non-empty segments with `/` and normalize; no segments is `"."`. @public */
|
|
30
|
+
const join = (...paths) => {
|
|
31
|
+
const joined = paths.filter((segment) => segment.length > 0).join("/");
|
|
32
|
+
return joined.length === 0 ? "." : normalize(joined);
|
|
33
|
+
};
|
|
34
|
+
/** Directory part of `path`: `"."` for a bare name, `"/"` for a root-level absolute path. @public */
|
|
35
|
+
const dirname = (path) => {
|
|
36
|
+
let end = path.length;
|
|
37
|
+
while (end > 1 && path.charCodeAt(end - 1) === SLASH) end--;
|
|
38
|
+
const trimmed = path.slice(0, end);
|
|
39
|
+
const index = trimmed.lastIndexOf("/");
|
|
40
|
+
if (index === -1) return ".";
|
|
41
|
+
if (index === 0) return "/";
|
|
42
|
+
return trimmed.slice(0, index);
|
|
43
|
+
};
|
|
44
|
+
/** Last segment of `path` with trailing slashes stripped; `""` for `""` or `"/"`. @public */
|
|
45
|
+
const basename = (path) => {
|
|
46
|
+
let end = path.length;
|
|
47
|
+
while (end > 0 && path.charCodeAt(end - 1) === SLASH) end--;
|
|
48
|
+
const trimmed = path.slice(0, end);
|
|
49
|
+
return trimmed.slice(trimmed.lastIndexOf("/") + 1);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
//#endregion
|
|
53
|
+
export { basename, dirname, join, normalize };
|