@opendikw/web-client 0.1.0-rc.1

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 (57) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +31 -0
  3. package/dist/agent/agentClient.d.ts +26 -0
  4. package/dist/agent/agentClient.js +112 -0
  5. package/dist/agent/index.d.ts +1 -0
  6. package/dist/agent/index.js +1 -0
  7. package/dist/connection/index.d.ts +1 -0
  8. package/dist/connection/index.js +1 -0
  9. package/dist/convert/index.d.ts +1 -0
  10. package/dist/convert/index.js +1 -0
  11. package/dist/convert/mineru-convert.d.ts +83 -0
  12. package/dist/convert/mineru-convert.js +565 -0
  13. package/dist/core/client.d.ts +94 -0
  14. package/dist/core/client.js +386 -0
  15. package/dist/core/index.d.ts +2 -0
  16. package/dist/core/index.js +2 -0
  17. package/dist/core/ndjson.d.ts +9 -0
  18. package/dist/core/ndjson.js +57 -0
  19. package/dist/document/format.d.ts +17 -0
  20. package/dist/document/format.js +100 -0
  21. package/dist/document/index.d.ts +5 -0
  22. package/dist/document/index.js +5 -0
  23. package/dist/document/lang.d.ts +5 -0
  24. package/dist/document/lang.js +15 -0
  25. package/dist/document/markdown-blocks.d.ts +8 -0
  26. package/dist/document/markdown-blocks.js +169 -0
  27. package/dist/document/markdown.d.ts +32 -0
  28. package/dist/document/markdown.js +199 -0
  29. package/dist/document/source-inline-refs.d.ts +35 -0
  30. package/dist/document/source-inline-refs.js +242 -0
  31. package/dist/import/frontmatter-merge.d.ts +5 -0
  32. package/dist/import/frontmatter-merge.js +89 -0
  33. package/dist/import/import-bundle.d.ts +90 -0
  34. package/dist/import/import-bundle.js +336 -0
  35. package/dist/import/index.d.ts +5 -0
  36. package/dist/import/index.js +5 -0
  37. package/dist/import/kebab-source-name.d.ts +5 -0
  38. package/dist/import/kebab-source-name.js +31 -0
  39. package/dist/import/md-asset-refs.d.ts +27 -0
  40. package/dist/import/md-asset-refs.js +97 -0
  41. package/dist/import/tar-reader.d.ts +9 -0
  42. package/dist/import/tar-reader.js +114 -0
  43. package/dist/import/tar.d.ts +8 -0
  44. package/dist/import/tar.js +104 -0
  45. package/dist/translate/index.d.ts +1 -0
  46. package/dist/translate/index.js +1 -0
  47. package/dist/translate/translate.d.ts +69 -0
  48. package/dist/translate/translate.js +437 -0
  49. package/dist/types/agent.d.ts +82 -0
  50. package/dist/types/agent.js +1 -0
  51. package/dist/types/auth.d.ts +15 -0
  52. package/dist/types/auth.js +1 -0
  53. package/dist/types/index.d.ts +388 -0
  54. package/dist/types/index.js +3 -0
  55. package/dist/types/trace.d.ts +27 -0
  56. package/dist/types/trace.js +7 -0
  57. package/package.json +60 -0
