@seed-hypermedia/client 0.0.81 → 0.0.83

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.
Files changed (38) hide show
  1. package/dist/auto-link.d.ts +3 -4
  2. package/dist/auto-link.mjs +3 -3
  3. package/dist/block-diff.mjs +1 -1
  4. package/dist/blocks-to-markdown.d.ts +16 -11
  5. package/dist/blocks-to-markdown.mjs +2 -2
  6. package/dist/capability.mjs +2 -2
  7. package/dist/{chunk-MZENVC36.mjs → chunk-2GOQZ3DV.mjs} +1 -1
  8. package/dist/{chunk-OZKQOI2X.mjs → chunk-3KLY2B27.mjs} +51 -4
  9. package/dist/{chunk-D2LTD2KZ.mjs → chunk-4VD46ULS.mjs} +1 -1
  10. package/dist/{chunk-C42YWLL2.mjs → chunk-CRMBYTZY.mjs} +1 -1
  11. package/dist/{chunk-2OMSCQ56.mjs → chunk-E4PGX55A.mjs} +1 -1
  12. package/dist/{chunk-YH52VFVR.mjs → chunk-E6YSFT63.mjs} +304 -185
  13. package/dist/{chunk-IREIRKVP.mjs → chunk-IOXGTLKJ.mjs} +1 -1
  14. package/dist/{chunk-MZUGCZJ6.mjs → chunk-KMQYEO46.mjs} +38 -11
  15. package/dist/{chunk-LCKCG574.mjs → chunk-NVWWEHIS.mjs} +1 -1
  16. package/dist/{chunk-RCVLNXG6.mjs → chunk-QBYF2PVS.mjs} +1 -1
  17. package/dist/{chunk-55OXHA27.mjs → chunk-SIYJHDMJ.mjs} +4 -2
  18. package/dist/{chunk-WGOIXGRU.mjs → chunk-TUSTJXWH.mjs} +3 -3
  19. package/dist/{chunk-BO2QSSIU.mjs → chunk-V2UNLCDS.mjs} +1 -1
  20. package/dist/{chunk-AIQJ6BI3.mjs → chunk-WJRXPYFE.mjs} +1 -1
  21. package/dist/chunk-XB6E7EVC.mjs +872 -0
  22. package/dist/client.mjs +2 -2
  23. package/dist/comment.mjs +2 -2
  24. package/dist/document-state.mjs +2 -2
  25. package/dist/editorblock-to-hmblock.mjs +2 -2
  26. package/dist/hm-resolver.mjs +2 -2
  27. package/dist/hm-types.d.ts +216 -114
  28. package/dist/hm-types.mjs +5 -1
  29. package/dist/hmblock-to-editorblock.mjs +2 -2
  30. package/dist/index.mjs +15 -15
  31. package/dist/markdown-to-blocks.d.ts +27 -32
  32. package/dist/markdown-to-blocks.mjs +3 -1
  33. package/dist/pdf-to-blocks.mjs +3 -3
  34. package/dist/resource-read.mjs +4 -4
  35. package/dist/tei-to-blocks.mjs +2 -2
  36. package/dist/unicode.mjs +1 -1
  37. package/package.json +1 -1
  38. package/dist/chunk-H7L4TI6X.mjs +0 -705
