@ultimat3/manifest 31.33.0 → 31.34.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.
Files changed (2) hide show
  1. package/package.json +8 -8
  2. package/src/docs-wiki.ts +89 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/manifest",
3
- "version": "31.33.0",
3
+ "version": "31.34.0",
4
4
  "description": "x.manifest.json: deterministic generated facts, contract diff, AGENTS.md budget",
5
5
  "keywords": [
6
6
  "ultimate",
@@ -43,12 +43,12 @@
43
43
  "test": "bun test"
44
44
  },
45
45
  "dependencies": {
46
- "@ultimat3/action": "31.33.0",
47
- "@ultimat3/core": "31.33.0",
48
- "@ultimat3/entity": "31.33.0",
49
- "@ultimat3/jobs": "31.33.0",
50
- "@ultimat3/query": "31.33.0",
51
- "@ultimat3/realtime": "31.33.0",
52
- "@ultimat3/schema": "31.33.0"
46
+ "@ultimat3/action": "31.34.0",
47
+ "@ultimat3/core": "31.34.0",
48
+ "@ultimat3/entity": "31.34.0",
49
+ "@ultimat3/jobs": "31.34.0",
50
+ "@ultimat3/query": "31.34.0",
51
+ "@ultimat3/realtime": "31.34.0",
52
+ "@ultimat3/schema": "31.34.0"
53
53
  }
54
54
  }
package/src/docs-wiki.ts CHANGED
@@ -28,7 +28,90 @@ export interface WikiHeading {
28
28
  readonly line: number;
29
29
  }
30
30
 
31
- const HEADING = /^ {0,3}(#{1,6})[ \t]+(.*?)(?:[ \t]+#+)?[ \t]*$/;
31
+ // Linear by construction, every reader below (CodeQL js/polynomial-redos, sweep 8b-3): the regexes
32
+ // they replace answered the same strings and retried a run from each of its characters —
33
+ // `/^ {0,3}(#{1,6})[ \t]+(.*?)(?:[ \t]+#+)?[ \t]*$/` took over 15 s on a CRLF heading of 10k
34
+ // spaces, the link pattern over 15 s on 100k `![`, the tag pattern 4.3 s on 100k `<`.
35
+
36
+ const isBlank = (char: string | undefined): boolean => char === ' ' || char === '\t';
37
+ /** What `.` refuses to match without the `s` flag: a heading line holding one is no heading. */
38
+ const LINE_TERMINATOR = /[\n\r\u2028\u2029]/;
39
+
40
+ /**
41
+ * An ATX heading, `#` to `######`: up to three spaces, the marks, a blank, then the text without
42
+ * its closing `#` run (only when a blank precedes it) and without trailing blanks.
43
+ */
44
+ function atxHeading(
45
+ text: string,
46
+ ): { readonly level: number; readonly heading: string } | undefined {
47
+ let at = 0;
48
+ while (at < 3 && text[at] === ' ') at += 1;
49
+ let marks = at;
50
+ while (text[marks] === '#') marks += 1;
51
+ const level = marks - at;
52
+ if (level < 1 || level > 6) return undefined;
53
+ let start = marks;
54
+ while (isBlank(text[start])) start += 1;
55
+ if (start === marks) return undefined;
56
+ const rest = text.slice(start);
57
+ if (LINE_TERMINATOR.test(rest)) return undefined;
58
+ let end = rest.length;
59
+ while (end > 0 && isBlank(rest[end - 1])) end -= 1;
60
+ let closing = end;
61
+ while (closing > 0 && rest[closing - 1] === '#') closing -= 1;
62
+ if (closing === end || closing === 0 || !isBlank(rest[closing - 1])) {
63
+ return { level, heading: rest.slice(0, end) };
64
+ }
65
+ while (isBlank(rest[closing - 1])) closing -= 1;
66
+ return { level, heading: rest.slice(0, closing) };
67
+ }
68
+
69
+ /**
70
+ * `text.replace(/\[([^\]]*)\]\([^)]*\)/g, '$1')`, as one forward walk: a `[` is closed by the
71
+ * first `]` after it, which must be followed by `(` and then a `)`. Every `[` before that `]` shares
72
+ * it, so a failed one skips past it; with no `]` or no `)` left, no later link can close either.
73
+ * An image's `!` stays behind, and the slug's character filter drops it like any other `!`.
74
+ */
75
+ function linksAsText(text: string): string {
76
+ let out = '';
77
+ let kept = 0;
78
+ let from = 0;
79
+ for (;;) {
80
+ const open = text.indexOf('[', from);
81
+ const close = open === -1 ? -1 : text.indexOf(']', open + 1);
82
+ if (close === -1) break;
83
+ if (text[close + 1] !== '(') {
84
+ from = close + 1;
85
+ continue;
86
+ }
87
+ const end = text.indexOf(')', close + 2);
88
+ if (end === -1) break;
89
+ out += text.slice(kept, open) + text.slice(open + 1, close);
90
+ kept = end + 1;
91
+ from = kept;
92
+ }
93
+ return out + text.slice(kept);
94
+ }
95
+
96
+ /** `text.replace(/<[^>]+>/g, '')`, as one forward walk: `<` to the next `>`, never `<>` itself. */
97
+ function withoutTags(text: string): string {
98
+ let out = '';
99
+ let kept = 0;
100
+ let from = 0;
101
+ for (;;) {
102
+ const open = text.indexOf('<', from);
103
+ const close = open === -1 ? -1 : text.indexOf('>', open + 1);
104
+ if (close === -1) break;
105
+ if (close === open + 1) {
106
+ from = close;
107
+ continue;
108
+ }
109
+ out += text.slice(kept, open);
110
+ kept = close + 1;
111
+ from = kept;
112
+ }
113
+ return out + text.slice(kept);
114
+ }
32
115
 
33
116
  /**
34
117
  * GitHub's heading slug: links become their text, tags and backticks drop, then every character
@@ -36,9 +119,7 @@ const HEADING = /^ {0,3}(#{1,6})[ \t]+(.*?)(?:[ \t]+#+)?[ \t]*$/;
36
119
  * rule `scripts/wiki-anchors.ts` holds every wiki link to — the one copy, which it imports.
37
120
  */
38
121
  export const githubSlug = (heading: string): string =>
39
- heading
40
- .replace(/!?\[([^\]]*)\]\([^)]*\)/g, '$1')
41
- .replace(/<[^>]+>/g, '')
122
+ withoutTags(linksAsText(heading))
42
123
  .replaceAll('`', '')
43
124
  .toLowerCase()
44
125
  .replace(/[^\p{L}\p{M}\p{N}\p{Pc} -]/gu, '')
@@ -55,14 +136,14 @@ export function wikiHeadings(markdown: string): readonly WikiHeading[] {
55
136
  return;
56
137
  }
57
138
  if (fenced) return;
58
- const match = HEADING.exec(text);
59
- if (match === null) return;
60
- const heading = match[2] ?? '';
139
+ const match = atxHeading(text);
140
+ if (match === undefined) return;
141
+ const { heading, level } = match;
61
142
  const base = githubSlug(heading);
62
143
  const count = seen.get(base) ?? 0;
63
144
  seen.set(base, count + 1);
64
145
  out.push({
65
- level: (match[1] ?? '#').length,
146
+ level,
66
147
  heading,
67
148
  anchor: count === 0 ? base : `${base}-${count}`,
68
149
  line,