@milkdown/preset-commonmark 7.14.0 → 7.15.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/lib/index.js CHANGED
@@ -4,7 +4,7 @@ import { TextSelection, Selection, PluginKey, Plugin } from "@milkdown/prose/sta
4
4
  import { findWrapping, ReplaceStep, AddMarkStep } from "@milkdown/prose/transform";
5
5
  import { $markAttr, $markSchema, $inputRule, $command, $useKeymap, $node, $remark, $nodeAttr, $nodeSchema, $ctx, $prose } from "@milkdown/utils";
6
6
  import { remarkStringifyOptionsCtx, commandsCtx, editorViewCtx } from "@milkdown/core";
7
- import { toggleMark, setBlockType, wrapIn } from "@milkdown/prose/commands";
7
+ import { toggleMark, setBlockType, wrapIn, joinBackward } from "@milkdown/prose/commands";
8
8
  import { visitParents } from "unist-util-visit-parents";
9
9
  import { expectDomTypeError } from "@milkdown/exception";
10
10
  import { textblockTypeInputRule, wrappingInputRule, InputRule } from "@milkdown/prose/inputrules";
@@ -13,8 +13,7 @@ import { Decoration, DecorationSet } from "@milkdown/prose/view";
13
13
  import { visit } from "unist-util-visit";
14
14
  import remarkInlineLinks from "remark-inline-links";
