@opendikw/web-client 0.1.3 → 0.1.4

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
@@ -18,6 +18,12 @@ Browser operations are provided by `/import` (archive and asset helpers),
18
18
  `/document` (Markdown parsing, references and formatting), and `/connection`
19
19
  (the default development Core URL). They do not own application routes or storage keys.
20
20
 
21
+ Markdown image extraction, heading wikilink normalization and details matching
22
+ use forward delimiter scans to avoid repeated searches on unclosed input. Image
23
+ paths, aliases and UTF-16 source offsets retain the Core-compatible grammar.
24
+ `rawDetailsPattern` supports `exec`, `matchAll` and `replace` directly; cloning
25
+ its `.source` only reproduces the opener, not the complete details scanner.
26
+
21
27
  Conversion and translation caches accept `{ namespace }`. Omit it to retain
22
28
  the existing cache database names and TTL. Applications may use an identity
23
29
  and Core-specific namespace to keep local caches separate.
@@ -92,8 +92,8 @@ function isImageOnlyLine(line) {
92
92
  }
93
93
  function collectRegexRanges(body, pattern) {
94
94
  const ranges = [];
95
- const flags = pattern.flags.includes("g") ? pattern.flags : `${pattern.flags}g`;
96
- const re = new RegExp(pattern.source, flags);
95
+ const re = pattern;
96
+ re.lastIndex = 0;
97
97
  let m;
98
98
  while ((m = re.exec(body)) !== null) {
99
99
  if (m[0].length === 0) {
@@ -1,3 +1,4 @@
1
+ export { rawDetailsPattern } from "./raw-details.js";
1
2
  export interface FrontmatterMeta {
2
3
  title?: string;
3
4
  id?: string;
@@ -26,7 +27,5 @@ export declare function parseMarkdownDocument(source: string, options?: ParseMar
26
27
  export declare function getMarkdownTitle(source: string): string | null;
27
28
  export declare function slugifyHeading(value: string): string;
28
29
  export declare function uniqueHeadingSlug(env: Record<string, unknown>, value: string): string;
29
- export declare const rawDetailsPattern: RegExp;
30
30
  export declare function parseDetailsOpenAttribute(attributes: string): boolean | null;
31
31
  export declare function extractHeadingsWithSlugs(body: string): HeadingEntry[];
32
- export {};
@@ -1,5 +1,8 @@
1
1
  import MarkdownIt from "markdown-it";
2
2
  import { installScientificInline } from "./scientific-inline.js";
3
+ import { scanWikilinks } from "./wikilink-scan.js";
4
+ export { rawDetailsPattern } from "./raw-details.js";
5
+ import { rawDetailsPattern } from "./raw-details.js";
3
6
  export function parseMarkdownDocument(source, options = {}) {
4
7
  const normalized = source.replace(/\r\n/g, "\n");
5
8
  const frontmatter = extractFrontmatter(normalized);
@@ -108,7 +111,18 @@ function normalizeHeading(value) {
108
111
  // slugs when one operates on the original body and the other on the enhanced
109
112
  // body, and Outline clicks silently no-op.
110
113
  function stripWikilinkSyntax(value) {
111
- return value.replace(/\[\[([^\]|]+)\|([^\]]+)\]\]/g, "$2").replace(/\[\[([^\]]+)\]\]/g, "$1");
114
+ // Keep the two legacy passes: aliases first, then any remaining plain links.
115
+ for (const aliasOnly of [true, false]) {
116
+ const parts = [];
117
+ let cursor = 0;
118
+ for (const link of scanWikilinks(value, false, aliasOnly)) {
119
+ parts.push(value.slice(cursor, link.start), aliasOnly ? link.label : value.slice(link.start + 2, link.end - 2));
120
+ cursor = link.end;
121
+ }
122
+ parts.push(value.slice(cursor));
123
+ value = parts.join("");
124
+ }
125
+ return value;
112
126
  }
113
127
  export function slugifyHeading(value) {
114
128
  return stripWikilinkSyntax(stripScientificInlineTags(value))
@@ -142,7 +156,6 @@ export function uniqueHeadingSlug(env, value) {
142
156
  // Shared with MarkdownView. Both files use these to identify the same set of
143
157
  // <details>...</details> blocks. Keep them here as the single source of truth
144
158
  // so heading slug extraction stays aligned with how MarkdownView renders.
145
- export const rawDetailsPattern = /<details\b([^>]*)>\s*<summary>([\s\S]*?)<\/summary>([\s\S]*?)<\/details>/gi;
146
159
  export function parseDetailsOpenAttribute(attributes) {
147
160
  const trimmed = attributes.trim();
148
161
  if (!trimmed) {
@@ -0,0 +1 @@
1
+ export declare const rawDetailsPattern: RegExp;
@@ -0,0 +1,97 @@
1
+ class RawDetailsPattern extends RegExp {
2
+ body;
3
+ matches = [];
4
+ constructor(_source, flags = "gi") {
5
+ super("<details\\b", flags);
6
+ }
7
+ exec(body) {
8
+ if (this.body !== body) {
9
+ this.body = body;
10
+ this.matches = scanDetails(body, this.ignoreCase);
11
+ }
12
+ const start = this.global || this.sticky ? Math.max(0, Math.floor(Number(this.lastIndex) || 0)) : 0;
13
+ // Binary lookup honors arbitrary lastIndex changes without rescanning the
14
+ // document. Symbol.split also uses a sticky copy of this RegExp.
15
+ let low = 0;
16
+ let high = this.matches.length;
17
+ while (low < high) {
18
+ const middle = (low + high) >>> 1;
19
+ if (this.matches[middle].start < start)
20
+ low = middle + 1;
21
+ else
22
+ high = middle;
23
+ }
24
+ const match = this.matches[low];
25
+ if (match && (!this.sticky || match.start === start)) {
26
+ if (this.global || this.sticky)
27
+ this.lastIndex = match.end;
28
+ // Native RegExp results are fresh arrays; callers may mutate them.
29
+ return Object.assign([
30
+ body.slice(match.start, match.end),
31
+ body.slice(match.attributesStart, match.tagEnd),
32
+ body.slice(match.summaryStart, match.summaryEnd),
33
+ body.slice(match.contentStart, match.end - "</details>".length),
34
+ ], {
35
+ index: match.start,
36
+ input: body,
37
+ });
38
+ }
39
+ if (this.global || this.sticky)
40
+ this.lastIndex = 0;
41
+ return null;
42
+ }
43
+ }
44
+ function scanDetails(body, ignoreCase) {
45
+ // ASCII folding keeps UTF-16 offsets intact (Unicode lowercasing may expand).
46
+ const folded = ignoreCase ? body.replace(/[A-Z]/g, (letter) => letter.toLowerCase()) : body;
47
+ const openings = new RegExp("<details\\b", ignoreCase ? "gi" : "g");
48
+ const matches = [];
49
+ let tagEnd = -1;
50
+ let summaryTagEnd = -1;
51
+ let summaryStart = -1;
52
+ let summaryEnd = -1;
53
+ let detailsEnd = -1;
54
+ for (let open = openings.exec(body); open; open = openings.exec(body)) {
55
+ const attributesStart = open.index + open[0].length;
56
+ if (tagEnd < attributesStart)
57
+ tagEnd = body.indexOf(">", attributesStart);
58
+ if (tagEnd < 0)
59
+ break;
60
+ if (summaryTagEnd !== tagEnd) {
61
+ summaryTagEnd = tagEnd;
62
+ summaryStart = tagEnd + 1;
63
+ while (/\s/.test(body[summaryStart] ?? ""))
64
+ summaryStart++;
65
+ summaryStart = folded.startsWith("<summary>", summaryStart)
66
+ ? summaryStart + "<summary>".length
67
+ : -1;
68
+ }
69
+ if (summaryStart < 0) {
70
+ openings.lastIndex = tagEnd + 1;
71
+ continue;
72
+ }
73
+ if (summaryEnd < summaryStart)
74
+ summaryEnd = folded.indexOf("</summary>", summaryStart);
75
+ if (summaryEnd < 0)
76
+ break;
77
+ const contentStart = summaryEnd + "</summary>".length;
78
+ if (detailsEnd < contentStart)
79
+ detailsEnd = folded.indexOf("</details>", contentStart);
80
+ if (detailsEnd < 0)
81
+ break;
82
+ const end = detailsEnd + "</details>".length;
83
+ // Keep numeric bounds for every opener, including nested ones. Arbitrary
84
+ // lastIndex searches can select them without storing overlapping strings.
85
+ matches.push({
86
+ start: open.index,
87
+ attributesStart,
88
+ tagEnd,
89
+ summaryStart,
90
+ summaryEnd,
91
+ contentStart,
92
+ end,
93
+ });
94
+ }
95
+ return matches;
96
+ }
97
+ export const rawDetailsPattern = new RawDetailsPattern();
@@ -0,0 +1,8 @@
1
+ interface WikilinkSpan {
2
+ start: number;
3
+ end: number;
4
+ target: string;
5
+ label: string | null;
6
+ }
7
+ export declare function scanWikilinks(text: string, image: boolean, aliasOnly?: boolean): Generator<WikilinkSpan>;
8
+ export {};
@@ -0,0 +1,42 @@
1
+ // Cache the next delimiters, including failed searches. Repeated openers must
2
+ // not search the same unclosed suffix again. Spans retain the legacy grammar:
3
+ // '[' is allowed in the target, '|' in aliases, and ']' ends either field.
4
+ export function* scanWikilinks(text, image, aliasOnly = false) {
5
+ const opener = image ? "![[" : "[[";
6
+ let cursor = 0;
7
+ let close = -1;
8
+ let pipe = -1;
9
+ while (cursor < text.length) {
10
+ const start = text.indexOf(opener, cursor);
11
+ if (start < 0)
12
+ return;
13
+ const contentStart = start + opener.length;
14
+ if (close < contentStart)
15
+ close = text.indexOf("]", contentStart);
16
+ if (close < 0)
17
+ return;
18
+ if (text[close + 1] !== "]") {
19
+ cursor = close + 1;
20
+ continue;
21
+ }
22
+ if (pipe < contentStart) {
23
+ const next = text.indexOf("|", contentStart);
24
+ pipe = next < 0 ? Infinity : next;
25
+ }
26
+ const hasAlias = pipe < close;
27
+ const validAlias = hasAlias && pipe > contentStart && pipe + 1 < close;
28
+ if (close === contentStart ||
29
+ (aliasOnly && !validAlias) ||
30
+ (image && hasAlias && !validAlias)) {
31
+ cursor = contentStart;
32
+ continue;
33
+ }
34
+ yield {
35
+ start,
36
+ end: close + 2,
37
+ target: text.slice(contentStart, hasAlias ? pipe : close),
38
+ label: hasAlias ? text.slice(pipe + 1, close) : null,
39
+ };
40
+ cursor = close + 2;
41
+ }
42
+ }
@@ -2,31 +2,104 @@
2
2
  // _is_remote, _resolve_local}. The web importer must agree byte-for-byte
3
3
  // on which paths are picked up — divergence shows up as missing assets
4
4
  // after import.
5
- const STANDARD_IMG_RE = /!\[([^\]]*)\]\(\s*([^)\n]+?)(?=\s+"[^"\n]*"\s*\)|\s*\))(?:\s+"[^"\n]*")?\s*\)/g;
6
- const WIKILINK_IMG_RE = /!\[\[([^\]|]+?)(?:\|([^\]]+))?\]\]/g;
5
+ import { scanWikilinks } from "../document/wikilink-scan.js";
7
6
  export function extractAssetRefs(body) {
8
7
  const refs = [];
9
- for (const m of body.matchAll(STANDARD_IMG_RE)) {
8
+ const scanDestination = destinationScanner(body);
9
+ let cursor = 0;
10
+ while (cursor < body.length) {
11
+ const start = body.indexOf("![", cursor);
12
+ if (start < 0)
13
+ break;
14
+ const labelEnd = body.indexOf("]", start + 2);
15
+ if (labelEnd < 0)
16
+ break;
17
+ // All openers before this ']' share the same destination. If it is invalid,
18
+ // skip the group rather than retrying the same tail for every nested '!['.
19
+ const destination = body[labelEnd + 1] === "(" ? scanDestination(labelEnd + 2) : null;
20
+ cursor = destination?.end ?? labelEnd + 1;
21
+ if (!destination)
22
+ continue;
10
23
  refs.push({
11
- originalPath: m[2],
12
- alt: m[1] ?? "",
13
- start: m.index ?? 0,
14
- end: (m.index ?? 0) + m[0].length,
24
+ originalPath: destination.path,
25
+ alt: body.slice(start + 2, labelEnd),
26
+ start,
27
+ end: destination.end,
15
28
  syntax: "markdown",
16
29
  });
17
30
  }
18
- for (const m of body.matchAll(WIKILINK_IMG_RE)) {
31
+ for (const link of scanWikilinks(body, true)) {
19
32
  refs.push({
20
- originalPath: m[1],
21
- alt: m[2] ?? "",
22
- start: m.index ?? 0,
23
- end: (m.index ?? 0) + m[0].length,
33
+ originalPath: link.target,
34
+ alt: link.label ?? "",
35
+ start: link.start,
36
+ end: link.end,
24
37
  syntax: "wikilink",
25
38
  });
26
39
  }
27
40
  refs.sort((a, b) => a.start - b.start);
28
41
  return refs;
29
42
  }
43
+ function destinationScanner(body) {
44
+ // Index possible endings once. Distinct '](' openers can otherwise retry the
45
+ // same unterminated path/title suffix, even without a backtracking regex.
46
+ const stops = Array.from(body.matchAll(/[)\n]/g), (match) => match.index);
47
+ const quotes = Array.from(body.matchAll(/["\n]/g));
48
+ const titles = [];
49
+ for (let i = 0; i + 1 < quotes.length; i++) {
50
+ const quote = quotes[i];
51
+ const close = quotes[i + 1];
52
+ if (quote[0] !== '"' || close[0] !== '"')
53
+ continue;
54
+ let pathEnd = quote.index;
55
+ while (pathEnd > 0 && /\s/.test(body[pathEnd - 1]))
56
+ pathEnd--;
57
+ if (pathEnd === quote.index)
58
+ continue;
59
+ let end = close.index + 1;
60
+ while (/\s/.test(body[end] ?? ""))
61
+ end++;
62
+ if (body[end] === ")")
63
+ titles.push({ pathEnd, end: end + 1 });
64
+ }
65
+ let stopIndex = 0;
66
+ let titleIndex = 0;
67
+ let whitespaceStop = -1;
68
+ let whitespaceEnd = -1;
69
+ return (start) => {
70
+ let pathStart = start;
71
+ while (/\s/.test(body[pathStart] ?? ""))
72
+ pathStart++;
73
+ if (body[pathStart] === ")" && pathStart > start) {
74
+ // Core's legacy grammar accepts a single whitespace path (except LF).
75
+ let last = pathStart - 1;
76
+ while (last >= start && body[last] === "\n")
77
+ last--;
78
+ return last < start ? null : { path: body[last], end: pathStart + 1 };
79
+ }
80
+ while (stopIndex < stops.length && stops[stopIndex] < pathStart)
81
+ stopIndex++;
82
+ while (titleIndex < titles.length && titles[titleIndex].pathEnd <= pathStart)
83
+ titleIndex++;
84
+ const stop = stops[stopIndex] ?? body.length;
85
+ const title = titles[titleIndex];
86
+ if (title && title.pathEnd <= stop)
87
+ return { path: body.slice(pathStart, title.pathEnd), end: title.end };
88
+ if (body[stop] === "\n") {
89
+ if (whitespaceStop !== stop) {
90
+ whitespaceStop = stop;
91
+ whitespaceEnd = stop;
92
+ while (/\s/.test(body[whitespaceEnd] ?? ""))
93
+ whitespaceEnd++;
94
+ }
95
+ if (body[whitespaceEnd] === ")")
96
+ return { path: body.slice(pathStart, stop).trimEnd(), end: whitespaceEnd + 1 };
97
+ }
98
+ if (body[stop] !== ")" || stop === pathStart)
99
+ return null;
100
+ return { path: body.slice(pathStart, stop).trimEnd(), end: stop + 1 };
101
+ };
102
+ }
30
103
  const REMOTE_SCHEMES = /^([a-zA-Z][a-zA-Z0-9+\-.]*):/;
31
104
  export function isRemoteRef(originalPath) {
32
105
  const m = originalPath.match(REMOTE_SCHEMES);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opendikw/web-client",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "engines": {