@seed-hypermedia/client 0.0.131 → 0.0.133

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 (60) hide show
  1. package/dist/apps.mjs +12 -9
  2. package/dist/auto-link.mjs +5 -5
  3. package/dist/block-diff.d.ts +37 -0
  4. package/dist/block-diff.mjs +10 -1
  5. package/dist/block-edits.d.ts +33 -0
  6. package/dist/block-edits.mjs +9 -0
  7. package/dist/blocks-to-markdown.d.ts +21 -6
  8. package/dist/blocks-to-markdown.mjs +3 -3
  9. package/dist/capability.mjs +2 -2
  10. package/dist/change.d.ts +2 -0
  11. package/dist/change.mjs +1 -1
  12. package/dist/{chunk-SUVJSSHG.mjs → chunk-4ELQ4LJL.mjs} +68 -9
  13. package/dist/chunk-53PY4AWT.mjs +163 -0
  14. package/dist/{chunk-3KLY2B27.mjs → chunk-7FPJWOXB.mjs} +148 -1
  15. package/dist/{chunk-QU6XHE3U.mjs → chunk-DGNAMVGH.mjs} +5 -2
  16. package/dist/{chunk-2XNVA2GK.mjs → chunk-ETIDW7ZJ.mjs} +1 -1
  17. package/dist/{chunk-ANR3R5NH.mjs → chunk-FU7FIYHI.mjs} +1 -1
  18. package/dist/{chunk-HY4NWSHF.mjs → chunk-H2R4GYNV.mjs} +4 -3
  19. package/dist/{chunk-2LSZJ2IM.mjs → chunk-HL43SJUP.mjs} +1 -1
  20. package/dist/{chunk-JM4WXPLC.mjs → chunk-IFELP3LQ.mjs} +1 -1
  21. package/dist/{chunk-IXSAJRZA.mjs → chunk-JQKYF5NB.mjs} +3 -3
  22. package/dist/{chunk-IRP2ZGH3.mjs → chunk-KAZNQFXV.mjs} +12 -8
  23. package/dist/{chunk-O5MHN2EO.mjs → chunk-KM2NR4YA.mjs} +1 -0
  24. package/dist/{chunk-RPBGROOJ.mjs → chunk-LDCWJXIP.mjs} +1 -1
  25. package/dist/{chunk-JRJ6U6B2.mjs → chunk-NEUDKUBC.mjs} +1 -1
  26. package/dist/{chunk-D3PRDISB.mjs → chunk-O36UBGLF.mjs} +3 -3
  27. package/dist/{chunk-4JFL3SUQ.mjs → chunk-Q7CWMEBZ.mjs} +1 -1
  28. package/dist/{chunk-NWOYBZWP.mjs → chunk-QFIEZZYJ.mjs} +215 -330
  29. package/dist/{chunk-QLR4OWHG.mjs → chunk-QS6PIRUE.mjs} +1 -1
  30. package/dist/{chunk-QRSUQQTZ.mjs → chunk-T7SWLR32.mjs} +2 -2
  31. package/dist/{chunk-WD332LPF.mjs → chunk-VSMUWHIY.mjs} +1 -1
  32. package/dist/{chunk-APL23ACF.mjs → chunk-YRNPRWYG.mjs} +1 -1
  33. package/dist/{chunk-ETQZ6PRJ.mjs → chunk-Z7VKSY7P.mjs} +3 -3
  34. package/dist/client.d.ts +2 -0
  35. package/dist/client.mjs +3 -3
  36. package/dist/comment.mjs +2 -2
  37. package/dist/create-document.mjs +2 -2
  38. package/dist/document-state.mjs +2 -2
  39. package/dist/editorblock-to-hmblock.mjs +2 -2
  40. package/dist/hm-resolver.mjs +2 -2
  41. package/dist/hm-types.d.ts +9 -0
  42. package/dist/hm-types.mjs +1 -1
  43. package/dist/hmblock-to-editorblock.mjs +2 -2
  44. package/dist/index.d.ts +5 -3
  45. package/dist/index.mjs +64 -50
  46. package/dist/markdown-to-blocks.d.ts +25 -5
  47. package/dist/markdown-to-blocks.mjs +7 -3
  48. package/dist/pdf-to-blocks.mjs +3 -3
  49. package/dist/prepare-change.d.ts +6 -0
  50. package/dist/prepare-change.mjs +4 -4
  51. package/dist/resource-read.mjs +5 -5
  52. package/dist/schema-engine.mjs +2 -2
  53. package/dist/schema-registry.generated.mjs +1 -1
  54. package/dist/schema-resolve.mjs +4 -4
  55. package/dist/schema-subtypes.mjs +4 -4
  56. package/dist/schema-types.generated.d.ts +2 -0
  57. package/dist/signed-blob.mjs +2 -2
  58. package/dist/tei-to-blocks.mjs +2 -2
  59. package/dist/unicode.mjs +1 -1
  60. package/package.json +1 -1