15
15
  function serializeText(state, node) {
16
- var _a;
17
- const lastIsHardBreak = node.childCount >= 1 && ((_a = node.lastChild) == null ? void 0 : _a.type.name) === "hardbreak";
16
+ const lastIsHardBreak = node.childCount >= 1 && node.lastChild?.type.name === "hardbreak";
18
17
  if (!lastIsHardBreak) {
19
18
  state.next(node.content);
20
19
  return;
@@ -258,7 +257,7 @@ const toggleInlineCodeCommand = $command(
258
257
  const { from, to } = selection;
259
258
  const has = state.doc.rangeHasMark(from, to, inlineCodeSchema.type(ctx));
260
259
  if (has) {
261
- dispatch == null ? void 0 : dispatch(tr.removeMark(from, to, inlineCodeSchema.type(ctx)));
260
+ dispatch?.(tr.removeMark(from, to, inlineCodeSchema.type(ctx)));
262
261
  return true;
263
262
  }
264
263
  const restMarksName = Object.keys(state.schema.marks).filter(
@@ -267,7 +266,7 @@ const toggleInlineCodeCommand = $command(
267
266
  restMarksName.map((name) => state.schema.marks[name]).forEach((t) => {
268
267
  tr.removeMark(from, to, t);
269
268
  });
270
- dispatch == null ? void 0 : dispatch(tr.addMark(from, to, inlineCodeSchema.type(ctx).create()));
269
+ dispatch?.(tr.addMark(from, to, inlineCodeSchema.type(ctx).create()));
271
270
  return true;
272
271
  }
273
272
  );
@@ -411,12 +410,9 @@ withMeta(docSchema, {
411
410
  function visitEmptyLine(ast) {
412
411
  return visitParents(
413
412
  ast,
414
- (node) => {
415
- var _a;
416
- return node.type === "html" && ["<br />", "<br>", "<br >", "<br/>"].includes(
417
- (_a = node.value) == null ? void 0 : _a.trim()
418
- );
419
- },
413
+ (node) => node.type === "html" && ["<br />", "<br>", "<br >", "<br/>"].includes(
414
+ node.value?.trim()
415
+ ),
420
416
  (node, parents) => {
421
417
  if (!parents.length) return;
422
418
  const parent = parents[parents.length - 1];
@@ -462,9 +458,8 @@ const paragraphSchema = $nodeSchema("paragraph", (ctx) => ({
462
458
  toMarkdown: {
463
459
  match: (node) => node.type.name === "paragraph",
464
460
  runner: (state, node) => {
465
- var _a;
466
461
  const view = ctx.get(editorViewCtx);
467
- const lastNode = (_a = view.state) == null ? void 0 : _a.doc.lastChild;
462
+ const lastNode = view.state?.doc.lastChild;
468
463
  state.openNode("paragraph");
469
464
  if ((!node.content || node.content.size === 0) && node !== lastNode && shouldPreserveEmptyLine(ctx)) {
470
465
  state.addNode("html", void 0, "<br />");
@@ -520,7 +515,7 @@ withMeta(paragraphKeymap.shortcuts, {
520
515
  });
521
516
  const headingIndex = Array(6).fill(0).map((_, i) => i + 1);
522
517
  function defaultHeadingIdGenerator(node) {
523
- return node.textContent.toLowerCase().trim().replace(/[^\w\s-]/g, "").replace(/\s+/g, "-");
518
+ return node.textContent.toLowerCase().trim().replace(/\s+/g, "-");
524
519
  }
525
520
  const headingIdGenerator = $ctx(
526
521
  defaultHeadingIdGenerator,
@@ -600,8 +595,7 @@ const wrapInHeadingInputRule = $inputRule((ctx) => {
600
595
  /^(?<hashes>#+)\s$/,
601
596
  headingSchema.type(ctx),
602
597
  (match) => {
603
- var _a, _b;
604
- const x = ((_b = (_a = match.groups) == null ? void 0 : _a.hashes) == null ? void 0 : _b.length) || 0;
598
+ const x = match.groups?.hashes?.length || 0;
605
599
  const view = ctx.get(editorViewCtx);
606
600
  const { $from } = view.state.selection;
607
601
  const node = $from.node();
@@ -620,7 +614,7 @@ withMeta(wrapInHeadingInputRule, {
620
614
  });
621
615
  const wrapInHeadingCommand = $command("WrapInHeading", (ctx) => {
622
616
  return (level) => {
623
- level ?? (level = 1);
617
+ level ??= 1;
624
618
  if (level < 1) return setBlockType(paragraphSchema.type(ctx));
625
619
  return setBlockType(headingSchema.type(ctx), { level });
626
620
  };
@@ -639,7 +633,7 @@ const downgradeHeadingCommand = $command(
639
633
  const level = node.attrs.level - 1;
640
634
  if (!level)
641
635
  return setBlockType(paragraphSchema.type(ctx))(state, dispatch, view);
642
- dispatch == null ? void 0 : dispatch(
636
+ dispatch?.(
643
637
  state.tr.setNodeMarkup(state.selection.$from.before(), void 0, {
644
638
  ...node.attrs,
645
639
  level
@@ -835,8 +829,7 @@ const codeBlockSchema = $nodeSchema("code_block", (ctx) => {
835
829
  toMarkdown: {
836
830
  match: (node) => node.type.name === "code_block",
837
831
  runner: (state, node) => {
838
- var _a;
839
- state.addNode("code", void 0, ((_a = node.content.firstChild) == null ? void 0 : _a.text) || "", {
832
+ state.addNode("code", void 0, node.content.firstChild?.text || "", {
840
833
  lang: node.attrs.language
841
834
  });
842
835
  }
@@ -855,12 +848,9 @@ const createCodeBlockInputRule = $inputRule(
855
848
  (ctx) => textblockTypeInputRule(
856
849
  /^```(?<language>[a-z]*)?[\s\n]$/,
857
850
  codeBlockSchema.type(ctx),
858
- (match) => {
859
- var _a;
860
- return {
861
- language: ((_a = match.groups) == null ? void 0 : _a.language) ?? ""
862
- };
863
- }
851
+ (match) => ({
852
+ language: match.groups?.language ?? ""
853
+ })
864
854
  )
865
855
  );
866
856
  withMeta(createCodeBlockInputRule, {
@@ -882,7 +872,7 @@ const updateCodeBlockLanguageCommand = $command(
882
872
  language: ""
883
873
  }) => (state, dispatch) => {
884
874
  if (pos >= 0) {
885
- dispatch == null ? void 0 : dispatch(state.tr.setNodeAttribute(pos, "language", language));
875
+ dispatch?.(state.tr.setNodeAttribute(pos, "language", language));
886
876
  return true;
887
877
  }
888
878
  return false;
@@ -1007,7 +997,7 @@ const updateImageCommand = $command(
1007
997
  if (src !== void 0) newAttrs.src = src;
1008
998
  if (alt !== void 0) newAttrs.alt = alt;
1009
999
  if (title !== void 0) newAttrs.title = title;
1010
- dispatch == null ? void 0 : dispatch(
1000
+ dispatch?.(
1011
1001
  state.tr.setNodeMarkup(pos, void 0, newAttrs).scrollIntoView()
1012
1002
  );
1013
1003
  return true;
@@ -1067,10 +1057,9 @@ const hardbreakSchema = $nodeSchema("hardbreak", (ctx) => ({
1067
1057
  parseMarkdown: {
1068
1058
  match: ({ type }) => type === "break",
1069
1059
  runner: (state, node, type) => {
1070
- var _a;
1071
1060
  state.addNode(type, {
1072
1061
  isInline: Boolean(
1073
- (_a = node.data) == null ? void 0 : _a.isInline
1062
+ node.data?.isInline
1074
1063
  )
1075
1064
  });
1076
1065
  }
@@ -1095,13 +1084,12 @@ withMeta(hardbreakSchema.ctx, {
1095
1084
  const insertHardbreakCommand = $command(
1096
1085
  "InsertHardbreak",
1097
1086
  (ctx) => () => (state, dispatch) => {
1098
- var _a;
1099
1087
  const { selection, tr } = state;
1100
1088
  if (!(selection instanceof TextSelection)) return false;
1101
1089
  if (selection.empty) {
1102
1090
  const node = selection.$from.node();
1103
- if (node.childCount > 0 && ((_a = node.lastChild) == null ? void 0 : _a.type.name) === "hardbreak") {
1104
- dispatch == null ? void 0 : dispatch(
1091
+ if (node.childCount > 0 && node.lastChild?.type.name === "hardbreak") {
1092
+ dispatch?.(
1105
1093
  tr.replaceRangeWith(
1106
1094
  selection.to - 1,
1107
1095
  selection.to,
@@ -1111,7 +1099,7 @@ const insertHardbreakCommand = $command(
1111
1099
  return true;
1112
1100
  }
1113
1101
  }
1114
- dispatch == null ? void 0 : dispatch(
1102
+ dispatch?.(
1115
1103
  tr.setMeta("hardbreak", true).replaceSelectionWith(hardbreakSchema.type(ctx).create()).scrollIntoView()
1116
1104
  );
1117
1105
  return true;
@@ -1504,11 +1492,8 @@ function liftFirstListItem(ctx) {
1504
1492
  const { empty, $from } = selection;
1505
1493
  if (!empty || $from.parentOffset !== 0) return false;
1506
1494
  const parentItem = $from.node(-1);
1507
- if (parentItem.type !== listItemSchema.type(ctx) || parentItem.firstChild !== $from.node())
1508
- return false;
1509
- const list = $from.node(-2);
1510
- if (list.childCount > 1) return false;
1511
- return liftListItem(listItemSchema.type(ctx))(state, dispatch, view);
1495
+ if (parentItem.type !== listItemSchema.type(ctx)) return false;
1496
+ return joinBackward(state, dispatch, view);
1512
1497
  };
1513
1498
  }
1514
1499
  const liftFirstListItemCommand = $command(
@@ -1710,7 +1695,7 @@ const clearTextInCurrentBlockCommand = $command(
1710
1695
  const left = from - $from.node().content.size;
1711
1696
  if (left < 0) return false;
1712
1697
  tr = tr.deleteRange(left, right);
1713
- dispatch == null ? void 0 : dispatch(tr);
1698
+ dispatch?.(tr);
1714
1699
  return true;
1715
1700
  }
1716
1701
  );
@@ -1726,7 +1711,7 @@ const setBlockTypeCommand = $command(
1726
1711
  } catch {
1727
1712
  return false;
1728
1713
  }
1729
- dispatch == null ? void 0 : dispatch(tr);
1714
+ dispatch?.(tr);
1730
1715
  return true;
1731
1716
  }
1732
1717
  );
@@ -1745,7 +1730,7 @@ const wrapInBlockTypeCommand = $command(
1745
1730
  } catch {
1746
1731
  return false;
1747
1732
  }
1748
- dispatch == null ? void 0 : dispatch(tr);
1733
+ dispatch?.(tr);
1749
1734
  return true;
1750
1735
  }
1751
1736
  );
@@ -1762,7 +1747,7 @@ const addBlockTypeCommand = $command(
1762
1747
  } catch {
1763
1748
  return false;
1764
1749
  }
1765
- dispatch == null ? void 0 : dispatch(tr);
1750
+ dispatch?.(tr);
1766
1751
  return true;
1767
1752
  }
1768
1753
  );
@@ -1779,7 +1764,7 @@ const selectTextNearPosCommand = $command(
1779
1764
  } catch {
1780
1765
  return false;
1781
1766
  }
1782
- dispatch == null ? void 0 : dispatch(tr.scrollIntoView());
1767
+ dispatch?.(tr.scrollIntoView());
1783
1768
  return true;
1784
1769
  }
1785
1770
  );
@@ -1929,7 +1914,7 @@ const remarkHtmlTransformer = $remark(
1929
1914
  () => () => (tree) => {
1930
1915
  flatMapWithDepth(tree, (node, _index, parent) => {
1931
1916
  if (!isHTML(node)) return [node];
1932
- if ((parent == null ? void 0 : parent.type) === "root") {
1917
+ if (parent?.type === "root") {
1933
1918
  node.children = [{ ...node }];
1934
1919
  delete node.value;
1935
1920
  node.type = "paragraph";
@@ -2194,7 +2179,7 @@ const syncListOrderPlugin = $prose((ctx) => {
2194
2179
  state.doc.descendants((node, pos, parent, index) => {
2195
2180
  if (node.type === bulletListType) {
2196
2181
  const base = node.maybeChild(0);
2197
- if ((base == null ? void 0 : base.type) === listItemType && base.attrs.listType === "ordered") {
2182
+ if (base?.type === listItemType && base.attrs.listType === "ordered") {
2198
2183
  needDispatch = true;
2199
2184
  tr.setNodeMarkup(pos, orderedListType, { spread: "true" });
2200
2185
  node.descendants((child, pos2, _parent, index2) => {
@@ -2206,14 +2191,14 @@ const syncListOrderPlugin = $prose((ctx) => {
2206
2191
  return false;
2207
2192
  });
2208
2193
  }
2209
- } else if (node.type === listItemType && (parent == null ? void 0 : parent.type) === orderedListType) {
2194
+ } else if (node.type === listItemType && parent?.type === orderedListType) {
2210
2195
  const attrs = { ...node.attrs };
2211
2196
  let changed = false;
2212
2197
  if (attrs.listType !== "ordered") {
2213
2198
  attrs.listType = "ordered";
2214
2199
  changed = true;
2215
2200
  }
2216
- const base = parent == null ? void 0 : parent.maybeChild(0);
2201
+ const base = parent?.maybeChild(0);
2217
2202
  if (base) changed = handleNodeItem(attrs, index);
2218
2203
  if (changed) {
2219
2204
  tr = tr.setNodeMarkup(pos, void 0, attrs);