@portabletext/markdown 1.2.0 → 1.3.1

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.js CHANGED
@@ -156,8 +156,14 @@ const DefaultBlockSpacingRenderer = ({
156
156
  value,
157
157
  listIndex
158
158
  }) => {
159
- const listStyle = value.listItem || "bullet", level = value.level || 1;
160
- return listStyle === "number" ? `${" ".repeat(level - 1)}${listIndex ?? 1}. ${children}` : `${" ".repeat(level - 1)}- ${children}`;
159
+ const listStyle = value.listItem || "bullet", level = value.level || 1, indent = " ".repeat(level - 1);
160
+ if (listStyle === "number")
161
+ return `${indent}${listIndex ?? 1}. ${children}`;
162
+ if (listStyle === "task") {
163
+ const marker = "checked" in value && typeof value.checked == "boolean" && value.checked ? "[x]" : "[ ]";
164
+ return `${indent}- ${marker} ${children}`;
165
+ }
166
+ return `${indent}- ${children}`;
161
167
  }, DefaultUnknownListItemRenderer = ({
162
168
  children
163
169
  }) => `- ${children}
@@ -221,27 +227,22 @@ ${value.code}
221
227
  const alt = escapeImageAndLinkText(value.alt ?? ""), title = value.title ? ` "${escapeImageAndLinkTitle(value.title)}"` : "";
222
228
  return `![${alt}](${value.src}${title})`;
223
229
  }, DefaultTableRenderer = ({ value, renderNode }) => {
224
- const headerRows = value.headerRows || 0, rows = value.rows, lines = [], getCellText = (cellBlocks) => cellBlocks.map(
230
+ const rows = value.rows, headerRow = rows.at(0);
231
+ if (!headerRow)
232
+ return "";
233
+ const getCellText = (cellBlocks) => cellBlocks.map(
225
234
  (block, index) => renderNode({
226
235
  node: block,
227
236
  index,
228
237
  isInline: !1,
229
238
  renderNode
230
239
  })
231
- ).join(" ").trim();
232
- for (let i = 0; i < headerRows; i++) {
233
- const row = rows[i];
234
- if (row) {
235
- const cellTexts = row.cells.map((cell) => getCellText(cell.value));
236
- lines.push(`| ${cellTexts.join(" | ")} |`);
237
- }
238
- }
239
- if (headerRows > 0 && rows[0]) {
240
- const separators = rows[0].cells.map(() => " --- ");
241
- lines.push(`|${separators.join("|")}|`);
242
- }
243
- for (let i = headerRows; i < rows.length; i++) {
244
- const row = rows[i];
240
+ ).join(" ").trim(), lines = [], headerCells = headerRow.cells.map((cell) => getCellText(cell.value));
241
+ lines.push(`| ${headerCells.join(" | ")} |`);
242
+ const separators = headerRow.cells.map(() => " --- ");
243
+ lines.push(`|${separators.join("|")}|`);
244
+ for (let i = 1; i < rows.length; i++) {
245
+ const row = rows.at(i);
245
246
  if (row) {
246
247
  const cellTexts = row.cells.map((cell) => getCellText(cell.value));
247
248
  lines.push(`| ${cellTexts.join(" | ")} |`);
@@ -264,7 +265,52 @@ ${value.code}
264
265
  `);
265
266
  return `> [!${value.tone.toUpperCase()}]
266
267
  ${prefixed}`;