package/dist/apps.mjs CHANGED
@@ -1,25 +1,28 @@
1
1
  import {
2
2
  documentUpdateBlobs
3
- } from "./chunk-IRP2ZGH3.mjs";
4
- import "./chunk-CBELM7KX.mjs";
5
- import "./chunk-O5MHN2EO.mjs";
6
- import "./chunk-PJ2RFT5A.mjs";
3
+ } from "./chunk-KAZNQFXV.mjs";
7
4
  import {
8
5
  resolveEditableDocument
9
- } from "./chunk-QLR4OWHG.mjs";
6
+ } from "./chunk-QS6PIRUE.mjs";
7
+ import "./chunk-CBELM7KX.mjs";
8
+ import "./chunk-KM2NR4YA.mjs";
9
+ import "./chunk-PJ2RFT5A.mjs";
10
+ import "./chunk-LVAHHQIK.mjs";
11
+ import "./chunk-DR3HJ6BV.mjs";
10
12
  import {
11
13
  computeReplaceOps,
12
14
  createBlocksMap,
13
15
  hmBlockNodeToBlockNode,
14
16
  toAPIBlockNode
15
- } from "./chunk-3KLY2B27.mjs";
16
- import "./chunk-LVAHHQIK.mjs";
17
- import "./chunk-DR3HJ6BV.mjs";
17
+ } from "./chunk-7FPJWOXB.mjs";
18
+ import "./chunk-4ELQ4LJL.mjs";
19
+ import "./chunk-QFIEZZYJ.mjs";
20
+ import "./chunk-Q7CWMEBZ.mjs";
18
21
  import {
19
22
  hmIdPathToEntityQueryPath,
20
23
  packHmId,
21
24
  unpackHmId
22
- } from "./chunk-QU6XHE3U.mjs";
25
+ } from "./chunk-DGNAMVGH.mjs";
23
26
  import "./chunk-AHQWDCHV.mjs";
24
27
  import "./chunk-7P6ASYW6.mjs";
25
28
 
@@ -4,15 +4,15 @@ import {
4
4
  documentContainsLinkToChild,
5
5
  documentHasSelfQuery,
6
6
  shouldAutoLinkParent
7
- } from "./chunk-D3PRDISB.mjs";
8
- import "./chunk-IRP2ZGH3.mjs";
7
+ } from "./chunk-O36UBGLF.mjs";
8
+ import "./chunk-KAZNQFXV.mjs";
9
+ import "./chunk-QS6PIRUE.mjs";
9
10
  import "./chunk-CBELM7KX.mjs";
10
- import "./chunk-O5MHN2EO.mjs";
11
+ import "./chunk-KM2NR4YA.mjs";
11
12
  import "./chunk-PJ2RFT5A.mjs";
12
- import "./chunk-QLR4OWHG.mjs";
13
13
  import "./chunk-LVAHHQIK.mjs";
14
14
  import "./chunk-DR3HJ6BV.mjs";
15
- import "./chunk-QU6XHE3U.mjs";
15
+ import "./chunk-DGNAMVGH.mjs";
16
16
  import "./chunk-AHQWDCHV.mjs";
17
17
  import "./chunk-7P6ASYW6.mjs";
