@semiont/api-client 0.4.21 → 0.4.22
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 +49 -91
- package/dist/index.d.ts +87 -1965
- package/dist/index.js +479 -4275
- package/dist/index.js.map +1 -1
- package/package.json +3 -7
- package/dist/utils/index.d.ts +0 -584
- package/dist/utils/index.js +0 -646
- package/dist/utils/index.js.map +0 -1
package/dist/utils/index.js
DELETED
|
@@ -1,646 +0,0 @@
|
|
|
1
|
-
export { getFragmentSelector, getSvgSelector, getTextPositionSelector, validateSvgMarkup } from '@semiont/core';
|
|
2
|
-
|
|
3
|
-
// src/utils/annotations.ts
|
|
4
|
-
function getBodySource(body) {
|
|
5
|
-
if (Array.isArray(body)) {
|
|
6
|
-
for (const item of body) {
|
|
7
|
-
if (typeof item === "object" && item !== null && "type" in item && "source" in item) {
|
|
8
|
-
const itemType = item.type;
|
|
9
|
-
const itemSource = item.source;
|
|
10
|
-
if (itemType === "SpecificResource" && typeof itemSource === "string") {
|
|
11
|
-
return itemSource;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
return null;
|
|
16
|
-
}
|
|
17
|
-
if (typeof body === "object" && body !== null && "type" in body && "source" in body) {
|
|
18
|
-
const bodyType = body.type;
|
|
19
|
-
const bodySource = body.source;
|
|
20
|
-
if (bodyType === "SpecificResource" && typeof bodySource === "string") {
|
|
21
|
-
return bodySource;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
function getBodyType(body) {
|
|
27
|
-
if (Array.isArray(body)) {
|
|
28
|
-
if (body.length === 0) {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
if (typeof body[0] === "object" && body[0] !== null && "type" in body[0]) {
|
|
32
|
-
const firstType = body[0].type;
|
|
33
|
-
if (firstType === "TextualBody" || firstType === "SpecificResource") {
|
|
34
|
-
return firstType;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
if (typeof body === "object" && body !== null && "type" in body) {
|
|
40
|
-
const bodyType = body.type;
|
|
41
|
-
if (bodyType === "TextualBody" || bodyType === "SpecificResource") {
|
|
42
|
-
return bodyType;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return null;
|
|
46
|
-
}
|
|
47
|
-
function isBodyResolved(body) {
|
|
48
|
-
return getBodySource(body) !== null;
|
|
49
|
-
}
|
|
50
|
-
function getTargetSource(target) {
|
|
51
|
-
if (typeof target === "string") {
|
|
52
|
-
return target;
|
|
53
|
-
}
|
|
54
|
-
return target.source;
|
|
55
|
-
}
|
|
56
|
-
function getTargetSelector(target) {
|
|
57
|
-
if (typeof target === "string") {
|
|
58
|
-
return void 0;
|
|
59
|
-
}
|
|
60
|
-
return target.selector;
|
|
61
|
-
}
|
|
62
|
-
function hasTargetSelector(target) {
|
|
63
|
-
return typeof target !== "string" && target.selector !== void 0;
|
|
64
|
-
}
|
|
65
|
-
function isHighlight(annotation) {
|
|
66
|
-
return annotation.motivation === "highlighting";
|
|
67
|
-
}
|
|
68
|
-
function isReference(annotation) {
|
|
69
|
-
return annotation.motivation === "linking";
|
|
70
|
-
}
|
|
71
|
-
function isAssessment(annotation) {
|
|
72
|
-
return annotation.motivation === "assessing";
|
|
73
|
-
}
|
|
74
|
-
function isComment(annotation) {
|
|
75
|
-
return annotation.motivation === "commenting";
|
|
76
|
-
}
|
|
77
|
-
function isTag(annotation) {
|
|
78
|
-
return annotation.motivation === "tagging";
|
|
79
|
-
}
|
|
80
|
-
function getCommentText(annotation) {
|
|
81
|
-
if (!isComment(annotation)) return void 0;
|
|
82
|
-
const body = Array.isArray(annotation.body) ? annotation.body[0] : annotation.body;
|
|
83
|
-
if (body && "value" in body) {
|
|
84
|
-
return body.value;
|
|
85
|
-
}
|
|
86
|
-
return void 0;
|
|
87
|
-
}
|
|
88
|
-
function isStubReference(annotation) {
|
|
89
|
-
return isReference(annotation) && !isBodyResolved(annotation.body);
|
|
90
|
-
}
|
|
91
|
-
function isResolvedReference(annotation) {
|
|
92
|
-
return isReference(annotation) && isBodyResolved(annotation.body);
|
|
93
|
-
}
|
|
94
|
-
function getExactText(selector) {
|
|
95
|
-
if (!selector) {
|
|
96
|
-
return "";
|
|
97
|
-
}
|
|
98
|
-
const selectors = Array.isArray(selector) ? selector : [selector];
|
|
99
|
-
const quoteSelector = selectors.find((s) => s.type === "TextQuoteSelector");
|
|
100
|
-
if (quoteSelector) {
|
|
101
|
-
return quoteSelector.exact;
|
|
102
|
-
}
|
|
103
|
-
return "";
|
|
104
|
-
}
|
|
105
|
-
function getAnnotationExactText(annotation) {
|
|
106
|
-
const selector = getTargetSelector(annotation.target);
|
|
107
|
-
return getExactText(selector);
|
|
108
|
-
}
|
|
109
|
-
function getPrimarySelector(selector) {
|
|
110
|
-
if (Array.isArray(selector)) {
|
|
111
|
-
if (selector.length === 0) {
|
|
112
|
-
throw new Error("Empty selector array");
|
|
113
|
-
}
|
|
114
|
-
const first = selector[0];
|
|
115
|
-
if (!first) {
|
|
116
|
-
throw new Error("Invalid selector array");
|
|
117
|
-
}
|
|
118
|
-
return first;
|
|
119
|
-
}
|
|
120
|
-
return selector;
|
|
121
|
-
}
|
|
122
|
-
function getTextQuoteSelector(selector) {
|
|
123
|
-
const selectors = Array.isArray(selector) ? selector : [selector];
|
|
124
|
-
const found = selectors.find((s) => s.type === "TextQuoteSelector");
|
|
125
|
-
if (!found) return null;
|
|
126
|
-
return found.type === "TextQuoteSelector" ? found : null;
|
|
127
|
-
}
|
|
128
|
-
function extractBoundingBox(svg) {
|
|
129
|
-
const viewBoxMatch = svg.match(/<svg[^>]*viewBox="([^"]+)"/);
|
|
130
|
-
if (viewBoxMatch) {
|
|
131
|
-
const values = viewBoxMatch[1].split(/\s+/).map(parseFloat);
|
|
132
|
-
if (values.length === 4 && values.every((v) => !isNaN(v))) {
|
|
133
|
-
return {
|
|
134
|
-
x: values[0],
|
|
135
|
-
y: values[1],
|
|
136
|
-
width: values[2],
|
|
137
|
-
height: values[3]
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
const svgTagMatch = svg.match(/<svg[^>]*>/);
|
|
142
|
-
if (svgTagMatch) {
|
|
143
|
-
const svgTag = svgTagMatch[0];
|
|
144
|
-
const widthMatch = svgTag.match(/width="([^"]+)"/);
|
|
145
|
-
const heightMatch = svgTag.match(/height="([^"]+)"/);
|
|
146
|
-
if (widthMatch && heightMatch) {
|
|
147
|
-
const width = parseFloat(widthMatch[1]);
|
|
148
|
-
const height = parseFloat(heightMatch[1]);
|
|
149
|
-
if (!isNaN(width) && !isNaN(height)) {
|
|
150
|
-
return { x: 0, y: 0, width, height };
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
return null;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// src/utils/fuzzy-anchor.ts
|
|
158
|
-
function normalizeText(text) {
|
|
159
|
-
return text.replace(/\s+/g, " ").replace(/[\u2018\u2019]/g, "'").replace(/[\u201C\u201D]/g, '"').replace(/\u2014/g, "--").replace(/\u2013/g, "-").trim();
|
|
160
|
-
}
|
|
161
|
-
function levenshteinDistance(str1, str2) {
|
|
162
|
-
const len1 = str1.length;
|
|
163
|
-
const len2 = str2.length;
|
|
164
|
-
const matrix = [];
|
|
165
|
-
for (let i = 0; i <= len1; i++) {
|
|
166
|
-
matrix[i] = [i];
|
|
167
|
-
}
|
|
168
|
-
for (let j = 0; j <= len2; j++) {
|
|
169
|
-
matrix[0][j] = j;
|
|
170
|
-
}
|
|
171
|
-
for (let i = 1; i <= len1; i++) {
|
|
172
|
-
for (let j = 1; j <= len2; j++) {
|
|
173
|
-
const cost = str1[i - 1] === str2[j - 1] ? 0 : 1;
|
|
174
|
-
const deletion = matrix[i - 1][j] + 1;
|
|
175
|
-
const insertion = matrix[i][j - 1] + 1;
|
|
176
|
-
const substitution = matrix[i - 1][j - 1] + cost;
|
|
177
|
-
matrix[i][j] = Math.min(deletion, insertion, substitution);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
return matrix[len1][len2];
|
|
181
|
-
}
|
|
182
|
-
function buildContentCache(content) {
|
|
183
|
-
return {
|
|
184
|
-
normalizedContent: normalizeText(content),
|
|
185
|
-
lowerContent: content.toLowerCase()
|
|
186
|
-
};
|
|
187
|
-
}
|
|
188
|
-
function findBestTextMatch(content, searchText, positionHint, cache) {
|
|
189
|
-
const maxFuzzyDistance = Math.max(5, Math.floor(searchText.length * 0.05));
|
|
190
|
-
const exactIndex = content.indexOf(searchText);
|
|
191
|
-
if (exactIndex !== -1) {
|
|
192
|
-
return {
|
|
193
|
-
start: exactIndex,
|
|
194
|
-
end: exactIndex + searchText.length,
|
|
195
|
-
matchQuality: "exact"
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
const normalizedSearch = normalizeText(searchText);
|
|
199
|
-
const normalizedIndex = cache.normalizedContent.indexOf(normalizedSearch);
|
|
200
|
-
if (normalizedIndex !== -1) {
|
|
201
|
-
let actualPos = 0;
|
|
202
|
-
let normalizedPos = 0;
|
|
203
|
-
while (normalizedPos < normalizedIndex && actualPos < content.length) {
|
|
204
|
-
const char = content[actualPos];
|
|
205
|
-
const normalizedChar = normalizeText(char);
|
|
206
|
-
if (normalizedChar) {
|
|
207
|
-
normalizedPos += normalizedChar.length;
|
|
208
|
-
}
|
|
209
|
-
actualPos++;
|
|
210
|
-
}
|
|
211
|
-
return {
|
|
212
|
-
start: actualPos,
|
|
213
|
-
end: actualPos + searchText.length,
|
|
214
|
-
matchQuality: "normalized"
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
const lowerSearch = searchText.toLowerCase();
|
|
218
|
-
const caseInsensitiveIndex = cache.lowerContent.indexOf(lowerSearch);
|
|
219
|
-
if (caseInsensitiveIndex !== -1) {
|
|
220
|
-
return {
|
|
221
|
-
start: caseInsensitiveIndex,
|
|
222
|
-
end: caseInsensitiveIndex + searchText.length,
|
|
223
|
-
matchQuality: "case-insensitive"
|
|
224
|
-
};
|
|
225
|
-
}
|
|
226
|
-
const windowSize = searchText.length;
|
|
227
|
-
const searchRadius = Math.min(500, content.length);
|
|
228
|
-
const searchStart = positionHint !== void 0 ? Math.max(0, positionHint - searchRadius) : 0;
|
|
229
|
-
const searchEnd = positionHint !== void 0 ? Math.min(content.length, positionHint + searchRadius) : content.length;
|
|
230
|
-
let bestMatch = null;
|
|
231
|
-
for (let i = searchStart; i <= searchEnd - windowSize; i++) {
|
|
232
|
-
const candidate = content.substring(i, i + windowSize);
|
|
233
|
-
const distance = levenshteinDistance(searchText, candidate);
|
|
234
|
-
if (distance <= maxFuzzyDistance) {
|
|
235
|
-
if (!bestMatch || distance < bestMatch.distance) {
|
|
236
|
-
bestMatch = { start: i, distance };
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
if (bestMatch) {
|
|
241
|
-
return {
|
|
242
|
-
start: bestMatch.start,
|
|
243
|
-
end: bestMatch.start + windowSize,
|
|
244
|
-
matchQuality: "fuzzy"
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
return null;
|
|
248
|
-
}
|
|
249
|
-
function findTextWithContext(content, exact, prefix, suffix, positionHint, cache) {
|
|
250
|
-
if (!exact) return null;
|
|
251
|
-
if (positionHint !== void 0 && positionHint >= 0 && positionHint + exact.length <= content.length) {
|
|
252
|
-
if (content.substring(positionHint, positionHint + exact.length) === exact) {
|
|
253
|
-
return { start: positionHint, end: positionHint + exact.length };
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
const occurrences = [];
|
|
257
|
-
let index = content.indexOf(exact);
|
|
258
|
-
while (index !== -1) {
|
|
259
|
-
occurrences.push(index);
|
|
260
|
-
index = content.indexOf(exact, index + 1);
|
|
261
|
-
}
|
|
262
|
-
if (occurrences.length === 0) {
|
|
263
|
-
const fuzzyMatch = findBestTextMatch(content, exact, positionHint, cache);
|
|
264
|
-
if (fuzzyMatch) {
|
|
265
|
-
return { start: fuzzyMatch.start, end: fuzzyMatch.end };
|
|
266
|
-
}
|
|
267
|
-
return null;
|
|
268
|
-
}
|
|
269
|
-
if (occurrences.length === 1) {
|
|
270
|
-
const pos2 = occurrences[0];
|
|
271
|
-
return { start: pos2, end: pos2 + exact.length };
|
|
272
|
-
}
|
|
273
|
-
if (prefix || suffix) {
|
|
274
|
-
for (const pos2 of occurrences) {
|
|
275
|
-
const actualPrefixStart = Math.max(0, pos2 - (prefix?.length || 0));
|
|
276
|
-
const actualPrefix = content.substring(actualPrefixStart, pos2);
|
|
277
|
-
const actualSuffixEnd = Math.min(content.length, pos2 + exact.length + (suffix?.length || 0));
|
|
278
|
-
const actualSuffix = content.substring(pos2 + exact.length, actualSuffixEnd);
|
|
279
|
-
const prefixMatch = !prefix || actualPrefix.endsWith(prefix);
|
|
280
|
-
const suffixMatch = !suffix || actualSuffix.startsWith(suffix);
|
|
281
|
-
if (prefixMatch && suffixMatch) {
|
|
282
|
-
return { start: pos2, end: pos2 + exact.length };
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
for (const pos2 of occurrences) {
|
|
286
|
-
const actualPrefix = content.substring(Math.max(0, pos2 - (prefix?.length || 0)), pos2);
|
|
287
|
-
const actualSuffix = content.substring(pos2 + exact.length, pos2 + exact.length + (suffix?.length || 0));
|
|
288
|
-
const fuzzyPrefixMatch = !prefix || actualPrefix.includes(prefix.trim());
|
|
289
|
-
const fuzzySuffixMatch = !suffix || actualSuffix.includes(suffix.trim());
|
|
290
|
-
if (fuzzyPrefixMatch && fuzzySuffixMatch) {
|
|
291
|
-
return { start: pos2, end: pos2 + exact.length };
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
const pos = occurrences[0];
|
|
296
|
-
return { start: pos, end: pos + exact.length };
|
|
297
|
-
}
|
|
298
|
-
function verifyPosition(content, position, expectedExact) {
|
|
299
|
-
const actualText = content.substring(position.start, position.end);
|
|
300
|
-
return actualText === expectedExact;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
// src/utils/locales.ts
|
|
304
|
-
var LOCALES = [
|
|
305
|
-
{ code: "ar", nativeName: "\u0627\u0644\u0639\u0631\u0628\u064A\u0629", englishName: "Arabic" },
|
|
306
|
-
{ code: "bn", nativeName: "\u09AC\u09BE\u0982\u09B2\u09BE", englishName: "Bengali" },
|
|
307
|
-
{ code: "cs", nativeName: "\u010Ce\u0161tina", englishName: "Czech" },
|
|
308
|
-
{ code: "da", nativeName: "Dansk", englishName: "Danish" },
|
|
309
|
-
{ code: "de", nativeName: "Deutsch", englishName: "German" },
|
|
310
|
-
{ code: "el", nativeName: "\u0395\u03BB\u03BB\u03B7\u03BD\u03B9\u03BA\u03AC", englishName: "Greek" },
|
|
311
|
-
{ code: "en", nativeName: "English", englishName: "English" },
|
|
312
|
-
{ code: "es", nativeName: "Espa\xF1ol", englishName: "Spanish" },
|
|
313
|
-
{ code: "fa", nativeName: "\u0641\u0627\u0631\u0633\u06CC", englishName: "Persian" },
|
|
314
|
-
{ code: "fi", nativeName: "Suomi", englishName: "Finnish" },
|
|
315
|
-
{ code: "fr", nativeName: "Fran\xE7ais", englishName: "French" },
|
|
316
|
-
{ code: "he", nativeName: "\u05E2\u05D1\u05E8\u05D9\u05EA", englishName: "Hebrew" },
|
|
317
|
-
{ code: "hi", nativeName: "\u0939\u093F\u0928\u094D\u0926\u0940", englishName: "Hindi" },
|
|
318
|
-
{ code: "id", nativeName: "Bahasa Indonesia", englishName: "Indonesian" },
|
|
319
|
-
{ code: "it", nativeName: "Italiano", englishName: "Italian" },
|
|
320
|
-
{ code: "ja", nativeName: "\u65E5\u672C\u8A9E", englishName: "Japanese" },
|
|
321
|
-
{ code: "ko", nativeName: "\uD55C\uAD6D\uC5B4", englishName: "Korean" },
|
|
322
|
-
{ code: "ms", nativeName: "Bahasa Melayu", englishName: "Malay" },
|
|
323
|
-
{ code: "nl", nativeName: "Nederlands", englishName: "Dutch" },
|
|
324
|
-
{ code: "no", nativeName: "Norsk", englishName: "Norwegian" },
|
|
325
|
-
{ code: "pl", nativeName: "Polski", englishName: "Polish" },
|
|
326
|
-
{ code: "pt", nativeName: "Portugu\xEAs", englishName: "Portuguese" },
|
|
327
|
-
{ code: "ro", nativeName: "Rom\xE2n\u0103", englishName: "Romanian" },
|
|
328
|
-
{ code: "sv", nativeName: "Svenska", englishName: "Swedish" },
|
|
329
|
-
{ code: "th", nativeName: "\u0E44\u0E17\u0E22", englishName: "Thai" },
|
|
330
|
-
{ code: "tr", nativeName: "T\xFCrk\xE7e", englishName: "Turkish" },
|
|
331
|
-
{ code: "uk", nativeName: "\u0423\u043A\u0440\u0430\u0457\u043D\u0441\u044C\u043A\u0430", englishName: "Ukrainian" },
|
|
332
|
-
{ code: "vi", nativeName: "Ti\u1EBFng Vi\u1EC7t", englishName: "Vietnamese" },
|
|
333
|
-
{ code: "zh", nativeName: "\u4E2D\u6587", englishName: "Chinese" }
|
|
334
|
-
];
|
|
335
|
-
var localeByCode = new Map(
|
|
336
|
-
LOCALES.map((locale) => [locale.code.toLowerCase(), locale])
|
|
337
|
-
);
|
|
338
|
-
function getLocaleInfo(code) {
|
|
339
|
-
if (!code) return void 0;
|
|
340
|
-
return localeByCode.get(code.toLowerCase());
|
|
341
|
-
}
|
|
342
|
-
function getLocaleNativeName(code) {
|
|
343
|
-
return getLocaleInfo(code)?.nativeName;
|
|
344
|
-
}
|
|
345
|
-
function getLocaleEnglishName(code) {
|
|
346
|
-
return getLocaleInfo(code)?.englishName;
|
|
347
|
-
}
|
|
348
|
-
function formatLocaleDisplay(code) {
|
|
349
|
-
if (!code) return void 0;
|
|
350
|
-
const info = getLocaleInfo(code);
|
|
351
|
-
if (!info) return code;
|
|
352
|
-
return `${info.nativeName} (${code.toLowerCase()})`;
|
|
353
|
-
}
|
|
354
|
-
function getAllLocaleCodes() {
|
|
355
|
-
return LOCALES.map((l) => l.code);
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
// src/utils/resources.ts
|
|
359
|
-
function getResourceId(resource) {
|
|
360
|
-
if (!resource) return void 0;
|
|
361
|
-
return resource["@id"] || void 0;
|
|
362
|
-
}
|
|
363
|
-
function getPrimaryRepresentation(resource) {
|
|
364
|
-
if (!resource?.representations) return void 0;
|
|
365
|
-
const reps = Array.isArray(resource.representations) ? resource.representations : [resource.representations];
|
|
366
|
-
return reps[0];
|
|
367
|
-
}
|
|
368
|
-
function getPrimaryMediaType(resource) {
|
|
369
|
-
return getPrimaryRepresentation(resource)?.mediaType;
|
|
370
|
-
}
|
|
371
|
-
function getChecksum(resource) {
|
|
372
|
-
return getPrimaryRepresentation(resource)?.checksum;
|
|
373
|
-
}
|
|
374
|
-
function getLanguage(resource) {
|
|
375
|
-
return getPrimaryRepresentation(resource)?.language;
|
|
376
|
-
}
|
|
377
|
-
function getStorageUri(resource) {
|
|
378
|
-
return getPrimaryRepresentation(resource)?.storageUri;
|
|
379
|
-
}
|
|
380
|
-
function getCreator(resource) {
|
|
381
|
-
if (!resource?.wasAttributedTo) return void 0;
|
|
382
|
-
return Array.isArray(resource.wasAttributedTo) ? resource.wasAttributedTo[0] : resource.wasAttributedTo;
|
|
383
|
-
}
|
|
384
|
-
function getDerivedFrom(resource) {
|
|
385
|
-
if (!resource?.wasDerivedFrom) return void 0;
|
|
386
|
-
return Array.isArray(resource.wasDerivedFrom) ? resource.wasDerivedFrom[0] : resource.wasDerivedFrom;
|
|
387
|
-
}
|
|
388
|
-
function isArchived(resource) {
|
|
389
|
-
return resource?.archived === true;
|
|
390
|
-
}
|
|
391
|
-
function getResourceEntityTypes(resource) {
|
|
392
|
-
return resource?.entityTypes || [];
|
|
393
|
-
}
|
|
394
|
-
function isDraft(resource) {
|
|
395
|
-
return resource?.isDraft === true;
|
|
396
|
-
}
|
|
397
|
-
function getNodeEncoding(charset) {
|
|
398
|
-
const normalized = charset.toLowerCase().replace(/[-_]/g, "");
|
|
399
|
-
const charsetMap = {
|
|
400
|
-
"utf8": "utf8",
|
|
401
|
-
"iso88591": "latin1",
|
|
402
|
-
"latin1": "latin1",
|
|
403
|
-
"ascii": "ascii",
|
|
404
|
-
"usascii": "ascii",
|
|
405
|
-
"utf16le": "utf16le",
|
|
406
|
-
"ucs2": "ucs2",
|
|
407
|
-
"binary": "binary",
|
|
408
|
-
"windows1252": "latin1",
|
|
409
|
-
// Windows-1252 is a superset of Latin-1
|
|
410
|
-
"cp1252": "latin1"
|
|
411
|
-
};
|
|
412
|
-
return charsetMap[normalized] || "utf8";
|
|
413
|
-
}
|
|
414
|
-
function decodeRepresentation(buffer, mediaType) {
|
|
415
|
-
const charsetMatch = mediaType.match(/charset=([^\s;]+)/i);
|
|
416
|
-
const charset = (charsetMatch?.[1] || "utf-8").toLowerCase();
|
|
417
|
-
const encoding = getNodeEncoding(charset);
|
|
418
|
-
return buffer.toString(encoding);
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
// src/utils/svg-utils.ts
|
|
422
|
-
function createRectangleSvg(start, end) {
|
|
423
|
-
const x = Math.min(start.x, end.x);
|
|
424
|
-
const y = Math.min(start.y, end.y);
|
|
425
|
-
const width = Math.abs(end.x - start.x);
|
|
426
|
-
const height = Math.abs(end.y - start.y);
|
|
427
|
-
return `<svg xmlns="http://www.w3.org/2000/svg"><rect x="${x}" y="${y}" width="${width}" height="${height}"/></svg>`;
|
|
428
|
-
}
|
|
429
|
-
function createPolygonSvg(points) {
|
|
430
|
-
if (points.length < 3) {
|
|
431
|
-
throw new Error("Polygon requires at least 3 points");
|
|
432
|
-
}
|
|
433
|
-
const pointsStr = points.map((p) => `${p.x},${p.y}`).join(" ");
|
|
434
|
-
return `<svg xmlns="http://www.w3.org/2000/svg"><polygon points="${pointsStr}"/></svg>`;
|
|
435
|
-
}
|
|
436
|
-
function createCircleSvg(center, radius) {
|
|
437
|
-
if (radius <= 0) {
|
|
438
|
-
throw new Error("Circle radius must be positive");
|
|
439
|
-
}
|
|
440
|
-
return `<svg xmlns="http://www.w3.org/2000/svg"><circle cx="${center.x}" cy="${center.y}" r="${radius}"/></svg>`;
|
|
441
|
-
}
|
|
442
|
-
function parseSvgSelector(svg) {
|
|
443
|
-
const rectMatch = svg.match(/<rect\s+([^>]+)\/>/);
|
|
444
|
-
if (rectMatch && rectMatch[1]) {
|
|
445
|
-
const attrs = rectMatch[1];
|
|
446
|
-
const x = parseFloat(attrs.match(/x="([^"]+)"/)?.[1] || "0");
|
|
447
|
-
const y = parseFloat(attrs.match(/y="([^"]+)"/)?.[1] || "0");
|
|
448
|
-
const width = parseFloat(attrs.match(/width="([^"]+)"/)?.[1] || "0");
|
|
449
|
-
const height = parseFloat(attrs.match(/height="([^"]+)"/)?.[1] || "0");
|
|
450
|
-
return {
|
|
451
|
-
type: "rect",
|
|
452
|
-
data: { x, y, width, height }
|
|
453
|
-
};
|
|
454
|
-
}
|
|
455
|
-
const polygonMatch = svg.match(/<polygon\s+points="([^"]+)"/);
|
|
456
|
-
if (polygonMatch && polygonMatch[1]) {
|
|
457
|
-
const pointsStr = polygonMatch[1];
|
|
458
|
-
const points = pointsStr.split(/\s+/).map((pair) => {
|
|
459
|
-
const [x, y] = pair.split(",").map(parseFloat);
|
|
460
|
-
return { x, y };
|
|
461
|
-
});
|
|
462
|
-
return {
|
|
463
|
-
type: "polygon",
|
|
464
|
-
data: { points }
|
|
465
|
-
};
|
|
466
|
-
}
|
|
467
|
-
const circleMatch = svg.match(/<circle\s+([^>]+)\/>/);
|
|
468
|
-
if (circleMatch && circleMatch[1]) {
|
|
469
|
-
const attrs = circleMatch[1];
|
|
470
|
-
const cx = parseFloat(attrs.match(/cx="([^"]+)"/)?.[1] || "0");
|
|
471
|
-
const cy = parseFloat(attrs.match(/cy="([^"]+)"/)?.[1] || "0");
|
|
472
|
-
const r = parseFloat(attrs.match(/r="([^"]+)"/)?.[1] || "0");
|
|
473
|
-
return {
|
|
474
|
-
type: "circle",
|
|
475
|
-
data: { cx, cy, r }
|
|
476
|
-
};
|
|
477
|
-
}
|
|
478
|
-
return null;
|
|
479
|
-
}
|
|
480
|
-
function normalizeCoordinates(point, displayWidth, displayHeight, imageWidth, imageHeight) {
|
|
481
|
-
return {
|
|
482
|
-
x: point.x / displayWidth * imageWidth,
|
|
483
|
-
y: point.y / displayHeight * imageHeight
|
|
484
|
-
};
|
|
485
|
-
}
|
|
486
|
-
function scaleSvgToNative(svg, displayWidth, displayHeight, imageWidth, imageHeight) {
|
|
487
|
-
const parsed = parseSvgSelector(svg);
|
|
488
|
-
if (!parsed) return svg;
|
|
489
|
-
const scaleX = imageWidth / displayWidth;
|
|
490
|
-
const scaleY = imageHeight / displayHeight;
|
|
491
|
-
switch (parsed.type) {
|
|
492
|
-
case "rect": {
|
|
493
|
-
const { x, y, width, height } = parsed.data;
|
|
494
|
-
return createRectangleSvg(
|
|
495
|
-
{ x: x * scaleX, y: y * scaleY },
|
|
496
|
-
{ x: (x + width) * scaleX, y: (y + height) * scaleY }
|
|
497
|
-
);
|
|
498
|
-
}
|
|
499
|
-
case "circle": {
|
|
500
|
-
const { cx, cy, r } = parsed.data;
|
|
501
|
-
return createCircleSvg(
|
|
502
|
-
{ x: cx * scaleX, y: cy * scaleY },
|
|
503
|
-
r * Math.min(scaleX, scaleY)
|
|
504
|
-
);
|
|
505
|
-
}
|
|
506
|
-
case "polygon": {
|
|
507
|
-
const points = parsed.data.points.map((p) => ({
|
|
508
|
-
x: p.x * scaleX,
|
|
509
|
-
y: p.y * scaleY
|
|
510
|
-
}));
|
|
511
|
-
return createPolygonSvg(points);
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
return svg;
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
// src/utils/text-context.ts
|
|
518
|
-
function extractContext(content, start, end) {
|
|
519
|
-
const CONTEXT_LENGTH = 64;
|
|
520
|
-
const MAX_EXTENSION = 32;
|
|
521
|
-
let prefix;
|
|
522
|
-
if (start > 0) {
|
|
523
|
-
let prefixStart = Math.max(0, start - CONTEXT_LENGTH);
|
|
524
|
-
let extensionCount = 0;
|
|
525
|
-
while (prefixStart > 0 && extensionCount < MAX_EXTENSION) {
|
|
526
|
-
const char = content[prefixStart - 1];
|
|
527
|
-
if (!char || /[\s.,;:!?'"()\[\]{}<>\/\\]/.test(char)) {
|
|
528
|
-
break;
|
|
529
|
-
}
|
|
530
|
-
prefixStart--;
|
|
531
|
-
extensionCount++;
|
|
532
|
-
}
|
|
533
|
-
prefix = content.substring(prefixStart, start);
|
|
534
|
-
}
|
|
535
|
-
let suffix;
|
|
536
|
-
if (end < content.length) {
|
|
537
|
-
let suffixEnd = Math.min(content.length, end + CONTEXT_LENGTH);
|
|
538
|
-
let extensionCount = 0;
|
|
539
|
-
while (suffixEnd < content.length && extensionCount < MAX_EXTENSION) {
|
|
540
|
-
const char = content[suffixEnd];
|
|
541
|
-
if (!char || /[\s.,;:!?'"()\[\]{}<>\/\\]/.test(char)) {
|
|
542
|
-
break;
|
|
543
|
-
}
|
|
544
|
-
suffixEnd++;
|
|
545
|
-
extensionCount++;
|
|
546
|
-
}
|
|
547
|
-
suffix = content.substring(end, suffixEnd);
|
|
548
|
-
}
|
|
549
|
-
return { prefix, suffix };
|
|
550
|
-
}
|
|
551
|
-
function validateAndCorrectOffsets(content, aiStart, aiEnd, exact) {
|
|
552
|
-
const textAtOffset = content.substring(aiStart, aiEnd);
|
|
553
|
-
if (textAtOffset === exact) {
|
|
554
|
-
const context2 = extractContext(content, aiStart, aiEnd);
|
|
555
|
-
return {
|
|
556
|
-
start: aiStart,
|
|
557
|
-
end: aiEnd,
|
|
558
|
-
exact,
|
|
559
|
-
prefix: context2.prefix,
|
|
560
|
-
suffix: context2.suffix,
|
|
561
|
-
corrected: false,
|
|
562
|
-
matchQuality: "exact"
|
|
563
|
-
};
|
|
564
|
-
}
|
|
565
|
-
const cache = buildContentCache(content);
|
|
566
|
-
const match = findBestTextMatch(content, exact, aiStart, cache);
|
|
567
|
-
if (!match) {
|
|
568
|
-
throw new Error(
|
|
569
|
-
"Cannot find acceptable match for text in content. All search strategies failed. Text may be hallucinated."
|
|
570
|
-
);
|
|
571
|
-
}
|
|
572
|
-
const actualText = content.substring(match.start, match.end);
|
|
573
|
-
const context = extractContext(content, match.start, match.end);
|
|
574
|
-
return {
|
|
575
|
-
start: match.start,
|
|
576
|
-
end: match.end,
|
|
577
|
-
exact: actualText,
|
|
578
|
-
// Use actual text from document, not AI's version
|
|
579
|
-
prefix: context.prefix,
|
|
580
|
-
suffix: context.suffix,
|
|
581
|
-
corrected: true,
|
|
582
|
-
fuzzyMatched: match.matchQuality !== "exact",
|
|
583
|
-
matchQuality: match.matchQuality
|
|
584
|
-
};
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
// src/utils/text-encoding.ts
|
|
588
|
-
function extractCharset(mediaType) {
|
|
589
|
-
const charsetMatch = mediaType.match(/charset=([^\s;]+)/i);
|
|
590
|
-
return (charsetMatch?.[1] || "utf-8").toLowerCase();
|
|
591
|
-
}
|
|
592
|
-
function decodeWithCharset(buffer, mediaType) {
|
|
593
|
-
const charset = extractCharset(mediaType);
|
|
594
|
-
const decoder = new TextDecoder(charset);
|
|
595
|
-
return decoder.decode(buffer);
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
// src/utils/validation.ts
|
|
599
|
-
var JWTTokenSchema = {
|
|
600
|
-
parse(token) {
|
|
601
|
-
if (typeof token !== "string") {
|
|
602
|
-
throw new Error("Token must be a string");
|
|
603
|
-
}
|
|
604
|
-
if (!token || token.length === 0) {
|
|
605
|
-
throw new Error("Token is required");
|
|
606
|
-
}
|
|
607
|
-
const jwtRegex = /^[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]*$/;
|
|
608
|
-
if (!jwtRegex.test(token)) {
|
|
609
|
-
throw new Error("Invalid JWT token format");
|
|
610
|
-
}
|
|
611
|
-
return token;
|
|
612
|
-
},
|
|
613
|
-
safeParse(token) {
|
|
614
|
-
try {
|
|
615
|
-
const validated = this.parse(token);
|
|
616
|
-
return { success: true, data: validated };
|
|
617
|
-
} catch (error) {
|
|
618
|
-
return {
|
|
619
|
-
success: false,
|
|
620
|
-
error: error instanceof Error ? error.message : "Invalid JWT token"
|
|
621
|
-
};
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
};
|
|
625
|
-
function validateData(schema, data) {
|
|
626
|
-
try {
|
|
627
|
-
const validated = schema.parse(data);
|
|
628
|
-
return { success: true, data: validated };
|
|
629
|
-
} catch (error) {
|
|
630
|
-
return {
|
|
631
|
-
success: false,
|
|
632
|
-
error: error instanceof Error ? error.message : "Validation failed"
|
|
633
|
-
};
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
function isValidEmail(email) {
|
|
637
|
-
if (email.length < 1 || email.length > 255) {
|
|
638
|
-
return false;
|
|
639
|
-
}
|
|
640
|
-
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
641
|
-
return emailRegex.test(email);
|
|
642
|
-
}
|
|
643
|
-
|
|
644
|
-
export { JWTTokenSchema, LOCALES, buildContentCache, createCircleSvg, createPolygonSvg, createRectangleSvg, decodeRepresentation, decodeWithCharset, extractBoundingBox, extractCharset, extractContext, findBestTextMatch, findTextWithContext, formatLocaleDisplay, getAllLocaleCodes, getAnnotationExactText, getBodySource, getBodyType, getChecksum, getCommentText, getCreator, getDerivedFrom, getExactText, getLanguage, getLocaleEnglishName, getLocaleInfo, getLocaleNativeName, getNodeEncoding, getPrimaryMediaType, getPrimaryRepresentation, getPrimarySelector, getResourceEntityTypes, getResourceId, getStorageUri, getTargetSelector, getTargetSource, getTextQuoteSelector, hasTargetSelector, isArchived, isAssessment, isBodyResolved, isComment, isDraft, isHighlight, isReference, isResolvedReference, isStubReference, isTag, isValidEmail, normalizeCoordinates, normalizeText, parseSvgSelector, scaleSvgToNative, validateAndCorrectOffsets, validateData, verifyPosition };
|
|
645
|
-
//# sourceMappingURL=index.js.map
|
|
646
|
-
//# sourceMappingURL=index.js.map
|