267
- }, DefaultUnknownTypeRenderer = ({
268
+ }, DefaultBlockquoteObjectRenderer = ({ value, renderNode }) => value.content.map(
269
+ (block, index) => renderNode({
270
+ node: { ...block, style: "normal" },
271
+ index,
272
+ isInline: !1,
273
+ renderNode
274
+ })
275
+ ).join(`
276
+
277
+ `).split(`
278
+ `).map((line) => line === "" ? ">" : `> ${line}`).join(`
279
+ `), DefaultListRenderer = ({ value, renderNode }) => {
280
+ const itemSeparator = value.items.some((item) => item.content.filter(
281
+ (block) => block._type !== "list"
282
+ ).length > 1) ? `
283
+
284
+ ` : `
285
+ `;
286
+ return value.items.map((item, itemIndex) => {
287
+ const marker = getListMarker(value.kind, itemIndex, item.checked), indentWidth = value.kind === "task" ? 2 : marker.length, indent = " ".repeat(indentWidth), renderedBlocks = item.content.map((block, blockIndex) => ({
288
+ isNestedList: block._type === "list",
289
+ text: renderNode({
290
+ node: block,
291
+ index: blockIndex,
292
+ isInline: !1,
293
+ renderNode
294
+ })
295
+ })), [first, ...rest] = renderedBlocks, head = `${marker}${first?.text ?? ""}`.trimEnd();
296
+ if (rest.length === 0)
297
+ return head;
298
+ const tail = rest.map((rendered) => {
299
+ const indented = rendered.text.split(`
300
+ `).map((line) => line === "" ? "" : `${indent}${line}`).join(`
301
+ `);
302
+ return rendered.isNestedList ? `
303
+ ${indented}` : `
304
+
305
+ ${indented}`;
306
+ }).join("");
307
+ return `${head}${tail}`;
308
+ }).join(itemSeparator);
309
+ };
310
+ function getListMarker(kind, itemIndex, checked) {
311
+ return kind === "number" ? `${itemIndex + 1}. ` : kind === "task" ? checked ? "- [x] " : "- [ ] " : "- ";
312
+ }
313
+ const DefaultUnknownTypeRenderer = ({
268
314
  value,
269
315
  isInline
270
316
  }) => {
@@ -363,6 +409,8 @@ const normalStyleDefinition = {
363
409
  name: "number"
364
410
  }, defaultUnorderedListItemDefinition = {
365
411
  name: "bullet"
412
+ }, defaultTaskListItemDefinition = {
413
+ name: "task"
366
414
  }, defaultStrongDecoratorDefinition = {
367
415
  name: "strong"
368
416
  }, defaultEmDecoratorDefinition = {
@@ -409,6 +457,9 @@ const normalStyleDefinition = {
409
457
  ]
410
458
  }, defaultSchema = compileSchema(
411
459
  defineSchema({
460
+ block: {
461
+ fields: [{ name: "checked", type: "boolean" }]
462
+ },
412
463
  styles: [
413
464
  normalStyleDefinition,
414
465
  h1StyleDefinition,
@@ -421,7 +472,8 @@ const normalStyleDefinition = {
421
472
  ],
422
473
  lists: [
423
474
  defaultOrderedListItemDefinition,
424
- defaultUnorderedListItemDefinition
475
+ defaultUnorderedListItemDefinition,
476
+ defaultTaskListItemDefinition
425
477
  ],
426
478
  decorators: [
427
479
  defaultStrongDecoratorDefinition,
@@ -525,7 +577,8 @@ const codeBlockMatcher = ({ context, value, isInline }) => {
525
577
  },
526
578
  listItem: {
527
579
  number: buildListItemMatcher(defaultOrderedListItemDefinition),
528
- bullet: buildListItemMatcher(defaultUnorderedListItemDefinition)
580
+ bullet: buildListItemMatcher(defaultUnorderedListItemDefinition),
581
+ task: buildListItemMatcher(defaultTaskListItemDefinition)
529
582
  },
530
583
  marks: {
531
584
  strong: buildDecoratorMatcher(defaultStrongDecoratorDefinition),
@@ -577,11 +630,51 @@ function markdownToPortableText(markdown, options) {
577
630
  html: !0,
578
631
  linkify: !0,
579
632
  typographer: !0
580
- }).enable(["strikethrough", "table"]).use(alert).parse(markdown, {}), portableText = [];
633
+ }).enable(["strikethrough", "table"]).use(alert).parse(markdown, {}), taskCheckedByListItemIndex = /* @__PURE__ */ new Map();
634
+ for (let i = 0; i < tokens.length; i++) {
635
+ if (tokens[i]?.type !== "list_item_open")
636
+ continue;
637
+ let inlineIndex = -1;
638
+ for (let j = i + 1; j < tokens.length; j++) {
639
+ const candidate = tokens[j];
640
+ if (candidate) {
641
+ if (candidate.type === "list_item_close")
642
+ break;
643
+ if (candidate.type === "inline") {
644
+ inlineIndex = j;
645
+ break;
646
+ }
647
+ }
648
+ }
649
+ if (inlineIndex === -1)
650
+ continue;
651
+ const inlineToken = tokens[inlineIndex];
652
+ if (!inlineToken)
653
+ continue;
654
+ const match = inlineToken.content.match(/^\[([ xX])\] /);
655
+ if (!match)
656
+ continue;
657
+ const checked = match[1] !== " ";
658
+ taskCheckedByListItemIndex.set(i, checked), inlineToken.content = inlineToken.content.slice(match[0].length);
659
+ const firstChild = inlineToken.children?.[0];
660
+ firstChild && typeof firstChild.content == "string" && (firstChild.content = firstChild.content.slice(match[0].length));
661
+ }
662
+ const portableText = [];
581
663
  let currentBlock = null;
582
664
  const currentListStack = [], markDefRefs = [];
583
- let currentMarkDefs = [], currentBlockquoteStyle = null, inListItem = !1, calloutStartIndex = null, calloutType = null, currentTable = null, currentTableRow = null, inTableHead = !1;
584
- const startBlock = (style) => {
665
+ let currentMarkDefs = [], currentBlockquoteStyle = null, inListItem = !1, calloutStartIndex = null, calloutStartTarget = null, calloutType = null;
666
+ const blockquoteStack = [];
667
+ let currentTable = null, currentTableRow = null, inTableHead = !1;
668
+ const listContainerStack = [], blockTarget = () => {
669
+ for (let i = listContainerStack.length - 1; i >= 0; i--) {
670
+ const frame = listContainerStack[i];
671
+ if (frame && frame.currentItem)
672
+ return frame.currentItem.content;
673
+ }
674
+ return portableText;
675
+ }, pushBlock = (block) => {
676
+ blockTarget().push(block);
677
+ }, startBlock = (style) => {
585
678
  flushBlock(), currentBlock = {
586
679
  _type: "block",
587
680
  style,
@@ -595,7 +688,7 @@ function markdownToPortableText(markdown, options) {
595
688
  _key: consolidatedOptions.keyGenerator(),
596
689
  text: "",
597
690
  marks: []
598
- }), currentBlock.markDefs = currentMarkDefs, portableText.push(currentBlock), currentBlock = null, currentMarkDefs = []);
691
+ }), currentBlock.markDefs = currentMarkDefs, pushBlock(currentBlock), currentBlock = null, currentMarkDefs = []);
599
692
  }, addSpan = (text) => {
600
693
  if (text.length === 0)
601
694
  return;
@@ -614,7 +707,7 @@ function markdownToPortableText(markdown, options) {
614
707
  text,
615
708
  marks: [...markDefRefs]
616
709
  });
617
- }, listLevel = () => currentListStack.length, ensureListBlock = (listItem) => {
710
+ }, listLevel = () => currentListStack.length, ensureListBlock = (listItem, checked) => {
618
711
  if (!currentBlock) {
619
712
  const style = currentBlockquoteStyle ?? consolidatedOptions.block.normal({
620
713
  context: { schema: consolidatedOptions.schema }
@@ -623,533 +716,679 @@ function markdownToPortableText(markdown, options) {
623
716
  }
624
717
  if (!currentBlock)
625
718
  throw new Error("Expected current block");
626
- (currentBlock.listItem !== listItem || currentBlock.level !== listLevel()) && (currentBlock.listItem = listItem, currentBlock.level = listLevel());
719
+ (currentBlock.listItem !== listItem || currentBlock.level !== listLevel()) && (currentBlock.listItem = listItem, currentBlock.level = listLevel()), checked !== void 0 && (currentBlock.checked = checked);
627
720
  };
628
- for (const token of tokens)
629
- switch (token.type) {
630
- // Paragraphs
631
- case "paragraph_open": {
632
- if (inListItem) {
633
- if (!currentBlock) {
634
- const listType = currentListStack.at(-1);
635
- listType && ensureListBlock(listType);
721
+ for (let tokenIndex = 0; tokenIndex < tokens.length; tokenIndex++) {
722
+ const token = tokens[tokenIndex];
723
+ if (token)
724
+ switch (token.type) {
725
+ // Paragraphs
726
+ case "paragraph_open": {
727
+ if (inListItem) {
728
+ if (listContainerStack.at(-1)) {
729
+ if (!currentBlock) {
730
+ const style2 = currentBlockquoteStyle ?? consolidatedOptions.block.normal({
731
+ context: { schema: consolidatedOptions.schema }
732
+ }) ?? "normal";
733
+ startBlock(style2);
734
+ }
735
+ break;
736
+ }
737
+ if (!currentBlock) {
738
+ const listType = currentListStack.at(-1);
739
+ listType && ensureListBlock(listType);
740
+ }
741
+ break;
636
742
  }
743
+ const style = currentBlockquoteStyle ?? consolidatedOptions.block.normal({
744
+ context: { schema: consolidatedOptions.schema }
745
+ });
746
+ if (!style) {
747
+ console.warn('No default style found, using "normal"'), startBlock("normal");
748
+ break;
749
+ }
750
+ startBlock(style);
637
751
  break;
638
752
  }
639
- const style = currentBlockquoteStyle ?? consolidatedOptions.block.normal({
640
- context: { schema: consolidatedOptions.schema }
641
- });
642
- if (!style) {
643
- console.warn('No default style found, using "normal"'), startBlock("normal");
644
- break;
645
- }
646
- startBlock(style);
647
- break;
648
- }
649
- case "paragraph_close":
650
- if (inListItem)
753
+ case "paragraph_close":
754
+ if (inListItem) {
755
+ listContainerStack.at(-1) && flushBlock();
756
+ break;
757
+ }
758
+ flushBlock();
651
759
  break;
652
- flushBlock();
653
- break;
654
- // Headings
655
- case "heading_open": {
656
- const level = Number(token?.tag?.slice(1)), headingMatcher = {
657
- 1: consolidatedOptions.block.h1,
658
- 2: consolidatedOptions.block.h2,
659
- 3: consolidatedOptions.block.h3,
660
- 4: consolidatedOptions.block.h4,
661
- 5: consolidatedOptions.block.h5,
662
- 6: consolidatedOptions.block.h6
663
- }[level], style = headingMatcher?.({
664
- context: { schema: consolidatedOptions.schema }
665
- }) ?? consolidatedOptions.block.normal({
666
- context: { schema: consolidatedOptions.schema }
667
- });
668
- if (!style) {
669
- console.warn('No heading style found, using "normal"'), startBlock("normal");
760
+ // Headings
761
+ case "heading_open": {
762
+ const level = Number(token?.tag?.slice(1)), headingMatcher = {
763
+ 1: consolidatedOptions.block.h1,
764
+ 2: consolidatedOptions.block.h2,
765
+ 3: consolidatedOptions.block.h3,
766
+ 4: consolidatedOptions.block.h4,
767
+ 5: consolidatedOptions.block.h5,
768
+ 6: consolidatedOptions.block.h6
769
+ }[level], style = headingMatcher?.({
770
+ context: { schema: consolidatedOptions.schema }
771
+ }) ?? consolidatedOptions.block.normal({
772
+ context: { schema: consolidatedOptions.schema }
773
+ });
774
+ if (!style) {
775
+ console.warn('No heading style found, using "normal"'), startBlock("normal");
776
+ break;
777
+ }
778
+ startBlock(style);
670
779
  break;
671
780
  }
672
- startBlock(style);
673
- break;
674
- }
675
- case "heading_close":
676
- flushBlock();
677
- break;
678
- // Blockquote
679
- case "blockquote_open": {
680
- flushBlock(), currentBlockquoteStyle = consolidatedOptions.block.blockquote({
681
- context: { schema: consolidatedOptions.schema }
682
- }) ?? consolidatedOptions.block.normal({
683
- context: { schema: consolidatedOptions.schema }
684
- }) ?? "normal";
685
- break;
686
- }
687
- case "blockquote_close": {
688
- flushBlock(), currentBlockquoteStyle = null;
689
- break;
690
- }
691
- // Lists
692
- case "bullet_list_open": {
693
- const listItem = consolidatedOptions.listItem.bullet({
694
- context: { schema: consolidatedOptions.schema }
695
- });
696
- if (!listItem) {
697
- currentListStack.push(null);
781
+ case "heading_close":
782
+ flushBlock();
783
+ break;
784
+ // Blockquote
785
+ case "blockquote_open": {
786
+ if (flushBlock(), consolidatedOptions.types.blockquote) {
787
+ const startTarget = blockTarget();
788
+ blockquoteStack.push({
789
+ startTarget,
790
+ startIndex: startTarget.length
791
+ });
792
+ break;
793
+ }
794
+ currentBlockquoteStyle = consolidatedOptions.block.blockquote({
795
+ context: { schema: consolidatedOptions.schema }
796
+ }) ?? consolidatedOptions.block.normal({
797
+ context: { schema: consolidatedOptions.schema }
798
+ }) ?? "normal";
698
799
  break;
699
800
  }
700
- currentListStack.push(listItem);
701
- break;
702
- }
703
- case "ordered_list_open": {
704
- const listItem = consolidatedOptions.listItem.number({
705
- context: { schema: consolidatedOptions.schema }
706
- });
707
- if (!listItem) {
708
- currentListStack.push(null);
801
+ case "blockquote_close": {
802
+ if (flushBlock(), consolidatedOptions.types.blockquote && blockquoteStack.length > 0) {
803
+ const frame = blockquoteStack.pop();
804
+ if (frame) {
805
+ const contentBlocks = frame.startTarget.splice(
806
+ frame.startIndex
807
+ ), blockquoteObject = consolidatedOptions.types.blockquote({
808
+ context: {
809
+ schema: consolidatedOptions.schema,
810
+ keyGenerator: consolidatedOptions.keyGenerator
811
+ },
812
+ value: { content: contentBlocks },
813
+ isInline: !1
814
+ });
815
+ if (blockquoteObject)
816
+ pushBlock(blockquoteObject);
817
+ else {
818
+ const blockquoteStyle = consolidatedOptions.block.blockquote({
819
+ context: { schema: consolidatedOptions.schema }
820
+ }) ?? consolidatedOptions.block.normal({
821
+ context: { schema: consolidatedOptions.schema }
822
+ }) ?? "blockquote";
823
+ for (const block of contentBlocks)
824
+ block._type === "block" ? pushBlock({
825
+ ...block,
826
+ style: blockquoteStyle
827
+ }) : pushBlock(block);
828
+ }
829
+ }
830
+ break;
831
+ }
832
+ currentBlockquoteStyle = null;
709
833
  break;
710
834
  }
711
- currentListStack.push(listItem);
712
- break;
713
- }
714
- case "bullet_list_close":
715
- case "ordered_list_close":
716
- currentListStack.pop();
717
- break;
718
- case "list_item_open": {
719
- const listType = currentListStack.at(-1);
720
- if (listType === void 0)
721
- throw new Error("Expected an open list");
722
- if (currentBlock && flushBlock(), listType === null) {
723
- const style = currentBlockquoteStyle ?? consolidatedOptions.block.normal({
835
+ // Lists
836
+ case "bullet_list_open": {
837
+ if (flushBlock(), consolidatedOptions.types.list) {
838
+ listContainerStack.push({
839
+ kind: "bullet",
840
+ items: [],
841
+ currentItem: null
842
+ }), currentListStack.push(null);
843
+ break;
844
+ }
845
+ const listItem = consolidatedOptions.listItem.bullet({
724
846
  context: { schema: consolidatedOptions.schema }
725
847
  });
726
- style ? startBlock(style) : (console.warn('No default style found, using "normal"'), startBlock("normal")), inListItem = !0;
848
+ if (listContainerStack.push(null), !listItem) {
849
+ currentListStack.push(null);
850
+ break;
851
+ }
852
+ currentListStack.push(listItem);
727
853
  break;
728
854
  }
729
- ensureListBlock(listType), inListItem = !0;
730
- break;
731
- }
732
- case "list_item_close":
733
- inListItem = !1, flushBlock();
734
- break;
735
- // Code fences / blocks
736
- case "fence": {
737
- flushBlock();
738
- const language = token.info.trim() || void 0, code = token.content.replace(/\n$/, ""), codeObject = consolidatedOptions.types.code({
739
- context: {
740
- schema: consolidatedOptions.schema,
741
- keyGenerator: consolidatedOptions.keyGenerator
742
- },
743
- value: { language, code },
744
- isInline: !1
745
- });
746
- if (!codeObject) {
747
- const style = consolidatedOptions.block.normal({
855
+ case "ordered_list_open": {
856
+ if (flushBlock(), consolidatedOptions.types.list) {
857
+ listContainerStack.push({
858
+ kind: "number",
859
+ items: [],
860
+ currentItem: null
861
+ }), currentListStack.push(null);
862
+ break;
863
+ }
864
+ const listItem = consolidatedOptions.listItem.number({
748
865
  context: { schema: consolidatedOptions.schema }
749
866
  });
750
- style ? startBlock(style) : (console.warn('No default style found, using "normal"'), startBlock("normal")), addSpan(code), flushBlock();
867
+ if (listContainerStack.push(null), !listItem) {
868
+ currentListStack.push(null);
869
+ break;
870
+ }
871
+ currentListStack.push(listItem);
751
872
  break;
752
873
  }
753
- portableText.push(codeObject);
754
- break;
755
- }
756
- // Horizontal rule
757
- case "hr": {
758
- flushBlock();
759
- const hrObject = consolidatedOptions.types.horizontalRule({
760
- context: {
761
- schema: consolidatedOptions.schema,
762
- keyGenerator: consolidatedOptions.keyGenerator
763
- },
764
- value: {},
765
- isInline: !1
766
- });
767
- if (!hrObject) {
768
- const style = consolidatedOptions.block.normal({
769
- context: { schema: consolidatedOptions.schema }
770
- });
771
- style ? startBlock(style) : (console.warn('No default style found, using "normal"'), startBlock("normal")), addSpan("---"), flushBlock();
874
+ case "bullet_list_close":
875
+ case "ordered_list_close": {
876
+ const frame = listContainerStack.pop();
877
+ if (currentListStack.pop(), frame && consolidatedOptions.types.list) {
878
+ const kind = frame.items.some(
879
+ (item) => "checked" in item
880
+ ) ? "task" : frame.kind, listObject = consolidatedOptions.types.list({
881
+ context: {
882
+ schema: consolidatedOptions.schema,
883
+ keyGenerator: consolidatedOptions.keyGenerator
884
+ },
885
+ value: { kind, items: frame.items },
886
+ isInline: !1
887
+ });
888
+ if (listObject)
889
+ pushBlock(listObject);
890
+ else {
891
+ const flatListItem = (kind === "task" ? consolidatedOptions.listItem.task?.({
892
+ context: { schema: consolidatedOptions.schema }
893
+ }) : kind === "number" ? consolidatedOptions.listItem.number({
894
+ context: { schema: consolidatedOptions.schema }
895
+ }) : consolidatedOptions.listItem.bullet({
896
+ context: { schema: consolidatedOptions.schema }
897
+ })) ?? null, level = listContainerStack.length + 1;
898
+ for (const item of frame.items)
899
+ for (const block of item.content)
900
+ if (block._type === "block" && flatListItem !== null && !("listItem" in block)) {
901
+ const flatBlock = {
902
+ ...block,
903
+ listItem: flatListItem,
904
+ level,
905
+ ...item.checked === void 0 ? {} : { checked: item.checked }
906
+ };
907
+ pushBlock(flatBlock);
908
+ } else
909
+ pushBlock(block);
910
+ }
911
+ }
772
912
  break;
773
913
  }
774
- portableText.push(hrObject);
775
- break;
776
- }
777
- // HTML block
778
- case "html_block": {
779
- flushBlock();
780
- const htmlContent = token.content.trim();
781
- if (!htmlContent)
914
+ case "list_item_open": {
915
+ const frame = listContainerStack.at(-1);
916
+ if (currentBlock && flushBlock(), frame) {
917
+ const taskChecked2 = taskCheckedByListItemIndex.get(tokenIndex);
918
+ frame.currentItem = {
919
+ _type: "list-item",
920
+ _key: consolidatedOptions.keyGenerator(),
921
+ ...taskChecked2 === void 0 ? {} : { checked: taskChecked2 },
922
+ content: []
923
+ }, inListItem = !0;
924
+ break;
925
+ }
926
+ const baseListType = currentListStack.at(-1);
927
+ if (baseListType === void 0)
928
+ throw new Error("Expected an open list");
929
+ const taskChecked = taskCheckedByListItemIndex.get(tokenIndex);
930
+ let listType = baseListType, checked;
931
+ if (taskChecked !== void 0) {
932
+ const taskListType = consolidatedOptions.listItem.task?.({
933
+ context: { schema: consolidatedOptions.schema }
934
+ });
935
+ taskListType && (listType = taskListType, checked = taskChecked);
936
+ }
937
+ if (listType === null) {
938
+ const style = currentBlockquoteStyle ?? consolidatedOptions.block.normal({
939
+ context: { schema: consolidatedOptions.schema }
940
+ });
941
+ style ? startBlock(style) : (console.warn('No default style found, using "normal"'), startBlock("normal")), inListItem = !0;
942
+ break;
943
+ }
944
+ ensureListBlock(listType, checked), inListItem = !0;
782
945
  break;
783
- const htmlObject = consolidatedOptions.types.html({
784
- context: {
785
- schema: consolidatedOptions.schema,
786
- keyGenerator: consolidatedOptions.keyGenerator
787
- },
788
- value: { html: htmlContent },
789
- isInline: !1
790
- });
791
- if (!htmlObject) {
792
- const style = consolidatedOptions.block.normal({
793
- context: { schema: consolidatedOptions.schema }
794
- });
795
- style ? startBlock(style) : (console.warn('No default style found, using "normal"'), startBlock("normal")), addSpan(htmlContent), flushBlock();
946
+ }
947
+ case "list_item_close": {
948
+ const frame = listContainerStack.at(-1);
949
+ if (frame && frame.currentItem) {
950
+ flushBlock(), frame.items.push(frame.currentItem), frame.currentItem = null, inListItem = !1;
951
+ break;
952
+ }
953
+ inListItem = !1, flushBlock();
796
954
  break;
797
955
  }
798
- portableText.push(htmlObject);
799
- break;
800
- }
801
- case "code_block": {
802
- flushBlock();
803
- const code = token.content.replace(/\n$/, ""), codeObject = consolidatedOptions.types.code({
804
- context: {
805
- schema: consolidatedOptions.schema,
806
- keyGenerator: consolidatedOptions.keyGenerator
807
- },
808
- value: { language: void 0, code },
809
- isInline: !1
810
- });
811
- if (codeObject)
812
- portableText.push(codeObject);
813
- else {
814
- const style = consolidatedOptions.block.normal({
815
- context: { schema: consolidatedOptions.schema }
956
+ // Code fences / blocks
957
+ case "fence": {
958
+ flushBlock();
959
+ const language = token.info.trim() || void 0, code = token.content.replace(/\n$/, ""), codeObject = consolidatedOptions.types.code({
960
+ context: {
961
+ schema: consolidatedOptions.schema,
962
+ keyGenerator: consolidatedOptions.keyGenerator
963
+ },
964
+ value: { language, code },
965
+ isInline: !1
816
966
  });
817
- style ? startBlock(style) : (console.warn('No default style found, using "normal"'), startBlock("normal")), addSpan(code), flushBlock();
818
- }
819
- break;
820
- }
821
- // Tables
822
- case "table_open":
823
- flushBlock(), currentTable = { rows: [], headerRows: 0 };
824
- break;
825
- case "table_close": {
826
- if (!currentTable)
967
+ if (!codeObject) {
968
+ const style = consolidatedOptions.block.normal({
969
+ context: { schema: consolidatedOptions.schema }
970
+ });
971
+ style ? startBlock(style) : (console.warn('No default style found, using "normal"'), startBlock("normal")), addSpan(code), flushBlock();
972
+ break;
973
+ }
974
+ pushBlock(codeObject);
827
975
  break;
828
- if (consolidatedOptions.types.table) {
829
- const tableObject = consolidatedOptions.types.table({
976
+ }
977
+ // Horizontal rule
978
+ case "hr": {
979
+ flushBlock();
980
+ const hrObject = consolidatedOptions.types.horizontalRule({
830
981
  context: {
831
982
  schema: consolidatedOptions.schema,
832
983
  keyGenerator: consolidatedOptions.keyGenerator
833
984
  },
834
- value: {
835
- rows: currentTable.rows,
836
- headerRows: currentTable.headerRows > 0 ? currentTable.headerRows : void 0
837
- },
985
+ value: {},
838
986
  isInline: !1
839
987
  });
840
- tableObject ? portableText.push(tableObject) : flattenTable(currentTable, portableText);
841
- } else
842
- flattenTable(currentTable, portableText);
843
- currentTable = null;
844
- break;
845
- }
846
- case "thead_open":
847
- inTableHead = !0;
848
- break;
849
- case "thead_close":
850
- inTableHead = !1;
851
- break;
852
- case "tbody_open":
853
- case "tbody_close":
854
- break;
855
- case "tr_open":
856
- currentTableRow = [];
857
- break;
858
- case "tr_close":
859
- currentTable && currentTableRow && (currentTable.rows.push({
860
- _key: consolidatedOptions.keyGenerator(),
861
- _type: "row",
862
- cells: currentTableRow
863
- }), inTableHead && currentTable.headerRows++), currentTableRow = null;
864
- break;
865
- case "th_open":
866
- case "td_open": {
867
- const style = consolidatedOptions.block.normal({
868
- context: { schema: consolidatedOptions.schema }
869
- });
870
- style ? startBlock(style) : (console.warn('No default style found, using "normal"'), startBlock("normal"));
871
- break;
872
- }
873
- case "th_close":
874
- case "td_close": {
875
- flushBlock();
876
- const cellBlocks = [];
877
- if (portableText.length > 0) {
878
- const lastBlock = portableText.at(-1);
879
- lastBlock && lastBlock._type === "block" && cellBlocks.push(portableText.pop());
880
- }
881
- cellBlocks.length === 0 && cellBlocks.push({
882
- _type: "block",
883
- style: consolidatedOptions.block.normal({
884
- context: { schema: consolidatedOptions.schema }
885
- }) || "normal",
886
- children: [
887
- {
888
- _type: consolidatedOptions.schema.span.name,
889
- _key: consolidatedOptions.keyGenerator(),
890
- text: "",
891
- marks: []
892
- }
893
- ],
894
- _key: consolidatedOptions.keyGenerator(),
895
- markDefs: []
896
- });
897
- const firstBlock = cellBlocks[0];
898
- if (cellBlocks.length === 1 && firstBlock && firstBlock._type === "block" && "children" in firstBlock && Array.isArray(firstBlock.children) && firstBlock.children.length === 1) {
899
- const onlyChild = firstBlock.children[0];
900
- typeof onlyChild == "object" && onlyChild !== null && "_type" in onlyChild && onlyChild._type !== consolidatedOptions.schema.span.name && onlyChild._type === "image" && (cellBlocks[0] = onlyChild);
988
+ if (!hrObject) {
989
+ const style = consolidatedOptions.block.normal({
990
+ context: { schema: consolidatedOptions.schema }
991
+ });
992
+ style ? startBlock(style) : (console.warn('No default style found, using "normal"'), startBlock("normal")), addSpan("---"), flushBlock();
993
+ break;
994
+ }
995
+ pushBlock(hrObject);
996
+ break;
901
997
  }
902
- currentTableRow !== null && currentTableRow.push({
903
- _type: "cell",
904
- _key: consolidatedOptions.keyGenerator(),
905
- value: cellBlocks
906
- });
907
- break;
908
- }
909
- // Inline container
910
- case "inline": {
911
- const inTableCell = currentTableRow !== null;
912
- if (token.children?.length === 1 && token.children[0]?.type === "image") {
913
- const imageToken = token.children[0];
914
- if (!imageToken)
998
+ // HTML block
999
+ case "html_block": {
1000
+ flushBlock();
1001
+ const htmlContent = token.content.trim();
1002
+ if (!htmlContent)
915
1003
  break;
916
- const src = imageToken.attrs?.find(([name]) => name === "src")?.at(1) || "", alt = unescapeImageAndLinkText(imageToken.content || ""), title = imageToken.attrs?.find(([name]) => name === "title")?.at(1) || void 0, blockImageObject = consolidatedOptions.types.image({
1004
+ const htmlObject = consolidatedOptions.types.html({
917
1005
  context: {
918
1006
  schema: consolidatedOptions.schema,
919
1007
  keyGenerator: consolidatedOptions.keyGenerator
920
1008
  },
921
- value: { src, alt, title },
1009
+ value: { html: htmlContent },
922
1010
  isInline: !1
923
1011
  });
924
- if (blockImageObject) {
925
- inTableCell ? currentBlock && "children" in currentBlock && currentBlock.children.push(
926
- blockImageObject
927
- ) : (currentBlock && "children" in currentBlock && currentBlock.children.length > 0 ? flushBlock() : (currentBlock = null, currentMarkDefs = []), portableText.push(blockImageObject));
1012
+ if (!htmlObject) {
1013
+ const style = consolidatedOptions.block.normal({
1014
+ context: { schema: consolidatedOptions.schema }
1015
+ });
1016
+ style ? startBlock(style) : (console.warn('No default style found, using "normal"'), startBlock("normal")), addSpan(htmlContent), flushBlock();
928
1017
  break;
929
1018
  }
930
- const inlineImageObject = consolidatedOptions.types.image({
1019
+ pushBlock(htmlObject);
1020
+ break;
1021
+ }
1022
+ case "code_block": {
1023
+ flushBlock();
1024
+ const code = token.content.replace(/\n$/, ""), codeObject = consolidatedOptions.types.code({
931
1025
  context: {
932
1026
  schema: consolidatedOptions.schema,
933
1027
  keyGenerator: consolidatedOptions.keyGenerator
934
1028
  },
935
- value: { src, alt, title },
936
- isInline: !0
1029
+ value: { language: void 0, code },
1030
+ isInline: !1
937
1031
  });
938
- if (inlineImageObject) {
939
- if (!currentBlock)
940
- if (inListItem) {
941
- const listType = currentListStack.at(-1);
942
- listType && ensureListBlock(listType);
943
- } else {
944
- const style = consolidatedOptions.block.normal({
945
- context: { schema: consolidatedOptions.schema }
946
- });
947
- style && startBlock(style);
948
- }
949
- currentBlock && "children" in currentBlock && currentBlock.children.push(
950
- inlineImageObject
951
- );
1032
+ if (codeObject)
1033
+ pushBlock(codeObject);
1034
+ else {
1035
+ const style = consolidatedOptions.block.normal({
1036
+ context: { schema: consolidatedOptions.schema }
1037
+ });
1038
+ style ? startBlock(style) : (console.warn('No default style found, using "normal"'), startBlock("normal")), addSpan(code), flushBlock();
1039
+ }
1040
+ break;
1041
+ }
1042
+ // Tables
1043
+ case "table_open":
1044
+ flushBlock(), currentTable = { rows: [], headerRows: 0 };
1045
+ break;
1046
+ case "table_close": {
1047
+ if (!currentTable)
952
1048
  break;
1049
+ if (consolidatedOptions.types.table) {
1050
+ const tableObject = consolidatedOptions.types.table({
1051
+ context: {
1052
+ schema: consolidatedOptions.schema,
1053
+ keyGenerator: consolidatedOptions.keyGenerator
1054
+ },
1055
+ value: {
1056
+ rows: currentTable.rows,
1057
+ headerRows: currentTable.headerRows > 0 ? currentTable.headerRows : void 0
1058
+ },
1059
+ isInline: !1
1060
+ });
1061
+ tableObject ? pushBlock(tableObject) : flattenTable(
1062
+ currentTable,
1063
+ blockTarget()
1064
+ );
1065
+ } else
1066
+ flattenTable(currentTable, blockTarget());
1067
+ currentTable = null;
1068
+ break;
1069
+ }
1070
+ case "thead_open":
1071
+ inTableHead = !0;
1072
+ break;
1073
+ case "thead_close":
1074
+ inTableHead = !1;
1075
+ break;
1076
+ case "tbody_open":
1077
+ case "tbody_close":
1078
+ break;
1079
+ case "tr_open":
1080
+ currentTableRow = [];
1081
+ break;
1082
+ case "tr_close":
1083
+ currentTable && currentTableRow && (currentTable.rows.push({
1084
+ _key: consolidatedOptions.keyGenerator(),
1085
+ _type: "row",
1086
+ cells: currentTableRow
1087
+ }), inTableHead && currentTable.headerRows++), currentTableRow = null;
1088
+ break;
1089
+ case "th_open":
1090
+ case "td_open": {
1091
+ const style = consolidatedOptions.block.normal({
1092
+ context: { schema: consolidatedOptions.schema }
1093
+ });
1094
+ style ? startBlock(style) : (console.warn('No default style found, using "normal"'), startBlock("normal"));
1095
+ break;
1096
+ }
1097
+ case "th_close":
1098
+ case "td_close": {
1099
+ flushBlock();
1100
+ const cellBlocks = [], target = blockTarget();
1101
+ if (target.length > 0) {
1102
+ const lastBlock = target.at(-1);
1103
+ lastBlock && lastBlock._type === "block" && cellBlocks.push(target.pop());
1104
+ }
1105
+ cellBlocks.length === 0 && cellBlocks.push({
1106
+ _type: "block",
1107
+ style: consolidatedOptions.block.normal({
1108
+ context: { schema: consolidatedOptions.schema }
1109
+ }) || "normal",
1110
+ children: [
1111
+ {
1112
+ _type: consolidatedOptions.schema.span.name,
1113
+ _key: consolidatedOptions.keyGenerator(),
1114
+ text: "",
1115
+ marks: []
1116
+ }
1117
+ ],
1118
+ _key: consolidatedOptions.keyGenerator(),
1119
+ markDefs: []
1120
+ });
1121
+ const firstBlock = cellBlocks[0];
1122
+ if (cellBlocks.length === 1 && firstBlock && firstBlock._type === "block" && "children" in firstBlock && Array.isArray(firstBlock.children) && firstBlock.children.length === 1) {
1123
+ const onlyChild = firstBlock.children[0];
1124
+ typeof onlyChild == "object" && onlyChild !== null && "_type" in onlyChild && onlyChild._type !== consolidatedOptions.schema.span.name && onlyChild._type === "image" && (cellBlocks[0] = onlyChild);
953
1125
  }
954
- addSpan(`![${alt}](${src})`);
1126
+ currentTableRow !== null && currentTableRow.push({
1127
+ _type: "cell",
1128
+ _key: consolidatedOptions.keyGenerator(),
1129
+ value: cellBlocks
1130
+ });
955
1131
  break;
956
1132
  }
957
- for (const childToken of token.children ?? [])
958
- switch (childToken.type) {
959
- case "text":
960
- addSpan(childToken.content);
1133
+ // Inline container
1134
+ case "inline": {
1135
+ const inTableCell = currentTableRow !== null;
1136
+ if (token.children?.length === 1 && token.children[0]?.type === "image") {
1137
+ const imageToken = token.children[0];
1138
+ if (!imageToken)
961
1139
  break;
962
- case "softbreak":
963
- case "hardbreak":
964
- addSpan(`
965
- `);
966
- break;
967
- case "code_inline": {
968
- const decorator = consolidatedOptions.marks.code({
969
- context: { schema: consolidatedOptions.schema }
970
- });
971
- if (!decorator) {
972
- addSpan(childToken.content);
973
- break;
974
- }
975
- markDefRefs.push(decorator), addSpan(childToken.content);
976
- const index = markDefRefs.lastIndexOf(decorator);
977
- index !== -1 && markDefRefs.splice(index, 1);
1140
+ const src = imageToken.attrs?.find(([name]) => name === "src")?.at(1) || "", alt = unescapeImageAndLinkText(imageToken.content || ""), title = imageToken.attrs?.find(([name]) => name === "title")?.at(1) || void 0, blockImageObject = consolidatedOptions.types.image({
1141
+ context: {
1142
+ schema: consolidatedOptions.schema,
1143
+ keyGenerator: consolidatedOptions.keyGenerator
1144
+ },
1145
+ value: { src, alt, title },
1146
+ isInline: !1
1147
+ });
1148
+ if (blockImageObject) {
1149
+ inTableCell ? currentBlock && "children" in currentBlock && currentBlock.children.push(
1150
+ blockImageObject
1151
+ ) : (currentBlock && "children" in currentBlock && currentBlock.children.length > 0 ? flushBlock() : (currentBlock = null, currentMarkDefs = []), pushBlock(blockImageObject));
978
1152
  break;
979
1153
  }
980
- case "strong_open": {
981
- const decorator = consolidatedOptions.marks.strong({
982
- context: { schema: consolidatedOptions.schema }
983
- });
984
- if (!decorator)
985
- break;
986
- markDefRefs.push(decorator);
1154
+ const inlineImageObject = consolidatedOptions.types.image({
1155
+ context: {
1156
+ schema: consolidatedOptions.schema,
1157
+ keyGenerator: consolidatedOptions.keyGenerator
1158
+ },
1159
+ value: { src, alt, title },
1160
+ isInline: !0
1161
+ });
1162
+ if (inlineImageObject) {
1163
+ if (!currentBlock)
1164
+ if (inListItem)
1165
+ if (listContainerStack.at(-1)) {
1166
+ const style = consolidatedOptions.block.normal({
1167
+ context: { schema: consolidatedOptions.schema }
1168
+ }) ?? "normal";
1169
+ startBlock(style);
1170
+ } else {
1171
+ const listType = currentListStack.at(-1);
1172
+ listType && ensureListBlock(listType);
1173
+ }
1174
+ else {
1175
+ const style = consolidatedOptions.block.normal({
1176
+ context: { schema: consolidatedOptions.schema }
1177
+ });
1178
+ style && startBlock(style);
1179
+ }
1180
+ currentBlock && "children" in currentBlock && currentBlock.children.push(
1181
+ inlineImageObject
1182
+ );
987
1183
  break;
988
1184
  }
989
- case "strong_close": {
990
- const decorator = consolidatedOptions.marks.strong({
991
- context: { schema: consolidatedOptions.schema }
992
- });
993
- if (!decorator)
1185
+ addSpan(`![${alt}](${src})`);
1186
+ break;
1187
+ }
1188
+ for (const childToken of token.children ?? [])
1189
+ switch (childToken.type) {
1190
+ case "text":
1191
+ addSpan(childToken.content);
994
1192
  break;
995
- const index = markDefRefs.lastIndexOf(decorator);
996
- index !== -1 && markDefRefs.splice(index, 1);
997
- break;
998
- }
999
- case "em_open": {
1000
- const decorator = consolidatedOptions.marks.em({
1001
- context: { schema: consolidatedOptions.schema }
1002
- });
1003
- if (!decorator)
1193
+ case "softbreak":
1194
+ case "hardbreak":
1195
+ addSpan(`
1196
+ `);
1004
1197
  break;
1005
- markDefRefs.push(decorator);
1006
- break;
1007
- }
1008
- case "em_close": {
1009
- const decorator = consolidatedOptions.marks.em({
1010
- context: { schema: consolidatedOptions.schema }
1011
- });
1012
- if (!decorator)
1198
+ case "code_inline": {
1199
+ const decorator = consolidatedOptions.marks.code({
1200
+ context: { schema: consolidatedOptions.schema }
1201
+ });
1202
+ if (!decorator) {
1203
+ addSpan(childToken.content);
1204
+ break;
1205
+ }
1206
+ markDefRefs.push(decorator), addSpan(childToken.content);
1207
+ const index = markDefRefs.lastIndexOf(decorator);
1208
+ index !== -1 && markDefRefs.splice(index, 1);
1013
1209
  break;
1014
- const index = markDefRefs.lastIndexOf(decorator);
1015
- index !== -1 && markDefRefs.splice(index, 1);
1016
- break;
1017
- }
1018
- case "s_open": {
1019
- const decorator = consolidatedOptions.marks.strikeThrough({
1020
- context: { schema: consolidatedOptions.schema }
1021
- });
1022
- if (!decorator)
1210
+ }
1211
+ case "strong_open": {
1212
+ const decorator = consolidatedOptions.marks.strong({
1213
+ context: { schema: consolidatedOptions.schema }
1214
+ });
1215
+ if (!decorator)
1216
+ break;
1217
+ markDefRefs.push(decorator);
1023
1218
  break;
1024
- markDefRefs.push(decorator);
1025
- break;
1026
- }
1027
- case "s_close": {
1028
- const decorator = consolidatedOptions.marks.strikeThrough({
1029
- context: { schema: consolidatedOptions.schema }
1030
- });
1031
- if (!decorator)
1219
+ }
1220
+ case "strong_close": {
1221
+ const decorator = consolidatedOptions.marks.strong({
1222
+ context: { schema: consolidatedOptions.schema }
1223
+ });
1224
+ if (!decorator)
1225
+ break;
1226
+ const index = markDefRefs.lastIndexOf(decorator);
1227
+ index !== -1 && markDefRefs.splice(index, 1);
1032
1228
  break;
1033
- const index = markDefRefs.lastIndexOf(decorator);
1034
- index !== -1 && markDefRefs.splice(index, 1);
1035
- break;
1036
- }
1037
- case "link_open": {
1038
- const href = childToken.attrs?.find(([name]) => name === "href")?.at(1);
1039
- if (!href)
1229
+ }
1230
+ case "em_open": {
1231
+ const decorator = consolidatedOptions.marks.em({
1232
+ context: { schema: consolidatedOptions.schema }
1233
+ });
1234
+ if (!decorator)
1235
+ break;
1236
+ markDefRefs.push(decorator);
1040
1237
  break;
1041
- const title = childToken.attrs?.find(([name]) => name === "title")?.at(1), linkObject = consolidatedOptions.marks.link({
1042
- context: {
1043
- schema: consolidatedOptions.schema,
1044
- keyGenerator: consolidatedOptions.keyGenerator
1045
- },
1046
- value: { href, title }
1047
- });
1048
- if (!linkObject)
1238
+ }
1239
+ case "em_close": {
1240
+ const decorator = consolidatedOptions.marks.em({
1241
+ context: { schema: consolidatedOptions.schema }
1242
+ });
1243
+ if (!decorator)
1244
+ break;
1245
+ const index = markDefRefs.lastIndexOf(decorator);
1246
+ index !== -1 && markDefRefs.splice(index, 1);
1049
1247
  break;
1050
- currentMarkDefs.push(linkObject), markDefRefs.push(linkObject._key);
1051
- break;
1052
- }
1053
- case "link_close": {
1054
- const markDefKeys = new Set(currentMarkDefs.map((d) => d._key));
1055
- let lastLinkIndex;
1056
- for (const markDefRef of markDefRefs.reverse())
1057
- if (markDefKeys.has(markDefRef)) {
1058
- lastLinkIndex = markDefRefs.indexOf(markDefRef);
1248
+ }
1249
+ case "s_open": {
1250
+ const decorator = consolidatedOptions.marks.strikeThrough({
1251
+ context: { schema: consolidatedOptions.schema }
1252
+ });
1253
+ if (!decorator)
1059
1254
  break;
1060
- }
1061
- if (lastLinkIndex !== void 0) {
1062
- const realIndex = markDefRefs.length - 1 - lastLinkIndex;
1063
- markDefRefs.splice(realIndex, 1);
1255
+ markDefRefs.push(decorator);
1256
+ break;
1064
1257
  }
1065
- break;
1066
- }
1067
- case "image": {
1068
- const src = childToken.attrs?.find(([name]) => name === "src")?.at(1) || "", alt = unescapeImageAndLinkText(childToken.content || ""), inlineImageObject = consolidatedOptions.types.image({
1069
- context: {
1070
- schema: consolidatedOptions.schema,
1071
- keyGenerator: consolidatedOptions.keyGenerator
1072
- },
1073
- value: { src, alt, title: void 0 },
1074
- isInline: !0
1075
- });
1076
- if (inlineImageObject) {
1077
- if (!currentBlock) {
1078
- const style2 = consolidatedOptions.block.normal({
1079
- context: { schema: consolidatedOptions.schema }
1080
- });
1081
- style2 ? startBlock(style2) : (console.warn('No default style found, using "normal"'), startBlock("normal"));
1258
+ case "s_close": {
1259
+ const decorator = consolidatedOptions.marks.strikeThrough({
1260
+ context: { schema: consolidatedOptions.schema }
1261
+ });
1262
+ if (!decorator)
1263
+ break;
1264
+ const index = markDefRefs.lastIndexOf(decorator);
1265
+ index !== -1 && markDefRefs.splice(index, 1);
1266
+ break;
1267
+ }
1268
+ case "link_open": {
1269
+ const href = childToken.attrs?.find(([name]) => name === "href")?.at(1);
1270
+ if (!href)
1271
+ break;
1272
+ const title = childToken.attrs?.find(([name]) => name === "title")?.at(1), linkObject = consolidatedOptions.marks.link({
1273
+ context: {
1274
+ schema: consolidatedOptions.schema,
1275
+ keyGenerator: consolidatedOptions.keyGenerator
1276
+ },
1277
+ value: { href, title }
1278
+ });
1279
+ if (!linkObject)
1280
+ break;
1281
+ currentMarkDefs.push(linkObject), markDefRefs.push(linkObject._key);
1282
+ break;
1283
+ }
1284
+ case "link_close": {
1285
+ const markDefKeys = new Set(currentMarkDefs.map((d) => d._key));
1286
+ let lastLinkIndex;
1287
+ for (const markDefRef of markDefRefs.reverse())
1288
+ if (markDefKeys.has(markDefRef)) {
1289
+ lastLinkIndex = markDefRefs.indexOf(markDefRef);
1290
+ break;
1291
+ }
1292
+ if (lastLinkIndex !== void 0) {
1293
+ const realIndex = markDefRefs.length - 1 - lastLinkIndex;
1294
+ markDefRefs.splice(realIndex, 1);
1082
1295
  }
1083
- if (!currentBlock)
1084
- throw new Error("Expected current block after startBlock");
1085
- currentBlock.children.push(
1086
- inlineImageObject
1087
- );
1088
1296
  break;
1089
1297
  }
1090
- const blockImageObject = consolidatedOptions.types.image({
1091
- context: {
1092
- schema: consolidatedOptions.schema,
1093
- keyGenerator: consolidatedOptions.keyGenerator
1094
- },
1095
- value: { src, alt, title: void 0 },
1096
- isInline: !1
1097
- });
1098
- if (!blockImageObject) {
1099
- addSpan(`![${alt}](${src})`);
1298
+ case "image": {
1299
+ const src = childToken.attrs?.find(([name]) => name === "src")?.at(1) || "", alt = unescapeImageAndLinkText(childToken.content || ""), inlineImageObject = consolidatedOptions.types.image({
1300
+ context: {
1301
+ schema: consolidatedOptions.schema,
1302
+ keyGenerator: consolidatedOptions.keyGenerator
1303
+ },
1304
+ value: { src, alt, title: void 0 },
1305
+ isInline: !0
1306
+ });
1307
+ if (inlineImageObject) {
1308
+ if (!currentBlock) {
1309
+ const style2 = consolidatedOptions.block.normal({
1310
+ context: { schema: consolidatedOptions.schema }
1311
+ });
1312
+ style2 ? startBlock(style2) : (console.warn('No default style found, using "normal"'), startBlock("normal"));
1313
+ }
1314
+ if (!currentBlock)
1315
+ throw new Error("Expected current block after startBlock");
1316
+ currentBlock.children.push(
1317
+ inlineImageObject
1318
+ );
1319
+ break;
1320
+ }
1321
+ const blockImageObject = consolidatedOptions.types.image({
1322
+ context: {
1323
+ schema: consolidatedOptions.schema,
1324
+ keyGenerator: consolidatedOptions.keyGenerator
1325
+ },
1326
+ value: { src, alt, title: void 0 },
1327
+ isInline: !1
1328
+ });
1329
+ if (!blockImageObject) {
1330
+ addSpan(`![${alt}](${src})`);
1331
+ break;
1332
+ }
1333
+ if (inTableCell) {
1334
+ currentBlock && "children" in currentBlock && currentBlock.children.push(
1335
+ blockImageObject
1336
+ );
1337
+ break;
1338
+ }
1339
+ flushBlock(), pushBlock(blockImageObject);
1340
+ const style = consolidatedOptions.block.normal({
1341
+ context: { schema: consolidatedOptions.schema }
1342
+ });
1343
+ style && startBlock(style);
1100
1344
  break;
1101
1345
  }
1102
- if (inTableCell) {
1103
- currentBlock && "children" in currentBlock && currentBlock.children.push(
1104
- blockImageObject
1105
- );
1346
+ case "html_inline": {
1347
+ consolidatedOptions.html.inline === "text" && addSpan(childToken.content);
1106
1348
  break;
1107
1349
  }
1108
- flushBlock(), portableText.push(blockImageObject);
1109
- const style = consolidatedOptions.block.normal({
1110
- context: { schema: consolidatedOptions.schema }
1111
- });
1112
- style && startBlock(style);
1113
- break;
1114
- }
1115
- case "html_inline": {
1116
- consolidatedOptions.html.inline === "text" && addSpan(childToken.content);
1117
- break;
1118
1350
  }
1351
+ break;
1352
+ }
1353
+ // Callouts (GFM alerts)
1354
+ case "alert_open": {
1355
+ flushBlock(), calloutStartTarget = blockTarget(), calloutStartIndex = calloutStartTarget.length, calloutType = token.markup, currentBlockquoteStyle = consolidatedOptions.block.blockquote({
1356
+ context: { schema: consolidatedOptions.schema }
1357
+ }) ?? consolidatedOptions.block.normal({
1358
+ context: { schema: consolidatedOptions.schema }
1359
+ }) ?? "normal";
1360
+ break;
1361
+ }
1362
+ case "alert_title":
1363
+ break;
1364
+ case "alert_close": {
1365
+ if (flushBlock(), calloutStartIndex !== null && calloutType !== null && calloutStartTarget !== null) {
1366
+ const contentBlocks = calloutStartTarget.splice(
1367
+ calloutStartIndex
1368
+ ), calloutObject = consolidatedOptions.types.callout?.({
1369
+ context: {
1370
+ schema: consolidatedOptions.schema,
1371
+ keyGenerator: consolidatedOptions.keyGenerator
1372
+ },
1373
+ value: { tone: calloutType, content: contentBlocks },
1374
+ isInline: !1
1375
+ });
1376
+ if (calloutObject)
1377
+ pushBlock(calloutObject);
1378
+ else
1379
+ for (const block of contentBlocks)
1380
+ pushBlock(block);
1119
1381
  }
1120
- break;
1121
- }
1122
- // Callouts (GFM alerts)
1123
- case "alert_open": {
1124
- flushBlock(), calloutStartIndex = portableText.length, calloutType = token.markup, currentBlockquoteStyle = consolidatedOptions.block.blockquote({
1125
- context: { schema: consolidatedOptions.schema }
1126
- }) ?? consolidatedOptions.block.normal({
1127
- context: { schema: consolidatedOptions.schema }
1128
- }) ?? "normal";
1129
- break;
1130
- }
1131
- case "alert_title":
1132
- break;
1133
- case "alert_close": {
1134
- if (flushBlock(), calloutStartIndex !== null && calloutType !== null) {
1135
- const contentBlocks = portableText.splice(calloutStartIndex), calloutObject = consolidatedOptions.types.callout?.({
1136
- context: {
1137
- schema: consolidatedOptions.schema,
1138
- keyGenerator: consolidatedOptions.keyGenerator
1139
- },
1140
- value: { tone: calloutType, content: contentBlocks },
1141
- isInline: !1
1142
- });
1143
- calloutObject ? portableText.push(calloutObject) : portableText.push(...contentBlocks);
1382
+ calloutStartIndex = null, calloutStartTarget = null, calloutType = null, currentBlockquoteStyle = null;
1383
+ break;
1144
1384
  }
1145
- calloutStartIndex = null, calloutType = null, currentBlockquoteStyle = null;
1146
- break;
1147
1385
  }
1148
- }
1386
+ }
1149
1387
  return flushBlock(), portableText;
1150
1388
  }
1151
1389
  export {
1152
1390
  DefaultBlockSpacingRenderer,
1391
+ DefaultBlockquoteObjectRenderer,
1153
1392
  DefaultBlockquoteRenderer,
1154
1393
  DefaultCalloutRenderer,
1155
1394
  DefaultCodeBlockRenderer,
@@ -1167,6 +1406,7 @@ export {
1167
1406
  DefaultImageRenderer,
1168
1407
  DefaultLinkRenderer,
1169
1408
  DefaultListItemRenderer,
1409
+ DefaultListRenderer,
1170
1410
  DefaultNormalRenderer,
1171
1411
  DefaultStrikeThroughRenderer,
1172
1412
  DefaultStrongRenderer,