18
18
  export {
@@ -16,6 +16,11 @@
16
16
  * re-derived from (row id, column id) against the old document, and
17
17
  * attributes markdown cannot express (column width, header column) are
18
18
  * carried over from the old blocks.
19
+ *
20
+ * A body written as markdown gets one more pass first (markdownUpdateTree):
21
+ * a block whose markdown is what the stored block reads as is the stored
22
+ * block, so nothing the dialect spells differently is stored again, and a
23
+ * block that lost its id comment gets its id back.
19
24
  */
20
25
  import type { DocumentOperation } from './change';
21
26
  import type { HMBlockNode } from './hm-types';
@@ -105,4 +110,36 @@ export declare function toAPIBlockNode(node: HMBlockNode): APIBlockNode;
105
110
  * (columnId, isHeader, width, …) are preserved in `block.attributes`.
106
111
  */
107
112
  export declare function hmBlockNodeToBlockNode(node: HMBlockNode): BlockNode;
113
+ /**
114
+ * The tree to diff against a stored document when its new body was written as markdown.
115
+ *
116
+ * Markdown spells some things in one way only: an annotation over several ranges comes back as one
117
+ * per range, a block with children gets an explicit `Group`, a mention gets its kind. A diff of
118
+ * the parsed blocks against the stored ones would store every such block again although its author
119
+ * changed nothing. So the stored document is written to markdown and parsed here, which says what
120
+ * each stored block reads as, and then:
121
+ *
122
+ * - a written block that reads the same as the stored block with its id is that block, as stored;
123
+ * - a written block with no stored id that reads the same as a stored block the body does not
124
+ * otherwise hold is that block: it gets the id back (the earliest such block in the document,
125
+ * when several read alike). Only blocks with text or a link are matched this way, and how
126
+ * their children are arranged does not count: markdown without id comments nests a list under
127
+ * the paragraph before it.
128
+ *
129
+ * Tables are rebound first (rebindTableIdentities). The result goes to computeReplaceOps. A body
130
+ * that is the document's own markdown, with or without its id comments, yields no operations.
131
+ */
132
+ export declare function markdownUpdateTree(oldContent: HMBlockNode[], written: BlockNode[]): BlockNode[];
133
+ /** What a content update does to a document, counted in blocks. */
134
+ export type ContentChange = {
135
+ /** Blocks the document holds now. */
136
+ blocks: number;
137
+ added: number;
138
+ rewritten: number;
139
+ /** Blocks that keep their id and end up under another parent or in another order. */
140
+ moved: number;
141
+ deleted: number;
142
+ };
143
+ /** Counts what `ops` (from computeReplaceOps over `newTree`) change in the stored document. */
144
+ export declare function describeContentChange(oldNodes: APIBlockNode[], newTree: BlockNode[], ops: DocumentOperation[]): ContentChange;
108
145
  export {};
@@ -1,16 +1,25 @@
1
1
  import {
2
2
  computeReplaceOps,
3
3
  createBlocksMap,
4
+ describeContentChange,
4
5
  hmBlockNodeToBlockNode,
6
+ markdownUpdateTree,
5
7
  matchBlockIds,
6
8
  rebindTableIdentities,
7
9
  toAPIBlockNode
8
- } from "./chunk-3KLY2B27.mjs";
10
+ } from "./chunk-7FPJWOXB.mjs";
11
+ import "./chunk-4ELQ4LJL.mjs";
12
+ import "./chunk-QFIEZZYJ.mjs";
13
+ import "./chunk-Q7CWMEBZ.mjs";
14
+ import "./chunk-DGNAMVGH.mjs";
15
+ import "./chunk-AHQWDCHV.mjs";
9
16
  import "./chunk-7P6ASYW6.mjs";
