@seed-hypermedia/client 0.0.67 → 0.0.69
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/LICENSE +201 -0
- package/dist/auth.d.ts +79 -0
- package/dist/auth.mjs +265 -0
- package/dist/auto-link.mjs +20 -0
- package/dist/base64.mjs +9 -0
- package/dist/blobs.d.ts +168 -0
- package/dist/blobs.mjs +50 -0
- package/dist/block-diff.d.ts +37 -1
- package/dist/block-diff.mjs +17 -0
- package/dist/blocks-to-markdown.mjs +22 -0
- package/dist/capability.mjs +11 -0
- package/dist/cbor.d.ts +13 -0
- package/dist/cbor.mjs +13 -0
- package/dist/change.mjs +23 -0
- package/dist/chunk-2MD3FXKB.mjs +14 -0
- package/dist/chunk-3TPCJCVO.mjs +24 -0
- package/dist/chunk-52P3E6H6.mjs +459 -0
- package/dist/{chunk-KCR3277A.mjs → chunk-5EWV4OPF.mjs} +1 -1
- package/dist/chunk-CBELM7KX.mjs +29 -0
- package/dist/chunk-DXDF36H5.mjs +289 -0
- package/dist/chunk-EY6RQVAP.mjs +138 -0
- package/dist/chunk-FYUZZ3HT.mjs +199 -0
- package/dist/chunk-GKGWSN6J.mjs +75 -0
- package/dist/chunk-H7L4TI6X.mjs +705 -0
- package/dist/chunk-HBIVZA6C.mjs +102 -0
- package/dist/chunk-I3YUHKYW.mjs +135 -0
- package/dist/chunk-KO4XH5L2.mjs +475 -0
- package/dist/chunk-LYECQKZE.mjs +49 -0
- package/dist/chunk-MKUWQB5W.mjs +297 -0
- package/dist/chunk-N3P7S5YZ.mjs +118 -0
- package/dist/chunk-NPG76WUS.mjs +348 -0
- package/dist/chunk-NX7YFFRX.mjs +124 -0
- package/dist/chunk-OZKQOI2X.mjs +334 -0
- package/dist/chunk-PPAY4WZX.mjs +40 -0
- package/dist/chunk-QS7TPMYT.mjs +411 -0
- package/dist/chunk-S2PVJK2P.mjs +517 -0
- package/dist/chunk-T5BTAJPW.mjs +725 -0
- package/dist/{chunk-EGCUJ6D6.mjs → chunk-UOL3F6XQ.mjs} +0 -12
- package/dist/chunk-VN7F33SS.mjs +108 -0
- package/dist/chunk-W4OGV22Y.mjs +57 -0
- package/dist/chunk-WJQSJ6UM.mjs +159 -0
- package/dist/chunk-XI2LXZM7.mjs +112 -0
- package/dist/chunk-YNVFRCUH.mjs +171 -0
- package/dist/chunk-YZX54L3G.mjs +33 -0
- package/dist/client.mjs +14 -0
- package/dist/comment.mjs +18 -0
- package/dist/contact.mjs +14 -0
- package/dist/document-state.mjs +8 -0
- package/dist/editor-types.mjs +0 -0
- package/dist/editorblock-to-hmblock.mjs +11 -0
- package/dist/encryption.mjs +13 -0
- package/dist/errors.mjs +11 -0
- package/dist/file-to-ipfs.mjs +13 -0
- package/dist/grobid.mjs +11 -0
- package/dist/hm-resolver.mjs +10 -0
- package/dist/hm-types.d.ts +566 -566
- package/dist/hmauth.d.ts +169 -0
- package/dist/hmauth.mjs +52 -0
- package/dist/hmblock-to-editorblock.mjs +12 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +126 -5006
- package/dist/keyfile.mjs +19 -0
- package/dist/markdown-to-blocks.d.ts +7 -0
- package/dist/markdown-to-blocks.mjs +15 -0
- package/dist/pdf-to-blocks-embedded.mjs +7 -0
- package/dist/pdf-to-blocks.mjs +11 -0
- package/dist/ref.mjs +12 -0
- package/dist/resource-read.mjs +14 -0
- package/dist/signing.mjs +13 -0
- package/dist/tei-to-blocks.mjs +8 -0
- package/dist/unicode.mjs +19 -0
- package/dist/vault-local.mjs +7 -6
- package/dist/vault.mjs +3 -2
- package/package.json +18 -28
- package/src/index.ts +0 -137
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
// src/pdf-to-blocks-embedded.ts
|
|
2
|
+
function generateBlockId() {
|
|
3
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
|
|
4
|
+
let id = "";
|
|
5
|
+
for (let i = 0; i < 8; i++) {
|
|
6
|
+
id += chars[Math.floor(Math.random() * chars.length)];
|
|
7
|
+
}
|
|
8
|
+
return id;
|
|
9
|
+
}
|
|
10
|
+
function isTextItem(item) {
|
|
11
|
+
return "str" in item;
|
|
12
|
+
}
|
|
13
|
+
function isBoldFont(fontName) {
|
|
14
|
+
const lower = fontName.toLowerCase();
|
|
15
|
+
return lower.includes("bold") || lower.includes("heavy") || lower.includes("black");
|
|
16
|
+
}
|
|
17
|
+
function isItalicFont(fontName) {
|
|
18
|
+
const lower = fontName.toLowerCase();
|
|
19
|
+
return lower.includes("italic") || lower.includes("oblique") || lower.includes("slant");
|
|
20
|
+
}
|
|
21
|
+
function isMonospaceFont(fontName) {
|
|
22
|
+
const lower = fontName.toLowerCase();
|
|
23
|
+
return lower.includes("mono") || lower.includes("courier") || lower.includes("consolas") || lower.includes("menlo") || lower.includes("firacode") || lower.includes("inconsolata") || lower.includes("source code") || lower.includes("dejavu sans mono") || lower.includes("lucida console");
|
|
24
|
+
}
|
|
25
|
+
function makeBlock(type, text, extra = {}) {
|
|
26
|
+
return {
|
|
27
|
+
block: {
|
|
28
|
+
id: generateBlockId(),
|
|
29
|
+
type,
|
|
30
|
+
text,
|
|
31
|
+
revision: "",
|
|
32
|
+
link: "",
|
|
33
|
+
attributes: {},
|
|
34
|
+
annotations: [],
|
|
35
|
+
...extra
|
|
36
|
+
},
|
|
37
|
+
children: []
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function makeHeading(text, annotations = []) {
|
|
41
|
+
return makeBlock("Heading", text, {
|
|
42
|
+
attributes: { childrenType: "Group" },
|
|
43
|
+
annotations
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
function makeParagraph(text, annotations = []) {
|
|
47
|
+
return makeBlock("Paragraph", text, { annotations });
|
|
48
|
+
}
|
|
49
|
+
function makeCode(text, language = "") {
|
|
50
|
+
return makeBlock("Code", text, {
|
|
51
|
+
attributes: language ? { language } : {}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
function makeList(items, listType) {
|
|
55
|
+
const container = makeBlock("Paragraph", "", {
|
|
56
|
+
attributes: { childrenType: listType }
|
|
57
|
+
});
|
|
58
|
+
container.children = items;
|
|
59
|
+
return container;
|
|
60
|
+
}
|
|
61
|
+
var MIN_HEADING_TEXT_LENGTH = 4;
|
|
62
|
+
function clusterFontSizes(lines) {
|
|
63
|
+
const sizeCount = /* @__PURE__ */ new Map();
|
|
64
|
+
for (const line of lines) {
|
|
65
|
+
if (line.runs.length === 0) continue;
|
|
66
|
+
const size = Math.round(line.fontSize * 10) / 10;
|
|
67
|
+
sizeCount.set(size, (sizeCount.get(size) || 0) + 1);
|
|
68
|
+
}
|
|
69
|
+
if (sizeCount.size === 0) {
|
|
70
|
+
return { bodySize: 12, headingLevels: /* @__PURE__ */ new Map(), boldHeadingLevel: 1 };
|
|
71
|
+
}
|
|
72
|
+
const clusters = Array.from(sizeCount.entries()).map(([size, count]) => ({ size, count })).sort((a, b) => b.count - a.count);
|
|
73
|
+
const bodySize = clusters[0].size;
|
|
74
|
+
const headingSizeCount = /* @__PURE__ */ new Map();
|
|
75
|
+
for (const line of lines) {
|
|
76
|
+
if (line.runs.length === 0) continue;
|
|
77
|
+
const text = line.runs.map((r) => r.text).join("").trim();
|
|
78
|
+
if (text.length < MIN_HEADING_TEXT_LENGTH) continue;
|
|
79
|
+
const size = Math.round(line.fontSize * 10) / 10;
|
|
80
|
+
if (size > bodySize + 1) {
|
|
81
|
+
headingSizeCount.set(size, (headingSizeCount.get(size) || 0) + 1);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const headingSizes = Array.from(headingSizeCount.keys()).sort((a, b) => b - a);
|
|
85
|
+
const headingLevels = /* @__PURE__ */ new Map();
|
|
86
|
+
for (let i = 0; i < headingSizes.length && i < 6; i++) {
|
|
87
|
+
headingLevels.set(headingSizes[i], i + 1);
|
|
88
|
+
}
|
|
89
|
+
const boldHeadingLevel = Math.min(headingLevels.size + 1, 6);
|
|
90
|
+
return { bodySize, headingLevels, boldHeadingLevel };
|
|
91
|
+
}
|
|
92
|
+
var BULLET_PATTERNS = /^[\u2022\u2023\u25E6\u2043\u2219•·‣⁃○◦▪▸►–—-]\s+/;
|
|
93
|
+
var NUMBERED_PATTERN = /^(\d+)[.)]\s+/;
|
|
94
|
+
var LETTER_PATTERN = /^[a-zA-Z][.)]\s+/;
|
|
95
|
+
function detectListType(text) {
|
|
96
|
+
const bulletMatch = text.match(BULLET_PATTERNS);
|
|
97
|
+
if (bulletMatch) {
|
|
98
|
+
return { type: "bullet", content: text.slice(bulletMatch[0].length) };
|
|
99
|
+
}
|
|
100
|
+
const numberedMatch = text.match(NUMBERED_PATTERN);
|
|
101
|
+
if (numberedMatch) {
|
|
102
|
+
return { type: "numbered", content: text.slice(numberedMatch[0].length) };
|
|
103
|
+
}
|
|
104
|
+
const letterMatch = text.match(LETTER_PATTERN);
|
|
105
|
+
if (letterMatch) {
|
|
106
|
+
return { type: "numbered", content: text.slice(letterMatch[0].length) };
|
|
107
|
+
}
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
async function extractTextRuns(doc) {
|
|
111
|
+
const allLines = [];
|
|
112
|
+
for (let pageNum = 1; pageNum <= doc.numPages; pageNum++) {
|
|
113
|
+
const page = await doc.getPage(pageNum);
|
|
114
|
+
await page.getOperatorList();
|
|
115
|
+
const textContent = await page.getTextContent({ includeMarkedContent: false });
|
|
116
|
+
const items = textContent.items.filter(isTextItem);
|
|
117
|
+
const styles = textContent.styles;
|
|
118
|
+
if (items.length === 0) continue;
|
|
119
|
+
const fontInfoCache = /* @__PURE__ */ new Map();
|
|
120
|
+
const getFontInfo = (fontName) => {
|
|
121
|
+
const cached = fontInfoCache.get(fontName);
|
|
122
|
+
if (cached) return cached;
|
|
123
|
+
let bold = false;
|
|
124
|
+
let italic = false;
|
|
125
|
+
let monospace = false;
|
|
126
|
+
try {
|
|
127
|
+
if (page.commonObjs.has(fontName)) {
|
|
128
|
+
const fontObj = page.commonObjs.get(fontName);
|
|
129
|
+
if (fontObj) {
|
|
130
|
+
bold = fontObj.bold === true || fontObj.black === true;
|
|
131
|
+
italic = fontObj.italic === true;
|
|
132
|
+
monospace = fontObj.isMonospace === true;
|
|
133
|
+
if (fontObj.name) {
|
|
134
|
+
if (!monospace) monospace = isMonospaceFont(fontObj.name);
|
|
135
|
+
if (!bold) bold = isBoldFont(fontObj.name);
|
|
136
|
+
if (!italic) italic = isItalicFont(fontObj.name);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
} catch {
|
|
141
|
+
}
|
|
142
|
+
if (!bold && !italic && !monospace) {
|
|
143
|
+
const style = styles[fontName];
|
|
144
|
+
if (style == null ? void 0 : style.fontFamily) {
|
|
145
|
+
monospace = style.fontFamily === "monospace" || isMonospaceFont(style.fontFamily);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (!bold) bold = isBoldFont(fontName);
|
|
149
|
+
if (!italic) italic = isItalicFont(fontName);
|
|
150
|
+
if (!monospace) monospace = isMonospaceFont(fontName);
|
|
151
|
+
const info = { bold, italic, monospace };
|
|
152
|
+
fontInfoCache.set(fontName, info);
|
|
153
|
+
return info;
|
|
154
|
+
};
|
|
155
|
+
const lineGroups = [];
|
|
156
|
+
let currentLineItems = [];
|
|
157
|
+
let currentY = null;
|
|
158
|
+
const Y_TOLERANCE = 2;
|
|
159
|
+
const sorted = [...items].sort((a, b) => {
|
|
160
|
+
const yDiff = b.transform[5] - a.transform[5];
|
|
161
|
+
if (Math.abs(yDiff) > Y_TOLERANCE) return yDiff;
|
|
162
|
+
return a.transform[4] - b.transform[4];
|
|
163
|
+
});
|
|
164
|
+
for (const item of sorted) {
|
|
165
|
+
const y = item.transform[5];
|
|
166
|
+
if (currentY === null || Math.abs(y - currentY) > Y_TOLERANCE) {
|
|
167
|
+
if (currentLineItems.length > 0) lineGroups.push(currentLineItems);
|
|
168
|
+
currentLineItems = [item];
|
|
169
|
+
currentY = y;
|
|
170
|
+
} else {
|
|
171
|
+
currentLineItems.push(item);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (currentLineItems.length > 0) lineGroups.push(currentLineItems);
|
|
175
|
+
for (const group of lineGroups) {
|
|
176
|
+
group.sort((a, b) => a.transform[4] - b.transform[4]);
|
|
177
|
+
const runs = group.filter((item) => item.str.length > 0).map((item) => {
|
|
178
|
+
const fontInfo = getFontInfo(item.fontName);
|
|
179
|
+
return {
|
|
180
|
+
text: item.str,
|
|
181
|
+
fontSize: Math.abs(item.transform[3]),
|
|
182
|
+
fontName: item.fontName,
|
|
183
|
+
x: item.transform[4],
|
|
184
|
+
y: item.transform[5],
|
|
185
|
+
width: item.width,
|
|
186
|
+
bold: fontInfo.bold,
|
|
187
|
+
italic: fontInfo.italic,
|
|
188
|
+
monospace: fontInfo.monospace
|
|
189
|
+
};
|
|
190
|
+
});
|
|
191
|
+
if (runs.length === 0) continue;
|
|
192
|
+
const firstRun = runs[0];
|
|
193
|
+
allLines.push({
|
|
194
|
+
runs,
|
|
195
|
+
y: firstRun.y,
|
|
196
|
+
x: firstRun.x,
|
|
197
|
+
fontSize: firstRun.fontSize,
|
|
198
|
+
fontName: firstRun.fontName,
|
|
199
|
+
bold: runs.every((r) => r.bold),
|
|
200
|
+
italic: runs.every((r) => r.italic),
|
|
201
|
+
monospace: runs.every((r) => r.monospace),
|
|
202
|
+
pageIndex: pageNum - 1
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return allLines;
|
|
207
|
+
}
|
|
208
|
+
var BOLD_HEADING_MAX_LENGTH = 100;
|
|
209
|
+
function normalizeForComparison(text) {
|
|
210
|
+
return text.trim().replace(/\s+/g, " ").toLowerCase();
|
|
211
|
+
}
|
|
212
|
+
function groupLinesIntoParagraphs(lines, bodySize, headingLevels, boldHeadingLevel) {
|
|
213
|
+
const groups = [];
|
|
214
|
+
let currentGroup = null;
|
|
215
|
+
for (let i = 0; i < lines.length; i++) {
|
|
216
|
+
const line = lines[i];
|
|
217
|
+
const roundedSize = Math.round(line.fontSize * 10) / 10;
|
|
218
|
+
let headingLevel = headingLevels.get(roundedSize);
|
|
219
|
+
const lineText = line.runs.map((r) => r.text).join("");
|
|
220
|
+
const listInfo = detectListType(lineText);
|
|
221
|
+
const isCode = line.monospace && !headingLevel;
|
|
222
|
+
if (!headingLevel && !isCode && line.bold && lineText.length > 0 && lineText.length <= BOLD_HEADING_MAX_LENGTH && Math.abs(roundedSize - bodySize) <= 1.5) {
|
|
223
|
+
headingLevel = boldHeadingLevel;
|
|
224
|
+
}
|
|
225
|
+
let lineType;
|
|
226
|
+
if (headingLevel) {
|
|
227
|
+
lineType = "heading";
|
|
228
|
+
} else if (isCode) {
|
|
229
|
+
lineType = "code";
|
|
230
|
+
} else if (listInfo) {
|
|
231
|
+
lineType = listInfo.type === "bullet" ? "list-bullet" : "list-numbered";
|
|
232
|
+
} else {
|
|
233
|
+
lineType = "paragraph";
|
|
234
|
+
}
|
|
235
|
+
const isPageBreak = i > 0 && lines[i - 1].pageIndex !== line.pageIndex;
|
|
236
|
+
const shouldStartNew = !currentGroup || isPageBreak || lineType === "heading" || currentGroup.type === "heading" || lineType === "list-bullet" || lineType === "list-numbered" || currentGroup.type === "list-bullet" || currentGroup.type === "list-numbered" || lineType === "code" !== (currentGroup.type === "code") || i > 0 && !isPageBreak && Math.abs(lines[i - 1].y - line.y) > line.fontSize * 1.8 && lineType !== "code";
|
|
237
|
+
if (shouldStartNew) {
|
|
238
|
+
if (currentGroup && currentGroup.lines.length > 0) groups.push(currentGroup);
|
|
239
|
+
currentGroup = { lines: [line], type: lineType, headingLevel };
|
|
240
|
+
} else {
|
|
241
|
+
currentGroup.lines.push(line);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (currentGroup && currentGroup.lines.length > 0) groups.push(currentGroup);
|
|
245
|
+
return groups;
|
|
246
|
+
}
|
|
247
|
+
function runsToTextAndAnnotations(runs, allBold, allItalic) {
|
|
248
|
+
const annotations = [];
|
|
249
|
+
let text = "";
|
|
250
|
+
for (const run of runs) {
|
|
251
|
+
if (run.text.length === 0) continue;
|
|
252
|
+
const start = text.length;
|
|
253
|
+
text += run.text;
|
|
254
|
+
const end = text.length;
|
|
255
|
+
if (run.bold && !allBold) {
|
|
256
|
+
annotations.push({ type: "Bold", starts: [start], ends: [end] });
|
|
257
|
+
}
|
|
258
|
+
if (run.italic && !allItalic) {
|
|
259
|
+
annotations.push({ type: "Italic", starts: [start], ends: [end] });
|
|
260
|
+
}
|
|
261
|
+
if (run.monospace) {
|
|
262
|
+
annotations.push({ type: "Code", starts: [start], ends: [end] });
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
const merged = mergeAdjacentAnnotations(annotations);
|
|
266
|
+
return { text, annotations: merged };
|
|
267
|
+
}
|
|
268
|
+
function mergeAdjacentAnnotations(annotations) {
|
|
269
|
+
if (annotations.length <= 1) return annotations;
|
|
270
|
+
const byType = /* @__PURE__ */ new Map();
|
|
271
|
+
for (const ann of annotations) {
|
|
272
|
+
const list = byType.get(ann.type) || [];
|
|
273
|
+
list.push(ann);
|
|
274
|
+
byType.set(ann.type, list);
|
|
275
|
+
}
|
|
276
|
+
const result = [];
|
|
277
|
+
for (const [, anns] of Array.from(byType)) {
|
|
278
|
+
anns.sort((a, b) => (a.starts[0] ?? 0) - (b.starts[0] ?? 0));
|
|
279
|
+
let current = { ...anns[0], starts: [...anns[0].starts], ends: [...anns[0].ends] };
|
|
280
|
+
for (let i = 1; i < anns.length; i++) {
|
|
281
|
+
const next = anns[i];
|
|
282
|
+
if ((current.ends[0] ?? 0) >= (next.starts[0] ?? 0)) {
|
|
283
|
+
current.ends[0] = Math.max(current.ends[0] ?? 0, next.ends[0] ?? 0);
|
|
284
|
+
} else {
|
|
285
|
+
result.push(current);
|
|
286
|
+
current = { ...next, starts: [...next.starts], ends: [...next.ends] };
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
result.push(current);
|
|
290
|
+
}
|
|
291
|
+
result.sort((a, b) => (a.starts[0] ?? 0) - (b.starts[0] ?? 0));
|
|
292
|
+
return result;
|
|
293
|
+
}
|
|
294
|
+
function buildHierarchy(blocks) {
|
|
295
|
+
const result = [];
|
|
296
|
+
const stack = [];
|
|
297
|
+
for (const { block, headingLevel } of blocks) {
|
|
298
|
+
if (headingLevel && headingLevel > 0) {
|
|
299
|
+
while (stack.length && stack[stack.length - 1].level >= headingLevel) {
|
|
300
|
+
stack.pop();
|
|
301
|
+
}
|
|
302
|
+
const parent = stack[stack.length - 1];
|
|
303
|
+
if (parent) {
|
|
304
|
+
if (!parent.node.children) parent.node.children = [];
|
|
305
|
+
parent.node.children.push(block);
|
|
306
|
+
} else {
|
|
307
|
+
result.push(block);
|
|
308
|
+
}
|
|
309
|
+
stack.push({ level: headingLevel, node: block });
|
|
310
|
+
} else {
|
|
311
|
+
const parent = stack[stack.length - 1];
|
|
312
|
+
if (parent) {
|
|
313
|
+
if (!parent.node.children) parent.node.children = [];
|
|
314
|
+
parent.node.children.push(block);
|
|
315
|
+
} else {
|
|
316
|
+
result.push(block);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return result;
|
|
321
|
+
}
|
|
322
|
+
async function extractTitle(doc) {
|
|
323
|
+
var _a;
|
|
324
|
+
if (doc.numPages === 0) return void 0;
|
|
325
|
+
const page = await doc.getPage(1);
|
|
326
|
+
await page.getOperatorList();
|
|
327
|
+
const textContent = await page.getTextContent({ includeMarkedContent: false });
|
|
328
|
+
const items = textContent.items.filter(isTextItem);
|
|
329
|
+
if (items.length === 0) return void 0;
|
|
330
|
+
let maxFontSize = 0;
|
|
331
|
+
let titleItems = [];
|
|
332
|
+
for (const item of items) {
|
|
333
|
+
const fontSize = Math.abs(item.transform[3]);
|
|
334
|
+
if (fontSize > maxFontSize + 0.5) {
|
|
335
|
+
maxFontSize = fontSize;
|
|
336
|
+
titleItems = [item];
|
|
337
|
+
} else if (Math.abs(fontSize - maxFontSize) <= 0.5) {
|
|
338
|
+
titleItems.push(item);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
const sizeCount = /* @__PURE__ */ new Map();
|
|
342
|
+
for (const item of items) {
|
|
343
|
+
const size = Math.round(Math.abs(item.transform[3]) * 10) / 10;
|
|
344
|
+
sizeCount.set(size, (sizeCount.get(size) || 0) + 1);
|
|
345
|
+
}
|
|
346
|
+
const bodySize = ((_a = Array.from(sizeCount.entries()).sort((a, b) => b[1] - a[1])[0]) == null ? void 0 : _a[0]) || 12;
|
|
347
|
+
if (maxFontSize <= bodySize + 0.5) {
|
|
348
|
+
return items.slice(0, 10).map((item) => item.str).join("").trim().slice(0, 100) || void 0;
|
|
349
|
+
}
|
|
350
|
+
titleItems.sort((a, b) => a.transform[4] - b.transform[4]);
|
|
351
|
+
return titleItems.map((item) => item.str).join("").trim() || void 0;
|
|
352
|
+
}
|
|
353
|
+
async function embeddedPdfToBlocks(pdfData) {
|
|
354
|
+
let pdfjsLib;
|
|
355
|
+
try {
|
|
356
|
+
pdfjsLib = await import("pdfjs-dist");
|
|
357
|
+
} catch {
|
|
358
|
+
throw new Error(
|
|
359
|
+
"pdfjs-dist is not installed. Install it for embedded PDF extraction:\n pnpm add pdfjs-dist\nOr provide a GROBID server URL for higher-fidelity extraction."
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
const loadingTask = pdfjsLib.getDocument({ data: new Uint8Array(pdfData) });
|
|
363
|
+
const doc = await loadingTask.promise;
|
|
364
|
+
if (doc.numPages === 0) {
|
|
365
|
+
return { metadata: { name: void 0 }, blocks: [] };
|
|
366
|
+
}
|
|
367
|
+
const title = await extractTitle(doc);
|
|
368
|
+
const lines = await extractTextRuns(doc);
|
|
369
|
+
if (lines.length === 0) {
|
|
370
|
+
return { metadata: { name: title }, blocks: [] };
|
|
371
|
+
}
|
|
372
|
+
const { bodySize, headingLevels, boldHeadingLevel } = clusterFontSizes(lines);
|
|
373
|
+
const paragraphGroups = groupLinesIntoParagraphs(lines, bodySize, headingLevels, boldHeadingLevel);
|
|
374
|
+
const flatBlocks = [];
|
|
375
|
+
for (const group of paragraphGroups) {
|
|
376
|
+
switch (group.type) {
|
|
377
|
+
case "heading": {
|
|
378
|
+
const allRuns = group.lines.flatMap((l) => l.runs);
|
|
379
|
+
const { text, annotations } = runsToTextAndAnnotations(allRuns, true, false);
|
|
380
|
+
if (text) {
|
|
381
|
+
flatBlocks.push({
|
|
382
|
+
block: makeHeading(text, annotations),
|
|
383
|
+
headingLevel: group.headingLevel
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
break;
|
|
387
|
+
}
|
|
388
|
+
case "code": {
|
|
389
|
+
const codeText = group.lines.map((l) => l.runs.map((r) => r.text).join("")).join("\n");
|
|
390
|
+
if (codeText) {
|
|
391
|
+
flatBlocks.push({ block: makeCode(codeText) });
|
|
392
|
+
}
|
|
393
|
+
break;
|
|
394
|
+
}
|
|
395
|
+
case "list-bullet":
|
|
396
|
+
case "list-numbered": {
|
|
397
|
+
const listType = group.type === "list-bullet" ? "Unordered" : "Ordered";
|
|
398
|
+
const items = [];
|
|
399
|
+
for (const line of group.lines) {
|
|
400
|
+
const lineText = line.runs.map((r) => r.text).join("");
|
|
401
|
+
const listInfo = detectListType(lineText);
|
|
402
|
+
const contentText = listInfo ? listInfo.content : lineText;
|
|
403
|
+
items.push(makeParagraph(contentText));
|
|
404
|
+
}
|
|
405
|
+
if (items.length) {
|
|
406
|
+
flatBlocks.push({ block: makeList(items, listType) });
|
|
407
|
+
}
|
|
408
|
+
break;
|
|
409
|
+
}
|
|
410
|
+
case "paragraph":
|
|
411
|
+
default: {
|
|
412
|
+
const allRuns = [];
|
|
413
|
+
const allBold = group.lines.every((l) => l.bold);
|
|
414
|
+
const allItalic = group.lines.every((l) => l.italic);
|
|
415
|
+
for (let i = 0; i < group.lines.length; i++) {
|
|
416
|
+
const line = group.lines[i];
|
|
417
|
+
if (i > 0 && allRuns.length > 0) {
|
|
418
|
+
const lastRun = allRuns[allRuns.length - 1];
|
|
419
|
+
if (lastRun.text.endsWith("-")) {
|
|
420
|
+
lastRun.text = lastRun.text.slice(0, -1);
|
|
421
|
+
} else if (!lastRun.text.endsWith(" ")) {
|
|
422
|
+
allRuns.push({ ...line.runs[0], text: " ", width: 0 });
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
allRuns.push(...line.runs);
|
|
426
|
+
}
|
|
427
|
+
const { text, annotations } = runsToTextAndAnnotations(allRuns, allBold, allItalic);
|
|
428
|
+
if (text) {
|
|
429
|
+
flatBlocks.push({ block: makeParagraph(text, annotations) });
|
|
430
|
+
}
|
|
431
|
+
break;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
const PAGE_NUMBER_RE = /^\d{1,3}$/;
|
|
436
|
+
for (let i = flatBlocks.length - 1; i >= 0; i--) {
|
|
437
|
+
const entry = flatBlocks[i];
|
|
438
|
+
if (entry.headingLevel) continue;
|
|
439
|
+
const blockText = (entry.block.block.text || "").trim();
|
|
440
|
+
if (PAGE_NUMBER_RE.test(blockText)) {
|
|
441
|
+
flatBlocks.splice(i, 1);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
if (title && flatBlocks.length > 0) {
|
|
445
|
+
const first = flatBlocks[0];
|
|
446
|
+
if (first.headingLevel) {
|
|
447
|
+
const blockText = first.block.block.text || "";
|
|
448
|
+
if (normalizeForComparison(blockText) === normalizeForComparison(title)) {
|
|
449
|
+
flatBlocks.shift();
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
const blocks = buildHierarchy(flatBlocks);
|
|
454
|
+
return { metadata: { name: title }, blocks };
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
export {
|
|
458
|
+
embeddedPdfToBlocks
|
|
459
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var SeedClientError = class extends Error {
|
|
3
|
+
status;
|
|
4
|
+
body;
|
|
5
|
+
constructor(message, status, body) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "SeedClientError";
|
|
8
|
+
this.status = status;
|
|
9
|
+
this.body = body;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var SeedNetworkError = class extends Error {
|
|
13
|
+
constructor(message, options) {
|
|
14
|
+
super(message, options);
|
|
15
|
+
this.name = "SeedNetworkError";
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var SeedValidationError = class extends Error {
|
|
19
|
+
constructor(message) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.name = "SeedValidationError";
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export {
|
|
26
|
+
SeedClientError,
|
|
27
|
+
SeedNetworkError,
|
|
28
|
+
SeedValidationError
|
|
29
|
+
};
|