@@ -0,0 +1,169 @@
1
+ // Splits a markdown body into ordered blocks for the Base reader's bilingual
2
+ // view. "text" blocks (paragraphs, headings, lists, quotes) are translated and
3
+ // shown paragraph-aligned with their translation; "special" blocks (fenced code
4
+ // incl. mermaid, pipe / raw-HTML tables, <details> charts, $$ display math, and
5
+ // thematic breaks) are rendered ONCE, centered, and never translated.
6
+ //
7
+ // Range-first: locate atomic special spans, then split the gaps between them on
8
+ // blank lines. The CommonMark fence scanner + range merge are reused from
9
+ // source-inline-refs, so a blank line or a `#` *inside* a code fence never
10
+ // starts a new block.
11
+ import { rawDetailsPattern } from "./markdown.js";
12
+ import { collectFencedCodeRanges, mergeRanges } from "./source-inline-refs.js";
13
+ const DISPLAY_MATH_PATTERN = /\$\$[\s\S]*?\$\$/g;
14
+ const RAW_TABLE_PATTERN = /<table\b[^>]*>[\s\S]*?<\/table>/gi;
15
+ // 3+ of the same marker (-, *, _), optionally space-separated, alone on a line.
16
+ const THEMATIC_BREAK = /^ {0,3}([-*_])(?: *\1){2,} *$/;
17
+ // Image embeds the reader renders: Obsidian `![[path]]` and CommonMark
18
+ // `![alt](url "title")`. Used to detect blocks that are *only* image(s).
19
+ const OBSIDIAN_IMAGE = /!\[\[[^\]]*\]\]/g;
20
+ const COMMONMARK_IMAGE = /!\[[^\]]*\]\([^)]*\)/g;
21
+ /** Split `body` into ordered text / special blocks (see module header). */
22
+ export function splitMarkdownBlocks(body) {
23
+ const text = body.replace(/\r\n/g, "\n");
24
+ const ranges = mergeRanges([
25
+ ...collectFencedCodeRanges(text),
26
+ ...collectRegexRanges(text, rawDetailsPattern),
27
+ ...collectRegexRanges(text, RAW_TABLE_PATTERN),
28
+ ...collectRegexRanges(text, DISPLAY_MATH_PATTERN),
29
+ ...collectPipeTableRanges(text),
30
+ ...collectThematicBreakRanges(text),
31
+ ...collectImageLineRanges(text),
32
+ ]);
33
+ const blocks = [];
34
+ let cursor = 0;
35
+ for (const range of ranges) {
36
+ if (range.start > cursor)
37
+ pushTextBlocks(blocks, text.slice(cursor, range.start));
38
+ const md = text.slice(range.start, range.end).trim();
39
+ if (md)
40
+ blocks.push({ kind: "special", md });
41
+ cursor = range.end;
42
+ }
43
+ if (cursor < text.length)
44
+ pushTextBlocks(blocks, text.slice(cursor));
45
+ return blocks;
46
+ }
47
+ /** Split a plain (non-special) stretch on blank lines into text blocks. */
48
+ function pushTextBlocks(blocks, chunk) {
49
+ for (const part of chunk.split(/\n[ \t]*\n/)) {
50
+ const md = part.trim();
51
+ if (md)
52
+ blocks.push({ kind: "text", md });
53
+ }
54
+ }
55
+ /** Standalone-image LINES are special: a figure on its own line renders once
56
+ * (centered) instead of being duplicated — and alt-translated — across both
57
+ * bilingual columns. Detecting at the *line* level (not the whole blank-line
58
+ * block) also splits a figure off a caption joined to it by a hard line break,
59
+ * which is how MinerU emits captioned figures (the Fig. 2 case on cho-cqa). A
60
+ * line inside a fenced code block / table / details is absorbed by that larger
61
+ * range during `mergeRanges`, so code samples showing image syntax are safe. */
62
+ function collectImageLineRanges(body) {
63
+ const { lines, starts } = lineIndex(body);
64
+ const ranges = [];
65
+ for (let i = 0; i < lines.length; i += 1) {
66
+ if (isImageOnlyLine(lines[i].trim())) {
67
+ ranges.push(rangeForLines(body, starts, lines, i, i));
68
+ }
69
+ }
70
+ return ranges;
71
+ }
72
+ /** True when `line` is non-empty and contains only image embed(s) plus
73
+ * link/whitespace punctuation — a standalone figure (incl. a linked image
74
+ * `[![alt](img)](href)`), not prose with an inline image. */
75
+ function isImageOnlyLine(line) {
76
+ if (line.length === 0)
77
+ return false;
78
+ // A list-item / blockquote line that holds only an image is still part of that
79
+ // structure; pulling it out as a standalone figure would break the surrounding
80
+ // list/quote, so leave it as translatable text. Covers nested / spaceless
81
+ // blockquote markers (`>>`, `>![[…]]`) too. (Ordered-list markers like `1.`
82
+ // carry a digit and are already excluded by the alphanumeric check below.)
83
+ if (/^(?:[-*+]\s|(?:>\s*)+)/.test(line))
84
+ return false;
85
+ let rest = line.replace(OBSIDIAN_IMAGE, "").replace(COMMONMARK_IMAGE, "");
86
+ if (rest === line)
87
+ return false; // no image embed present
88
+ // A linked image `[![alt](img)](href)` leaves an empty-label link `[](href)`
89
+ // once the image is removed — strip that shell so the URL doesn't read as prose.
90
+ rest = rest.replace(/\[\s*\]\([^)]*\)/g, "");
91
+ return !/[\p{L}\p{N}]/u.test(rest); // only brackets / punctuation / space left
92
+ }
93
+ function collectRegexRanges(body, pattern) {
94
+ const ranges = [];
95
+ const flags = pattern.flags.includes("g") ? pattern.flags : `${pattern.flags}g`;
96
+ const re = new RegExp(pattern.source, flags);
97
+ let m;
98
+ while ((m = re.exec(body)) !== null) {
99
+ if (m[0].length === 0) {
100
+ re.lastIndex += 1;
101
+ continue;
102
+ }
103
+ ranges.push({ start: m.index, end: m.index + m[0].length });
104
+ }
105
+ return ranges;
106
+ }
107
+ function lineIndex(body) {
108
+ const lines = body.split("\n");
109
+ const starts = [];
110
+ let offset = 0;
111
+ for (const line of lines) {
112
+ starts.push(offset);
113
+ offset += line.length + 1; // +1 for the \n separator
114
+ }
115
+ return { lines, starts };
116
+ }
117
+ function rangeForLines(body, starts, lines, first, last) {
118
+ const start = starts[first];
119
+ let end = starts[last] + lines[last].length;
120
+ if (end < body.length && body[end] === "\n")
121
+ end += 1; // include trailing newline
122
+ return { start, end };
123
+ }
124
+ /** A pipe-table delimiter row: only |-: whitespace, with at least one of each
125
+ * pipe and dash (e.g. `|---|:--:|`, `--- | ---`). */
126
+ function isTableDelimiter(line) {
127
+ const t = line.trim();
128
+ return t.includes("|") && t.includes("-") && /^[|\-:\s]+$/.test(t);
129
+ }
130
+ function collectPipeTableRanges(body) {
131
+ const { lines, starts } = lineIndex(body);
132
+ const ranges = [];
133
+ let i = 0;
134
+ while (i < lines.length) {
135
+ const header = lines[i];
136
+ const delimiter = lines[i + 1];
137
+ if (header.includes("|") &&
138
+ header.trim() !== "" &&
139
+ delimiter !== undefined &&
140
+ isTableDelimiter(delimiter)) {
141
+ let last = i + 1;
142
+ while (last + 1 < lines.length &&
143
+ lines[last + 1].trim() !== "" &&
144
+ lines[last + 1].includes("|")) {
145
+ last += 1;
146
+ }
147
+ ranges.push(rangeForLines(body, starts, lines, i, last));
148
+ i = last + 1;
149
+ }
150
+ else {
151
+ i += 1;
152
+ }
153
+ }
154
+ return ranges;
155
+ }
156
+ function collectThematicBreakRanges(body) {
157
+ const { lines, starts } = lineIndex(body);
158
+ const ranges = [];
159
+ for (let i = 0; i < lines.length; i += 1) {
160
+ if (!THEMATIC_BREAK.test(lines[i]))
161
+ continue;
162
+ // A marker line directly under non-blank text is a setext heading underline,
163
+ // not a thematic break — leave it with the text so it renders as a heading.
164
+ if (i > 0 && lines[i - 1].trim() !== "")
165
+ continue;
166
+ ranges.push(rangeForLines(body, starts, lines, i, i));
167
+ }
168
+ return ranges;
169
+ }
@@ -0,0 +1,32 @@
1
+ export interface FrontmatterMeta {
2
+ title?: string;
3
+ id?: string;
4
+ type?: string;
5
+ kind?: string;
6
+ status?: string;
7
+ created?: string;
8
+ updated?: string;
9
+ tags?: string[];
10
+ sources?: string[];
11
+ [key: string]: string | string[] | undefined;
12
+ }
13
+ export interface ParsedMarkdownDocument {
14
+ body: string;
15
+ meta: FrontmatterMeta;
16
+ }
17
+ export interface HeadingEntry {
18
+ level: number;
19
+ title: string;
20
+ slug: string;
21
+ }
22
+ interface ParseMarkdownOptions {
23
+ stripDuplicateTitle?: string | false;
24
+ }
25
+ export declare function parseMarkdownDocument(source: string, options?: ParseMarkdownOptions): ParsedMarkdownDocument;
26
+ export declare function getMarkdownTitle(source: string): string | null;
27
+ export declare function slugifyHeading(value: string): string;
28
+ export declare function uniqueHeadingSlug(env: Record<string, unknown>, value: string): string;
29
+ export declare const rawDetailsPattern: RegExp;
30
+ export declare function parseDetailsOpenAttribute(attributes: string): boolean | null;
31
+ export declare function extractHeadingsWithSlugs(body: string): HeadingEntry[];
32
+ export {};
@@ -0,0 +1,199 @@
1
+ import MarkdownIt from "markdown-it";
2
+ export function parseMarkdownDocument(source, options = {}) {
3
+ const normalized = source.replace(/\r\n/g, "\n");
4
+ const frontmatter = extractFrontmatter(normalized);
5
+ if (!frontmatter) {
6
+ return { body: normalized.trimStart(), meta: {} };
7
+ }
8
+ const meta = parseYamlSubset(frontmatter.raw);
9
+ const duplicateTitle = options.stripDuplicateTitle === false ? undefined : (options.stripDuplicateTitle ?? meta.title);
10
+ const body = duplicateTitle
11
+ ? stripDuplicateTopHeading(frontmatter.body.trimStart(), duplicateTitle)
12
+ : frontmatter.body.trimStart();
13
+ return { body, meta };
14
+ }
15
+ export function getMarkdownTitle(source) {
16
+ const parsed = parseMarkdownDocument(source);
17
+ if (parsed.meta.title) {
18
+ return parsed.meta.title;
19
+ }
20
+ const heading = /^#\s+(.+)$/m.exec(parsed.body);
21
+ return heading?.[1]?.trim() || null;
22
+ }
23
+ function extractFrontmatter(source) {
24
+ if (!source.startsWith("---\n")) {
25
+ return null;
26
+ }
27
+ const end = source.indexOf("\n---\n", 4);
28
+ if (end === -1) {
29
+ return null;
30
+ }
31
+ return {
32
+ raw: source.slice(4, end),
33
+ body: source.slice(end + "\n---\n".length),
34
+ };
35
+ }
36
+ function parseYamlSubset(raw) {
37
+ const meta = {};
38
+ let currentListKey = null;
39
+ for (const line of raw.split("\n")) {
40
+ if (!line.trim()) {
41
+ continue;
42
+ }
43
+ const listItem = /^\s*-\s+(.+)$/.exec(line);
44
+ if (listItem && currentListKey) {
45
+ const value = cleanScalar(listItem[1]);
46
+ const existing = meta[currentListKey];
47
+ if (Array.isArray(existing)) {
48
+ existing.push(value);
49
+ }
50
+ else if (typeof existing === "string") {
51
+ meta[currentListKey] = [existing, value];
52
+ }
53
+ else {
54
+ meta[currentListKey] = [value];
55
+ }
56
+ continue;
57
+ }
58
+ const pair = /^([A-Za-z0-9_-]+):\s*(.*)$/.exec(line);
59
+ if (!pair) {
60
+ currentListKey = null;
61
+ continue;
62
+ }
63
+ const key = pair[1];
64
+ const value = pair[2];
65
+ if (!value.trim()) {
66
+ meta[key] = [];
67
+ currentListKey = key;
68
+ }
69
+ else {
70
+ meta[key] = cleanScalar(value);
71
+ currentListKey = null;
72
+ }
73
+ }
74
+ return meta;
75
+ }
76
+ function cleanScalar(value) {
77
+ const trimmed = value.trim();
78
+ if ((trimmed.startsWith("'") && trimmed.endsWith("'")) ||
79
+ (trimmed.startsWith('"') && trimmed.endsWith('"'))) {
80
+ return trimmed.slice(1, -1);
81
+ }
82
+ return trimmed;
83
+ }
84
+ function stripDuplicateTopHeading(body, title) {
85
+ if (!title) {
86
+ return body;
87
+ }
88
+ const match = /^#\s+(.+)\n+/.exec(body);
89
+ if (!match) {
90
+ return body;
91
+ }
92
+ if (normalizeHeading(match[1]) !== normalizeHeading(title)) {
93
+ return body;
94
+ }
95
+ return body.slice(match[0].length);
96
+ }
97
+ function normalizeHeading(value) {
98
+ return value.trim().replace(/\s+/g, " ").toLowerCase();
99
+ }
100
+ // Strip Obsidian-style wikilink markup so heading slugs reflect the rendered
101
+ // visible text instead of the markup. `[[label|literal]]` -> `literal`,
102
+ // `[[label]]` -> `label`. Critical for source-page outline alignment:
103
+ // `injectInlineRefs` may wrap a K-page title inside a heading, and both
104
+ // MarkdownView's `heading_open` (renders the DOM `id`) and
105
+ // `extractHeadingsWithSlugs` (drives the Outline tab) feed `inline.content`
106
+ // through this slugifier. Without this strip the two paths produce different
107
+ // slugs when one operates on the original body and the other on the enhanced
108
+ // body, and Outline clicks silently no-op.
109
+ function stripWikilinkSyntax(value) {
110
+ return value.replace(/\[\[([^\]|]+)\|([^\]]+)\]\]/g, "$2").replace(/\[\[([^\]]+)\]\]/g, "$1");
111
+ }
112
+ export function slugifyHeading(value) {
113
+ return stripWikilinkSyntax(value)
114
+ .trim()
115
+ .toLowerCase()
116
+ .normalize("NFKD")
117
+ .replace(/[\u0300-\u036f]/g, "")
118
+ .replace(/[^\p{L}\p{N}\s_-]+/gu, "")
119
+ .replace(/[\s_]+/g, "-")
120
+ .replace(/-+/g, "-")
121
+ .replace(/^-|-$/g, "");
122
+ }
123
+ export function uniqueHeadingSlug(env, value) {
124
+ const slug = slugifyHeading(value);
125
+ if (!slug) {
126
+ return "";
127
+ }
128
+ const counts = env.headingSlugCounts instanceof Map
129
+ ? env.headingSlugCounts
130
+ : new Map();
131
+ env.headingSlugCounts = counts;
132
+ const count = counts.get(slug) ?? 0;
133
+ counts.set(slug, count + 1);
134
+ const unique = count === 0 ? slug : `${slug}-${count + 1}`;
135
+ // The bilingual reader renders the translated column through a prefixed env so
136
+ // its heading ids can't collide with the source column's in the same DOM
137
+ // (e.g. a term identical in both languages). Default (no prefix) is unchanged.
138
+ const prefix = typeof env.headingSlugPrefix === "string" ? env.headingSlugPrefix : "";
139
+ return prefix ? `${prefix}${unique}` : unique;
140
+ }
141
+ // Shared with MarkdownView. Both files use these to identify the same set of
142
+ // <details>...</details> blocks. Keep them here as the single source of truth
143
+ // so heading slug extraction stays aligned with how MarkdownView renders.
144
+ export const rawDetailsPattern = /<details\b([^>]*)>\s*<summary>([\s\S]*?)<\/summary>([\s\S]*?)<\/details>/gi;
145
+ export function parseDetailsOpenAttribute(attributes) {
146
+ const trimmed = attributes.trim();
147
+ if (!trimmed) {
148
+ return false;
149
+ }
150
+ return /^open(?:\s*=\s*(?:"open"|'open'|open|""))?$/i.test(trimmed) ? true : null;
151
+ }
152
+ // Minimal markdown-it parser used only to enumerate headings. Mirrors the
153
+ // parser config used by MarkdownView so the slugs produced here match the
154
+ // `id` attributes that MarkdownView writes onto the rendered DOM.
155
+ const headingParser = new MarkdownIt({ html: false, linkify: true, typographer: true });
156
+ // MarkdownView renders any details block whose `open` attribute parses as
157
+ // safe inside its own recursive renderMarkdown() call with a fresh slug
158
+ // counter. If extractHeadingsWithSlugs included headings from those blocks in
159
+ // the same env as the main body, a heading that shares text with one outside
160
+ // would silently collide on the produced slug — outline jumps to "intro-2"
161
+ // would never find an element because the in-details heading is rendered with
162
+ // id="intro". Drop those blocks here so this extractor only enumerates
163
+ // headings that share an env with the main render pass.
164
+ function stripPreprocessedDetails(body) {
165
+ return body.replace(rawDetailsPattern, (raw, attributes) => {
166
+ const open = parseDetailsOpenAttribute(attributes);
167
+ if (open === null) {
168
+ return raw;
169
+ }
170
+ return "\n\n";
171
+ });
172
+ }
173
+ export function extractHeadingsWithSlugs(body) {
174
+ const stripped = stripPreprocessedDetails(body);
175
+ const env = {};
176
+ const tokens = headingParser.parse(stripped, env);
177
+ const headings = [];
178
+ for (let i = 0; i < tokens.length; i += 1) {
179
+ const token = tokens[i];
180
+ if (token.type !== "heading_open") {
181
+ continue;
182
+ }
183
+ const level = Number(token.tag.slice(1));
184
+ const inline = tokens[i + 1];
185
+ if (!inline || inline.type !== "inline") {
186
+ continue;
187
+ }
188
+ const title = (inline.content ?? "").trim();
189
+ if (!title) {
190
+ continue;
191
+ }
192
+ const slug = uniqueHeadingSlug(env, title);
193
+ if (!slug) {
194
+ continue;
195
+ }
196
+ headings.push({ level, title, slug });
197
+ }
198
+ return headings;
199
+ }
@@ -0,0 +1,35 @@
1
+ export interface InlineRefMatch {
2
+ path: string;
3
+ title: string;
4
+ }
5
+ export interface InjectInlineRefsResult {
6
+ body: string;
7
+ matchedPaths: Set<string>;
8
+ }
9
+ export interface ProtectedRange {
10
+ start: number;
11
+ end: number;
12
+ }
13
+ /**
14
+ * CommonMark fenced code scanner. Recognizes:
15
+ * - 0-3 leading spaces on the opener and closer
16
+ * - Backtick or tilde runs of length ≥3
17
+ * - Closing fence of the same character, length ≥ opener, optional trailing whitespace
18
+ * - Unclosed fence at EOF (CommonMark treats the rest of the document as the block)
19
+ *
20
+ * Implemented as a line scanner because nested-fence semantics and length
21
+ * comparison (≥ opener) don't fit cleanly into a single regex.
22
+ */
23
+ export declare function collectFencedCodeRanges(body: string): ProtectedRange[];
24
+ export declare function mergeRanges(ranges: ProtectedRange[]): ProtectedRange[];
25
+ /**
26
+ * Source 层 read tab 渲染前的预处理:对每个 ref 的 title 在 body 中首次
27
+ * 字面出现的位置,合成 `[[title|原文本]]` wikilink 标记。产物丢回 markdown-it
28
+ * 由现有 wikilink rule 渲染为可点击 inline-wikilink 按钮。
29
+ *
30
+ * 已识别受保护区段:frontmatter / fenced & indented code(含 mermaid)/
31
+ * inline code / display + inline math / raw HTML 块(details/table/...) /
32
+ * existing wikilink(含 image embed) / markdown link 整体。
33
+ * Heading text 不算受保护 — 允许在 # heading 文本里命中。
34
+ */
35
+ export declare function injectInlineRefs(body: string, refs: ReadonlyArray<InlineRefMatch>): InjectInlineRefsResult;
@@ -0,0 +1,242 @@
1
+ function escapeRegExp(value) {
2
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
3
+ }
4
+ function hasAscii(value) {
5
+ return /[A-Za-z0-9_]/.test(value);
6
+ }
7
+ function meetsMinLength(title) {
8
+ // ASCII-bearing titles need ≥3 chars; CJK / pure-symbol titles need ≥2.
9
+ const minLen = hasAscii(title) ? 3 : 2;
10
+ return title.length >= minLen;
11
+ }
12
+ function buildTitlePattern(title) {
13
+ const escaped = escapeRegExp(title);
14
+ if (hasAscii(title)) {
15
+ return new RegExp(`(?<![\\w])${escaped}(?![\\w])`, "iu");
16
+ }
17
+ return new RegExp(escaped, "iu");
18
+ }
19
+ /** Sort by title length desc, so longer titles win when they share a prefix. */
20
+ function sortRefsLongestFirst(refs) {
21
+ return [...refs].sort((a, b) => b.title.length - a.title.length);
22
+ }
23
+ /** Try to inject ONE ref's first occurrence into the plain segments only. */
24
+ function injectOneRef(segments, ref) {
25
+ const pattern = buildTitlePattern(ref.title);
26
+ for (let i = 0; i < segments.length; i++) {
27
+ const seg = segments[i];
28
+ if (seg.kind !== "plain") {
29
+ continue;
30
+ }
31
+ const match = pattern.exec(seg.text);
32
+ if (!match) {
33
+ continue;
34
+ }
35
+ const index = match.index;
36
+ const literal = match[0];
37
+ const before = seg.text.slice(0, index);
38
+ const after = seg.text.slice(index + literal.length);
39
+ const wrapped = { kind: "protected", text: `[[${ref.title}|${literal}]]` };
40
+ const replacement = [];
41
+ if (before)
42
+ replacement.push({ kind: "plain", text: before });
43
+ replacement.push(wrapped);
44
+ if (after)
45
+ replacement.push({ kind: "plain", text: after });
46
+ segments.splice(i, 1, ...replacement);
47
+ return true;
48
+ }
49
+ return false;
50
+ }
51
+ const FRONTMATTER_PATTERN = /^---\n[\s\S]*?\n---\n?/;
52
+ // Indented code: a sequence of one-or-more lines starting with 4 spaces,
53
+ // preceded by a blank line (or BOS). Simplified to "at least 4 leading
54
+ // spaces on a fresh paragraph line."
55
+ const INDENTED_CODE_PATTERN = /(^|\n\n)((?: {4}[^\n]*(?:\n|$))+)/g;
56
+ const INLINE_CODE_PATTERN = /(`+)(?:.+?)\1/g;
57
+ const DISPLAY_MATH_PATTERN = /\$\$[\s\S]*?\$\$/g;
58
+ // Inline math: opening $ must not be preceded by '\' and not followed by '$';
59
+ // closing $ must not be preceded by '\'. Content single-line.
60
+ const INLINE_MATH_PATTERN = /(?<!\\)\$(?!\$)((?:\\\$|[^\n$])+?)(?<!\\)\$/g;
61
+ // Raw HTML block: a `<tag ...>...</tag>` span where tag is a known block tag.
62
+ // Conservative whitelist matching the markdown reader's sanitizer scope.
63
+ const RAW_HTML_BLOCK_PATTERN = /<(details|table|summary|div|section|article|aside|nav|header|footer)\b[^>]*>[\s\S]*?<\/\1>/gi;
64
+ // Existing wikilink (with optional image bang) — must NOT be wrapped again.
65
+ const EXISTING_WIKILINK_PATTERN = /!?\[\[[^\]\n]+?\]\]/g;
66
+ // Markdown inline link: [text](url). The URL allows one level of balanced
67
+ // parens (e.g. `https://example.com/path(v2)`) — deeper nesting is too rare
68
+ // to chase with a non-recursive regex.
69
+ const MARKDOWN_LINK_PATTERN = /\[(?:\\\]|[^\]\n])+?\]\((?:\\\)|\([^()\n]*\)|[^()\n])+?\)/g;
70
+ // Reference-style links: [text][label] (full) and [text][] (collapsed).
71
+ // Shortcut form `[label]` overlaps with stray bracketed text and needs the
72
+ // definition table to disambiguate — we only protect the explicit two-bracket
73
+ // forms here.
74
+ const REFERENCE_LINK_PATTERN = /\[(?:\\\]|[^\]\n])+?\]\[(?:\\\]|[^\]\n])*\]/g;
75
+ // Link reference definition: `[label]: url`. Must start on its own line with
76
+ // 0-3 spaces of indent. The whitespace after `:` is optional — markdown-it
77
+ // (and CommonMark in practice) accepts `[arch]:https://example.com/Foo`.
78
+ // Multi-line title definitions are not protected (rare in source notes).
79
+ const LINK_DEFINITION_PATTERN = /^ {0,3}\[(?:\\\]|[^\]\n])+?\]:[ \t]*\S[^\n]*$/gm;
80
+ /**
81
+ * CommonMark fenced code scanner. Recognizes:
82
+ * - 0-3 leading spaces on the opener and closer
83
+ * - Backtick or tilde runs of length ≥3
84
+ * - Closing fence of the same character, length ≥ opener, optional trailing whitespace
85
+ * - Unclosed fence at EOF (CommonMark treats the rest of the document as the block)
86
+ *
87
+ * Implemented as a line scanner because nested-fence semantics and length
88
+ * comparison (≥ opener) don't fit cleanly into a single regex.
89
+ */
90
+ export function collectFencedCodeRanges(body) {
91
+ const ranges = [];
92
+ const lines = body.split("\n");
93
+ const lineStarts = [];
94
+ let offset = 0;
95
+ for (const line of lines) {
96
+ lineStarts.push(offset);
97
+ offset += line.length + 1; // +1 for the \n separator
98
+ }
99
+ let i = 0;
100
+ while (i < lines.length) {
101
+ const opener = /^( {0,3})(`{3,}|~{3,})/.exec(lines[i]);
102
+ if (!opener) {
103
+ i++;
104
+ continue;
105
+ }
106
+ const fenceChar = opener[2][0];
107
+ const fenceLen = opener[2].length;
108
+ const closingRe = new RegExp(`^ {0,3}\\${fenceChar}{${fenceLen},}[ \\t]*$`);
109
+ let j = i + 1;
110
+ while (j < lines.length && !closingRe.test(lines[j])) {
111
+ j++;
112
+ }
113
+ const startPos = lineStarts[i];
114
+ let endPos;
115
+ if (j < lines.length) {
116
+ // Include the closing line and its trailing newline if present.
117
+ endPos = lineStarts[j] + lines[j].length;
118
+ if (endPos < body.length && body[endPos] === "\n")
119
+ endPos++;
120
+ }
121
+ else {
122
+ endPos = body.length;
123
+ }
124
+ ranges.push({ start: startPos, end: endPos });
125
+ i = j + 1;
126
+ }
127
+ return ranges;
128
+ }
129
+ function collectProtectedRanges(body) {
130
+ const ranges = [];
131
+ const fm = FRONTMATTER_PATTERN.exec(body);
132
+ if (fm) {
133
+ ranges.push({ start: 0, end: fm[0].length });
134
+ }
135
+ ranges.push(...collectFencedCodeRanges(body));
136
+ let m;
137
+ const indented = new RegExp(INDENTED_CODE_PATTERN.source, INDENTED_CODE_PATTERN.flags);
138
+ while ((m = indented.exec(body)) !== null) {
139
+ const lead = m[1].length;
140
+ ranges.push({ start: m.index + lead, end: m.index + m[0].length });
141
+ }
142
+ const inlineCode = new RegExp(INLINE_CODE_PATTERN.source, INLINE_CODE_PATTERN.flags);
143
+ while ((m = inlineCode.exec(body)) !== null) {
144
+ ranges.push({ start: m.index, end: m.index + m[0].length });
145
+ }
146
+ const displayMath = new RegExp(DISPLAY_MATH_PATTERN.source, DISPLAY_MATH_PATTERN.flags);
147
+ while ((m = displayMath.exec(body)) !== null) {
148
+ ranges.push({ start: m.index, end: m.index + m[0].length });
149
+ }
150
+ const inlineMath = new RegExp(INLINE_MATH_PATTERN.source, INLINE_MATH_PATTERN.flags);
151
+ while ((m = inlineMath.exec(body)) !== null) {
152
+ ranges.push({ start: m.index, end: m.index + m[0].length });
153
+ }
154
+ const rawHtml = new RegExp(RAW_HTML_BLOCK_PATTERN.source, RAW_HTML_BLOCK_PATTERN.flags);
155
+ while ((m = rawHtml.exec(body)) !== null) {
156
+ ranges.push({ start: m.index, end: m.index + m[0].length });
157
+ }
158
+ const wikilinks = new RegExp(EXISTING_WIKILINK_PATTERN.source, EXISTING_WIKILINK_PATTERN.flags);
159
+ while ((m = wikilinks.exec(body)) !== null) {
160
+ ranges.push({ start: m.index, end: m.index + m[0].length });
161
+ }
162
+ const mdLinks = new RegExp(MARKDOWN_LINK_PATTERN.source, MARKDOWN_LINK_PATTERN.flags);
163
+ while ((m = mdLinks.exec(body)) !== null) {
164
+ ranges.push({ start: m.index, end: m.index + m[0].length });
165
+ }
166
+ const refLinks = new RegExp(REFERENCE_LINK_PATTERN.source, REFERENCE_LINK_PATTERN.flags);
167
+ while ((m = refLinks.exec(body)) !== null) {
168
+ ranges.push({ start: m.index, end: m.index + m[0].length });
169
+ }
170
+ const linkDefs = new RegExp(LINK_DEFINITION_PATTERN.source, LINK_DEFINITION_PATTERN.flags);
171
+ while ((m = linkDefs.exec(body)) !== null) {
172
+ ranges.push({ start: m.index, end: m.index + m[0].length });
173
+ }
174
+ return mergeRanges(ranges);
175
+ }
176
+ export function mergeRanges(ranges) {
177
+ if (ranges.length <= 1)
178
+ return ranges.slice().sort((a, b) => a.start - b.start);
179
+ const sorted = ranges.slice().sort((a, b) => a.start - b.start);
180
+ const merged = [sorted[0]];
181
+ for (let i = 1; i < sorted.length; i++) {
182
+ const last = merged[merged.length - 1];
183
+ const next = sorted[i];
184
+ if (next.start <= last.end) {
185
+ last.end = Math.max(last.end, next.end);
186
+ }
187
+ else {
188
+ merged.push(next);
189
+ }
190
+ }
191
+ return merged;
192
+ }
193
+ function sliceByRanges(body, ranges) {
194
+ if (!ranges.length)
195
+ return [{ kind: "plain", text: body }];
196
+ const segs = [];
197
+ let cursor = 0;
198
+ for (const range of ranges) {
199
+ if (range.start > cursor) {
200
+ segs.push({ kind: "plain", text: body.slice(cursor, range.start) });
201
+ }
202
+ segs.push({ kind: "protected", text: body.slice(range.start, range.end) });
203
+ cursor = range.end;
204
+ }
205
+ if (cursor < body.length) {
206
+ segs.push({ kind: "plain", text: body.slice(cursor) });
207
+ }
208
+ return segs;
209
+ }
210
+ /**
211
+ * Source 层 read tab 渲染前的预处理:对每个 ref 的 title 在 body 中首次
212
+ * 字面出现的位置,合成 `[[title|原文本]]` wikilink 标记。产物丢回 markdown-it
213
+ * 由现有 wikilink rule 渲染为可点击 inline-wikilink 按钮。
214
+ *
215
+ * 已识别受保护区段:frontmatter / fenced & indented code(含 mermaid)/
216
+ * inline code / display + inline math / raw HTML 块(details/table/...) /
217
+ * existing wikilink(含 image embed) / markdown link 整体。
218
+ * Heading text 不算受保护 — 允许在 # heading 文本里命中。
219
+ */
220
+ export function injectInlineRefs(body, refs) {
221
+ const matchedPaths = new Set();
222
+ // Normalize line endings before range collection — line-oriented recognizers
223
+ // (frontmatter, fenced code, indented code, display math) expect LF. The
224
+ // enhanced output also uses LF; the Source tab still shows the original
225
+ // page.body so CRLF preservation isn't required downstream.
226
+ const normalized = body.replace(/\r\n/g, "\n");
227
+ const segments = sliceByRanges(normalized, collectProtectedRanges(normalized));
228
+ for (const ref of sortRefsLongestFirst(refs)) {
229
+ if (!meetsMinLength(ref.title)) {
230
+ continue;
231
+ }
232
+ if (matchedPaths.has(ref.path)) {
233
+ // Same path already injected via an earlier (possibly longer-title)
234
+ // entry — skip duplicates so each K page gets at most one inline link.
235
+ continue;
236
+ }
237
+ if (injectOneRef(segments, ref)) {
238
+ matchedPaths.add(ref.path);
239
+ }
240
+ }
241
+ return { body: segments.map((s) => s.text).join(""), matchedPaths };
242
+ }
@@ -0,0 +1,5 @@
1
+ export interface FrontmatterFields {
2
+ original_filename: string;
3
+ converter?: string;
4
+ }
5
+ export declare function mergeFrontmatter(text: string, fields: FrontmatterFields): string;