@okfit/core 0.9.1 → 0.9.2

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.
@@ -130,15 +130,48 @@ const footnoteUndefined = rule("footnote-undefined", (context, severity) => {
130
130
  /** GitHub-style heading slug: lowercase, punctuation dropped, spaces to hyphens, duplicates suffixed `-1`, `-2`, ... */
131
131
  const headingSlugs = (concept) => {
132
132
  const seen = /* @__PURE__ */ new Map();
133
- const slugs = /* @__PURE__ */ new Set();
133
+ const slugs = [];
134
134
  for (const heading of concept.document.headings) {
135
135
  const base = heading.text.toLowerCase().replace(/[^\p{L}\p{N}\s-]/gu, "").trim().replace(/\s+/g, "-");
136
136
  const count = seen.get(base) ?? 0;
137
137
  seen.set(base, count + 1);
138
- slugs.add(count === 0 ? base : `${base}-${count}`);
138
+ slugs.push(count === 0 ? base : `${base}-${count}`);
139
139
  }
140
140
  return slugs;
141
141
  };
142
+ /** Levenshtein distance (two-row DP). */
143
+ const editDistance = (a, b) => {
144
+ let previous = Array.from({ length: b.length + 1 }, (_, index) => index);
145
+ for (let i = 1; i <= a.length; i++) {
146
+ const current = [i];
147
+ for (let j = 1; j <= b.length; j++) {
148
+ const cost = a[i - 1] === b[j - 1] ? 0 : 1;
149
+ current[j] = Math.min((previous[j] ?? 0) + 1, (current[j - 1] ?? 0) + 1, (previous[j - 1] ?? 0) + cost);
150
+ }
151
+ previous = current;
152
+ }
153
+ return previous[b.length] ?? 0;
154
+ };
155
+ /** The closest slug within max(2, ⌊|fragment| / 3⌋) edits, ties to the earliest heading; else undefined. */
156
+ const nearestSlug = (fragment, slugs) => {
157
+ const limit = Math.max(2, Math.floor(fragment.length / 3));
158
+ let best;
159
+ for (const slug of slugs) {
160
+ const distance = editDistance(fragment, slug);
161
+ if (distance <= limit && (best === void 0 || distance < best.distance)) best = {
162
+ slug,
163
+ distance
164
+ };
165
+ }
166
+ return best?.slug;
167
+ };
168
+ const candidateHint = (fragment, slugs) => {
169
+ if (slugs.length === 0) return "it has no headings";
170
+ const near = nearestSlug(fragment, slugs);
171
+ if (near !== void 0) return `did you mean "#${near}"?`;
172
+ const shown = slugs.slice(0, 5).map((slug) => `#${slug}`).join(", ");
173
+ return slugs.length > 5 ? `its headings: ${shown} (+${slugs.length - 5} more)` : `its headings: ${shown}`;
174
+ };
142
175
  const fragmentOf = (raw) => {
143
176
  const hash = raw.indexOf("#");
144
177
  if (hash < 0) return void 0;
@@ -167,8 +200,10 @@ const brokenLinks = rule("broken-links", (context, severity) => {
167
200
  };
168
201
  const check = (file, raw, targetId, target, position) => {
169
202
  const fragment = fragmentOf(raw);
170
- if (fragment === void 0 || fragment === "" || slugsOf(targetId, target).has(fragment)) return;
171
- const message = `Link target "${raw}" exists but has no heading "#${fragment}"`;
203
+ if (fragment === void 0 || fragment === "") return;
204
+ const slugs = slugsOf(targetId, target);
205
+ if (slugs.includes(fragment)) return;
206
+ const message = `Link target "${raw}" exists but has no heading "#${fragment}"; ${candidateHint(fragment, slugs)}`;
172
207
  anchors.push(diagnostic(file, "broken-links", severity, message, position));
173
208
  };
174
209
  for (const link of context.graph.edges) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@okfit/core",
3
- "version": "0.9.1",
3
+ "version": "0.9.2",
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": [