10
17
  export {
11
18
  computeReplaceOps,
12
19
  createBlocksMap,
20
+ describeContentChange,
13
21
  hmBlockNodeToBlockNode,
22
+ markdownUpdateTree,
14
23
  matchBlockIds,
15
24
  rebindTableIdentities,
16
25
  toAPIBlockNode
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Edits that name the blocks they change.
3
+ *
4
+ * Replacing a document's whole body to change one sentence costs the whole document, and every
5
+ * block left out of the new body is deleted. An edit list changes blocks by id instead and leaves
6
+ * the rest of the tree as it is stored:
7
+ *
8
+ * {replace: ID, content} one block's own content; its children stay
9
+ * {after | before | into: ID, content} new blocks beside a block, or as its last children
10
+ * {move: ID, after | before | into: ID} a block with its children, somewhere else
11
+ * {delete: ID | [ID, …]} a block with its children
12
+ *
13
+ * `content` is markdown in the dialect of `markdown-to-blocks.ts`. Edits apply in order, each to
14
+ * the result of the one before, and all or none: the first edit that cannot apply throws
15
+ * {@link BlockEditError} and the input tree is untouched.
16
+ */
17
+ import type { BlockNode } from './markdown-to-blocks';
18
+ /** An edit that cannot apply. `index` is its position in the list. */
19
+ export declare class BlockEditError extends Error {
20
+ index: number;
21
+ constructor(index: number, message: string);
22
+ }
23
+ export type BlockEditResult = {
24
+ /** The edited tree. Blocks no edit named are the same objects as in the input. */
25
+ tree: BlockNode[];
26
+ /** Every block an edit wrote: the replacements and the new blocks, with their children. */
27
+ written: BlockNode[];
28
+ };
29
+ /**
30
+ * Applies `edits` to `tree`. `parse` turns an edit's markdown into blocks (the caller decides what
31
+ * else that involves, such as resolving links).
32
+ */
33
+ export declare function applyBlockEdits(tree: BlockNode[], edits: unknown, parse: (markdown: string) => BlockNode[]): BlockEditResult;
@@ -0,0 +1,9 @@
1
+ import {
2
+ BlockEditError,
3
+ applyBlockEdits
4
+ } from "./chunk-53PY4AWT.mjs";
5
+ import "./chunk-7P6ASYW6.mjs";
6
+ export {
7
+ BlockEditError,
8
+ applyBlockEdits
9
+ };
@@ -8,10 +8,12 @@
8
8
  * - nesting by indentation, heading level by heading depth
9
9
  * - annotations as markdown markers, split at boundaries so they nest
10
10
  *
11
- * This module is purely synchronous and does NOT resolve embeds, mentions,
12
- * or queries over the network. For resolved output, see the
13
- * `documentToResolvedMarkdown` family below, which is a display format and
14
- * not round-trip safe.
11
+ * `blocksToMarkdown` is synchronous and does not touch the network. The
12
+ * `documentToResolvedMarkdown` family below is the same writer with what the
13
+ * blocks point at added for the reader: the name behind a mention, and the
14
+ * content of an embed or the results of a query between
15
+ * `<!-- resolved: … -->` and `<!-- /resolved -->`, which the parser skips.
16
+ * Both outputs parse back to the document they were written from.
15
17
  *
16
18
  * Round-trip: `parseMarkdown(blocksToMarkdown(doc))` reproduces `doc`, and
17
19
  * exporting that parse reproduces the same markdown. Use
@@ -22,6 +24,12 @@ import type { HMBlockNode, HMComment, HMDocument, HMMetadata } from './hm-types'
22
24
  export type BlocksToMarkdownOptions = {
23
25
  /** Format ipfs:// URLs as https gateway URLs. Default: true. */
24
26
  ipfsGateway?: boolean;
27
+ /**
28
+ * The name to print for an inline mention of `link`. When set, a mention is written
29
+ * `[@Name](hm://…)` instead of `<hm://…>`; the parser reads only the link, so any name
30
+ * (the empty string included) parses to the same mention.
31
+ */
32
+ mentionLabel?: (link: string, kind?: string) => string;
25
33
  };
26
34
  /**
27
35
  * Emit YAML frontmatter from HMMetadata. Every defined key is emitted,
@@ -64,12 +72,19 @@ export declare function parseDraftFilename(filename: string): {
64
72
  id: string;
65
73
  ext: string;
66
74
  };
67
- /** Options for Seed-client-backed markdown rendering that resolves embeds and mentions. */
75
+ /** Options for Seed-client-backed markdown rendering that resolves embeds, queries and mentions. */
68
76
  export type ResolvedMarkdownOptions = {
69
77
  client: Pick<SeedClient, 'request'>;
70
78
  maxDepth?: number;
79
+ /** Format ipfs:// URLs as https gateway URLs. Default: true. */
80
+ ipfsGateway?: boolean;
71
81
  };
72
- /** Convert a document to markdown, resolving inline mentions and block embeds through a Seed client. */
82
+ /**
83
+ * Convert a document to markdown with what its blocks point at shown beside them: mentions carry
84
+ * the name of their target, and each embed or query is followed by its content between
85
+ * `<!-- resolved: … -->` and `<!-- /resolved -->`. The blocks themselves are written exactly as
86
+ * `blocksToMarkdown` writes them, so the output parses back to the document.
87
+ */
73
88
  export declare function documentToResolvedMarkdown(doc: HMDocument, options: ResolvedMarkdownOptions): Promise<string>;
74
89
  /** Convert comment content to markdown, resolving inline mentions and block embeds through a Seed client. */
75
90
  export declare function commentToResolvedMarkdown(comment: HMComment, options: ResolvedMarkdownOptions): Promise<string>;
@@ -7,9 +7,9 @@ import {
7
7
  emitFrontmatter,
8
8
  parseDraftFilename,
9
9
  slugify
10
- } from "./chunk-NWOYBZWP.mjs";
11
- import "./chunk-4JFL3SUQ.mjs";
12
- import "./chunk-QU6XHE3U.mjs";
10
+ } from "./chunk-QFIEZZYJ.mjs";
11
+ import "./chunk-Q7CWMEBZ.mjs";
12
+ import "./chunk-DGNAMVGH.mjs";
13
13
  import "./chunk-AHQWDCHV.mjs";
