@aeye/resources 0.3.9
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/aeye-resources-graph.md +158 -0
- package/aeye-resources-parsers.md +175 -0
- package/aeye-resources-slicers.md +158 -0
- package/aeye-resources.md +186 -0
- package/dist/default.d.ts +8 -0
- package/dist/default.d.ts.map +1 -0
- package/dist/default.js +31 -0
- package/dist/default.js.map +1 -0
- package/dist/graph.d.ts +93 -0
- package/dist/graph.d.ts.map +1 -0
- package/dist/graph.js +154 -0
- package/dist/graph.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/parsers/code.d.ts +3 -0
- package/dist/parsers/code.d.ts.map +1 -0
- package/dist/parsers/code.js +28 -0
- package/dist/parsers/code.js.map +1 -0
- package/dist/parsers/docx.d.ts +10 -0
- package/dist/parsers/docx.d.ts.map +1 -0
- package/dist/parsers/docx.js +89 -0
- package/dist/parsers/docx.js.map +1 -0
- package/dist/parsers/excel.d.ts +9 -0
- package/dist/parsers/excel.d.ts.map +1 -0
- package/dist/parsers/excel.js +161 -0
- package/dist/parsers/excel.js.map +1 -0
- package/dist/parsers/html.d.ts +3 -0
- package/dist/parsers/html.d.ts.map +1 -0
- package/dist/parsers/html.js +37 -0
- package/dist/parsers/html.js.map +1 -0
- package/dist/parsers/image.d.ts +3 -0
- package/dist/parsers/image.d.ts.map +1 -0
- package/dist/parsers/image.js +45 -0
- package/dist/parsers/image.js.map +1 -0
- package/dist/parsers/index.d.ts +10 -0
- package/dist/parsers/index.d.ts.map +1 -0
- package/dist/parsers/index.js +10 -0
- package/dist/parsers/index.js.map +1 -0
- package/dist/parsers/markdown.d.ts +3 -0
- package/dist/parsers/markdown.d.ts.map +1 -0
- package/dist/parsers/markdown.js +28 -0
- package/dist/parsers/markdown.js.map +1 -0
- package/dist/parsers/pdf.d.ts +10 -0
- package/dist/parsers/pdf.d.ts.map +1 -0
- package/dist/parsers/pdf.js +165 -0
- package/dist/parsers/pdf.js.map +1 -0
- package/dist/parsers/text.d.ts +4 -0
- package/dist/parsers/text.d.ts.map +1 -0
- package/dist/parsers/text.js +42 -0
- package/dist/parsers/text.js.map +1 -0
- package/dist/parsers/zip.d.ts +3 -0
- package/dist/parsers/zip.d.ts.map +1 -0
- package/dist/parsers/zip.js +131 -0
- package/dist/parsers/zip.js.map +1 -0
- package/dist/registry.d.ts +81 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +322 -0
- package/dist/registry.js.map +1 -0
- package/dist/renderers/index.d.ts +3 -0
- package/dist/renderers/index.d.ts.map +1 -0
- package/dist/renderers/index.js +2 -0
- package/dist/renderers/index.js.map +1 -0
- package/dist/renderers/poppler.d.ts +38 -0
- package/dist/renderers/poppler.d.ts.map +1 -0
- package/dist/renderers/poppler.js +92 -0
- package/dist/renderers/poppler.js.map +1 -0
- package/dist/resolvers/file.d.ts +3 -0
- package/dist/resolvers/file.d.ts.map +1 -0
- package/dist/resolvers/file.js +68 -0
- package/dist/resolvers/file.js.map +1 -0
- package/dist/resolvers/index.d.ts +4 -0
- package/dist/resolvers/index.d.ts.map +1 -0
- package/dist/resolvers/index.js +4 -0
- package/dist/resolvers/index.js.map +1 -0
- package/dist/resolvers/url.d.ts +3 -0
- package/dist/resolvers/url.d.ts.map +1 -0
- package/dist/resolvers/url.js +91 -0
- package/dist/resolvers/url.js.map +1 -0
- package/dist/resolvers/zip.d.ts +8 -0
- package/dist/resolvers/zip.d.ts.map +1 -0
- package/dist/resolvers/zip.js +73 -0
- package/dist/resolvers/zip.js.map +1 -0
- package/dist/segmenters/default.d.ts +7 -0
- package/dist/segmenters/default.d.ts.map +1 -0
- package/dist/segmenters/default.js +7 -0
- package/dist/segmenters/default.js.map +1 -0
- package/dist/segmenters/index.d.ts +4 -0
- package/dist/segmenters/index.d.ts.map +1 -0
- package/dist/segmenters/index.js +3 -0
- package/dist/segmenters/index.js.map +1 -0
- package/dist/segmenters/langchain.d.ts +33 -0
- package/dist/segmenters/langchain.d.ts.map +1 -0
- package/dist/segmenters/langchain.js +110 -0
- package/dist/segmenters/langchain.js.map +1 -0
- package/dist/slicers/code.d.ts +3 -0
- package/dist/slicers/code.d.ts.map +1 -0
- package/dist/slicers/code.js +78 -0
- package/dist/slicers/code.js.map +1 -0
- package/dist/slicers/index.d.ts +4 -0
- package/dist/slicers/index.d.ts.map +1 -0
- package/dist/slicers/index.js +4 -0
- package/dist/slicers/index.js.map +1 -0
- package/dist/slicers/markdown.d.ts +3 -0
- package/dist/slicers/markdown.d.ts.map +1 -0
- package/dist/slicers/markdown.js +70 -0
- package/dist/slicers/markdown.js.map +1 -0
- package/dist/slicers/text.d.ts +3 -0
- package/dist/slicers/text.d.ts.map +1 -0
- package/dist/slicers/text.js +43 -0
- package/dist/slicers/text.js.map +1 -0
- package/dist/types.d.ts +306 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +97 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +788 -0
- package/dist/utils.js.map +1 -0
- package/dist/zip-internal.d.ts +28 -0
- package/dist/zip-internal.d.ts.map +1 -0
- package/dist/zip-internal.js +52 -0
- package/dist/zip-internal.js.map +1 -0
- package/package.json +104 -0
package/dist/utils.js
ADDED
|
@@ -0,0 +1,788 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
import { Readable } from "node:stream";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
export const DEFAULT_MAX_CHARS = 2000;
|
|
6
|
+
export const DEFAULT_MIN_CHARS = 400;
|
|
7
|
+
export const CODE_TYPES = [
|
|
8
|
+
"javascript",
|
|
9
|
+
"typescript",
|
|
10
|
+
"jsx",
|
|
11
|
+
"tsx",
|
|
12
|
+
"python",
|
|
13
|
+
"ruby",
|
|
14
|
+
"go",
|
|
15
|
+
"java",
|
|
16
|
+
"c",
|
|
17
|
+
"cpp",
|
|
18
|
+
"csharp",
|
|
19
|
+
"php",
|
|
20
|
+
"rust",
|
|
21
|
+
"swift",
|
|
22
|
+
"kotlin",
|
|
23
|
+
"scala",
|
|
24
|
+
"sql",
|
|
25
|
+
"shell",
|
|
26
|
+
"css",
|
|
27
|
+
"scss",
|
|
28
|
+
"less",
|
|
29
|
+
"dockerfile",
|
|
30
|
+
"graphql",
|
|
31
|
+
"lua",
|
|
32
|
+
"r",
|
|
33
|
+
"dart"
|
|
34
|
+
];
|
|
35
|
+
/** Default file-extension → resource type mappings. Seeds the registry; override via the registry. */
|
|
36
|
+
export const DEFAULT_EXTENSION_TYPES = {
|
|
37
|
+
".txt": "text",
|
|
38
|
+
".text": "text",
|
|
39
|
+
".log": "text",
|
|
40
|
+
".md": "markdown",
|
|
41
|
+
".markdown": "markdown",
|
|
42
|
+
".mdx": "markdown",
|
|
43
|
+
".html": "html",
|
|
44
|
+
".htm": "html",
|
|
45
|
+
".json": "json",
|
|
46
|
+
".jsonl": "json",
|
|
47
|
+
".csv": "csv",
|
|
48
|
+
".tsv": "tsv",
|
|
49
|
+
".yaml": "yaml",
|
|
50
|
+
".yml": "yaml",
|
|
51
|
+
".xml": "xml",
|
|
52
|
+
".svg": "svg",
|
|
53
|
+
".toml": "toml",
|
|
54
|
+
".ini": "ini",
|
|
55
|
+
".conf": "ini",
|
|
56
|
+
".js": "javascript",
|
|
57
|
+
".mjs": "javascript",
|
|
58
|
+
".cjs": "javascript",
|
|
59
|
+
".ts": "typescript",
|
|
60
|
+
".mts": "typescript",
|
|
61
|
+
".cts": "typescript",
|
|
62
|
+
".jsx": "jsx",
|
|
63
|
+
".tsx": "tsx",
|
|
64
|
+
".py": "python",
|
|
65
|
+
".rb": "ruby",
|
|
66
|
+
".go": "go",
|
|
67
|
+
".java": "java",
|
|
68
|
+
".c": "c",
|
|
69
|
+
".h": "c",
|
|
70
|
+
".cpp": "cpp",
|
|
71
|
+
".cc": "cpp",
|
|
72
|
+
".cxx": "cpp",
|
|
73
|
+
".hpp": "cpp",
|
|
74
|
+
".cs": "csharp",
|
|
75
|
+
".php": "php",
|
|
76
|
+
".rs": "rust",
|
|
77
|
+
".swift": "swift",
|
|
78
|
+
".kt": "kotlin",
|
|
79
|
+
".kts": "kotlin",
|
|
80
|
+
".scala": "scala",
|
|
81
|
+
".sql": "sql",
|
|
82
|
+
".sh": "shell",
|
|
83
|
+
".bash": "shell",
|
|
84
|
+
".zsh": "shell",
|
|
85
|
+
".ps1": "shell",
|
|
86
|
+
".css": "css",
|
|
87
|
+
".scss": "scss",
|
|
88
|
+
".less": "less",
|
|
89
|
+
".graphql": "graphql",
|
|
90
|
+
".gql": "graphql",
|
|
91
|
+
".lua": "lua",
|
|
92
|
+
".r": "r",
|
|
93
|
+
".dart": "dart",
|
|
94
|
+
".png": "image",
|
|
95
|
+
".jpg": "image",
|
|
96
|
+
".jpeg": "image",
|
|
97
|
+
".gif": "image",
|
|
98
|
+
".webp": "image",
|
|
99
|
+
".bmp": "image",
|
|
100
|
+
".ico": "image",
|
|
101
|
+
".avif": "image",
|
|
102
|
+
".pdf": "pdf",
|
|
103
|
+
".xlsx": "excel",
|
|
104
|
+
".xls": "excel",
|
|
105
|
+
".docx": "docx",
|
|
106
|
+
".doc": "docx",
|
|
107
|
+
".zip": "zip",
|
|
108
|
+
".tar.gz": "zip",
|
|
109
|
+
".tgz": "zip",
|
|
110
|
+
".jar": "zip"
|
|
111
|
+
};
|
|
112
|
+
/** Built-in lookup map for the default extension types (used by the standalone inference helpers). */
|
|
113
|
+
const DEFAULT_EXTENSION_TYPE_MAP = new Map(Object.entries(DEFAULT_EXTENSION_TYPES));
|
|
114
|
+
/** Default mime-pattern → resource type mappings. Seeds the registry; override via the registry. */
|
|
115
|
+
export const DEFAULT_MIME_TYPE_PATTERNS = [
|
|
116
|
+
[/^text\/markdown/i, "markdown"],
|
|
117
|
+
[/^text\/html/i, "html"],
|
|
118
|
+
[/^application\/json/i, "json"],
|
|
119
|
+
[/^application\/([a-z.+-]*\+)?json/i, "json"],
|
|
120
|
+
[/^text\/csv/i, "csv"],
|
|
121
|
+
[/^text\/tab-separated-values/i, "tsv"],
|
|
122
|
+
[/^application\/xml/i, "xml"],
|
|
123
|
+
[/^text\/xml/i, "xml"],
|
|
124
|
+
[/^application\/pdf/i, "pdf"],
|
|
125
|
+
[/^application\/vnd\.openxmlformats-officedocument\.spreadsheetml\.sheet/i, "excel"],
|
|
126
|
+
[/^application\/vnd\.ms-excel/i, "excel"],
|
|
127
|
+
[/^application\/vnd\.openxmlformats-officedocument\.wordprocessingml\.document/i, "docx"],
|
|
128
|
+
[/^application\/msword/i, "docx"],
|
|
129
|
+
[/^application\/zip/i, "zip"],
|
|
130
|
+
[/^application\/x-zip-compressed/i, "zip"],
|
|
131
|
+
[/^application\/x-tar/i, "zip"],
|
|
132
|
+
[/^application\/gzip/i, "zip"],
|
|
133
|
+
[/^image\//i, "image"],
|
|
134
|
+
[/^text\//i, "text"]
|
|
135
|
+
];
|
|
136
|
+
/**
|
|
137
|
+
* Merges per-type registry options (the base) with per-call options (the overrides). Per-call options
|
|
138
|
+
* win at the top level; the nested `pdf` and `code` option objects are shallow merged so a caller can
|
|
139
|
+
* override a single flag without dropping registry-configured defaults. Works across parse and slice
|
|
140
|
+
* options alike, enabling per-type configuration of any behavior from the registry.
|
|
141
|
+
*/
|
|
142
|
+
export function mergeOptions(base, overrides) {
|
|
143
|
+
if (!base)
|
|
144
|
+
return { ...(overrides ?? {}) };
|
|
145
|
+
if (!overrides)
|
|
146
|
+
return { ...base };
|
|
147
|
+
const merged = { ...base, ...overrides };
|
|
148
|
+
if (base.pdf || overrides.pdf) {
|
|
149
|
+
merged.pdf = { ...base.pdf, ...overrides.pdf };
|
|
150
|
+
}
|
|
151
|
+
if (base.code || overrides.code) {
|
|
152
|
+
merged.code = { ...base.code, ...overrides.code };
|
|
153
|
+
}
|
|
154
|
+
return merged;
|
|
155
|
+
}
|
|
156
|
+
export function assertNotAborted(signal) {
|
|
157
|
+
if (signal?.aborted) {
|
|
158
|
+
throw new Error("Operation aborted");
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
export async function collectInput(input) {
|
|
162
|
+
if (input instanceof Uint8Array) {
|
|
163
|
+
return input;
|
|
164
|
+
}
|
|
165
|
+
if (input instanceof ArrayBuffer) {
|
|
166
|
+
return new Uint8Array(input);
|
|
167
|
+
}
|
|
168
|
+
if (input instanceof URL) {
|
|
169
|
+
return Buffer.from(input.toString(), "utf8");
|
|
170
|
+
}
|
|
171
|
+
if (typeof input === "string") {
|
|
172
|
+
return Buffer.from(input, "utf8");
|
|
173
|
+
}
|
|
174
|
+
// Factory function that produces an async iterable (lazy streaming)
|
|
175
|
+
if (typeof input === "function") {
|
|
176
|
+
const chunks = [];
|
|
177
|
+
for await (const chunk of input()) {
|
|
178
|
+
chunks.push(chunk);
|
|
179
|
+
}
|
|
180
|
+
return Buffer.concat(chunks);
|
|
181
|
+
}
|
|
182
|
+
// Web ReadableStream
|
|
183
|
+
if (isReadableStream(input)) {
|
|
184
|
+
const chunks = [];
|
|
185
|
+
const reader = input.getReader();
|
|
186
|
+
try {
|
|
187
|
+
while (true) {
|
|
188
|
+
const { done, value } = await reader.read();
|
|
189
|
+
if (done)
|
|
190
|
+
break;
|
|
191
|
+
chunks.push(value);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
finally {
|
|
195
|
+
reader.releaseLock();
|
|
196
|
+
}
|
|
197
|
+
return Buffer.concat(chunks);
|
|
198
|
+
}
|
|
199
|
+
if (input instanceof Readable || isAsyncIterable(input) || isIterable(input)) {
|
|
200
|
+
const chunks = [];
|
|
201
|
+
for await (const chunk of input) {
|
|
202
|
+
chunks.push(typeof chunk === "string" ? Buffer.from(chunk, "utf8") : chunk);
|
|
203
|
+
}
|
|
204
|
+
return Buffer.concat(chunks);
|
|
205
|
+
}
|
|
206
|
+
throw new Error("Unsupported resource input");
|
|
207
|
+
}
|
|
208
|
+
function isAsyncIterable(value) {
|
|
209
|
+
return Boolean(value && typeof value[Symbol.asyncIterator] === "function");
|
|
210
|
+
}
|
|
211
|
+
function isIterable(value) {
|
|
212
|
+
return Boolean(value && typeof value[Symbol.iterator] === "function");
|
|
213
|
+
}
|
|
214
|
+
function isReadableStream(value) {
|
|
215
|
+
return Boolean(value && typeof value.getReader === "function" && typeof value.locked !== "undefined");
|
|
216
|
+
}
|
|
217
|
+
export async function readText(input) {
|
|
218
|
+
const content = await collectInput(input);
|
|
219
|
+
return Buffer.from(content).toString("utf8");
|
|
220
|
+
}
|
|
221
|
+
export function basenameFromLocation(location) {
|
|
222
|
+
if (/^https?:\/\//i.test(location)) {
|
|
223
|
+
const url = new URL(location);
|
|
224
|
+
const name = path.posix.basename(url.pathname);
|
|
225
|
+
return name || url.hostname || location;
|
|
226
|
+
}
|
|
227
|
+
if (location.startsWith("file://")) {
|
|
228
|
+
return path.basename(fileURLToPath(location));
|
|
229
|
+
}
|
|
230
|
+
return path.basename(location) || location;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Infers a resource type from a location using the supplied extension map. Recognizes Dockerfiles and
|
|
234
|
+
* compound extensions (e.g. `.tar.gz`) by scanning suffixes from the longest match.
|
|
235
|
+
*/
|
|
236
|
+
export function inferTypeFromExtension(location, extensionTypes) {
|
|
237
|
+
const lower = location.toLowerCase();
|
|
238
|
+
if (lower.endsWith("/dockerfile") || path.basename(lower) === "dockerfile") {
|
|
239
|
+
return "dockerfile";
|
|
240
|
+
}
|
|
241
|
+
const base = path.basename(lower);
|
|
242
|
+
for (let dot = base.indexOf("."); dot !== -1; dot = base.indexOf(".", dot + 1)) {
|
|
243
|
+
const type = extensionTypes.get(base.slice(dot));
|
|
244
|
+
if (type) {
|
|
245
|
+
return type;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return undefined;
|
|
249
|
+
}
|
|
250
|
+
/** Infers a resource type from a mime type using the supplied pattern list. */
|
|
251
|
+
export function inferTypeFromMimePatterns(mimeType, patterns) {
|
|
252
|
+
if (!mimeType) {
|
|
253
|
+
return undefined;
|
|
254
|
+
}
|
|
255
|
+
for (const [pattern, type] of patterns) {
|
|
256
|
+
if (pattern.test(mimeType)) {
|
|
257
|
+
return type;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return undefined;
|
|
261
|
+
}
|
|
262
|
+
export function inferTypeFromLocation(location, mimeType) {
|
|
263
|
+
return inferTypeFromMimePatterns(mimeType, DEFAULT_MIME_TYPE_PATTERNS)
|
|
264
|
+
?? inferTypeFromExtension(location, DEFAULT_EXTENSION_TYPE_MAP);
|
|
265
|
+
}
|
|
266
|
+
export function inferTypeFromMimeType(mimeType) {
|
|
267
|
+
return inferTypeFromMimePatterns(mimeType, DEFAULT_MIME_TYPE_PATTERNS);
|
|
268
|
+
}
|
|
269
|
+
export function createResourceId(location, type) {
|
|
270
|
+
return `${location}::${type}`;
|
|
271
|
+
}
|
|
272
|
+
export function createPartId(resource, index, suffix = "part") {
|
|
273
|
+
return `${resource.id}::${suffix}/${index}`;
|
|
274
|
+
}
|
|
275
|
+
export function createSliceId(part, index) {
|
|
276
|
+
return `${part.id}::slice/${index}`;
|
|
277
|
+
}
|
|
278
|
+
export function createLinkId(location, value, index) {
|
|
279
|
+
return `${location}::link/${index}:${value}`;
|
|
280
|
+
}
|
|
281
|
+
export function dedupeLinks(links) {
|
|
282
|
+
const seen = new Set();
|
|
283
|
+
return links.filter((link) => {
|
|
284
|
+
const key = `${link.location}|${link.value}|${link.title ?? ""}`;
|
|
285
|
+
if (seen.has(key)) {
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
seen.add(key);
|
|
289
|
+
return true;
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
export function splitTextByBoundaries(text, maxChars = DEFAULT_MAX_CHARS, minChars = DEFAULT_MIN_CHARS, boundaries = ["\n\n", "\n", " "]) {
|
|
293
|
+
if (text.length <= maxChars) {
|
|
294
|
+
return [{ start: 0, end: text.length, text }];
|
|
295
|
+
}
|
|
296
|
+
const segments = [];
|
|
297
|
+
let cursor = 0;
|
|
298
|
+
while (cursor < text.length) {
|
|
299
|
+
const remaining = text.length - cursor;
|
|
300
|
+
if (remaining <= maxChars) {
|
|
301
|
+
segments.push({ start: cursor, end: text.length, text: text.slice(cursor) });
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
304
|
+
const targetEnd = cursor + maxChars;
|
|
305
|
+
const minimumEnd = Math.min(text.length, cursor + minChars);
|
|
306
|
+
let bestEnd = -1;
|
|
307
|
+
for (const boundary of boundaries) {
|
|
308
|
+
const found = text.lastIndexOf(boundary, targetEnd);
|
|
309
|
+
if (found >= minimumEnd) {
|
|
310
|
+
bestEnd = found + boundary.length;
|
|
311
|
+
break;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
if (bestEnd === -1 || bestEnd <= cursor) {
|
|
315
|
+
bestEnd = targetEnd;
|
|
316
|
+
}
|
|
317
|
+
segments.push({ start: cursor, end: bestEnd, text: text.slice(cursor, bestEnd) });
|
|
318
|
+
cursor = bestEnd;
|
|
319
|
+
}
|
|
320
|
+
return segments;
|
|
321
|
+
}
|
|
322
|
+
export function buildEmbedText(context, options) {
|
|
323
|
+
if (options.buildEmbedText) {
|
|
324
|
+
return options.buildEmbedText(context);
|
|
325
|
+
}
|
|
326
|
+
const lines = [...context.contextLines];
|
|
327
|
+
if (options.includeLinks !== false && context.slice.links.length > 0) {
|
|
328
|
+
lines.push(`Links: ${context.slice.links.map((link) => link.value).join(", ")}`);
|
|
329
|
+
}
|
|
330
|
+
lines.push(context.slice.text);
|
|
331
|
+
return lines.filter(Boolean).join(options.embedSeparator ?? "\n\n");
|
|
332
|
+
}
|
|
333
|
+
export function collectEmbedContext(resource, part, slice, options) {
|
|
334
|
+
const lines = [];
|
|
335
|
+
if (options.includeResourceLocation !== false) {
|
|
336
|
+
lines.push(`Resource: ${resource.location}`);
|
|
337
|
+
}
|
|
338
|
+
if (options.includeResourceType !== false) {
|
|
339
|
+
lines.push(`Type: ${resource.type}`);
|
|
340
|
+
}
|
|
341
|
+
if (options.includePartLocation !== false) {
|
|
342
|
+
lines.push(`Part: ${part.location}`);
|
|
343
|
+
}
|
|
344
|
+
if (options.includePartKind) {
|
|
345
|
+
lines.push(`Kind: ${part.kind}`);
|
|
346
|
+
}
|
|
347
|
+
if (options.includeContext !== false) {
|
|
348
|
+
if (slice.context?.headings?.length) {
|
|
349
|
+
lines.push(`Headings: ${slice.context.headings.join(" > ")}`);
|
|
350
|
+
}
|
|
351
|
+
if (slice.context?.declaration) {
|
|
352
|
+
lines.push(`Declaration: ${slice.context.declaration}`);
|
|
353
|
+
}
|
|
354
|
+
if (slice.context?.summary) {
|
|
355
|
+
lines.push(`Summary: ${slice.context.summary}`);
|
|
356
|
+
}
|
|
357
|
+
if (slice.context?.prefixes?.length) {
|
|
358
|
+
lines.push(...slice.context.prefixes.map((prefix) => `Context: ${prefix}`));
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
return lines;
|
|
362
|
+
}
|
|
363
|
+
export function finalizeSlice(resource, part, slice, options) {
|
|
364
|
+
const incomplete = {
|
|
365
|
+
...slice,
|
|
366
|
+
embedText: ""
|
|
367
|
+
};
|
|
368
|
+
const contextLines = collectEmbedContext(resource, part, incomplete, options);
|
|
369
|
+
return {
|
|
370
|
+
...incomplete,
|
|
371
|
+
embedText: buildEmbedText({ resource, part, slice: incomplete, contextLines }, options)
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
export function extractLinksFromMarkdown(text, location) {
|
|
375
|
+
const links = [];
|
|
376
|
+
let index = 0;
|
|
377
|
+
for (let cursor = 0; cursor < text.length; cursor++) {
|
|
378
|
+
if (text[cursor] !== "[") {
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
const labelEnd = text.indexOf("]", cursor + 1);
|
|
382
|
+
if (labelEnd === -1 || text[labelEnd + 1] !== "(") {
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
const valueEnd = text.indexOf(")", labelEnd + 2);
|
|
386
|
+
if (valueEnd === -1) {
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
const label = text.slice(cursor + 1, labelEnd).trim();
|
|
390
|
+
const rawValue = text.slice(labelEnd + 2, valueEnd).trim();
|
|
391
|
+
const value = rawValue.split(/\s+/, 1)[0]?.trim();
|
|
392
|
+
if (!value) {
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
links.push({
|
|
396
|
+
id: createLinkId(location, value, index++),
|
|
397
|
+
value,
|
|
398
|
+
location,
|
|
399
|
+
title: label || undefined,
|
|
400
|
+
kind: isExternalLink(value) ? "external" : "resource"
|
|
401
|
+
});
|
|
402
|
+
cursor = valueEnd;
|
|
403
|
+
}
|
|
404
|
+
return links;
|
|
405
|
+
}
|
|
406
|
+
export function extractLinksFromHtml(text, location) {
|
|
407
|
+
const links = [];
|
|
408
|
+
let index = 0;
|
|
409
|
+
forEachHtmlTag(text, ({ attributes }) => {
|
|
410
|
+
for (const attribute of ["href", "src"]) {
|
|
411
|
+
const value = attributes[attribute];
|
|
412
|
+
if (!value) {
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
links.push({
|
|
416
|
+
id: createLinkId(location, value, index++),
|
|
417
|
+
value,
|
|
418
|
+
location,
|
|
419
|
+
kind: isExternalLink(value) ? "external" : "resource"
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
return dedupeLinks(links);
|
|
424
|
+
}
|
|
425
|
+
function readQuotedValue(text, start) {
|
|
426
|
+
const quote = text[start];
|
|
427
|
+
if (quote !== "\"" && quote !== "'") {
|
|
428
|
+
return undefined;
|
|
429
|
+
}
|
|
430
|
+
const end = text.indexOf(quote, start + 1);
|
|
431
|
+
if (end === -1) {
|
|
432
|
+
return undefined;
|
|
433
|
+
}
|
|
434
|
+
return {
|
|
435
|
+
value: text.slice(start + 1, end),
|
|
436
|
+
end
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
function parseHtmlAttributes(input) {
|
|
440
|
+
const attributes = {};
|
|
441
|
+
let cursor = 0;
|
|
442
|
+
while (cursor < input.length) {
|
|
443
|
+
while (cursor < input.length && /\s/.test(input[cursor])) {
|
|
444
|
+
cursor++;
|
|
445
|
+
}
|
|
446
|
+
if (cursor >= input.length) {
|
|
447
|
+
break;
|
|
448
|
+
}
|
|
449
|
+
let nameEnd = cursor;
|
|
450
|
+
while (nameEnd < input.length && /[^\s=]/.test(input[nameEnd])) {
|
|
451
|
+
nameEnd++;
|
|
452
|
+
}
|
|
453
|
+
const name = input.slice(cursor, nameEnd).toLowerCase();
|
|
454
|
+
cursor = nameEnd;
|
|
455
|
+
while (cursor < input.length && /\s/.test(input[cursor])) {
|
|
456
|
+
cursor++;
|
|
457
|
+
}
|
|
458
|
+
if (input[cursor] !== "=") {
|
|
459
|
+
continue;
|
|
460
|
+
}
|
|
461
|
+
cursor++;
|
|
462
|
+
while (cursor < input.length && /\s/.test(input[cursor])) {
|
|
463
|
+
cursor++;
|
|
464
|
+
}
|
|
465
|
+
const quoted = readQuotedValue(input, cursor);
|
|
466
|
+
if (!quoted) {
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
attributes[name] = quoted.value;
|
|
470
|
+
cursor = quoted.end + 1;
|
|
471
|
+
}
|
|
472
|
+
return attributes;
|
|
473
|
+
}
|
|
474
|
+
function forEachHtmlTag(text, onTag) {
|
|
475
|
+
let cursor = 0;
|
|
476
|
+
while (cursor < text.length) {
|
|
477
|
+
const start = text.indexOf("<", cursor);
|
|
478
|
+
if (start === -1) {
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
const end = text.indexOf(">", start + 1);
|
|
482
|
+
if (end === -1) {
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
const rawTag = text.slice(start + 1, end).trim();
|
|
486
|
+
const closing = rawTag.startsWith("/");
|
|
487
|
+
const normalized = closing ? rawTag.slice(1).trim() : rawTag;
|
|
488
|
+
const nameMatch = normalized.match(/^[a-zA-Z0-9:-]+/);
|
|
489
|
+
if (nameMatch) {
|
|
490
|
+
const name = nameMatch[0].toLowerCase();
|
|
491
|
+
const attributes = parseHtmlAttributes(normalized.slice(name.length));
|
|
492
|
+
onTag({ name, closing, attributes });
|
|
493
|
+
}
|
|
494
|
+
cursor = end + 1;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
export function extractLinksFromCode(text, location) {
|
|
498
|
+
const links = [];
|
|
499
|
+
let index = 0;
|
|
500
|
+
const addValue = (value) => {
|
|
501
|
+
if (!value) {
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
links.push({
|
|
505
|
+
id: createLinkId(location, value, index++),
|
|
506
|
+
value,
|
|
507
|
+
location,
|
|
508
|
+
kind: isExternalLink(value) ? "external" : "resource"
|
|
509
|
+
});
|
|
510
|
+
};
|
|
511
|
+
for (const line of text.split(/\r?\n/)) {
|
|
512
|
+
const trimmed = line.trim();
|
|
513
|
+
if (!trimmed) {
|
|
514
|
+
continue;
|
|
515
|
+
}
|
|
516
|
+
if (trimmed.startsWith("import ") || trimmed.startsWith("export ")) {
|
|
517
|
+
addValue(readQuotedSpecifier(trimmed));
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
if (trimmed.includes("require(")) {
|
|
521
|
+
addValue(readCallSpecifier(trimmed, "require"));
|
|
522
|
+
}
|
|
523
|
+
if (trimmed.includes("import(")) {
|
|
524
|
+
addValue(readCallSpecifier(trimmed, "import"));
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
return dedupeLinks(links);
|
|
528
|
+
}
|
|
529
|
+
function readQuotedSpecifier(line) {
|
|
530
|
+
for (let cursor = 0; cursor < line.length; cursor++) {
|
|
531
|
+
const quoted = readQuotedValue(line, cursor);
|
|
532
|
+
if (quoted) {
|
|
533
|
+
return quoted.value;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
return undefined;
|
|
537
|
+
}
|
|
538
|
+
function readCallSpecifier(line, callName) {
|
|
539
|
+
const start = line.indexOf(`${callName}(`);
|
|
540
|
+
if (start === -1) {
|
|
541
|
+
return undefined;
|
|
542
|
+
}
|
|
543
|
+
const quoted = readQuotedValue(line, start + callName.length + 1);
|
|
544
|
+
return quoted?.value;
|
|
545
|
+
}
|
|
546
|
+
export function extractLinksFromText(text, location) {
|
|
547
|
+
const links = [];
|
|
548
|
+
const pattern = /https?:\/\/[^\s)\]>'"]+/g;
|
|
549
|
+
let match;
|
|
550
|
+
let index = 0;
|
|
551
|
+
while ((match = pattern.exec(text))) {
|
|
552
|
+
links.push({
|
|
553
|
+
id: createLinkId(location, match[0], index++),
|
|
554
|
+
value: match[0],
|
|
555
|
+
location,
|
|
556
|
+
kind: "external"
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
return links;
|
|
560
|
+
}
|
|
561
|
+
export function isExternalLink(value) {
|
|
562
|
+
return /^(?:[a-z][a-z0-9+.-]*:)?\/\//i.test(value) || /^mailto:/i.test(value);
|
|
563
|
+
}
|
|
564
|
+
/** True for absolute http(s) URLs. */
|
|
565
|
+
export function isHttpUrl(value) {
|
|
566
|
+
return /^https?:\/\//i.test(value);
|
|
567
|
+
}
|
|
568
|
+
/** True when the link carries an explicit URI scheme (e.g. `http:`, `mailto:`, `data:`, `ftp:`). */
|
|
569
|
+
export function hasUriScheme(value) {
|
|
570
|
+
return /^[a-z][a-z0-9+.-]*:/i.test(value);
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* Resolves a relative link against an http(s) base URL, returning an absolute URL.
|
|
574
|
+
* Handles relative paths, root-relative paths (`/x`), protocol-relative (`//host/x`), and fragments.
|
|
575
|
+
*/
|
|
576
|
+
export function resolveAgainstUrl(link, baseUrl) {
|
|
577
|
+
return new URL(link, baseUrl).toString();
|
|
578
|
+
}
|
|
579
|
+
export function htmlToMarkdown(html) {
|
|
580
|
+
const safeHtml = stripHtmlBlocks(html, ["script", "style"]);
|
|
581
|
+
const parts = [];
|
|
582
|
+
let cursor = 0;
|
|
583
|
+
while (cursor < safeHtml.length) {
|
|
584
|
+
const tagStart = safeHtml.indexOf("<", cursor);
|
|
585
|
+
if (tagStart === -1) {
|
|
586
|
+
parts.push(safeHtml.slice(cursor));
|
|
587
|
+
break;
|
|
588
|
+
}
|
|
589
|
+
parts.push(safeHtml.slice(cursor, tagStart));
|
|
590
|
+
const tagEnd = safeHtml.indexOf(">", tagStart + 1);
|
|
591
|
+
if (tagEnd === -1) {
|
|
592
|
+
break;
|
|
593
|
+
}
|
|
594
|
+
const rawTag = safeHtml.slice(tagStart + 1, tagEnd).trim();
|
|
595
|
+
const closing = rawTag.startsWith("/");
|
|
596
|
+
const normalized = closing ? rawTag.slice(1).trim() : rawTag;
|
|
597
|
+
const name = normalized.split(/\s+/, 1)[0]?.toLowerCase();
|
|
598
|
+
const attributes = parseHtmlAttributes(normalized.slice(name?.length ?? 0));
|
|
599
|
+
if (name) {
|
|
600
|
+
if (!closing && /^h[1-6]$/.test(name)) {
|
|
601
|
+
parts.push(`\n\n${"#".repeat(Number(name[1]))} `);
|
|
602
|
+
}
|
|
603
|
+
else if (!closing && name === "li") {
|
|
604
|
+
parts.push("\n- ");
|
|
605
|
+
}
|
|
606
|
+
else if (!closing && name === "br") {
|
|
607
|
+
parts.push("\n");
|
|
608
|
+
}
|
|
609
|
+
else if (!closing && name === "img" && attributes.src) {
|
|
610
|
+
parts.push(``);
|
|
611
|
+
}
|
|
612
|
+
else if (closing && /^h[1-6]$/.test(name)) {
|
|
613
|
+
parts.push("\n\n");
|
|
614
|
+
}
|
|
615
|
+
else if (closing && ["p", "div", "section", "article", "header", "footer", "ul", "ol", "li"].includes(name)) {
|
|
616
|
+
parts.push("\n\n");
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
cursor = tagEnd + 1;
|
|
620
|
+
}
|
|
621
|
+
return collapseWhitespace(decodeHtmlEntities(parts.join(""))).trim();
|
|
622
|
+
}
|
|
623
|
+
export function stripTags(value) {
|
|
624
|
+
let output = "";
|
|
625
|
+
let insideTag = false;
|
|
626
|
+
for (const character of value) {
|
|
627
|
+
if (character === "<") {
|
|
628
|
+
insideTag = true;
|
|
629
|
+
output += " ";
|
|
630
|
+
continue;
|
|
631
|
+
}
|
|
632
|
+
if (character === ">") {
|
|
633
|
+
insideTag = false;
|
|
634
|
+
continue;
|
|
635
|
+
}
|
|
636
|
+
if (!insideTag) {
|
|
637
|
+
output += character;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
return collapseInlineWhitespace(decodeHtmlEntities(output)).trim();
|
|
641
|
+
}
|
|
642
|
+
export function toFilePath(location) {
|
|
643
|
+
return location.startsWith("file://") ? fileURLToPath(location) : location;
|
|
644
|
+
}
|
|
645
|
+
/**
|
|
646
|
+
* Classifies a resource location so a caller can know exactly how to fetch it from the string alone:
|
|
647
|
+
* an http(s) URL, an entry inside a (self-contained) zip archive, an absolute file path / file:// URL,
|
|
648
|
+
* or a `relative` link that still requires a base location to resolve.
|
|
649
|
+
*/
|
|
650
|
+
export function inferLocationScheme(location) {
|
|
651
|
+
if (isHttpUrl(location)) {
|
|
652
|
+
return "url";
|
|
653
|
+
}
|
|
654
|
+
if (isZipEntryLocation(location)) {
|
|
655
|
+
const parsed = parseZipEntryLocation(location);
|
|
656
|
+
return isSelfContainedLocation(parsed.zipLocation) ? "zip-entry" : "relative";
|
|
657
|
+
}
|
|
658
|
+
if (location.startsWith("file://") || path.isAbsolute(location)) {
|
|
659
|
+
return "file";
|
|
660
|
+
}
|
|
661
|
+
return "relative";
|
|
662
|
+
}
|
|
663
|
+
/** True when a location can be fetched without a base location (URL, absolute file, or zip entry). */
|
|
664
|
+
export function isSelfContainedLocation(location) {
|
|
665
|
+
return inferLocationScheme(location) !== "relative";
|
|
666
|
+
}
|
|
667
|
+
/** Default resource types treated as markup (href/src links extracted) by the text parser. */
|
|
668
|
+
export const DEFAULT_MARKUP_TYPES = ["xml", "svg"];
|
|
669
|
+
/** Default image file-extension → mime sub-type mappings, used to label binary image parts. */
|
|
670
|
+
export const DEFAULT_IMAGE_EXTENSION_MIME = {
|
|
671
|
+
png: "image/png",
|
|
672
|
+
jpg: "image/jpeg",
|
|
673
|
+
jpeg: "image/jpeg",
|
|
674
|
+
gif: "image/gif",
|
|
675
|
+
webp: "image/webp",
|
|
676
|
+
bmp: "image/bmp",
|
|
677
|
+
ico: "image/x-icon",
|
|
678
|
+
avif: "image/avif",
|
|
679
|
+
tif: "image/tiff",
|
|
680
|
+
tiff: "image/tiff",
|
|
681
|
+
svg: "image/svg+xml",
|
|
682
|
+
};
|
|
683
|
+
/** Resolves an image mime type from a file name's extension, falling back to image/png. */
|
|
684
|
+
export function imageMimeTypeFromLocation(fileName, fallback = "image/png") {
|
|
685
|
+
const ext = fileName.split(".").pop()?.toLowerCase() ?? "";
|
|
686
|
+
return DEFAULT_IMAGE_EXTENSION_MIME[ext] ?? fallback;
|
|
687
|
+
}
|
|
688
|
+
/** Separator embedded in a resource location to address an entry inside a zip archive. */
|
|
689
|
+
export const ZIP_ENTRY_MARKER = "#entry/";
|
|
690
|
+
/** True when a location points at an entry inside a zip archive (e.g. `bundle.zip#entry/doc.md`). */
|
|
691
|
+
export function isZipEntryLocation(location) {
|
|
692
|
+
return location.includes(ZIP_ENTRY_MARKER);
|
|
693
|
+
}
|
|
694
|
+
/** Builds the canonical location for an entry inside a zip archive. */
|
|
695
|
+
export function buildZipEntryLocation(zipLocation, entryName) {
|
|
696
|
+
return `${zipLocation}${ZIP_ENTRY_MARKER}${entryName}`;
|
|
697
|
+
}
|
|
698
|
+
/** Splits a zip-entry location into the archive location and the entry name within it. */
|
|
699
|
+
export function parseZipEntryLocation(location) {
|
|
700
|
+
const index = location.indexOf(ZIP_ENTRY_MARKER);
|
|
701
|
+
if (index === -1) {
|
|
702
|
+
return undefined;
|
|
703
|
+
}
|
|
704
|
+
return {
|
|
705
|
+
zipLocation: location.slice(0, index),
|
|
706
|
+
entryName: location.slice(index + ZIP_ENTRY_MARKER.length),
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
/**
|
|
710
|
+
* Resolves a (possibly relative) link found inside a zip entry to a normalized entry name within the
|
|
711
|
+
* same archive. Entry paths use POSIX separators; `..`/`.` are resolved, query/fragment are dropped,
|
|
712
|
+
* and a leading `/` is treated as the archive root. A pure fragment resolves to the same entry.
|
|
713
|
+
*/
|
|
714
|
+
export function resolveZipEntryName(baseEntryName, link) {
|
|
715
|
+
const clean = link.split("#")[0].split("?")[0];
|
|
716
|
+
if (clean === "") {
|
|
717
|
+
return baseEntryName;
|
|
718
|
+
}
|
|
719
|
+
const target = clean.startsWith("/")
|
|
720
|
+
? clean
|
|
721
|
+
: path.posix.join(path.posix.dirname(baseEntryName), clean);
|
|
722
|
+
return path.posix.normalize(target).replace(/^(\.\/)+/, "").replace(/^\/+/, "");
|
|
723
|
+
}
|
|
724
|
+
export function normalizeDeclaration(line) {
|
|
725
|
+
return line.replace(/\s+/g, " ").trim();
|
|
726
|
+
}
|
|
727
|
+
export function toHeadingContext(pathSegments) {
|
|
728
|
+
if (pathSegments.length === 0) {
|
|
729
|
+
return undefined;
|
|
730
|
+
}
|
|
731
|
+
return { headings: pathSegments.slice() };
|
|
732
|
+
}
|
|
733
|
+
function stripHtmlBlocks(html, tagNames) {
|
|
734
|
+
const lower = html.toLowerCase();
|
|
735
|
+
let result = "";
|
|
736
|
+
let cursor = 0;
|
|
737
|
+
while (cursor < html.length) {
|
|
738
|
+
let matchedTag;
|
|
739
|
+
let matchedStart = html.length;
|
|
740
|
+
for (const tagName of tagNames) {
|
|
741
|
+
const candidate = lower.indexOf(`<${tagName}`, cursor);
|
|
742
|
+
if (candidate !== -1 && candidate < matchedStart) {
|
|
743
|
+
matchedStart = candidate;
|
|
744
|
+
matchedTag = tagName;
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
if (!matchedTag) {
|
|
748
|
+
result += html.slice(cursor);
|
|
749
|
+
break;
|
|
750
|
+
}
|
|
751
|
+
result += html.slice(cursor, matchedStart);
|
|
752
|
+
const openEnd = html.indexOf(">", matchedStart);
|
|
753
|
+
if (openEnd === -1) {
|
|
754
|
+
break;
|
|
755
|
+
}
|
|
756
|
+
const closeTagStart = lower.indexOf(`</${matchedTag}`, openEnd + 1);
|
|
757
|
+
if (closeTagStart === -1) {
|
|
758
|
+
cursor = openEnd + 1;
|
|
759
|
+
continue;
|
|
760
|
+
}
|
|
761
|
+
const closeTagEnd = html.indexOf(">", closeTagStart + matchedTag.length + 2);
|
|
762
|
+
if (closeTagEnd === -1) {
|
|
763
|
+
break;
|
|
764
|
+
}
|
|
765
|
+
cursor = closeTagEnd + 1;
|
|
766
|
+
}
|
|
767
|
+
return result;
|
|
768
|
+
}
|
|
769
|
+
function collapseWhitespace(text) {
|
|
770
|
+
return text
|
|
771
|
+
.replace(/\r/g, "")
|
|
772
|
+
.replace(/[ \t]+\n/g, "\n")
|
|
773
|
+
.replace(/\n[ \t]+/g, "\n")
|
|
774
|
+
.replace(/\n{3,}/g, "\n\n");
|
|
775
|
+
}
|
|
776
|
+
function collapseInlineWhitespace(text) {
|
|
777
|
+
return text.replace(/\s+/g, " ");
|
|
778
|
+
}
|
|
779
|
+
function decodeHtmlEntities(text) {
|
|
780
|
+
return text
|
|
781
|
+
.replace(/ /g, " ")
|
|
782
|
+
.replace(/</g, "<")
|
|
783
|
+
.replace(/>/g, ">")
|
|
784
|
+
.replace(/"/g, "\"")
|
|
785
|
+
.replace(/'/g, "'")
|
|
786
|
+
.replace(/&/g, "&");
|
|
787
|
+
}
|
|
788
|
+
//# sourceMappingURL=utils.js.map
|