@@ -1,705 +0,0 @@
1
- // src/markdown-to-blocks.ts
2
- import { parse as parseYaml } from "yaml";
3
- var ID_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
4
- function generateBlockId() {
5
- let id = "";
6
- for (let i = 0; i < 8; i++) {
7
- id += ID_CHARS[Math.floor(Math.random() * ID_CHARS.length)];
8
- }
9
- return id;
10
- }
11
- function parseInlineFormatting(raw) {
12
- const annotations = [];
13
- let text = "";
14
- let i = 0;
15
- while (i < raw.length) {
16
- if (raw[i] === "\\" && i + 1 < raw.length) {
17
- text += raw[i + 1];
18
- i += 2;
19
- continue;
20
- }
21
- if (raw[i] === "!" && i + 1 < raw.length && raw[i + 1] === "[") {
22
- const closeBracket = findClosingBracket(raw, i + 1);
23
- if (closeBracket !== -1 && closeBracket + 1 < raw.length && raw[closeBracket + 1] === "(") {
24
- const closeParen = raw.indexOf(")", closeBracket + 2);
25
- if (closeParen !== -1) {
26
- const altText = raw.slice(i + 2, closeBracket);
27
- if (altText) {
28
- text += altText;
29
- }
30
- i = closeParen + 1;
31
- continue;
32
- }
33
- }
34
- }
35
- if (raw[i] === "<") {
36
- const end = raw.indexOf(">", i + 1);
37
- if (end !== -1) {
38
- const inner = raw.slice(i + 1, end);
39
- if (/^[a-zA-Z][a-zA-Z0-9+.-]*:\S*$/.test(inner) && !/[\s<>]/.test(inner)) {
40
- const start = text.length;
41
- if (inner.startsWith("hm://")) {
42
- text += "\uFFFC";
43
- annotations.push({
44
- type: "Embed",
45
- starts: [start],
46
- ends: [text.length],
47
- link: inner
48
- });
49
- } else {
50
- text += inner;
51
- annotations.push({
52
- type: "Link",
53
- starts: [start],
54
- ends: [text.length],
55
- link: inner
56
- });
57
- }
58
- i = end + 1;
59
- continue;
60
- }
61
- }
62
- }
63
- if (raw[i] === "[") {
64
- const closeBracket = findClosingBracket(raw, i);
65
- if (closeBracket !== -1 && closeBracket + 1 < raw.length && raw[closeBracket + 1] === "(") {
66
- const closeParen = raw.indexOf(")", closeBracket + 2);
67
- if (closeParen !== -1) {
68
- const linkText = raw.slice(i + 1, closeBracket);
69
- const url = raw.slice(closeBracket + 2, closeParen);
70
- const parsed = parseInlineFormatting(linkText);
71
- const start = text.length;
72
- text += parsed.text;
73
- const end = text.length;
74
- for (const ann of parsed.annotations) {
75
- annotations.push({
76
- ...ann,
77
- starts: ann.starts.map((s) => s + start),
78
- ends: ann.ends.map((e) => e + start)
79
- });
80
- }
81
- annotations.push({
82
- type: "Link",
83
- starts: [start],
84
- ends: [end],
85
- link: url
86
- });
87
- i = closeParen + 1;
88
- continue;
89
- }
90
- }
91
- }
92
- if (raw[i] === "*" && raw[i + 1] === "*") {
93
- const end = raw.indexOf("**", i + 2);
94
- if (end !== -1) {
95
- const inner = raw.slice(i + 2, end);
96
- const parsed = parseInlineFormatting(inner);
97
- const start = text.length;
98
- text += parsed.text;
99
- for (const ann of parsed.annotations) {
100
- annotations.push({
101
- ...ann,
102
- starts: ann.starts.map((s) => s + start),
103
- ends: ann.ends.map((e) => e + start)
104
- });
105
- }
106
- annotations.push({
107
- type: "Bold",
108
- starts: [start],
109
- ends: [text.length]
110
- });
111
- i = end + 2;
112
- continue;
113
- }
114
- }
115
- if (raw[i] === "*" && raw[i + 1] !== "*") {
116
- const end = findSingleDelimiter(raw, "*", i + 1);
117
- if (end !== -1) {
118
- const inner = raw.slice(i + 1, end);
119
- const parsed = parseInlineFormatting(inner);
120
- const start = text.length;
121
- text += parsed.text;
122
- for (const ann of parsed.annotations) {
123
- annotations.push({
124
- ...ann,
125
- starts: ann.starts.map((s) => s + start),
126
- ends: ann.ends.map((e) => e + start)
127
- });
128
- }
129
- annotations.push({
130
- type: "Italic",
131
- starts: [start],
132
- ends: [text.length]
133
- });
134
- i = end + 1;
135
- continue;
136
- }
137
- }
138
- if (raw[i] === "`") {
139
- const end = raw.indexOf("`", i + 1);
140
- if (end !== -1) {
141
- const start = text.length;
142
- text += raw.slice(i + 1, end);
143
- annotations.push({
144
- type: "Code",
145
- starts: [start],
146
- ends: [text.length]
147
- });
148
- i = end + 1;
149
- continue;
150
- }
151
- }
152
- text += raw[i];
153
- i++;
154
- }
155
- return { text, annotations };
156
- }
157
- function findClosingBracket(s, openPos) {
158
- let depth = 0;
159
- for (let i = openPos; i < s.length; i++) {
160
- if (s[i] === "[") depth++;
161
- if (s[i] === "]") {
162
- depth--;
163
- if (depth === 0) return i;
164
- }
165
- }
166
- return -1;
167
- }
168
- function findSingleDelimiter(s, delim, start) {
169
- for (let i = start; i < s.length; i++) {
170
- if (s[i] === delim && s[i + 1] !== delim && (i === 0 || s[i - 1] !== delim)) {
171
- return i;
172
- }
173
- }
174
- return -1;
175
- }
176
- var BLOCK_ID_RE = /\s*<!--\s*id:([A-Za-z0-9_-]+)\s*-->\s*$/;
177
- function stripBlockId(s) {
178
- const m = s.match(BLOCK_ID_RE);
179
- if (m) {
180
- return { text: s.slice(0, m.index).trimEnd(), id: m[1] };
181
- }
182
- return { text: s };
183
- }
184
- var STANDALONE_ID_RE = /^\s*<!--\s*id:([A-Za-z0-9_-]+)\s*-->\s*$/;
185
- function tokenize(markdown) {
186
- const lines = markdown.split("\n");
187
- const blocks = [];
188
- let i = 0;
189
- let pendingContainerId;
190
- while (i < lines.length) {
191
- const line = lines[i];
192
- if (line.trim() === "") {
193
- i++;
194
- continue;
195
- }
196
- const standaloneMatch = line.match(STANDALONE_ID_RE);
197
- if (standaloneMatch) {
198
- pendingContainerId = standaloneMatch[1];
199
- i++;
200
- continue;
201
- }
202
- if (line.trim().startsWith("```")) {
203
- const fenceContent = line.trim().slice(3).trim();
204
- const { text: language, id: id2 } = stripBlockId(fenceContent);
205
- const codeLines = [];
206
- i++;
207
- while (i < lines.length && !lines[i].trim().startsWith("```")) {
208
- codeLines.push(lines[i]);
209
- i++;
210
- }
211
- i++;
212
- blocks.push({ kind: "code", language, text: codeLines.join("\n"), id: id2 });
213
- pendingContainerId = void 0;
214
- continue;
215
- }
216
- if (line.trim().startsWith("$$")) {
217
- const fenceContent = line.trim().slice(2).trim();
218
- const { id: id2 } = stripBlockId(fenceContent);
219
- const mathLines = [];
220
- i++;
221
- while (i < lines.length && !lines[i].trim().startsWith("$$")) {
222
- mathLines.push(lines[i]);
223
- i++;
224
- }
225
- i++;
226
- blocks.push({ kind: "math", text: mathLines.join("\n"), id: id2 });
227
- pendingContainerId = void 0;
228
- continue;
229
- }
230
- const headingMatch = line.trim().match(/^(#{1,6})\s+(.+)$/);
231
- if (headingMatch) {
232
- const { text, id: id2 } = stripBlockId(headingMatch[2]);
233
- blocks.push({ kind: "heading", level: headingMatch[1].length, text, id: id2 });
234
- i++;
235
- pendingContainerId = void 0;
236
- continue;
237
- }
238
- const imageMatch = line.trim().match(/^!\[([^\]]*)\]\(([^)]+)\)(.*)$/);
239
- if (imageMatch) {
240
- let url = imageMatch[2];
241
- const trailing = imageMatch[3] || "";
242
- const { id: id2 } = stripBlockId(trailing);
243
- if (url && !url.match(/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//)) {
244
- url = `file://${url}`;
245
- }
246
- blocks.push({ kind: "image", alt: imageMatch[1], url, id: id2 });
247
- i++;
248
- pendingContainerId = void 0;
249
- continue;
250
- }
251
- if (line.trim().startsWith("|")) {
252
- const tableLines = [];
253
- while (i < lines.length && lines[i].trim().startsWith("|")) {
254
- tableLines.push(lines[i]);
255
- i++;
256
- }
257
- blocks.push({ kind: "table", lines: tableLines, containerId: pendingContainerId });
258
- pendingContainerId = void 0;
259
- continue;
260
- }
261
- if (/^[-*+]\s+/.test(line.trim())) {
262
- const items = [];
263
- while (i < lines.length && /^[-*+]\s+/.test(lines[i].trim())) {
264
- const raw = lines[i].trim().replace(/^[-*+]\s+/, "");
265
- const { text, id: id2 } = stripBlockId(raw);
266
- items.push({ text, id: id2 });
267
- i++;
268
- }
269
- blocks.push({ kind: "ul", items, containerId: pendingContainerId });
270
- pendingContainerId = void 0;
271
- continue;
272
- }
273
- if (/^\d+\.\s+/.test(line.trim())) {
274
- const items = [];
275
- while (i < lines.length && /^\d+\.\s+/.test(lines[i].trim())) {
276
- const raw = lines[i].trim().replace(/^\d+\.\s+/, "");
277
- const { text, id: id2 } = stripBlockId(raw);
278
- items.push({ text, id: id2 });
279
- i++;
280
- }
281
- blocks.push({ kind: "ol", items, containerId: pendingContainerId });
282
- pendingContainerId = void 0;
283
- continue;
284
- }
285
- const paraLines = [];
286
- while (i < lines.length && lines[i].trim() !== "" && !lines[i].trim().startsWith("```") && !lines[i].trim().startsWith("$$") && !lines[i].trim().startsWith("#") && !lines[i].trim().startsWith("|") && !lines[i].match(STANDALONE_ID_RE) && !/^!\[([^\]]*)\]\(([^)]+)\)/.test(lines[i].trim()) && !/^[-*+]\s+/.test(lines[i].trim()) && !/^\d+\.\s+/.test(lines[i].trim())) {
287
- paraLines.push(lines[i].trimStart());
288
- i++;
289
- }
290
- if (paraLines.length === 0) {
291
- paraLines.push(line.trimStart());
292
- i++;
293
- }
294
- const firstLine = paraLines[0];
295
- const { text: cleanFirst, id } = stripBlockId(firstLine);
296
- paraLines[0] = cleanFirst;
297
- blocks.push({ kind: "paragraph", text: paraLines.join("\n"), id });
298
- pendingContainerId = void 0;
299
- }
300
- return blocks;
301
- }
302
- function makeBlockNode(block, children = []) {
303
- return { block, children };
304
- }
305
- function createParagraphNode(rawText, id) {
306
- const { text, annotations } = parseInlineFormatting(rawText);
307
- return makeBlockNode({
308
- type: "Paragraph",
309
- id: id || generateBlockId(),
310
- text,
311
- annotations
312
- });
313
- }
314
- function createHeadingNode(rawText, id) {
315
- const { text, annotations } = parseInlineFormatting(rawText);
316
- return makeBlockNode(
317
- {
318
- type: "Heading",
319
- id: id || generateBlockId(),
320
- text,
321
- annotations,
322
- childrenType: "Group"
323
- },
324
- []
325
- );
326
- }
327
- function createCodeNode(text, language, id) {
328
- return makeBlockNode({
329
- type: "Code",
330
- id: id || generateBlockId(),
331
- text,
332
- annotations: [],
333
- language: language || void 0
334
- });
335
- }
336
- function createMathNode(text, id) {
337
- return makeBlockNode({
338
- type: "Math",
339
- id: id || generateBlockId(),
340
- text,
341
- annotations: []
342
- });
343
- }
344
- function createImageNode(alt, url, id) {
345
- return makeBlockNode({
346
- type: "Image",
347
- id: id || generateBlockId(),
348
- text: alt,
349
- annotations: [],
350
- link: url
351
- });
352
- }
353
- function createListContainerNode(items, listType, containerId) {
354
- return makeBlockNode(
355
- {
356
- type: "Paragraph",
357
- id: containerId || generateBlockId(),
358
- text: "",
359
- annotations: [],
360
- childrenType: listType
361
- },
362
- items
363
- );
364
- }
365
- var COL_ID_RE = /<!--\s*col:([A-Za-z0-9_-]+)\s*-->/;
366
- var CELL_ID_RE = /<!--\s*id:([A-Za-z0-9_-]+)\s*-->/;
367
- function splitTableRow(line) {
368
- const { text, id: afterPipeId } = stripBlockId(line.trim());
369
- let s = text.trim();
370
- if (s.startsWith("|")) s = s.slice(1);
371
- const rawCells = [];
372
- let cur = "";
373
- for (let i = 0; i < s.length; i++) {
374
- if (s[i] === "\\" && s[i + 1] === "|") {
375
- cur += "|";
376
- i++;
377
- } else if (s[i] === "|") {
378
- rawCells.push(cur.trim());
379
- cur = "";
380
- } else {
381
- cur += s[i];
382
- }
383
- }
384
- const last = cur.trim();
385
- if (last !== "" || rawCells.length === 0) rawCells.push(last);
386
- let rowId = afterPipeId;
387
- const cells = rawCells.map((raw) => {
388
- let cellText = raw;
389
- const idMatch = cellText.match(CELL_ID_RE);
390
- if (idMatch) {
391
- rowId = idMatch[1];
392
- cellText = cellText.replace(idMatch[0], "");
393
- }
394
- let colId;
395
- const colMatch = cellText.match(COL_ID_RE);
396
- if (colMatch) {
397
- colId = colMatch[1];
398
- cellText = cellText.replace(colMatch[0], "");
399
- }
400
- return { text: cellText.trim(), colId };
401
- });
402
- return { cells, id: rowId };
403
- }
404
- function isSeparatorRow(cells) {
405
- return cells.length > 0 && cells.every((c) => /^:?-+:?$/.test(c.text));
406
- }
407
- function createTableCellNode(rawText, columnId) {
408
- const { text, annotations } = parseInlineFormatting(rawText.replace(/<br\s*\/?>/gi, "\n"));
409
- return makeBlockNode({
410
- type: "Paragraph",
411
- id: generateBlockId(),
412
- text,
413
- annotations,
414
- attributes: { columnId }
415
- });
416
- }
417
- function createTableNode(lines, containerId) {
418
- var _a;
419
- const rawRows = lines.map(splitTableRow);
420
- let headerRow;
421
- let bodyRows;
422
- if (rawRows.length >= 2 && isSeparatorRow(rawRows[1].cells)) {
423
- headerRow = rawRows[0];
424
- bodyRows = rawRows.slice(2).filter((r) => !isSeparatorRow(r.cells));
425
- } else {
426
- bodyRows = rawRows.filter((r) => !isSeparatorRow(r.cells));
427
- }
428
- const headerCells = (headerRow == null ? void 0 : headerRow.cells) ?? [];
429
- const colCount = Math.max(headerCells.length, ...bodyRows.map((r) => r.cells.length), 0);
430
- const columnIds = [];
431
- for (let idx = 0; idx < colCount; idx++) {
432
- columnIds.push(((_a = headerCells[idx]) == null ? void 0 : _a.colId) || generateBlockId());
433
- }
434
- const columnNodes = columnIds.map(
435
- (id) => makeBlockNode({ type: "TableColumn", id, text: "", annotations: [] })
436
- );
437
- const rowNodes = [];
438
- const hasHeaderRow = headerCells.some((c) => c.text !== "");
439
- if (headerRow && hasHeaderRow) {
440
- const cells = columnIds.map((columnId, idx) => {
441
- var _a2;
442
- return createTableCellNode(((_a2 = headerCells[idx]) == null ? void 0 : _a2.text) ?? "", columnId);
443
- });
444
- rowNodes.push(
445
- makeBlockNode(
446
- {
447
- type: "TableRow",
448
- id: headerRow.id || generateBlockId(),
449
- text: "",
450
- annotations: [],
451
- attributes: { isHeader: true }
452
- },
453
- cells
454
- )
455
- );
456
- }
457
- for (const row of bodyRows) {
458
- const cells = columnIds.map((columnId, idx) => {
459
- var _a2;
460
- return createTableCellNode(((_a2 = row.cells[idx]) == null ? void 0 : _a2.text) ?? "", columnId);
461
- });
462
- rowNodes.push(
463
- makeBlockNode(
464
- {
465
- type: "TableRow",
466
- id: row.id || generateBlockId(),
467
- text: "",
468
- annotations: []
469
- },
470
- cells
471
- )
472
- );
473
- }
474
- return makeBlockNode(
475
- {
476
- type: "Table",
477
- id: containerId || generateBlockId(),
478
- text: "",
479
- annotations: []
480
- },
481
- [...columnNodes, ...rowNodes]
482
- );
483
- }
484
- function coerceString(value) {
485
- if (value == null) return void 0;
486
- if (value instanceof Date) {
487
- return value.toISOString().split("T")[0];
488
- }
489
- if (typeof value === "string") return value;
490
- return String(value);
491
- }
492
- var METADATA_STRING_KEYS = [
493
- "name",
494
- "summary",
495
- "displayAuthor",
496
- "displayPublishTime",
497
- "icon",
498
- "cover",
499
- "siteUrl",
500
- "layout",
501
- "seedExperimentalLogo",
502
- "importCategories",
503
- "importTags"
504
- ];
505
- var METADATA_BOOLEAN_KEYS = ["showOutline", "showActivity"];
506
- var METADATA_ENUM_KEYS = ["seedExperimentalHomeOrder", "contentWidth", "childrenType"];
507
- function parseFrontmatter(markdown) {
508
- const trimmed = markdown.trimStart();
509
- if (!trimmed.startsWith("---")) {
510
- return { content: markdown, metadata: {} };
511
- }
512
- const endIndex = trimmed.indexOf("\n---", 3);
513
- if (endIndex === -1) {
514
- return { content: markdown, metadata: {} };
515
- }
516
- const yamlBlock = trimmed.slice(3, endIndex).trim();
517
- const rest = trimmed.slice(endIndex + 4);
518
- const metadata = {};
519
- try {
520
- const parsed = parseYaml(yamlBlock);
521
- if (!parsed || typeof parsed !== "object") {
522
- return { content: rest, metadata };
523
- }
524
- for (const key of METADATA_STRING_KEYS) {
525
- const val = coerceString(parsed[key]);
526
- if (val !== void 0) metadata[key] = val;
527
- }
528
- if (!metadata.name && parsed["title"]) {
529
- const val = coerceString(parsed["title"]);
530
- if (val !== void 0) metadata.name = val;
531
- }
532
- for (const key of METADATA_BOOLEAN_KEYS) {
533
- if (parsed[key] != null) metadata[key] = Boolean(parsed[key]);
534
- }
535
- for (const key of METADATA_ENUM_KEYS) {
536
- const val = coerceString(parsed[key]);
537
- if (val !== void 0) metadata[key] = val;
538
- }
539
- if (parsed["theme"] && typeof parsed["theme"] === "object") {
540
- const themeInput = parsed["theme"];
541
- const theme = {};
542
- const hl = themeInput["headerLayout"];
543
- if (hl === "Center" || hl === "") theme.headerLayout = hl;
544
- if (Object.keys(theme).length > 0) metadata.theme = theme;
545
- }
546
- } catch {
547
- return { content: markdown, metadata: {} };
548
- }
549
- return { content: rest, metadata };
550
- }
551
- function parseMarkdown(markdown) {
552
- const { content, metadata } = parseFrontmatter(markdown);
553
- const tokens = tokenize(content);
554
- const rootNodes = [];
555
- const headingStack = [];
556
- function currentParent() {
557
- return headingStack.length > 0 ? headingStack[headingStack.length - 1].node : void 0;
558
- }
559
- function currentSiblings() {
560
- var _a;
561
- return ((_a = currentParent()) == null ? void 0 : _a.children) ?? rootNodes;
562
- }
563
- function unmergeHeadingList() {
564
- const parent = currentParent();
565
- if (!parent || parent.block.type !== "Heading") return;
566
- const ct = parent.block.childrenType;
567
- if (ct !== "Unordered" && ct !== "Ordered") return;
568
- parent.children = [createListContainerNode(parent.children, ct)];
569
- parent.block.childrenType = "Group";
570
- }
571
- function addToCurrentParent(node) {
572
- unmergeHeadingList();
573
- const nodes = Array.isArray(node) ? node : [node];
574
- currentSiblings().push(...nodes);
575
- }
576
- function findListTarget() {
577
- const parent = currentParent();
578
- const parentType = parent == null ? void 0 : parent.block.childrenType;
579
- if (parentType === "Unordered" || parentType === "Ordered") return void 0;
580
- const siblings = currentSiblings();
581
- const prev = siblings[siblings.length - 1];
582
- if (prev) {
583
- const canNest = prev.block.type === "Paragraph" && prev.block.text !== "" && prev.children.length === 0 && !prev.block.childrenType;
584
- return canNest ? prev : void 0;
585
- }
586
- return parent;
587
- }
588
- function addList(items, listType, containerId) {
589
- const itemNodes = items.map((item) => createParagraphNode(item.text, item.id));
590
- if (!containerId) {
591
- const target = findListTarget();
592
- if (target) {
593
- target.block.childrenType = listType;
594
- target.children.push(...itemNodes);
595
- return;
596
- }
597
- }
598
- addToCurrentParent(createListContainerNode(itemNodes, listType, containerId));
599
- }
600
- for (const token of tokens) {
601
- switch (token.kind) {
602
- case "heading": {
603
- const headingNode = createHeadingNode(token.text, token.id);
604
- while (headingStack.length > 0 && headingStack[headingStack.length - 1].level >= token.level) {
605
- headingStack.pop();
606
- }
607
- addToCurrentParent(headingNode);
608
- headingStack.push({ level: token.level, node: headingNode });
609
- break;
610
- }
611
- case "paragraph":
612
- addToCurrentParent(createParagraphNode(token.text, token.id));
613
- break;
614
- case "code":
615
- addToCurrentParent(createCodeNode(token.text, token.language, token.id));
616
- break;
617
- case "math":
618
- addToCurrentParent(createMathNode(token.text, token.id));
619
- break;
620
- case "image":
621
- addToCurrentParent(createImageNode(token.alt, token.url, token.id));
622
- break;
623
- case "table":
624
- addToCurrentParent(createTableNode(token.lines, token.containerId));
625
- break;
626
- case "ul":
627
- addList(token.items, "Unordered", token.containerId);
628
- break;
629
- case "ol":
630
- addList(token.items, "Ordered", token.containerId);
631
- break;
632
- }
633
- }
634
- return { tree: rootNodes, metadata };
635
- }
636
- function markdownBlockNodesToHMBlockNodes(nodes) {
637
- return nodes.map((node) => {
638
- const { block } = node;
639
- const attributes = { ...block.attributes };
640
- if (block.childrenType !== void 0) {
641
- attributes.childrenType = block.childrenType;
642
- }
643
- if (block.language !== void 0) {
644
- attributes.language = block.language;
645
- }
646
- const hmBlock = {
647
- type: block.type,
648
- id: block.id,
649
- text: block.text,
650
- annotations: block.annotations.map((a) => ({
651
- type: a.type,
652
- starts: a.starts,
653
- ends: a.ends,
654
- ...a.link !== void 0 ? { link: a.link } : {}
655
- })),
656
- attributes
657
- };
658
- if (block.link !== void 0) {
659
- hmBlock.link = block.link;
660
- }
661
- return {
662
- block: hmBlock,
663
- children: node.children.length > 0 ? markdownBlockNodesToHMBlockNodes(node.children) : void 0
664
- };
665
- });
666
- }
667
- function flattenToOperations(tree, parentId = "") {
668
- const ops = [];
669
- const blockIds = [];
670
- for (const node of tree) {
671
- const block = {
672
- ...node.block.attributes,
673
- type: node.block.type,
674
- id: node.block.id,
675
- text: node.block.text,
676
- annotations: node.block.annotations
677
- };
678
- if (node.block.language !== void 0) {
679
- block["language"] = node.block.language;
680
- }
681
- if (node.block.childrenType !== void 0) {
682
- block["childrenType"] = node.block.childrenType;
683
- }
684
- if (node.block.link !== void 0) {
685
- block["link"] = node.block.link;
686
- }
687
- ops.push({ type: "ReplaceBlock", block });
688
- blockIds.push(node.block.id);
689
- if (node.children.length > 0) {
690
- ops.push(...flattenToOperations(node.children, node.block.id));
691
- }
692
- }
693
- if (blockIds.length > 0) {
694
- ops.push({ type: "MoveBlocks", blocks: blockIds, parent: parentId });
695
- }
696
- return ops;
697
- }
698
-
699
- export {
700
- parseInlineFormatting,
701
- parseFrontmatter,
702
- parseMarkdown,
703
- markdownBlockNodesToHMBlockNodes,
704
- flattenToOperations
705
- };