14
14
  import "./chunk-7P6ASYW6.mjs";
15
15
  export {
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  createCapability,
3
3
  resolveCapability
4
- } from "./chunk-RPBGROOJ.mjs";
4
+ } from "./chunk-LDCWJXIP.mjs";
5
5
  import "./chunk-LVAHHQIK.mjs";
6
6
  import "./chunk-GBSWXJGU.mjs";
7
7
  import "./chunk-3TPCJCVO.mjs";
8
8
  import "./chunk-DR3HJ6BV.mjs";
9
- import "./chunk-QU6XHE3U.mjs";
9
+ import "./chunk-DGNAMVGH.mjs";
10
10
  import "./chunk-AHQWDCHV.mjs";
11
11
  import "./chunk-7P6ASYW6.mjs";
12
12
  export {
package/dist/change.d.ts CHANGED
@@ -38,6 +38,8 @@ export type CreateChangeOpsInput = {
38
38
  depth?: number;
39
39
  /** Timestamp (defaults to Date.now()) */
40
40
  ts?: bigint;
41
+ /** Optional human-readable description of the change, like a commit message. */
42
+ message?: string;
41
43
  };
42
44
  /**
43
45
  * Build unsigned change CBOR bytes from native ops.
package/dist/change.mjs CHANGED
@@ -8,7 +8,7 @@ import {
8
8
  signDocumentChange,
9
9
  signPreparedChange,
10
10
  visibilityToCbor
11
- } from "./chunk-O5MHN2EO.mjs";
11
+ } from "./chunk-KM2NR4YA.mjs";
12
12
  import "./chunk-PJ2RFT5A.mjs";
13
13
  import "./chunk-LVAHHQIK.mjs";
14
14
  import "./chunk-DR3HJ6BV.mjs";
@@ -417,6 +417,23 @@ function createTypedNode(visible, comment) {
417
417
  }
418
418
  var COL_ID_RE = /<!--\s*col:([A-Za-z0-9_-]+)(?:\s+attrs:(\{.*?\}))?\s*-->/;
419
419
  var CELL_ID_RE = /<!--\s*id:([A-Za-z0-9_-]+)\s*-->/;
420
+ function matchUnescaped(text, re) {
421
+ const all = new RegExp(re.source, "g");
422
+ for (let match = all.exec(text); match; match = all.exec(text)) {
423
+ let slashes = 0;
424
+ while (text[match.index - 1 - slashes] === "\\") slashes++;
425
+ if (slashes % 2 === 0) return match;
426
+ }
427
+ return null;
428
+ }
429
+ function trimCell(raw) {
430
+ const start = raw.replace(/^\s+/, "");
431
+ const end = start.replace(/\s+$/, "");
432
+ if (end.length === start.length) return end;
433
+ let slashes = 0;
434
+ while (end[end.length - 1 - slashes] === "\\") slashes++;
435
+ return slashes % 2 === 1 ? end + start[end.length] : end;
436
+ }
420
437
  function splitTableRow(line) {
421
438
  const { text, id: afterPipeId } = stripBlockId(line.trim());
422
439
  let s = text.trim();
@@ -428,25 +445,25 @@ function splitTableRow(line) {
428
445
  cur += "|";
429
446
  i++;
430
447
  } else if (s[i] === "|") {
431
- rawCells.push(cur.trim());
448
+ rawCells.push(cur);
432
449
  cur = "";
433
450
  } else {
434
451
  cur += s[i];
435
452
  }
436
453
  }
437
- const last = cur.trim();
438
- if (last !== "" || rawCells.length === 0) rawCells.push(last);
454
+ if (cur.trim() !== "" || rawCells.length === 0) rawCells.push(cur);
439
455
  let rowId = afterPipeId;
440
456
  const cells = rawCells.map((raw) => {
441
457
  let cellText = raw;
442
- const idMatch = cellText.match(CELL_ID_RE);
458
+ const cut = (match) => cellText.slice(0, match.index) + cellText.slice(match.index + match[0].length);
459
+ const idMatch = matchUnescaped(cellText, CELL_ID_RE);
443
460
  if (idMatch) {
444
461
  rowId = idMatch[1];
445
- cellText = cellText.replace(idMatch[0], "");
462
+ cellText = cut(idMatch);
446
463
  }
447
464
  let colId;
448
465
  let colAttrs;
449
- const colMatch = cellText.match(COL_ID_RE);
466
+ const colMatch = matchUnescaped(cellText, COL_ID_RE);
450
467
  if (colMatch) {
451
468
  colId = colMatch[1];
452
469
  if (colMatch[2]) {
@@ -455,9 +472,9 @@ function splitTableRow(line) {
455
472
  } catch {
456
473
  }
457
474
  }
458
- cellText = cellText.replace(colMatch[0], "");
475
+ cellText = cut(colMatch);
459
476
  }
460
- return { text: cellText.trim(), colId, colAttrs };
477
+ return { text: trimCell(cellText), colId, colAttrs };
461
478
  });
462
479
  return { cells, id: rowId };
463
480
  }
@@ -675,10 +692,50 @@ function collapseResolvedEmbeds(markdown) {
675
692
  }
676
693
  return out.join("\n");
677
694
  }
695
+ var RESOLVED_OPEN = /^\s*<!--\s*resolved(?::[^>]*?)?\s*-->\s*$/;
696
+ var RESOLVED_CLOSE = /^\s*<!--\s*\/resolved\s*-->\s*$/;
697
+ var MarkdownResolvedRangeError = class extends Error {
698
+ constructor(message) {
699
+ super(message);
700
+ this.name = "MarkdownResolvedRangeError";
701
+ }
702
+ };
703
+ function stripResolvedRanges(markdown, options = {}) {
704
+ if (!/<!--\s*resolved/.test(markdown)) return markdown;
705
+ const out = [];
706
+ let depth = 0;
707
+ let fence = null;
708
+ for (const line of markdown.split("\n")) {
709
+ const trimmed = line.trim();
710
+ if (fence === null) {
711
+ if (RESOLVED_OPEN.test(line)) {
712
+ depth++;
713
+ continue;
714
+ }
715
+ if (depth > 0 && RESOLVED_CLOSE.test(line)) {
716
+ depth--;
717
+ continue;
718
+ }
719
+ const start = trimmed.replace(/^(?:(?:[-*+]|\d+[.)]|>) )+/, "");
720
+ const opening = /^(`{3,})/.exec(start);
721
+ if (opening) fence = opening[1].length;
722
+ else if (start.startsWith("$$")) fence = "$$";
723
+ } else if (fence === "$$" ? trimmed === "$$" : /^`{3,}$/.test(trimmed) && trimmed.length >= fence) {
724
+ fence = null;
725
+ }
726
+ if (depth === 0) out.push(line);
727
+ }
728
+ if (depth > 0 && options.strict !== false) {
729
+ throw new MarkdownResolvedRangeError(
730
+ "A `<!-- resolved -->` range is never closed with `<!-- /resolved -->`. Those ranges show what an embed or a query points at and are not part of the document: remove the range, or restore its closing line."
731
+ );
732
+ }
733
+ return out.join("\n");
734
+ }
678
735
  function parseMarkdown(markdown, options = {}) {
679
736
  var _a;
680
737
  const { content, metadata } = parseFrontmatter(markdown, options);
681
- const lines = collapseResolvedEmbeds(content).split("\n");
738
+ const lines = collapseResolvedEmbeds(stripResolvedRanges(content, { strict: options.strictResolvedRanges })).split("\n");
682
739
  const infos = lines.map(analyzeLine);
683
740
  const root = { list: [], childIndent: 0 };
684
741
  const stack = [root];
@@ -969,6 +1026,8 @@ export {
969
1026
  MarkdownFrontmatterError,
970
1027
  parseFrontmatter,
971
1028
  collapseResolvedEmbeds,
1029
+ MarkdownResolvedRangeError,
1030
+ stripResolvedRanges,
972
1031
  parseMarkdown,
973
1032
  markdownBlockNodesToHMBlockNodes,
974
1033
  flattenToOperations
@@ -0,0 +1,163 @@
1
+ // src/block-edits.ts
2
+ var BlockEditError = class extends Error {
3
+ constructor(index, message) {
4
+ super(`Edit ${index + 1}: ${message}`);
5
+ this.index = index;
6
+ this.name = "BlockEditError";
7
+ }
8
+ };
9
+ var POSITIONS = ["after", "before", "into"];
10
+ var LIST_TYPES = /* @__PURE__ */ new Set(["Unordered", "Ordered", "Blockquote"]);
11
+ var FORMS = "{replace: ID, content}, {after | before | into: ID, content}, {move: ID, after | before | into: ID} or {delete: ID}";
12
+ function locate(nodes, id, parent) {
13
+ for (let index = 0; index < nodes.length; index++) {
14
+ const node = nodes[index];
15
+ if (node.block.id === id) return { node, siblings: nodes, index, parent };
16
+ const found = locate(node.children, id, node);
17
+ if (found) return found;
18
+ }
19
+ return void 0;
20
+ }
21
+ function copyTree(nodes) {
22
+ return nodes.map((node) => ({ block: node.block, children: copyTree(node.children) }));
23
+ }
24
+ function isBareList(node) {
25
+ return node.block.type === "Paragraph" && !node.block.text && !node.block.link && node.children.length > 0 && LIST_TYPES.has(node.block.childrenType || "");
26
+ }
27
+ function fragmentUnder(parent, nodes) {
28
+ const only = nodes.length === 1 ? nodes[0] : void 0;
29
+ if (only && parent && isBareList(only) && parent.block.childrenType === only.block.childrenType) return only.children;
30
+ return nodes;
31
+ }
32
+ function idOf(index, value, what) {
33
+ if (typeof value !== "string" || !value)
34
+ throw new BlockEditError(index, `${what} must be a block id (the X of an \`<!-- id:X -->\` comment).`);
35
+ return value;
36
+ }
37
+ function contentOf(index, edit) {
38
+ if (typeof edit.content !== "string") throw new BlockEditError(index, "`content` must be a markdown string.");
39
+ return edit.content;
40
+ }
41
+ function applyBlockEdits(tree, edits, parse) {
42
+ if (!Array.isArray(edits) || edits.length === 0) throw new BlockEditError(0, `edits must be a list of ${FORMS}.`);
43
+ const root = copyTree(tree);
44
+ const written = [];
45
+ const find = (index, id) => {
46
+ const found = locate(root, id);
47
+ if (!found)
48
+ throw new BlockEditError(
49
+ index,
50
+ `the document has no block with id "${id}". Read the document for its current block ids; an earlier edit in this list may have deleted it.`
51
+ );
52
+ return found;
53
+ };
54
+ const slot = (index, position, id) => {
55
+ const target = find(index, id);
56
+ if (position === "into") {
57
+ return { siblings: target.node.children, at: target.node.children.length, parent: target.node, target };
58
+ }
59
+ return {
60
+ siblings: target.siblings,
61
+ at: position === "after" ? target.index + 1 : target.index,
62
+ parent: target.parent,
63
+ target
64
+ };
65
+ };
66
+ const adopt = (parent, nodes) => {
67
+ if (!parent || parent.children.length > 0) return fragmentUnder(parent, nodes);
68
+ const only = nodes.length === 1 ? nodes[0] : void 0;
69
+ const nests = only && isBareList(only) && parent.block.type === "Paragraph" && !!parent.block.text;
70
+ const childrenType = nests ? only.block.childrenType : parent.block.childrenType || "Group";
71
+ if (parent.block.childrenType !== childrenType) parent.block = { ...parent.block, childrenType };
72
+ return nests ? only.children : fragmentUnder(parent, nodes);
73
+ };
74
+ edits.forEach((raw, index) => {
75
+ if (!raw || typeof raw !== "object" || Array.isArray(raw))
76
+ throw new BlockEditError(index, `an edit is one of ${FORMS}.`);
77
+ const edit = raw;
78
+ const positions = POSITIONS.filter((key) => edit[key] !== void 0);
79
+ const verbs = ["replace", "move", "delete"].filter((key) => edit[key] !== void 0);
80
+ const known = /* @__PURE__ */ new Set(["replace", "move", "delete", "content", ...POSITIONS]);
81
+ const unknown = Object.keys(edit).filter((key) => !known.has(key));
82
+ if (unknown.length || verbs.length > 1 || positions.length > 1)
83
+ throw new BlockEditError(index, `an edit is one of ${FORMS}.`);
84
+ if (edit.delete !== void 0) {
85
+ if (positions.length || edit.content !== void 0)
86
+ throw new BlockEditError(index, "a delete takes only the id, or a list of ids, to delete.");
87
+ const ids = Array.isArray(edit.delete) ? edit.delete : [edit.delete];
88
+ for (const id of ids) {
89
+ const target = find(index, idOf(index, id, "`delete`"));
90
+ target.siblings.splice(target.index, 1);
91
+ }
92
+ return;
93
+ }
94
+ if (edit.move !== void 0) {
95
+ if (positions.length !== 1 || edit.content !== void 0)
96
+ throw new BlockEditError(index, "a move names where the block goes: one of after, before or into.");
97
+ const moved = find(index, idOf(index, edit.move, "`move`"));
98
+ const position2 = positions[0];
99
+ const anchor = idOf(index, edit[position2], `\`${position2}\``);
100
+ if (locate([moved.node], anchor))
101
+ throw new BlockEditError(
102
+ index,
103
+ `block "${moved.node.block.id}" cannot move ${position2} itself or its children.`
104
+ );
105
+ moved.siblings.splice(moved.index, 1);
106
+ const to2 = slot(index, position2, anchor);
107
+ if (to2.parent && !to2.parent.children.length && !to2.parent.block.childrenType)
108
+ to2.parent.block = { ...to2.parent.block, childrenType: "Group" };
109
+ to2.siblings.splice(to2.at, 0, moved.node);
110
+ return;
111
+ }
112
+ if (edit.replace !== void 0) {
113
+ if (positions.length)
114
+ throw new BlockEditError(index, "a replace takes the id and `content`. Add blocks with a separate edit.");
115
+ const target = find(index, idOf(index, edit.replace, "`replace`"));
116
+ const nodes2 = fragmentUnder(target.parent, parse(contentOf(index, edit)));
117
+ const first = nodes2[0];
118
+ if (!first) throw new BlockEditError(index, "the replacement is empty. To remove the block, use {delete: ID}.");
119
+ const kept = target.node.block.type === "Table" ? [] : target.node.children;
120
+ if (first.block.type === "Table" ? kept.length > 0 : first.children.length > 0 && kept.length > 0)
121
+ throw new BlockEditError(
122
+ index,
123
+ `block "${target.node.block.id}" has children, which a replace keeps, so the replacement cannot bring children of its own. Write only the block itself, and change its children by their ids or add to them with {into: "${target.node.block.id}", content}.`
124
+ );
125
+ const children = first.block.type === "Table" || !kept.length ? first.children : kept;
126
+ const childrenType = kept.length ? target.node.block.childrenType : first.block.childrenType;
127
+ const { childrenType: _written, ...own } = first.block;
128
+ const replacement = {
129
+ block: { ...own, id: target.node.block.id, ...childrenType ? { childrenType } : {} },
130
+ children
131
+ };
132
+ target.siblings.splice(target.index, 1, replacement, ...nodes2.slice(1));
133
+ written.push(replacement, ...nodes2.slice(1));
134
+ return;
135
+ }
136
+ if (positions.length !== 1) throw new BlockEditError(index, `an edit is one of ${FORMS}.`);
137
+ const position = positions[0];
138
+ const to = slot(index, position, idOf(index, edit[position], `\`${position}\``));
139
+ const nodes = adopt(to.parent, parse(contentOf(index, edit)));
140
+ if (!nodes.length) throw new BlockEditError(index, "`content` holds no blocks to add.");
141
+ to.siblings.splice(to.at, 0, ...nodes);
142
+ written.push(...nodes);
143
+ });
144
+ const seen = /* @__PURE__ */ new Set();
145
+ const check = (nodes) => {
146
+ for (const node of nodes) {
147
+ if (seen.has(node.block.id))
148
+ throw new BlockEditError(
149
+ edits.length - 1,
150
+ `block id "${node.block.id}" would be in the document twice. Leave id comments off new blocks; an id is only for the block that already has it.`
151
+ );
152
+ seen.add(node.block.id);
153
+ check(node.children);
154
+ }
155
+ };
156
+ check(root);
157
+ return { tree: root, written };
158
+ }
159
+
160
+ export {
161
+ BlockEditError,
162
+ applyBlockEdits
163
+ };