@seed-hypermedia/client 0.0.68 → 0.0.70
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 +266 -0
- package/dist/auto-link.d.ts +3 -2
- package/dist/auto-link.mjs +21 -0
- package/dist/base64.mjs +9 -0
- package/dist/blobs.d.ts +170 -0
- package/dist/blobs.mjs +51 -0
- package/dist/block-diff.mjs +17 -0
- package/dist/blocks-to-markdown.mjs +22 -0
- package/dist/capability.d.ts +7 -2
- package/dist/capability.mjs +14 -0
- package/dist/cbor.d.ts +13 -0
- package/dist/cbor.mjs +13 -0
- package/dist/change.d.ts +8 -7
- package/dist/change.mjs +24 -0
- package/dist/chunk-2MD3FXKB.mjs +14 -0
- package/dist/chunk-37WRGUK4.mjs +125 -0
- package/dist/chunk-3KESF5XJ.mjs +297 -0
- package/dist/chunk-3TPCJCVO.mjs +24 -0
- package/dist/chunk-4Q3SAL5R.mjs +75 -0
- package/dist/chunk-52P3E6H6.mjs +459 -0
- package/dist/chunk-BUPMCRR4.mjs +48 -0
- package/dist/chunk-CBELM7KX.mjs +29 -0
- package/dist/{chunk-KCR3277A.mjs → chunk-DBHL3BYB.mjs} +9 -14
- package/dist/chunk-DR3HJ6BV.mjs +25 -0
- package/dist/chunk-EU36RFR3.mjs +159 -0
- package/dist/chunk-EY6RQVAP.mjs +138 -0
- package/dist/chunk-H7L4TI6X.mjs +705 -0
- package/dist/chunk-HBIVZA6C.mjs +102 -0
- package/dist/chunk-IT5LLTMP.mjs +209 -0
- package/dist/chunk-JT57MV7Y.mjs +136 -0
- package/dist/chunk-KO4XH5L2.mjs +475 -0
- package/dist/chunk-LVAHHQIK.mjs +41 -0
- package/dist/chunk-LYECQKZE.mjs +49 -0
- package/dist/chunk-N3P7S5YZ.mjs +118 -0
- package/dist/chunk-NPG76WUS.mjs +348 -0
- package/dist/chunk-NYWCMQTH.mjs +412 -0
- package/dist/chunk-OZKQOI2X.mjs +334 -0
- package/dist/chunk-PJ2RFT5A.mjs +113 -0
- package/dist/chunk-PPAY4WZX.mjs +40 -0
- package/dist/chunk-S2PVJK2P.mjs +517 -0
- package/dist/chunk-SHOAYNJY.mjs +289 -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-YNVFRCUH.mjs +171 -0
- package/dist/client.d.ts +3 -2
- package/dist/client.mjs +15 -0
- package/dist/comment.d.ts +5 -4
- package/dist/comment.mjs +19 -0
- package/dist/contact.d.ts +5 -4
- package/dist/contact.mjs +15 -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/hmauth.d.ts +169 -0
- package/dist/hmauth.mjs +53 -0
- package/dist/hmblock-to-editorblock.mjs +12 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +132 -5453
- package/dist/keyfile.mjs +19 -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.d.ts +5 -4
- package/dist/ref.mjs +13 -0
- package/dist/resource-read.mjs +15 -0
- package/dist/signer.d.ts +42 -0
- package/dist/signer.mjs +9 -0
- package/dist/signing.d.ts +10 -2
- package/dist/signing.mjs +16 -0
- package/dist/tei-to-blocks.mjs +8 -0
- package/dist/unicode.mjs +19 -0
- package/dist/vault-local.mjs +10 -6
- package/dist/vault.d.ts +14 -4
- package/dist/vault.mjs +6 -2
- package/package.json +18 -28
- package/src/index.ts +0 -144
|
@@ -0,0 +1,517 @@
|
|
|
1
|
+
import {
|
|
2
|
+
codePointLength
|
|
3
|
+
} from "./chunk-SQEUO7FO.mjs";
|
|
4
|
+
|
|
5
|
+
// src/tei-to-blocks.ts
|
|
6
|
+
function generateBlockId() {
|
|
7
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
|
|
8
|
+
let id = "";
|
|
9
|
+
for (let i = 0; i < 8; i++) {
|
|
10
|
+
id += chars[Math.floor(Math.random() * chars.length)];
|
|
11
|
+
}
|
|
12
|
+
return id;
|
|
13
|
+
}
|
|
14
|
+
function makeBlock(type, text, extra = {}) {
|
|
15
|
+
return {
|
|
16
|
+
block: {
|
|
17
|
+
id: generateBlockId(),
|
|
18
|
+
type,
|
|
19
|
+
text,
|
|
20
|
+
revision: "",
|
|
21
|
+
link: "",
|
|
22
|
+
attributes: {},
|
|
23
|
+
annotations: [],
|
|
24
|
+
...extra
|
|
25
|
+
},
|
|
26
|
+
children: []
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function makeHeading(text) {
|
|
30
|
+
return makeBlock("Heading", text, {
|
|
31
|
+
attributes: { childrenType: "Group" }
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
function makeParagraph(text, annotations = []) {
|
|
35
|
+
return makeBlock("Paragraph", text, { annotations });
|
|
36
|
+
}
|
|
37
|
+
function makeMath(text) {
|
|
38
|
+
return makeBlock("Math", text);
|
|
39
|
+
}
|
|
40
|
+
function makeCode(text, language = "") {
|
|
41
|
+
return makeBlock("Code", text, {
|
|
42
|
+
attributes: language ? { language } : {}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
function makeImage(link, caption = "") {
|
|
46
|
+
return makeBlock("Image", caption, { link });
|
|
47
|
+
}
|
|
48
|
+
function makeList(items, listType) {
|
|
49
|
+
const container = makeBlock("Paragraph", "", {
|
|
50
|
+
attributes: { childrenType: listType }
|
|
51
|
+
});
|
|
52
|
+
container.children = items;
|
|
53
|
+
return container;
|
|
54
|
+
}
|
|
55
|
+
function parseInlineContent(_$, el) {
|
|
56
|
+
const annotations = [];
|
|
57
|
+
let text = "";
|
|
58
|
+
function walk(node, active) {
|
|
59
|
+
var _a, _b, _c;
|
|
60
|
+
if (node.type === "text") {
|
|
61
|
+
const raw = node.data || "";
|
|
62
|
+
const normalized = raw.replace(/\s+/g, " ");
|
|
63
|
+
text += normalized;
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (node.type !== "tag") return;
|
|
67
|
+
let isBold = active.bold || false;
|
|
68
|
+
let isItalic = active.italic || false;
|
|
69
|
+
let isCode = active.code || false;
|
|
70
|
+
let linkHref = active.link;
|
|
71
|
+
const tagName = ((_a = node.name) == null ? void 0 : _a.toLowerCase()) || "";
|
|
72
|
+
if (tagName === "hi") {
|
|
73
|
+
const rend = ((_b = node.attribs) == null ? void 0 : _b.rend) || "";
|
|
74
|
+
if (rend === "bold" || rend === "b") isBold = true;
|
|
75
|
+
if (rend === "italic" || rend === "i") isItalic = true;
|
|
76
|
+
if (rend === "monospace" || rend === "code") isCode = true;
|
|
77
|
+
}
|
|
78
|
+
if (tagName === "ref") {
|
|
79
|
+
const target = (_c = node.attribs) == null ? void 0 : _c.target;
|
|
80
|
+
if (target && target.startsWith("http")) {
|
|
81
|
+
linkHref = target;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const start = codePointLength(text);
|
|
85
|
+
for (const child of node.children || []) {
|
|
86
|
+
walk(child, { bold: isBold, italic: isItalic, code: isCode, link: linkHref });
|
|
87
|
+
}
|
|
88
|
+
const end = codePointLength(text);
|
|
89
|
+
if (end <= start) return;
|
|
90
|
+
if (isBold && !active.bold && tagName === "hi") {
|
|
91
|
+
annotations.push({ type: "Bold", starts: [start], ends: [end] });
|
|
92
|
+
}
|
|
93
|
+
if (isItalic && !active.italic && tagName === "hi") {
|
|
94
|
+
annotations.push({ type: "Italic", starts: [start], ends: [end] });
|
|
95
|
+
}
|
|
96
|
+
if (isCode && !active.code && tagName === "hi") {
|
|
97
|
+
annotations.push({ type: "Code", starts: [start], ends: [end] });
|
|
98
|
+
}
|
|
99
|
+
if (linkHref && !active.link && tagName === "ref") {
|
|
100
|
+
annotations.push({
|
|
101
|
+
type: "Link",
|
|
102
|
+
starts: [start],
|
|
103
|
+
ends: [end],
|
|
104
|
+
link: linkHref
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
for (const child of el.contents().toArray()) {
|
|
109
|
+
walk(child, {});
|
|
110
|
+
}
|
|
111
|
+
const trimmedText = text.trim();
|
|
112
|
+
const leadingLen = text.length - text.trimStart().length;
|
|
113
|
+
const adjustedAnnotations = annotations.map((a) => ({
|
|
114
|
+
...a,
|
|
115
|
+
starts: a.starts.map((p) => Math.max(0, p - leadingLen)),
|
|
116
|
+
ends: a.ends.map((p) => Math.max(0, p - leadingLen))
|
|
117
|
+
})).filter((a) => (a.ends[0] ?? 0) > (a.starts[0] ?? 0));
|
|
118
|
+
adjustedAnnotations.sort((a, b) => {
|
|
119
|
+
const aStart = a.starts[0] ?? 0;
|
|
120
|
+
const bStart = b.starts[0] ?? 0;
|
|
121
|
+
if (aStart !== bStart) return aStart - bStart;
|
|
122
|
+
const aEnd = a.ends[0] ?? 0;
|
|
123
|
+
const bEnd = b.ends[0] ?? 0;
|
|
124
|
+
if (aEnd !== bEnd) return aEnd - bEnd;
|
|
125
|
+
return a.type < b.type ? -1 : a.type > b.type ? 1 : 0;
|
|
126
|
+
});
|
|
127
|
+
return { text: trimmedText, annotations: adjustedAnnotations };
|
|
128
|
+
}
|
|
129
|
+
function extractTitle($) {
|
|
130
|
+
const titleEl = $('teiHeader fileDesc titleStmt title[level="a"]');
|
|
131
|
+
if (titleEl.length) return titleEl.text().trim();
|
|
132
|
+
const anyTitle = $("teiHeader fileDesc titleStmt title");
|
|
133
|
+
if (anyTitle.length) return anyTitle.first().text().trim();
|
|
134
|
+
return "";
|
|
135
|
+
}
|
|
136
|
+
function extractAuthors($) {
|
|
137
|
+
const authors = [];
|
|
138
|
+
$("teiHeader fileDesc sourceDesc biblStruct analytic author persName").each((_i, el) => {
|
|
139
|
+
const forenames = [];
|
|
140
|
+
$(el).find("forename").each((_j, fn) => {
|
|
141
|
+
forenames.push($(fn).text().trim());
|
|
142
|
+
});
|
|
143
|
+
const surname = $(el).find("surname").text().trim();
|
|
144
|
+
const name = [...forenames, surname].filter(Boolean).join(" ");
|
|
145
|
+
if (name) authors.push(name);
|
|
146
|
+
});
|
|
147
|
+
if (authors.length === 0) {
|
|
148
|
+
$("teiHeader sourceDesc biblStruct analytic author").each((_i, el) => {
|
|
149
|
+
const persName = $(el).find("persName");
|
|
150
|
+
if (persName.length) {
|
|
151
|
+
const forenames = [];
|
|
152
|
+
persName.find("forename").each((_j, fn) => {
|
|
153
|
+
forenames.push($(fn).text().trim());
|
|
154
|
+
});
|
|
155
|
+
const surname = persName.find("surname").text().trim();
|
|
156
|
+
const name = [...forenames, surname].filter(Boolean).join(" ");
|
|
157
|
+
if (name) authors.push(name);
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
return authors.join(", ");
|
|
162
|
+
}
|
|
163
|
+
function extractAbstract($) {
|
|
164
|
+
const abs = $("teiHeader profileDesc abstract");
|
|
165
|
+
if (!abs.length) return "";
|
|
166
|
+
const parts = [];
|
|
167
|
+
abs.find("p").each((_i, el) => {
|
|
168
|
+
const t = $(el).text().trim();
|
|
169
|
+
if (t) parts.push(t);
|
|
170
|
+
});
|
|
171
|
+
if (parts.length) return parts.join("\n\n");
|
|
172
|
+
return abs.text().trim();
|
|
173
|
+
}
|
|
174
|
+
function extractDate($) {
|
|
175
|
+
const dateEl = $('teiHeader fileDesc publicationStmt date[type="published"]');
|
|
176
|
+
const when = dateEl.attr("when");
|
|
177
|
+
if (when) return when;
|
|
178
|
+
return dateEl.text().trim();
|
|
179
|
+
}
|
|
180
|
+
function parseCoords(coordsStr) {
|
|
181
|
+
if (!coordsStr) return null;
|
|
182
|
+
const first = coordsStr.split(";")[0];
|
|
183
|
+
if (!first) return null;
|
|
184
|
+
const parts = first.split(",").map(Number);
|
|
185
|
+
if (parts.length < 5 || parts.some(isNaN)) return null;
|
|
186
|
+
return {
|
|
187
|
+
page: parts[0],
|
|
188
|
+
x: parts[1],
|
|
189
|
+
y: parts[2],
|
|
190
|
+
width: parts[3],
|
|
191
|
+
height: parts[4]
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
function formatTable($, tableEl) {
|
|
195
|
+
const rows = [];
|
|
196
|
+
tableEl.find("row").each((_i, row) => {
|
|
197
|
+
const cells = [];
|
|
198
|
+
$(row).find("cell").each((_j, cell) => {
|
|
199
|
+
cells.push($(cell).text().trim());
|
|
200
|
+
});
|
|
201
|
+
rows.push(cells);
|
|
202
|
+
});
|
|
203
|
+
if (rows.length === 0) return tableEl.text().trim();
|
|
204
|
+
const colWidths = [];
|
|
205
|
+
for (const row of rows) {
|
|
206
|
+
for (let i = 0; i < row.length; i++) {
|
|
207
|
+
colWidths[i] = Math.max(colWidths[i] || 0, (row[i] || "").length);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const lines = [];
|
|
211
|
+
for (let r = 0; r < rows.length; r++) {
|
|
212
|
+
const row = rows[r];
|
|
213
|
+
const line = row.map((cell, c) => cell.padEnd(colWidths[c] || 0)).join(" | ");
|
|
214
|
+
lines.push(line);
|
|
215
|
+
if (r === 0) {
|
|
216
|
+
lines.push(colWidths.map((w) => "-".repeat(w)).join("-+-"));
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return lines.join("\n");
|
|
220
|
+
}
|
|
221
|
+
function formatBiblStruct($, bib) {
|
|
222
|
+
const authors = [];
|
|
223
|
+
bib.find("author persName").each((_i, el) => {
|
|
224
|
+
const forenames = [];
|
|
225
|
+
$(el).find("forename").each((_j, fn) => {
|
|
226
|
+
forenames.push($(fn).text().trim());
|
|
227
|
+
});
|
|
228
|
+
const surname = $(el).find("surname").text().trim();
|
|
229
|
+
const name = [...forenames, surname].filter(Boolean).join(" ");
|
|
230
|
+
if (name) authors.push(name);
|
|
231
|
+
});
|
|
232
|
+
const title = bib.find("analytic title").text().trim() || bib.find("monogr title").text().trim();
|
|
233
|
+
const journal = bib.find('monogr title[level="j"]').text().trim();
|
|
234
|
+
const volume = bib.find('biblScope[unit="volume"]').text().trim();
|
|
235
|
+
const pages = bib.find('biblScope[unit="page"]');
|
|
236
|
+
const from = pages.attr("from") || "";
|
|
237
|
+
const to = pages.attr("to") || "";
|
|
238
|
+
const pageStr = from && to ? `${from}-${to}` : pages.text().trim();
|
|
239
|
+
const year = bib.find('date[type="published"]').attr("when") || bib.find('date[type="published"]').text().trim();
|
|
240
|
+
const doi = bib.find('idno[type="DOI"]').text().trim();
|
|
241
|
+
const parts = [];
|
|
242
|
+
if (authors.length) parts.push(authors.join(", "));
|
|
243
|
+
if (title) parts.push(`"${title}"`);
|
|
244
|
+
if (journal) {
|
|
245
|
+
let jPart = journal;
|
|
246
|
+
if (volume) jPart += ` ${volume}`;
|
|
247
|
+
if (pageStr) jPart += `: ${pageStr}`;
|
|
248
|
+
parts.push(jPart);
|
|
249
|
+
}
|
|
250
|
+
if (year) parts.push(`(${year})`);
|
|
251
|
+
let result = parts.join(". ");
|
|
252
|
+
if (doi) result += `. DOI: ${doi}`;
|
|
253
|
+
return result || bib.text().trim().replace(/\s+/g, " ");
|
|
254
|
+
}
|
|
255
|
+
function inferHeadingLevel(_$, headEl) {
|
|
256
|
+
var _a, _b;
|
|
257
|
+
let depth = 0;
|
|
258
|
+
let parent = headEl.parent();
|
|
259
|
+
while (parent.length) {
|
|
260
|
+
const tag = (_b = (_a = parent[0]) == null ? void 0 : _a.name) == null ? void 0 : _b.toLowerCase();
|
|
261
|
+
if (tag === "body" || tag === "text") break;
|
|
262
|
+
if (tag === "div") depth++;
|
|
263
|
+
parent = parent.parent();
|
|
264
|
+
}
|
|
265
|
+
return Math.min(depth + 1, 6);
|
|
266
|
+
}
|
|
267
|
+
async function processBodyElements($, bodyEl, opts) {
|
|
268
|
+
const elements = [];
|
|
269
|
+
async function walkChildren(container) {
|
|
270
|
+
var _a;
|
|
271
|
+
for (const child of container.children().toArray()) {
|
|
272
|
+
const $child = $(child);
|
|
273
|
+
const tagName = ((_a = child.name) == null ? void 0 : _a.toLowerCase()) || "";
|
|
274
|
+
if (tagName === "div") {
|
|
275
|
+
await walkChildren($child);
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
if (tagName === "head") {
|
|
279
|
+
const text = $child.text().trim();
|
|
280
|
+
if (!text) continue;
|
|
281
|
+
const level = inferHeadingLevel($, $child);
|
|
282
|
+
const heading = makeHeading(text);
|
|
283
|
+
elements.push({ type: "heading", level, blockNode: heading });
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
if (tagName === "p") {
|
|
287
|
+
const { text, annotations } = parseInlineContent($, $child);
|
|
288
|
+
if (!text) continue;
|
|
289
|
+
elements.push({
|
|
290
|
+
type: "content",
|
|
291
|
+
blockNode: makeParagraph(text, annotations)
|
|
292
|
+
});
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
if (tagName === "formula") {
|
|
296
|
+
const label = $child.find("label").text().trim();
|
|
297
|
+
$child.find("label").remove();
|
|
298
|
+
let formulaText = $child.text().trim();
|
|
299
|
+
if (label) formulaText = `${formulaText} (${label})`;
|
|
300
|
+
if (formulaText) {
|
|
301
|
+
elements.push({
|
|
302
|
+
type: "content",
|
|
303
|
+
blockNode: makeMath(formulaText)
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
if (tagName === "figure") {
|
|
309
|
+
const figType = $child.attr("type");
|
|
310
|
+
if (figType === "table") {
|
|
311
|
+
const label2 = $child.find("head").text().trim();
|
|
312
|
+
const caption2 = $child.find("figDesc").text().trim();
|
|
313
|
+
const tableEl = $child.find("table");
|
|
314
|
+
let tableText = "";
|
|
315
|
+
if (tableEl.length) {
|
|
316
|
+
tableText = formatTable($, tableEl);
|
|
317
|
+
} else {
|
|
318
|
+
tableText = $child.text().trim();
|
|
319
|
+
}
|
|
320
|
+
const header = [label2, caption2].filter(Boolean).join(": ");
|
|
321
|
+
const fullText = header ? `${header}
|
|
322
|
+
|
|
323
|
+
${tableText}` : tableText;
|
|
324
|
+
if (fullText) {
|
|
325
|
+
elements.push({
|
|
326
|
+
type: "content",
|
|
327
|
+
blockNode: makeCode(fullText)
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
const figId = $child.attr("xml:id") || "";
|
|
333
|
+
const label = $child.find("label").text().trim();
|
|
334
|
+
const caption = $child.find("figDesc").text().trim();
|
|
335
|
+
const coordsStr = $child.attr("coords");
|
|
336
|
+
const coords = parseCoords(coordsStr);
|
|
337
|
+
const figureInfo = { id: figId, label, caption, coords };
|
|
338
|
+
let imageLink = null;
|
|
339
|
+
if (opts.extractFigureImage && coords) {
|
|
340
|
+
imageLink = await opts.extractFigureImage(figureInfo);
|
|
341
|
+
}
|
|
342
|
+
if (imageLink) {
|
|
343
|
+
const captionText = [label ? `Figure ${label}` : "", caption].filter(Boolean).join(": ");
|
|
344
|
+
elements.push({
|
|
345
|
+
type: "content",
|
|
346
|
+
blockNode: makeImage(imageLink, captionText)
|
|
347
|
+
});
|
|
348
|
+
} else {
|
|
349
|
+
const captionText = [label ? `Figure ${label}` : "Figure", caption].filter(Boolean).join(": ");
|
|
350
|
+
if (captionText) {
|
|
351
|
+
elements.push({
|
|
352
|
+
type: "content",
|
|
353
|
+
blockNode: makeParagraph(captionText, [
|
|
354
|
+
{
|
|
355
|
+
type: "Italic",
|
|
356
|
+
starts: [0],
|
|
357
|
+
ends: [codePointLength(captionText)]
|
|
358
|
+
}
|
|
359
|
+
])
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
if (tagName === "list") {
|
|
366
|
+
const items = [];
|
|
367
|
+
$child.find("item").each((_i, item) => {
|
|
368
|
+
const { text, annotations } = parseInlineContent($, $(item));
|
|
369
|
+
if (text) items.push(makeParagraph(text, annotations));
|
|
370
|
+
});
|
|
371
|
+
if (items.length) {
|
|
372
|
+
elements.push({
|
|
373
|
+
type: "content",
|
|
374
|
+
blockNode: makeList(items, "Unordered")
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
if (tagName === "note") {
|
|
380
|
+
const { text, annotations } = parseInlineContent($, $child);
|
|
381
|
+
if (text) {
|
|
382
|
+
annotations.push({
|
|
383
|
+
type: "Italic",
|
|
384
|
+
starts: [0],
|
|
385
|
+
ends: [codePointLength(text)]
|
|
386
|
+
});
|
|
387
|
+
elements.push({
|
|
388
|
+
type: "content",
|
|
389
|
+
blockNode: makeParagraph(text, annotations)
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
await walkChildren(bodyEl);
|
|
397
|
+
return elements;
|
|
398
|
+
}
|
|
399
|
+
function processReferences($) {
|
|
400
|
+
const elements = [];
|
|
401
|
+
const biblStructs = $("back listBibl biblStruct");
|
|
402
|
+
if (!biblStructs.length) return elements;
|
|
403
|
+
elements.push({
|
|
404
|
+
type: "heading",
|
|
405
|
+
level: 2,
|
|
406
|
+
blockNode: makeHeading("References")
|
|
407
|
+
});
|
|
408
|
+
biblStructs.each((_i, bib) => {
|
|
409
|
+
const formatted = formatBiblStruct($, $(bib));
|
|
410
|
+
if (formatted) {
|
|
411
|
+
elements.push({
|
|
412
|
+
type: "content",
|
|
413
|
+
blockNode: makeParagraph(`[${_i + 1}] ${formatted}`)
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
});
|
|
417
|
+
return elements;
|
|
418
|
+
}
|
|
419
|
+
function buildHierarchy(elements) {
|
|
420
|
+
var _a;
|
|
421
|
+
const blocks = [];
|
|
422
|
+
const headingStack = [];
|
|
423
|
+
for (const element of elements) {
|
|
424
|
+
if (element.type === "heading" && element.level && element.blockNode) {
|
|
425
|
+
while (headingStack.length > 0 && (((_a = headingStack[headingStack.length - 1]) == null ? void 0 : _a.level) ?? 0) >= element.level) {
|
|
426
|
+
headingStack.pop();
|
|
427
|
+
}
|
|
428
|
+
if (headingStack.length === 0) {
|
|
429
|
+
blocks.push(element.blockNode);
|
|
430
|
+
} else {
|
|
431
|
+
const parent = headingStack[headingStack.length - 1].blockNode;
|
|
432
|
+
if (!parent.children) parent.children = [];
|
|
433
|
+
parent.children.push(element.blockNode);
|
|
434
|
+
}
|
|
435
|
+
headingStack.push({ level: element.level, blockNode: element.blockNode });
|
|
436
|
+
} else if (element.type === "content" && element.blockNode) {
|
|
437
|
+
if (headingStack.length === 0) {
|
|
438
|
+
blocks.push(element.blockNode);
|
|
439
|
+
} else {
|
|
440
|
+
const parent = headingStack[headingStack.length - 1].blockNode;
|
|
441
|
+
if (!parent.children) parent.children = [];
|
|
442
|
+
parent.children.push(element.blockNode);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
return blocks;
|
|
447
|
+
}
|
|
448
|
+
async function teiToBlocks(teiXml, opts = {}) {
|
|
449
|
+
const { load } = await import("cheerio");
|
|
450
|
+
const $ = load(teiXml, { xml: true });
|
|
451
|
+
const title = extractTitle($);
|
|
452
|
+
const displayAuthor = extractAuthors($);
|
|
453
|
+
const summary = extractAbstract($);
|
|
454
|
+
const displayPublishTime = extractDate($);
|
|
455
|
+
const metadata = {
|
|
456
|
+
name: title,
|
|
457
|
+
...displayAuthor ? { displayAuthor } : {},
|
|
458
|
+
...summary ? { summary } : {},
|
|
459
|
+
...displayPublishTime ? { displayPublishTime } : {}
|
|
460
|
+
};
|
|
461
|
+
const allElements = [];
|
|
462
|
+
const abstractEl = $("teiHeader profileDesc abstract");
|
|
463
|
+
if (abstractEl.length) {
|
|
464
|
+
allElements.push({
|
|
465
|
+
type: "heading",
|
|
466
|
+
level: 2,
|
|
467
|
+
blockNode: makeHeading("Abstract")
|
|
468
|
+
});
|
|
469
|
+
abstractEl.find("p").each((_i, p) => {
|
|
470
|
+
const { text, annotations } = parseInlineContent($, $(p));
|
|
471
|
+
if (text) {
|
|
472
|
+
allElements.push({
|
|
473
|
+
type: "content",
|
|
474
|
+
blockNode: makeParagraph(text, annotations)
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
if (!abstractEl.find("p").length) {
|
|
479
|
+
const absText = abstractEl.text().trim();
|
|
480
|
+
if (absText) {
|
|
481
|
+
allElements.push({
|
|
482
|
+
type: "content",
|
|
483
|
+
blockNode: makeParagraph(absText)
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
const body = $("text body");
|
|
489
|
+
if (body.length) {
|
|
490
|
+
const bodyElements = await processBodyElements($, body, opts);
|
|
491
|
+
allElements.push(...bodyElements);
|
|
492
|
+
}
|
|
493
|
+
const ack = $('back div[type="acknowledgement"], back div[type="acknowledgment"]');
|
|
494
|
+
if (ack.length) {
|
|
495
|
+
allElements.push({
|
|
496
|
+
type: "heading",
|
|
497
|
+
level: 2,
|
|
498
|
+
blockNode: makeHeading("Acknowledgements")
|
|
499
|
+
});
|
|
500
|
+
ack.find("p").each((_i, p) => {
|
|
501
|
+
const { text, annotations } = parseInlineContent($, $(p));
|
|
502
|
+
if (text) {
|
|
503
|
+
allElements.push({
|
|
504
|
+
type: "content",
|
|
505
|
+
blockNode: makeParagraph(text, annotations)
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
allElements.push(...processReferences($));
|
|
511
|
+
const blocks = buildHierarchy(allElements);
|
|
512
|
+
return { metadata, blocks };
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
export {
|
|
516
|
+
teiToBlocks
|
|
517
|
+
};
|