@seed-hypermedia/client 0.0.3 → 0.0.5

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/dist/index.mjs CHANGED
@@ -1,5 +1,7 @@
1
1
  import {
2
2
  HMActionSchema,
3
+ HMBlockButtonAlignmentSchema,
4
+ HMBlockSchema,
3
5
  HMRequestSchema,
4
6
  HYPERMEDIA_SCHEME,
5
7
  codePointLength,
@@ -12,8 +14,9 @@ import {
12
14
  parseCustomURL,
13
15
  parseFragment,
14
16
  serializeBlockRange,
17
+ toNumber,
15
18
  unpackHmId
16
- } from "./chunk-X57BIZUK.mjs";
19
+ } from "./chunk-B5WCMFR5.mjs";
17
20
 
18
21
  // src/capability.ts
19
22
  import { encode as cborEncode2 } from "@ipld/dag-cbor";
@@ -792,6 +795,33 @@ async function deleteComment(input, signer) {
792
795
  const encoded = cborEncode6(tombstone);
793
796
  return toPublishInput(encoded, []);
794
797
  }
798
+ async function updateComment(input, signer) {
799
+ const parts = input.commentId.split("/");
800
+ const tsid = parts[1];
801
+ if (!tsid) {
802
+ throw new Error(`Invalid comment ID format: ${input.commentId}`);
803
+ }
804
+ const signerKey = await signer.getPublicKey();
805
+ cleanContentOfUndefined(input.content);
806
+ const trimmedContent = trimTrailingEmptyBlocks(input.content);
807
+ const comment = {
808
+ type: "Comment",
809
+ id: tsid,
810
+ body: blocksToPublishable(trimmedContent),
811
+ space: new Uint8Array(base58btc3.decode(input.targetAccount)),
812
+ path: input.targetPath,
813
+ version: input.targetVersion.split(".").map((v) => CID3.parse(v)),
814
+ signer: new Uint8Array(signerKey),
815
+ ts: BigInt(Date.now()),
816
+ sig: new Uint8Array(64)
817
+ };
818
+ if (input.replyParentVersion) comment.replyParent = CID3.parse(input.replyParentVersion);
819
+ if (input.rootReplyCommentVersion) comment.threadRoot = CID3.parse(input.rootReplyCommentVersion);
820
+ if (input.visibility) comment.visibility = input.visibility;
821
+ comment.sig = await signObject(signer, comment);
822
+ const encoded = cborEncode6(comment);
823
+ return toPublishInput(encoded, []);
824
+ }
795
825
  async function commentRecordIdFromBlob(blobData) {
796
826
  const decoded = cborDecode2(blobData);
797
827
  if (decoded.type !== "Comment") {
@@ -2517,6 +2547,37 @@ function parseMarkdown(markdown) {
2517
2547
  }
2518
2548
  return { tree: rootNodes, metadata };
2519
2549
  }
2550
+ function markdownBlockNodesToHMBlockNodes(nodes) {
2551
+ return nodes.map((node) => {
2552
+ const { block } = node;
2553
+ const attributes = {};
2554
+ if (block.childrenType !== void 0) {
2555
+ attributes.childrenType = block.childrenType;
2556
+ }
2557
+ if (block.language !== void 0) {
2558
+ attributes.language = block.language;
2559
+ }
2560
+ const hmBlock = {
2561
+ type: block.type,
2562
+ id: block.id,
2563
+ text: block.text,
2564
+ annotations: block.annotations.map((a) => ({
2565
+ type: a.type,
2566
+ starts: a.starts,
2567
+ ends: a.ends,
2568
+ ...a.link !== void 0 ? { link: a.link } : {}
2569
+ })),
2570
+ attributes
2571
+ };
2572
+ if (block.link !== void 0) {
2573
+ hmBlock.link = block.link;
2574
+ }
2575
+ return {
2576
+ block: hmBlock,
2577
+ children: node.children.length > 0 ? markdownBlockNodesToHMBlockNodes(node.children) : void 0
2578
+ };
2579
+ });
2580
+ }
2520
2581
  function flattenToOperations(tree, parentId = "") {
2521
2582
  const ops = [];
2522
2583
  const blockIds = [];
@@ -2777,6 +2838,23 @@ function formatMediaUrl(url, useGateway) {
2777
2838
  function indent(depth) {
2778
2839
  return " ".repeat(depth);
2779
2840
  }
2841
+ function slugify(title) {
2842
+ return title.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 60);
2843
+ }
2844
+ function draftFilename(slug, id) {
2845
+ if (!slug) return `${id}.md`;
2846
+ return `${slug}_${id}.md`;
2847
+ }
2848
+ function parseDraftFilename(filename) {
2849
+ const lastDot = filename.lastIndexOf(".");
2850
+ const ext = lastDot >= 0 ? filename.slice(lastDot) : "";
2851
+ const base = lastDot >= 0 ? filename.slice(0, lastDot) : filename;
2852
+ const lastUnderscore = base.lastIndexOf("_");
2853
+ if (lastUnderscore >= 0 && ext === ".md") {
2854
+ return { id: base.slice(lastUnderscore + 1), ext };
2855
+ }
2856
+ return { id: base, ext };
2857
+ }
2780
2858
 
2781
2859
  // src/block-diff.ts
2782
2860
  function createBlocksMap(nodes, parentId = "") {
@@ -3140,12 +3218,640 @@ function generateBlockId5() {
3140
3218
  }
3141
3219
  return id;
3142
3220
  }
3221
+
3222
+ // src/unicode.ts
3223
+ var AnnotationSet = class {
3224
+ annotations;
3225
+ constructor() {
3226
+ this.annotations = /* @__PURE__ */ new Map();
3227
+ }
3228
+ addSpan(type, attributes, start, end) {
3229
+ const id = this._annotationId(type, attributes);
3230
+ let annotation = this.annotations.get(id);
3231
+ if (!annotation) {
3232
+ const annAttrs = attributes ? { ...attributes } : {};
3233
+ let link;
3234
+ if (type == "Link" || type == "Embed") {
3235
+ link = attributes == null ? void 0 : attributes.link;
3236
+ delete annAttrs.link;
3237
+ }
3238
+ annotation = {
3239
+ type,
3240
+ attributes: annAttrs,
3241
+ link: link || "",
3242
+ starts: [],
3243
+ ends: []
3244
+ };
3245
+ this.annotations.set(id, annotation);
3246
+ }
3247
+ addSpanToAnnotation(annotation, start, end);
3248
+ }
3249
+ _annotationId(type, attributes) {
3250
+ if (attributes) {
3251
+ if (attributes.link) {
3252
+ return `${type}-${attributes.link}`;
3253
+ }
3254
+ if (attributes.href) {
3255
+ return `${type}-${attributes.href}`;
3256
+ }
3257
+ }
3258
+ return type;
3259
+ }
3260
+ list() {
3261
+ const keys = Array.from(this.annotations.keys()).sort();
3262
+ let out = new Array(keys.length);
3263
+ for (let i in keys) {
3264
+ const annotation = this.annotations.get(keys[i]);
3265
+ if (annotation) out[i] = annotation;
3266
+ }
3267
+ out = out.sort((a, b) => {
3268
+ let startA = a.starts[0];
3269
+ let startB = b.starts[0];
3270
+ return (startA || 0) - (startB || 0);
3271
+ });
3272
+ return out;
3273
+ }
3274
+ };
3275
+ function addSpanToAnnotation(annotation, start, end) {
3276
+ if (!annotation.starts) {
3277
+ annotation.starts = [];
3278
+ }
3279
+ if (!annotation.ends) {
3280
+ annotation.ends = [];
3281
+ }
3282
+ if (annotation.starts.length == 0) {
3283
+ pushSpanToAnnotation(annotation, start, end);
3284
+ return;
3285
+ }
3286
+ const lastIdx = annotation.starts.length - 1;
3287
+ if (annotation.ends[lastIdx] == start) {
3288
+ annotation.ends[lastIdx] = end;
3289
+ return;
3290
+ }
3291
+ pushSpanToAnnotation(annotation, start, end);
3292
+ }
3293
+ function pushSpanToAnnotation(annotation, start, end) {
3294
+ annotation.starts.push(start);
3295
+ annotation.ends.push(end);
3296
+ }
3297
+
3298
+ // src/editorblock-to-hmblock.ts
3299
+ function toHMBlockType(editorBlockType) {
3300
+ if (editorBlockType === "heading") return "Heading";
3301
+ if (editorBlockType === "paragraph") return "Paragraph";
3302
+ if (editorBlockType === "code-block") return "Code";
3303
+ if (editorBlockType === "math") return "Math";
3304
+ if (editorBlockType === "image") return "Image";
3305
+ if (editorBlockType === "video") return "Video";
3306
+ if (editorBlockType === "file") return "File";
3307
+ if (editorBlockType === "button") return "Button";
3308
+ if (editorBlockType === "embed") return "Embed";
3309
+ if (editorBlockType === "web-embed") return "WebEmbed";
3310
+ if (editorBlockType === "query") return "Query";
3311
+ return void 0;
3312
+ }
3313
+ function editorBlockToHMBlock(editorBlock) {
3314
+ var _a, _b, _c, _d, _e, _f;
3315
+ const blockType = toHMBlockType(editorBlock.type);
3316
+ if (!blockType) throw new Error("Unsupported block type " + editorBlock.type);
3317
+ let block = {
3318
+ id: editorBlock.id,
3319
+ type: blockType,
3320
+ attributes: {},
3321
+ text: "",
3322
+ annotations: []
3323
+ };
3324
+ let leaves = flattenLeaves(editorBlock.content);
3325
+ if (editorBlock.props.childrenType == "Group") {
3326
+ block.attributes.childrenType = "Group";
3327
+ } else if (editorBlock.props.childrenType == "Unordered") {
3328
+ block.attributes.childrenType = "Unordered";
3329
+ } else if (editorBlock.props.childrenType == "Ordered") {
3330
+ block.attributes.childrenType = "Ordered";
3331
+ } else if (editorBlock.props.childrenType == "Blockquote") {
3332
+ block.attributes.childrenType = "Blockquote";
3333
+ } else if (editorBlock.props.childrenType == "Grid") {
3334
+ block.attributes.childrenType = "Grid";
3335
+ if (editorBlock.props.columnCount) {
3336
+ block.attributes.columnCount = Number(editorBlock.props.columnCount);
3337
+ }
3338
+ }
3339
+ const annotations = new AnnotationSet();
3340
+ let pos = 0;
3341
+ for (let leaf of leaves) {
3342
+ const start = pos;
3343
+ const charCount = codePointLength(leaf.text);
3344
+ const end = start + charCount;
3345
+ if ((_a = leaf.styles) == null ? void 0 : _a.bold) {
3346
+ annotations.addSpan("Bold", null, start, end);
3347
+ }
3348
+ if ((_b = leaf.styles) == null ? void 0 : _b.italic) {
3349
+ annotations.addSpan("Italic", null, start, end);
3350
+ }
3351
+ if ((_c = leaf.styles) == null ? void 0 : _c.underline) {
3352
+ annotations.addSpan("Underline", null, start, end);
3353
+ }
3354
+ if ((_d = leaf.styles) == null ? void 0 : _d.strike) {
3355
+ annotations.addSpan("Strike", null, start, end);
3356
+ }
3357
+ if ((_e = leaf.styles) == null ? void 0 : _e.code) {
3358
+ annotations.addSpan("Code", null, start, end);
3359
+ }
3360
+ if ((_f = leaf.styles) == null ? void 0 : _f.math) {
3361
+ annotations.addSpan("Math", null, start, end);
3362
+ }
3363
+ if (leaf.type == "inline-embed") {
3364
+ annotations.addSpan("Embed", { link: leaf.link }, start, end);
3365
+ }
3366
+ if (leaf.type == "link") {
3367
+ annotations.addSpan("Link", { link: leaf.href }, start, end);
3368
+ }
3369
+ block.text += leaf.text;
3370
+ pos += charCount;
3371
+ }
3372
+ let outAnnotations = annotations.list();
3373
+ if (outAnnotations) {
3374
+ block.annotations = outAnnotations;
3375
+ }
3376
+ const blockCode = block.type === "Code" ? block : void 0;
3377
+ if (blockCode && editorBlock.type == "code-block") {
3378
+ blockCode.attributes.language = editorBlock.props.language;
3379
+ }
3380
+ const blockImage = block.type === "Image" ? block : void 0;
3381
+ if (blockImage && editorBlock.type == "image") {
3382
+ if (editorBlock.props.url && !editorBlock.props.mediaRef) {
3383
+ blockImage.link = editorBlock.props.url;
3384
+ } else if (editorBlock.props.mediaRef) {
3385
+ blockImage.link = "";
3386
+ } else if (editorBlock.props.displaySrc) {
3387
+ blockImage.link = editorBlock.props.displaySrc;
3388
+ } else if (editorBlock.props.src) {
3389
+ blockImage.link = editorBlock.props.src;
3390
+ } else {
3391
+ blockImage.link = "";
3392
+ }
3393
+ const width = toNumber(editorBlock.props.width);
3394
+ if (width) {
3395
+ blockImage.attributes.width = width;
3396
+ }
3397
+ }
3398
+ const blockVideo = block.type === "Video" ? block : void 0;
3399
+ if (blockVideo && editorBlock.type == "video") {
3400
+ blockVideo.text = "";
3401
+ if (editorBlock.props.url && !editorBlock.props.mediaRef) {
3402
+ blockVideo.link = editorBlock.props.url;
3403
+ } else if (editorBlock.props.mediaRef) {
3404
+ blockVideo.link = "";
3405
+ }
3406
+ const width = toNumber(editorBlock.props.width);
3407
+ if (width) blockVideo.attributes.width = width;
3408
+ if (editorBlock.props.name) {
3409
+ blockVideo.attributes.name = editorBlock.props.name;
3410
+ }
3411
+ }
3412
+ const blockFile = block.type === "File" ? block : void 0;
3413
+ if (blockFile && editorBlock.type == "file") {
3414
+ if (editorBlock.props.url && !editorBlock.props.mediaRef) {
3415
+ blockFile.link = editorBlock.props.url;
3416
+ } else if (editorBlock.props.mediaRef) {
3417
+ blockFile.link = "";
3418
+ }
3419
+ if (editorBlock.props.name) blockFile.attributes.name = editorBlock.props.name;
3420
+ const size = toNumber(editorBlock.props.size);
3421
+ if (size) blockFile.attributes.size = size;
3422
+ }
3423
+ const blockButton = block.type === "Button" ? block : void 0;
3424
+ if (blockButton && editorBlock.type == "button") {
3425
+ if (editorBlock.props.url) blockButton.link = editorBlock.props.url;
3426
+ if (editorBlock.props.name) blockButton.attributes.name = editorBlock.props.name;
3427
+ if (editorBlock.props.alignment)
3428
+ blockButton.attributes.alignment = HMBlockButtonAlignmentSchema.parse(editorBlock.props.alignment);
3429
+ }
3430
+ const blockWebEmbed = block.type === "WebEmbed" ? block : void 0;
3431
+ if (blockWebEmbed && editorBlock.type == "web-embed" && editorBlock.props.url) {
3432
+ blockWebEmbed.link = editorBlock.props.url;
3433
+ }
3434
+ const blockEmbed = block.type === "Embed" ? block : void 0;
3435
+ if (blockEmbed && editorBlock.type == "embed") {
3436
+ block.text = "";
3437
+ if (editorBlock.props.url) blockEmbed.link = editorBlock.props.url;
3438
+ if (editorBlock.props.view) blockEmbed.attributes.view = editorBlock.props.view;
3439
+ }
3440
+ const blockQuery = block.type === "Query" ? block : void 0;
3441
+ if (blockQuery && editorBlock.type == "query") {
3442
+ blockQuery.attributes.style = editorBlock.props.style;
3443
+ blockQuery.attributes.columnCount = Number(editorBlock.props.columnCount);
3444
+ const query = {
3445
+ includes: [],
3446
+ sort: []
3447
+ };
3448
+ if (editorBlock.props.queryIncludes) query.includes = JSON.parse(editorBlock.props.queryIncludes);
3449
+ if (editorBlock.props.querySort) query.sort = JSON.parse(editorBlock.props.querySort);
3450
+ if (editorBlock.props.queryLimit) query.limit = Number(editorBlock.props.queryLimit);
3451
+ blockQuery.attributes.query = query;
3452
+ blockQuery.attributes.banner = editorBlock.props.banner == "true";
3453
+ }
3454
+ const blockParse = HMBlockSchema.safeParse(block);
3455
+ if (blockParse.success) {
3456
+ return blockParse.data;
3457
+ }
3458
+ const failedParse = blockParse;
3459
+ console.error("Failed to validate block for writing", block, failedParse.error);
3460
+ throw new Error("Failed to validate block for writing " + JSON.stringify(failedParse.error));
3461
+ }
3462
+ function editorBlocksToHMBlockNodes(editorBlocks) {
3463
+ return editorBlocks.map((block) => {
3464
+ var _a, _b;
3465
+ try {
3466
+ return {
3467
+ block: editorBlockToHMBlock(block),
3468
+ children: ((_a = block.children) == null ? void 0 : _a.length) ? editorBlocksToHMBlockNodes(block.children) : void 0
3469
+ };
3470
+ } catch {
3471
+ return {
3472
+ block: {
3473
+ id: block.id || "unknown",
3474
+ type: "Paragraph",
3475
+ text: `[Unsupported block type: ${block.type}]`,
3476
+ annotations: [],
3477
+ attributes: {}
3478
+ },
3479
+ children: ((_b = block.children) == null ? void 0 : _b.length) ? editorBlocksToHMBlockNodes(block.children) : void 0
3480
+ };
3481
+ }
3482
+ }).filter(Boolean);
3483
+ }
3484
+ function flattenLeaves(content) {
3485
+ let result = [];
3486
+ for (let i = 0; i < content.length; i++) {
3487
+ const leaf = content[i];
3488
+ if (!leaf) continue;
3489
+ if (leaf.type == "link") {
3490
+ let nestedLeaves = flattenLeaves(leaf.content).map(
3491
+ (l) => ({
3492
+ ...l,
3493
+ href: leaf.href,
3494
+ type: "link"
3495
+ })
3496
+ );
3497
+ result.push(...nestedLeaves);
3498
+ }
3499
+ if (leaf.type == "inline-embed") {
3500
+ result.push({
3501
+ ...leaf,
3502
+ text: "\uFFFC",
3503
+ link: leaf.link
3504
+ });
3505
+ }
3506
+ if (leaf.type == "text") {
3507
+ result.push(leaf);
3508
+ }
3509
+ }
3510
+ return result;
3511
+ }
3512
+
3513
+ // src/hmblock-to-editorblock.ts
3514
+ function toEditorBlockType(hmBlockType) {
3515
+ if (hmBlockType === "Heading") return "heading";
3516
+ if (hmBlockType === "Paragraph") return "paragraph";
3517
+ if (hmBlockType === "Code") return "code-block";
3518
+ if (hmBlockType === "Math") return "math";
3519
+ if (hmBlockType === "Image") return "image";
3520
+ if (hmBlockType === "Video") return "video";
3521
+ if (hmBlockType === "File") return "file";
3522
+ if (hmBlockType === "Button") return "button";
3523
+ if (hmBlockType === "Embed") return "embed";
3524
+ if (hmBlockType === "WebEmbed") return "web-embed";
3525
+ if (hmBlockType === "Nostr") return "nostr";
3526
+ if (hmBlockType === "Query") return "query";
3527
+ return "unknown";
3528
+ }
3529
+ function hmBlocksToEditorContent(blocks, opts = { level: 1 }) {
3530
+ const childRecursiveOpts = {
3531
+ level: opts.level || 0
3532
+ };
3533
+ return blocks.map((hmBlock) => {
3534
+ var _a, _b, _c;
3535
+ let res = hmBlock.block ? hmBlockToEditorBlock(hmBlock.block) : null;
3536
+ if (res && ((_a = hmBlock.children) == null ? void 0 : _a.length)) {
3537
+ const childrenType = (_c = ((_b = hmBlock.block) == null ? void 0 : _b.attributes) || {}) == null ? void 0 : _c.childrenType;
3538
+ const validChildrenType = childrenType === "Group" || childrenType === "Ordered" || childrenType === "Unordered" || childrenType === "Blockquote" || childrenType === "Grid" ? childrenType : "Group";
3539
+ res.children = hmBlocksToEditorContent(hmBlock.children, {
3540
+ level: childRecursiveOpts.level ? childRecursiveOpts.level + 1 : 1,
3541
+ childrenType: validChildrenType
3542
+ });
3543
+ }
3544
+ return res;
3545
+ }).filter((block) => block !== null);
3546
+ }
3547
+ function hmBlockToEditorBlock(block) {
3548
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
3549
+ const blockType = toEditorBlockType(block.type);
3550
+ if (blockType === "unknown") {
3551
+ return {
3552
+ id: block.id,
3553
+ type: "unknown",
3554
+ content: [],
3555
+ props: {
3556
+ revision: block.revision,
3557
+ originalType: block.type,
3558
+ originalData: JSON.stringify(block)
3559
+ },
3560
+ children: []
3561
+ };
3562
+ }
3563
+ let out = {
3564
+ id: block.id,
3565
+ type: blockType,
3566
+ content: [],
3567
+ props: {
3568
+ revision: block.revision
3569
+ },
3570
+ children: []
3571
+ };
3572
+ const attributes = block.attributes || {};
3573
+ if ("childrenType" in attributes && attributes.childrenType) {
3574
+ const childrenType = attributes.childrenType;
3575
+ if (childrenType === "Group" || childrenType === "Ordered" || childrenType === "Unordered" || childrenType === "Blockquote" || childrenType === "Grid") {
3576
+ ;
3577
+ out.props.childrenType = childrenType;
3578
+ }
3579
+ if (childrenType === "Grid" && attributes.columnCount != null) {
3580
+ ;
3581
+ out.props.columnCount = String(attributes.columnCount);
3582
+ }
3583
+ }
3584
+ if (["code-block", "video", "image", "file", "button", "embed", "web-embed", "math", "nostr"].includes(blockType)) {
3585
+ if (block.link) {
3586
+ ;
3587
+ out.props.url = block.link;
3588
+ }
3589
+ if (blockType == "code-block") {
3590
+ out.type = "code-block";
3591
+ }
3592
+ if (block.attributes) {
3593
+ Object.entries(block.attributes).forEach(([key, value]) => {
3594
+ if (value !== void 0) {
3595
+ if (key == "width" || key == "size") {
3596
+ if (typeof value == "number") {
3597
+ ;
3598
+ out.props[key] = String(value);
3599
+ }
3600
+ } else {
3601
+ ;
3602
+ out.props[key] = value;
3603
+ }
3604
+ }
3605
+ });
3606
+ }
3607
+ }
3608
+ if (block.type === "Query") {
3609
+ const queryProps = out.props;
3610
+ queryProps.style = (_a = block.attributes) == null ? void 0 : _a.style;
3611
+ queryProps.columnCount = String(((_b = block.attributes) == null ? void 0 : _b.columnCount) || "");
3612
+ queryProps.queryIncludes = JSON.stringify(((_d = (_c = block.attributes) == null ? void 0 : _c.query) == null ? void 0 : _d.includes) || []);
3613
+ queryProps.querySort = JSON.stringify(((_f = (_e = block.attributes) == null ? void 0 : _e.query) == null ? void 0 : _f.sort) || {});
3614
+ queryProps.banner = ((_g = block.attributes) == null ? void 0 : _g.banner) ? "true" : "false";
3615
+ queryProps.queryLimit = String(((_i = (_h = block.attributes) == null ? void 0 : _h.query) == null ? void 0 : _i.limit) || "");
3616
+ }
3617
+ const blockText = block.text || "";
3618
+ const leaves = out.content;
3619
+ let leaf = null;
3620
+ let inlineBlockContent = null;
3621
+ let textStart = 0;
3622
+ let i = 0;
3623
+ const stopPoint = block.text ? block.text.length - 1 : 0;
3624
+ let pos = 0;
3625
+ const leafAnnotations = /* @__PURE__ */ new Set();
3626
+ if (blockText == "") {
3627
+ leaves.push({ type: "text", text: blockText, styles: {} });
3628
+ return out;
3629
+ }
3630
+ while (i < blockText.length) {
3631
+ let ul = 1;
3632
+ let annotationsChanged = trackPosAnnotations(pos);
3633
+ let surrogate = isSurrogate(blockText, i);
3634
+ if (surrogate) {
3635
+ ul++;
3636
+ let onlyOneSurrogate = pos + ul;
3637
+ if (onlyOneSurrogate == blockText.length) {
3638
+ if (!leaf) {
3639
+ startLeaf(leafAnnotations);
3640
+ }
3641
+ finishLeaf(textStart, i + 2);
3642
+ if (inlineBlockContent) {
3643
+ const lastLeaf = leaves[leaves.length - 1];
3644
+ if (lastLeaf && !isText(lastLeaf)) {
3645
+ }
3646
+ leaves.push(inlineBlockContent);
3647
+ inlineBlockContent = null;
3648
+ }
3649
+ return out;
3650
+ }
3651
+ }
3652
+ if (stopPoint < 0) {
3653
+ console.warn("STOP IS LESS THAN ZERO", block);
3654
+ }
3655
+ if (i == stopPoint) {
3656
+ if (annotationsChanged) {
3657
+ if (leaf) {
3658
+ finishLeaf(textStart, i);
3659
+ }
3660
+ startLeaf(leafAnnotations);
3661
+ } else {
3662
+ startLeaf(leafAnnotations);
3663
+ }
3664
+ finishLeaf(textStart, i + 1);
3665
+ if (inlineBlockContent) {
3666
+ const lastLeaf = leaves[leaves.length - 1];
3667
+ if (lastLeaf && !isText(lastLeaf)) {
3668
+ }
3669
+ leaves.push(inlineBlockContent);
3670
+ inlineBlockContent = null;
3671
+ }
3672
+ return out;
3673
+ }
3674
+ if (!leaf) {
3675
+ startLeaf(leafAnnotations);
3676
+ advance(ul);
3677
+ continue;
3678
+ }
3679
+ if (annotationsChanged) {
3680
+ finishLeaf(textStart, i);
3681
+ startLeaf(leafAnnotations);
3682
+ }
3683
+ advance(ul);
3684
+ if (i == blockText.length) {
3685
+ finishLeaf(textStart, i);
3686
+ return out;
3687
+ }
3688
+ }
3689
+ throw Error("BUG: should not get here");
3690
+ function advance(codeUnits) {
3691
+ pos++;
3692
+ i += codeUnits;
3693
+ }
3694
+ function startLeaf(posAnnotations) {
3695
+ const newLeaf = {
3696
+ type: "text",
3697
+ text: "",
3698
+ styles: {}
3699
+ };
3700
+ leaf = newLeaf;
3701
+ let linkAnnotation = null;
3702
+ for (const l of Array.from(posAnnotations)) {
3703
+ const annotationData = l;
3704
+ if (annotationData.type === "Link") {
3705
+ linkAnnotation = {
3706
+ type: "Link",
3707
+ href: annotationData.link || ""
3708
+ };
3709
+ }
3710
+ if (annotationData.type === "Embed") {
3711
+ linkAnnotation = {
3712
+ type: "Embed",
3713
+ link: annotationData.link || ""
3714
+ };
3715
+ }
3716
+ if (["Bold", "Italic", "Strike", "Underline", "Code", "Range"].includes(annotationData.type)) {
3717
+ const styleKey = annotationData.type.toLowerCase();
3718
+ newLeaf.styles[styleKey] = true;
3719
+ }
3720
+ }
3721
+ if (linkAnnotation) {
3722
+ if (linkAnnotation.type === "Embed") {
3723
+ leaves.push({
3724
+ type: "inline-embed",
3725
+ styles: {},
3726
+ link: linkAnnotation.link || ""
3727
+ });
3728
+ textStart = i + 1;
3729
+ } else if (inlineBlockContent) {
3730
+ if (linkChangedIdentity(linkAnnotation)) {
3731
+ leaves.push(inlineBlockContent);
3732
+ if (linkAnnotation.type === "Link") {
3733
+ inlineBlockContent = {
3734
+ type: "link",
3735
+ content: [],
3736
+ href: linkAnnotation.href || ""
3737
+ };
3738
+ } else {
3739
+ inlineBlockContent = {
3740
+ type: "inline-embed",
3741
+ styles: {},
3742
+ link: linkAnnotation.link || ""
3743
+ };
3744
+ }
3745
+ }
3746
+ } else {
3747
+ if (linkAnnotation.type === "Link") {
3748
+ inlineBlockContent = {
3749
+ type: "link",
3750
+ content: [],
3751
+ href: linkAnnotation.href || ""
3752
+ };
3753
+ } else {
3754
+ inlineBlockContent = {
3755
+ type: "inline-embed",
3756
+ styles: {},
3757
+ link: linkAnnotation.link || ""
3758
+ };
3759
+ }
3760
+ }
3761
+ } else {
3762
+ if (inlineBlockContent) {
3763
+ leaves.push(inlineBlockContent);
3764
+ inlineBlockContent = null;
3765
+ }
3766
+ }
3767
+ }
3768
+ function linkChangedIdentity(annotation) {
3769
+ if (!inlineBlockContent) return false;
3770
+ let currentLink = inlineBlockContent.link || inlineBlockContent.href;
3771
+ return currentLink != annotation.link && currentLink != annotation.href;
3772
+ }
3773
+ function finishLeaf(low, high) {
3774
+ let newValue = blockText.substring(low, high);
3775
+ if (leaf) leaf.text = newValue;
3776
+ textStart = high;
3777
+ if (inlineBlockContent) {
3778
+ if (leaf && inlineBlockContent.type == "link") {
3779
+ ;
3780
+ inlineBlockContent.content.push(leaf);
3781
+ } else if (inlineBlockContent.type == "inline-embed" && leaf) {
3782
+ } else if (leaf) {
3783
+ const typedLeaf = {
3784
+ type: "text",
3785
+ text: "",
3786
+ styles: leaf.styles
3787
+ };
3788
+ inlineBlockContent.content.push(typedLeaf);
3789
+ }
3790
+ } else {
3791
+ if (leaf && !(leaf.type === "text" && leaf.text === "" && Object.keys(leaf.styles).length === 0)) {
3792
+ leaves.push(leaf);
3793
+ }
3794
+ }
3795
+ }
3796
+ function trackPosAnnotations(pos2) {
3797
+ let annotationsChanged = false;
3798
+ if (!block.annotations) {
3799
+ return false;
3800
+ }
3801
+ const blockAnnotations = block.annotations;
3802
+ blockAnnotations.forEach((l) => {
3803
+ let spanIdx = annotationContains(l, pos2);
3804
+ if (spanIdx === -1) {
3805
+ if (leafAnnotations.delete(l)) {
3806
+ annotationsChanged = true;
3807
+ }
3808
+ return;
3809
+ }
3810
+ if (leafAnnotations.has(l)) {
3811
+ return;
3812
+ }
3813
+ leafAnnotations.add(l);
3814
+ annotationsChanged = true;
3815
+ });
3816
+ return annotationsChanged;
3817
+ }
3818
+ }
3819
+ function annotationContains(annotation, pos) {
3820
+ let low = 0;
3821
+ let high = annotation.starts.length - 1;
3822
+ let mid = 0;
3823
+ while (low <= high) {
3824
+ mid = Math.floor((low + high) / 2);
3825
+ const endAtMid = annotation.ends[mid];
3826
+ if (endAtMid === void 0) break;
3827
+ if (endAtMid <= pos) {
3828
+ low = mid + 1;
3829
+ } else {
3830
+ high = mid - 1;
3831
+ }
3832
+ }
3833
+ if (low == annotation.starts.length) {
3834
+ return -1;
3835
+ }
3836
+ const startAtLow = annotation.starts[low];
3837
+ const endAtLow = annotation.ends[low];
3838
+ if (startAtLow !== void 0 && endAtLow !== void 0 && startAtLow <= pos && pos < endAtLow) {
3839
+ return low;
3840
+ }
3841
+ return -1;
3842
+ }
3843
+ function isText(entry) {
3844
+ return (entry == null ? void 0 : entry.type) && entry.type == "text" && typeof entry.text == "string";
3845
+ }
3143
3846
  export {
3847
+ AnnotationSet,
3144
3848
  DEFAULT_GROBID_URL,
3145
3849
  HYPERMEDIA_SCHEME,
3146
3850
  SeedClientError,
3147
3851
  SeedNetworkError,
3148
3852
  SeedValidationError,
3853
+ addSpanToAnnotation,
3854
+ annotationContains,
3149
3855
  autoLinkChildToParent,
3150
3856
  blocksToMarkdown,
3151
3857
  codePointLength,
@@ -3170,6 +3876,9 @@ export {
3170
3876
  deleteContact,
3171
3877
  documentContainsLinkToChild,
3172
3878
  documentHasSelfQuery,
3879
+ draftFilename,
3880
+ editorBlockToHMBlock,
3881
+ editorBlocksToHMBlockNodes,
3173
3882
  embeddedPdfToBlocks,
3174
3883
  emitFrontmatter,
3175
3884
  entityQueryPathToHmIdPath,
@@ -3179,27 +3888,34 @@ export {
3179
3888
  getHMQueryString,
3180
3889
  hasFileLinks,
3181
3890
  hmBlockNodeToBlockNode,
3891
+ hmBlockToEditorBlock,
3892
+ hmBlocksToEditorContent,
3182
3893
  hmIdPathToEntityQueryPath,
3183
3894
  isGrobidAvailable,
3184
3895
  isSurrogate,
3896
+ markdownBlockNodesToHMBlockNodes,
3185
3897
  matchBlockIds,
3186
3898
  packBaseId,
3187
3899
  packHmId,
3188
3900
  parseCustomURL,
3901
+ parseDraftFilename,
3189
3902
  parseFragment,
3190
3903
  parseFrontmatter,
3191
3904
  parseInlineFormatting,
3192
3905
  parseMarkdown,
3193
3906
  pdfToBlocks,
3194
3907
  processFulltextDocument,
3908
+ pushSpanToAnnotation,
3195
3909
  resolveDocumentState,
3196
3910
  resolveFileLinksInBlocks,
3197
3911
  serializeBlockRange,
3198
3912
  shouldAutoLinkParent,
3199
3913
  signDocumentChange,
3200
3914
  signPreparedChange,
3915
+ slugify,
3201
3916
  teiToBlocks,
3202
3917
  trimTrailingEmptyBlocks,
3203
3918
  unpackHmId,
3919
+ updateComment,
3204
3920
  updateContact
3205
3921
  };