@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,334 @@
|
|
|
1
|
+
// src/block-diff.ts
|
|
2
|
+
function createBlocksMap(nodes, parentId = "") {
|
|
3
|
+
const result = {};
|
|
4
|
+
nodes.forEach((node, idx) => {
|
|
5
|
+
var _a, _b, _c;
|
|
6
|
+
if (!((_a = node.block) == null ? void 0 : _a.id)) return;
|
|
7
|
+
const prev = idx > 0 ? nodes[idx - 1] : void 0;
|
|
8
|
+
const prevId = ((_b = prev == null ? void 0 : prev.block) == null ? void 0 : _b.id) || "";
|
|
9
|
+
result[node.block.id] = {
|
|
10
|
+
parent: parentId,
|
|
11
|
+
left: prevId,
|
|
12
|
+
block: node.block
|
|
13
|
+
};
|
|
14
|
+
if ((_c = node.children) == null ? void 0 : _c.length) {
|
|
15
|
+
Object.assign(result, createBlocksMap(node.children, node.block.id));
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
20
|
+
function matchBlockIds(oldNodes, newNodes) {
|
|
21
|
+
return newNodes.map((newNode, idx) => {
|
|
22
|
+
const oldNode = idx < oldNodes.length ? oldNodes[idx] : void 0;
|
|
23
|
+
let matchedId = newNode.block.id;
|
|
24
|
+
if (oldNode && oldNode.block.type === newNode.block.type) {
|
|
25
|
+
matchedId = oldNode.block.id;
|
|
26
|
+
}
|
|
27
|
+
const matchedChildren = matchBlockIds((oldNode == null ? void 0 : oldNode.children) ?? [], newNode.children);
|
|
28
|
+
return {
|
|
29
|
+
block: { ...newNode.block, id: matchedId },
|
|
30
|
+
children: matchedChildren
|
|
31
|
+
};
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
function extraAttributes(attrs) {
|
|
35
|
+
if (!attrs) return {};
|
|
36
|
+
const { childrenType: _c, language: _l, ...rest } = attrs;
|
|
37
|
+
return rest;
|
|
38
|
+
}
|
|
39
|
+
function oldHeaderCellText(row, columnId) {
|
|
40
|
+
var _a, _b;
|
|
41
|
+
if (!row) return void 0;
|
|
42
|
+
for (const cell of row.children || []) {
|
|
43
|
+
if (((_b = (_a = cell.block) == null ? void 0 : _a.attributes) == null ? void 0 : _b.columnId) === columnId) return cell.block.text || "";
|
|
44
|
+
}
|
|
45
|
+
return void 0;
|
|
46
|
+
}
|
|
47
|
+
function rebindTableIdentities(oldNodes, newNodes) {
|
|
48
|
+
const oldTables = /* @__PURE__ */ new Map();
|
|
49
|
+
const indexTables = (nodes) => {
|
|
50
|
+
var _a, _b;
|
|
51
|
+
for (const node of nodes) {
|
|
52
|
+
if (((_a = node.block) == null ? void 0 : _a.type) === "Table") oldTables.set(node.block.id, node);
|
|
53
|
+
if ((_b = node.children) == null ? void 0 : _b.length) indexTables(node.children);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
indexTables(oldNodes);
|
|
57
|
+
const walk = (nodes) => nodes.map((node) => {
|
|
58
|
+
if (node.block.type === "Table") {
|
|
59
|
+
return rebindTable(oldTables.get(node.block.id), node);
|
|
60
|
+
}
|
|
61
|
+
return { ...node, children: walk(node.children) };
|
|
62
|
+
});
|
|
63
|
+
return walk(newNodes);
|
|
64
|
+
}
|
|
65
|
+
function rebindTable(oldTable, newTable) {
|
|
66
|
+
const newCols = newTable.children.filter((c) => c.block.type === "TableColumn");
|
|
67
|
+
const newRows = newTable.children.filter((c) => c.block.type === "TableRow");
|
|
68
|
+
const otherChildren = newTable.children.filter((c) => c.block.type !== "TableColumn" && c.block.type !== "TableRow");
|
|
69
|
+
const oldCols = ((oldTable == null ? void 0 : oldTable.children) || []).filter((c) => {
|
|
70
|
+
var _a;
|
|
71
|
+
return ((_a = c.block) == null ? void 0 : _a.type) === "TableColumn";
|
|
72
|
+
});
|
|
73
|
+
const oldRows = ((oldTable == null ? void 0 : oldTable.children) || []).filter((c) => {
|
|
74
|
+
var _a;
|
|
75
|
+
return ((_a = c.block) == null ? void 0 : _a.type) === "TableRow";
|
|
76
|
+
});
|
|
77
|
+
const oldColById = new Map(oldCols.map((c) => [c.block.id, c]));
|
|
78
|
+
const oldRowById = new Map(oldRows.map((r) => [r.block.id, r]));
|
|
79
|
+
const usedOldCols = /* @__PURE__ */ new Set();
|
|
80
|
+
const matchedOldCol = new Array(newCols.length).fill(void 0);
|
|
81
|
+
newCols.forEach((col, idx) => {
|
|
82
|
+
const old = oldColById.get(col.block.id);
|
|
83
|
+
if (old && !usedOldCols.has(old.block.id)) {
|
|
84
|
+
matchedOldCol[idx] = old;
|
|
85
|
+
usedOldCols.add(old.block.id);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
const oldHeaderRow = oldRows.find((r) => {
|
|
89
|
+
var _a;
|
|
90
|
+
return ((_a = r.block.attributes) == null ? void 0 : _a.isHeader) === true;
|
|
91
|
+
});
|
|
92
|
+
const newHeaderRow = newRows.find((r) => {
|
|
93
|
+
var _a;
|
|
94
|
+
return ((_a = r.block.attributes) == null ? void 0 : _a.isHeader) === true;
|
|
95
|
+
});
|
|
96
|
+
if (oldHeaderRow && newHeaderRow) {
|
|
97
|
+
const textToOldCol = /* @__PURE__ */ new Map();
|
|
98
|
+
for (const old of oldCols) {
|
|
99
|
+
if (usedOldCols.has(old.block.id)) continue;
|
|
100
|
+
const text = oldHeaderCellText(oldHeaderRow, old.block.id);
|
|
101
|
+
if (!text) continue;
|
|
102
|
+
textToOldCol.set(text, textToOldCol.has(text) ? null : old);
|
|
103
|
+
}
|
|
104
|
+
newCols.forEach((col, idx) => {
|
|
105
|
+
if (matchedOldCol[idx]) return;
|
|
106
|
+
const cell = newHeaderRow.children.find((c) => {
|
|
107
|
+
var _a;
|
|
108
|
+
return ((_a = c.block.attributes) == null ? void 0 : _a.columnId) === col.block.id;
|
|
109
|
+
});
|
|
110
|
+
const text = (cell == null ? void 0 : cell.block.text) || "";
|
|
111
|
+
const old = text ? textToOldCol.get(text) : void 0;
|
|
112
|
+
if (old && !usedOldCols.has(old.block.id)) {
|
|
113
|
+
matchedOldCol[idx] = old;
|
|
114
|
+
usedOldCols.add(old.block.id);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
const remainingOldCols = oldCols.filter((c) => !usedOldCols.has(c.block.id));
|
|
119
|
+
let remainingIdx = 0;
|
|
120
|
+
newCols.forEach((_, idx) => {
|
|
121
|
+
if (matchedOldCol[idx]) return;
|
|
122
|
+
const old = remainingOldCols[remainingIdx];
|
|
123
|
+
if (old) {
|
|
124
|
+
matchedOldCol[idx] = old;
|
|
125
|
+
usedOldCols.add(old.block.id);
|
|
126
|
+
remainingIdx++;
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
const colIdMap = /* @__PURE__ */ new Map();
|
|
130
|
+
const finalCols = newCols.map((col, idx) => {
|
|
131
|
+
const old = matchedOldCol[idx];
|
|
132
|
+
const finalId = old ? old.block.id : col.block.id;
|
|
133
|
+
colIdMap.set(col.block.id, finalId);
|
|
134
|
+
const carried = { ...extraAttributes(old == null ? void 0 : old.block.attributes), ...col.block.attributes };
|
|
135
|
+
const block = {
|
|
136
|
+
...col.block,
|
|
137
|
+
id: finalId,
|
|
138
|
+
...Object.keys(carried).length > 0 ? { attributes: carried } : {}
|
|
139
|
+
};
|
|
140
|
+
return { block, children: [] };
|
|
141
|
+
});
|
|
142
|
+
const usedOldRows = /* @__PURE__ */ new Set();
|
|
143
|
+
const matchedOldRow = new Array(newRows.length).fill(void 0);
|
|
144
|
+
newRows.forEach((row, idx) => {
|
|
145
|
+
const old = oldRowById.get(row.block.id);
|
|
146
|
+
if (old && !usedOldRows.has(old.block.id)) {
|
|
147
|
+
matchedOldRow[idx] = old;
|
|
148
|
+
usedOldRows.add(old.block.id);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
const remainingOldRows = oldRows.filter((r) => !usedOldRows.has(r.block.id));
|
|
152
|
+
let remainingRowIdx = 0;
|
|
153
|
+
newRows.forEach((_, idx) => {
|
|
154
|
+
if (matchedOldRow[idx]) return;
|
|
155
|
+
const old = remainingOldRows[remainingRowIdx];
|
|
156
|
+
if (old) {
|
|
157
|
+
matchedOldRow[idx] = old;
|
|
158
|
+
usedOldRows.add(old.block.id);
|
|
159
|
+
remainingRowIdx++;
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
const finalRows = newRows.map((row, idx) => {
|
|
163
|
+
const old = matchedOldRow[idx];
|
|
164
|
+
const rowId = old ? old.block.id : row.block.id;
|
|
165
|
+
const { isHeader: _oldIsHeader, ...oldRowExtras } = extraAttributes(old == null ? void 0 : old.block.attributes);
|
|
166
|
+
const rowAttrs = { ...oldRowExtras, ...row.block.attributes };
|
|
167
|
+
const cells = row.children.map((cell) => {
|
|
168
|
+
var _a;
|
|
169
|
+
const newColumnId = (_a = cell.block.attributes) == null ? void 0 : _a.columnId;
|
|
170
|
+
const finalColumnId = typeof newColumnId === "string" ? colIdMap.get(newColumnId) ?? newColumnId : newColumnId;
|
|
171
|
+
const oldCell = old == null ? void 0 : old.children.find((c) => {
|
|
172
|
+
var _a2, _b;
|
|
173
|
+
return ((_b = (_a2 = c.block) == null ? void 0 : _a2.attributes) == null ? void 0 : _b.columnId) === finalColumnId;
|
|
174
|
+
});
|
|
175
|
+
const cellAttrs = {
|
|
176
|
+
...extraAttributes(oldCell == null ? void 0 : oldCell.block.attributes),
|
|
177
|
+
...cell.block.attributes,
|
|
178
|
+
columnId: finalColumnId
|
|
179
|
+
};
|
|
180
|
+
const block2 = {
|
|
181
|
+
...cell.block,
|
|
182
|
+
id: oldCell ? oldCell.block.id : cell.block.id,
|
|
183
|
+
attributes: cellAttrs
|
|
184
|
+
};
|
|
185
|
+
return { block: block2, children: cell.children };
|
|
186
|
+
});
|
|
187
|
+
const block = {
|
|
188
|
+
...row.block,
|
|
189
|
+
id: rowId,
|
|
190
|
+
...Object.keys(rowAttrs).length > 0 ? { attributes: rowAttrs } : {}
|
|
191
|
+
};
|
|
192
|
+
return { block, children: cells };
|
|
193
|
+
});
|
|
194
|
+
const stripLateHeader = (nodes) => {
|
|
195
|
+
nodes.forEach((node, idx) => {
|
|
196
|
+
var _a;
|
|
197
|
+
if (idx === 0 || ((_a = node.block.attributes) == null ? void 0 : _a.isHeader) !== true) return;
|
|
198
|
+
const { isHeader: _h, ...rest } = node.block.attributes;
|
|
199
|
+
node.block = { ...node.block, attributes: rest };
|
|
200
|
+
});
|
|
201
|
+
};
|
|
202
|
+
stripLateHeader(finalCols);
|
|
203
|
+
stripLateHeader(finalRows);
|
|
204
|
+
const tableAttrs = { ...extraAttributes(oldTable == null ? void 0 : oldTable.block.attributes), ...newTable.block.attributes };
|
|
205
|
+
const tableBlock = {
|
|
206
|
+
...newTable.block,
|
|
207
|
+
...Object.keys(tableAttrs).length > 0 ? { attributes: tableAttrs } : {}
|
|
208
|
+
};
|
|
209
|
+
return { block: tableBlock, children: [...finalCols, ...finalRows, ...otherChildren] };
|
|
210
|
+
}
|
|
211
|
+
function computeReplaceOps(oldMap, matchedTree, parentId = "") {
|
|
212
|
+
const touchedIds = /* @__PURE__ */ new Set();
|
|
213
|
+
const ops = computeReplaceOpsInner(oldMap, matchedTree, parentId, touchedIds);
|
|
214
|
+
const deletedIds = Object.keys(oldMap).filter((id) => !touchedIds.has(id));
|
|
215
|
+
if (deletedIds.length > 0) {
|
|
216
|
+
ops.push({ type: "DeleteBlocks", blocks: deletedIds });
|
|
217
|
+
}
|
|
218
|
+
return ops;
|
|
219
|
+
}
|
|
220
|
+
function computeReplaceOpsInner(oldMap, matchedTree, parentId, touchedIds) {
|
|
221
|
+
const ops = [];
|
|
222
|
+
const blockIdsAtLevel = [];
|
|
223
|
+
matchedTree.forEach((node) => {
|
|
224
|
+
const blockId = node.block.id;
|
|
225
|
+
touchedIds.add(blockId);
|
|
226
|
+
blockIdsAtLevel.push(blockId);
|
|
227
|
+
const oldEntry = oldMap[blockId];
|
|
228
|
+
const isNew = !oldEntry;
|
|
229
|
+
const block = {
|
|
230
|
+
...node.block.attributes,
|
|
231
|
+
type: node.block.type,
|
|
232
|
+
id: blockId,
|
|
233
|
+
text: node.block.text,
|
|
234
|
+
annotations: node.block.annotations
|
|
235
|
+
};
|
|
236
|
+
if (node.block.language !== void 0) {
|
|
237
|
+
block.language = node.block.language;
|
|
238
|
+
}
|
|
239
|
+
if (node.block.childrenType !== void 0) {
|
|
240
|
+
block.childrenType = node.block.childrenType;
|
|
241
|
+
}
|
|
242
|
+
if (node.block.link !== void 0) {
|
|
243
|
+
block.link = node.block.link;
|
|
244
|
+
}
|
|
245
|
+
if (isNew) {
|
|
246
|
+
ops.push({ type: "ReplaceBlock", block });
|
|
247
|
+
} else {
|
|
248
|
+
if (!isBlockContentEqual(oldEntry.block, node.block)) {
|
|
249
|
+
ops.push({ type: "ReplaceBlock", block });
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (node.children.length > 0) {
|
|
253
|
+
ops.push(...computeReplaceOpsInner(oldMap, node.children, blockId, touchedIds));
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
if (blockIdsAtLevel.length > 0) {
|
|
257
|
+
ops.push({
|
|
258
|
+
type: "MoveBlocks",
|
|
259
|
+
blocks: blockIdsAtLevel,
|
|
260
|
+
parent: parentId
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
return ops;
|
|
264
|
+
}
|
|
265
|
+
function attrsEqual(a, b) {
|
|
266
|
+
const keys = Object.keys({ ...a, ...b });
|
|
267
|
+
for (const key of keys) {
|
|
268
|
+
const av = a[key];
|
|
269
|
+
const bv = b[key];
|
|
270
|
+
if (av === bv) continue;
|
|
271
|
+
if (JSON.stringify(av) !== JSON.stringify(bv)) return false;
|
|
272
|
+
}
|
|
273
|
+
return true;
|
|
274
|
+
}
|
|
275
|
+
function isBlockContentEqual(old, newBlock) {
|
|
276
|
+
if (old.type !== newBlock.type) return false;
|
|
277
|
+
if ((old.text || "") !== (newBlock.text || "")) return false;
|
|
278
|
+
if ((old.link || "") !== (newBlock.link || "")) return false;
|
|
279
|
+
const oldAnn = old.annotations || [];
|
|
280
|
+
const newAnn = newBlock.annotations || [];
|
|
281
|
+
if (oldAnn.length !== newAnn.length) return false;
|
|
282
|
+
if (oldAnn.length > 0 && JSON.stringify(oldAnn) !== JSON.stringify(newAnn)) {
|
|
283
|
+
return false;
|
|
284
|
+
}
|
|
285
|
+
const oldAttrs = old.attributes || {};
|
|
286
|
+
if ((oldAttrs.childrenType || "") !== (newBlock.childrenType || "")) {
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
if ((oldAttrs.language || "") !== (newBlock.language || "")) return false;
|
|
290
|
+
if (!attrsEqual(extraAttributes(oldAttrs), newBlock.attributes || {})) return false;
|
|
291
|
+
return true;
|
|
292
|
+
}
|
|
293
|
+
function toAPIBlockNode(node) {
|
|
294
|
+
const block = node.block;
|
|
295
|
+
return {
|
|
296
|
+
block: {
|
|
297
|
+
id: block.id,
|
|
298
|
+
type: block.type,
|
|
299
|
+
text: block.text || "",
|
|
300
|
+
link: block.link || "",
|
|
301
|
+
annotations: block.annotations || [],
|
|
302
|
+
attributes: block.attributes || {}
|
|
303
|
+
},
|
|
304
|
+
children: (node.children || []).map(toAPIBlockNode)
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
function hmBlockNodeToBlockNode(node) {
|
|
308
|
+
const b = node.block;
|
|
309
|
+
const attrs = b.attributes || {};
|
|
310
|
+
const extra = extraAttributes(attrs);
|
|
311
|
+
const block = {
|
|
312
|
+
type: b.type || "",
|
|
313
|
+
id: b.id || "",
|
|
314
|
+
text: b.text || "",
|
|
315
|
+
annotations: b.annotations || [],
|
|
316
|
+
...attrs.childrenType ? { childrenType: attrs.childrenType } : {},
|
|
317
|
+
...attrs.language ? { language: attrs.language } : {},
|
|
318
|
+
...b.link ? { link: b.link } : {},
|
|
319
|
+
...Object.keys(extra).length > 0 ? { attributes: extra } : {}
|
|
320
|
+
};
|
|
321
|
+
return {
|
|
322
|
+
block,
|
|
323
|
+
children: (node.children || []).map(hmBlockNodeToBlockNode)
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
export {
|
|
328
|
+
createBlocksMap,
|
|
329
|
+
matchBlockIds,
|
|
330
|
+
rebindTableIdentities,
|
|
331
|
+
computeReplaceOps,
|
|
332
|
+
toAPIBlockNode,
|
|
333
|
+
hmBlockNodeToBlockNode
|
|
334
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// src/grobid.ts
|
|
2
|
+
var DEFAULT_GROBID_URL = "http://localhost:8070";
|
|
3
|
+
async function isGrobidAvailable(grobidUrl = DEFAULT_GROBID_URL, timeoutMs = 3e3) {
|
|
4
|
+
try {
|
|
5
|
+
const controller = new AbortController();
|
|
6
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
7
|
+
const response = await fetch(`${grobidUrl}/api/isalive`, {
|
|
8
|
+
signal: controller.signal
|
|
9
|
+
});
|
|
10
|
+
clearTimeout(timer);
|
|
11
|
+
return response.ok;
|
|
12
|
+
} catch {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
async function processFulltextDocument(pdfBuffer, options) {
|
|
17
|
+
const baseUrl = (options == null ? void 0 : options.grobidUrl) || DEFAULT_GROBID_URL;
|
|
18
|
+
const url = `${baseUrl}/api/processFulltextDocument`;
|
|
19
|
+
const formData = new FormData();
|
|
20
|
+
formData.append("input", new Blob([pdfBuffer]), "document.pdf");
|
|
21
|
+
const coords = (options == null ? void 0 : options.teiCoordinates) || ["figure"];
|
|
22
|
+
for (const coord of coords) {
|
|
23
|
+
formData.append("teiCoordinates", coord);
|
|
24
|
+
}
|
|
25
|
+
const response = await fetch(url, {
|
|
26
|
+
method: "POST",
|
|
27
|
+
body: formData
|
|
28
|
+
});
|
|
29
|
+
if (!response.ok) {
|
|
30
|
+
const body = await response.text().catch(() => "");
|
|
31
|
+
throw new Error(`GROBID error (${response.status}): ${response.statusText}${body ? ` \u2014 ${body}` : ""}`);
|
|
32
|
+
}
|
|
33
|
+
return response.text();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export {
|
|
37
|
+
DEFAULT_GROBID_URL,
|
|
38
|
+
isGrobidAvailable,
|
|
39
|
+
processFulltextDocument
|
|
40
|
+
};
|