@milkdown/preset-commonmark 5.3.4 → 5.5.0

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.es.js CHANGED
@@ -18,7 +18,7 @@ var __spreadValues = (a, b) => {
18
18
  };
19
19
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
20
  import { createMark, createShortcut, createNode, createPlugin, AtomList } from "@milkdown/utils";
21
- import { createCmdKey, createCmd, schemaCtx, themeToolCtx } from "@milkdown/core";
21
+ import { createCmdKey, createCmd, schemaCtx, themeToolCtx, editorViewCtx } from "@milkdown/core";
22
22
  import { markRule, toggleMark, TextSelection, InputRule, wrappingInputRule, wrapIn, textblockTypeInputRule, setBlockType, Plugin, PluginKey, ReplaceStep, AddMarkStep, Selection, findSelectedNodeOfType, splitListItem, sinkListItem, liftListItem } from "@milkdown/prose";
23
23
  import links from "remark-inline-links";
24
24
  const SupportedKeys = {
@@ -42,7 +42,7 @@ const SupportedKeys = {
42
42
  LiftListItem: "LiftListItem"
43
43
  };
44
44
  const id$a = "code_inline";
45
- const ToggleInlineCode = createCmdKey();
45
+ const ToggleInlineCode = createCmdKey("ToggleInlineCode");
46
46
  const codeInline = createMark((utils) => {
47
47
  const style = utils.getStyle(({ palette, size, font }, { css }) => css`
48
48
  background-color: ${palette("neutral")};
@@ -64,7 +64,7 @@ const codeInline = createMark((utils) => {
64
64
  match: (node) => node.type === "inlineCode",
65
65
  runner: (state, node, markType) => {
66
66
  state.openMark(markType);
67
- state.addText(node.value);
67
+ state.addText(node["value"]);
68
68
  state.closeMark(markType);
69
69
  }
70
70
  },
@@ -83,7 +83,7 @@ const codeInline = createMark((utils) => {
83
83
  };
84
84
  });
85
85
  const id$9 = "em";
86
- const ToggleItalic = createCmdKey();
86
+ const ToggleItalic = createCmdKey("ToggleItalic");
87
87
  const em = createMark((utils) => ({
88
88
  id: id$9,
89
89
  schema: () => ({
@@ -117,8 +117,8 @@ const em = createMark((utils) => ({
117
117
  [SupportedKeys.Em]: createShortcut(ToggleItalic, "Mod-i")
118
118
  }
119
119
  }));
120
- const ToggleLink = createCmdKey();
121
- const ModifyLink = createCmdKey();
120
+ const ToggleLink = createCmdKey("ToggleLink");
121
+ const ModifyLink = createCmdKey("ModifyLink");
122
122
  const id$8 = "link";
123
123
  const link = createMark((utils) => {
124
124
  const style = utils.getStyle((themeTool, { css }) => {
@@ -157,8 +157,8 @@ const link = createMark((utils) => {
157
157
  parseMarkdown: {
158
158
  match: (node) => node.type === "link",
159
159
  runner: (state, node, markType) => {
160
- const url = node.url;
161
- const title = node.title;
160
+ const url = node["url"];
161
+ const title = node["title"];
162
162
  state.openMark(markType, { href: url, title });
163
163
  state.next(node.children);
164
164
  state.closeMark(markType);
@@ -168,8 +168,8 @@ const link = createMark((utils) => {
168
168
  match: (mark) => mark.type.name === id$8,
169
169
  runner: (state, mark) => {
170
170
  state.withMark(mark, "link", void 0, {
171
- title: mark.attrs.title,
172
- url: mark.attrs.href
171
+ title: mark.attrs["title"],
172
+ url: mark.attrs["href"]
173
173
  });
174
174
  }
175
175
  }
@@ -218,7 +218,7 @@ const link = createMark((utils) => {
218
218
  };
219
219
  });
220
220
  const id$7 = "strong";
221
- const ToggleBold = createCmdKey();
221
+ const ToggleBold = createCmdKey("ToggleBold");
222
222
  const strong = createMark((utils) => {
223
223
  const style = utils.getStyle((_, { css }) => css`
224
224
  font-weight: 600;
@@ -259,7 +259,7 @@ const strong = createMark((utils) => {
259
259
  });
260
260
  const marks = [codeInline(), em(), strong(), link()];
261
261
  const id$6 = "blockquote";
262
- const WrapInBlockquote = createCmdKey();
262
+ const WrapInBlockquote = createCmdKey("WrapInBlockquote");
263
263
  const blockquote = createNode((utils) => {
264
264
  const style = utils.getStyle((themeTool, { css }) => css`
265
265
  padding-left: 1.875rem;
@@ -298,7 +298,7 @@ const blockquote = createNode((utils) => {
298
298
  }
299
299
  };
300
300
  });
301
- const WrapInBulletList = createCmdKey();
301
+ const WrapInBulletList = createCmdKey("WrapInBulletList");
302
302
  const bulletList = createNode((utils) => {
303
303
  const id2 = "bullet_list";
304
304
  return {
@@ -350,7 +350,7 @@ const languageOptions = [
350
350
  ];
351
351
  const backtickInputRegex = /^```(?<language>[a-z]*)?[\s\n]$/;
352
352
  const tildeInputRegex = /^~~~(?<language>[a-z]*)?[\s\n]$/;
353
- const TurnIntoCodeFence = createCmdKey();
353
+ const TurnIntoCodeFence = createCmdKey("TurnIntoCodeFence");
354
354
  const id$5 = "fence";
355
355
  const codeFence = createNode((utils, options) => {
356
356
  const style = utils.getStyle(({ palette, mixin, size, font }, { css }) => {
@@ -456,6 +456,9 @@ const codeFence = createNode((utils, options) => {
456
456
  pre {
457
457
  font-family: ${font.code};
458
458
  margin: 0 1.2rem !important;
459
+ white-space: pre;
460
+ overflow: auto;
461
+ ${scrollbar("x")};
459
462
  }
460
463
  `;
461
464
  });
@@ -483,7 +486,7 @@ const codeFence = createNode((utils, options) => {
483
486
  if (!(dom instanceof HTMLElement)) {
484
487
  throw new Error("Parse DOM error.");
485
488
  }
486
- return { language: dom.dataset.language };
489
+ return { language: dom.dataset["language"] };
487
490
  }
488
491
  }
489
492
  ],
@@ -491,7 +494,7 @@ const codeFence = createNode((utils, options) => {
491
494
  return [
492
495
  "pre",
493
496
  {
494
- "data-language": node.attrs.language,
497
+ "data-language": node.attrs["language"],
495
498
  class: utils.getClassName(node.attrs, "code-fence", style)
496
499
  },
497
500
  ["code", { spellCheck: "false" }, 0]
@@ -500,8 +503,8 @@ const codeFence = createNode((utils, options) => {
500
503
  parseMarkdown: {
501
504
  match: ({ type }) => type === "code",
502
505
  runner: (state, node, type) => {
503
- const language = node.lang;
504
- const value = node.value;
506
+ const language = node["lang"];
507
+ const value = node["value"];
505
508
  state.openNode(type, { language });
506
509
  if (value) {
507
510
  state.addText(value);
@@ -514,7 +517,7 @@ const codeFence = createNode((utils, options) => {
514
517
  runner: (state, node) => {
515
518
  var _a;
516
519
  state.addNode("code", void 0, ((_a = node.content.firstChild) == null ? void 0 : _a.text) || "", {
517
- lang: node.attrs.language
520
+ lang: node.attrs["language"]
518
521
  });
519
522
  }
520
523
  }
@@ -562,7 +565,7 @@ const codeFence = createNode((utils, options) => {
562
565
  const { tr } = view.state;
563
566
  view.dispatch(tr.setNodeMarkup(getPos(), void 0, {
564
567
  fold: true,
565
- language: el.dataset.value
568
+ language: el.dataset["value"]
566
569
  }));
567
570
  });
568
571
  valueWrapper.addEventListener("mousedown", (e) => {
@@ -573,16 +576,16 @@ const codeFence = createNode((utils, options) => {
573
576
  const { tr } = view.state;
574
577
  view.dispatch(tr.setNodeMarkup(getPos(), void 0, {
575
578
  fold: false,
576
- language: container.dataset.language
579
+ language: container.dataset["language"]
577
580
  }));
578
581
  });
579
582
  document.addEventListener("mousedown", () => {
580
- if (!view.editable || select.dataset.fold === "true")
583
+ if (!view.editable || select.dataset["fold"] === "true")
581
584
  return;
582
585
  const { tr } = view.state;
583
586
  view.dispatch(tr.setNodeMarkup(getPos(), void 0, {
584
587
  fold: true,
585
- language: container.dataset.language
588
+ language: container.dataset["language"]
586
589
  }));
587
590
  });
588
591
  ((options == null ? void 0 : options.languageList) || languageOptions).forEach((lang) => {
@@ -603,19 +606,19 @@ const codeFence = createNode((utils, options) => {
603
606
  codeContent.style.whiteSpace = "inherit";
604
607
  container.append(selectWrapper, pre);
605
608
  container.setAttribute("class", utils.getClassName(node.attrs, "code-fence", style));
606
- container.setAttribute("data-language", node.attrs.language);
607
- value.innerText = node.attrs.language || "--";
608
- select.setAttribute("data-fold", node.attrs.fold ? "true" : "false");
609
+ container.setAttribute("data-language", node.attrs["language"]);
610
+ value.innerText = node.attrs["language"] || "--";
611
+ select.setAttribute("data-fold", node.attrs["fold"] ? "true" : "false");
609
612
  return {
610
613
  dom: container,
611
614
  contentDOM: codeContent,
612
615
  update: (updatedNode) => {
613
616
  if (updatedNode.type.name !== id$5)
614
617
  return false;
615
- const lang = updatedNode.attrs.language;
616
- container.dataset.language = lang;
618
+ const lang = updatedNode.attrs["language"];
619
+ container.dataset["language"] = lang;
617
620
  value.innerText = lang || "--";
618
- select.setAttribute("data-fold", updatedNode.attrs.fold ? "true" : "false");
621
+ select.setAttribute("data-fold", updatedNode.attrs["fold"] ? "true" : "false");
619
622
  return true;
620
623
  }
621
624
  };
@@ -643,7 +646,7 @@ const doc = createNode(() => {
643
646
  })
644
647
  };
645
648
  });
646
- const InsertHardbreak = createCmdKey();
649
+ const InsertHardbreak = createCmdKey("InsertHardbreak");
647
650
  const hardbreak = createNode((utils) => {
648
651
  return {
649
652
  id: "hardbreak",
@@ -677,11 +680,13 @@ const hardbreak = createNode((utils) => {
677
680
  },
678
681
  prosePlugins: (type) => [
679
682
  new Plugin({
680
- key: new PluginKey("hardbreak-marks"),
683
+ key: new PluginKey("MILKDOWN_PLUGIN_HARDBREAK_MARKS"),
681
684
  appendTransaction: (trs, _oldState, newState) => {
682
685
  if (!trs.length)
683
686
  return;
684
687
  const [tr] = trs;
688
+ if (!tr)
689
+ return;
685
690
  const [step] = tr.steps;
686
691
  const isInsertHr = tr.getMeta("hardbreak");
687
692
  if (isInsertHr) {
@@ -709,7 +714,7 @@ const hardbreak = createNode((utils) => {
709
714
  };
710
715
  });
711
716
  const headingIndex = Array(6).fill(0).map((_, i) => i + 1);
712
- const TurnIntoHeading = createCmdKey();
717
+ const TurnIntoHeading = createCmdKey("TurnIntoHeading");
713
718
  const headingPluginKey = new PluginKey("MILKDOWN_PLUGIN_ID");
714
719
  const heading = createNode((utils) => {
715
720
  const id2 = "heading";
@@ -771,10 +776,10 @@ const heading = createNode((utils) => {
771
776
  })),
772
777
  toDOM: (node) => {
773
778
  return [
774
- `h${node.attrs.level}`,
779
+ `h${node.attrs["level"]}`,
775
780
  {
776
- id: node.attrs.id || node.textContent.split(" ").join("-").toLocaleLowerCase(),
777
- class: utils.getClassName(node.attrs, `heading h${node.attrs.level}`, style(node.attrs.level))
781
+ id: node.attrs["id"] || node.textContent.split(" ").join("-").toLocaleLowerCase(),
782
+ class: utils.getClassName(node.attrs, `heading h${node.attrs["level"]}`, style(node.attrs["level"]))
778
783
  },
779
784
  0
780
785
  ];
@@ -782,7 +787,7 @@ const heading = createNode((utils) => {
782
787
  parseMarkdown: {
783
788
  match: ({ type }) => type === id2,
784
789
  runner: (state, node, type) => {
785
- const depth = node.depth;
790
+ const depth = node["depth"];
786
791
  state.openNode(type, { level: depth });
787
792
  state.next(node.children);
788
793
  state.closeNode();
@@ -791,7 +796,7 @@ const heading = createNode((utils) => {
791
796
  toMarkdown: {
792
797
  match: (node) => node.type.name === id2,
793
798
  runner: (state, node) => {
794
- state.openNode("heading", void 0, { depth: node.attrs.level });
799
+ state.openNode("heading", void 0, { depth: node.attrs["level"] });
795
800
  state.next(node.content);
796
801
  state.closeNode();
797
802
  }
@@ -809,31 +814,64 @@ const heading = createNode((utils) => {
809
814
  [SupportedKeys.H5]: createShortcut(TurnIntoHeading, "Mod-Alt-5", 5),
810
815
  [SupportedKeys.H6]: createShortcut(TurnIntoHeading, "Mod-Alt-6", 6)
811
816
  },
812
- prosePlugins: (type) => [
813
- new Plugin({
814
- key: headingPluginKey,
815
- appendTransaction: (transactions, _, nextState) => {
816
- const tr = nextState.tr;
817
- let modified = false;
818
- if (transactions.some((transaction) => transaction.docChanged)) {
819
- nextState.doc.descendants((node, pos) => {
820
- if (node.type === type) {
821
- const attrs = node.attrs;
822
- tr.setNodeMarkup(pos, void 0, __spreadProps(__spreadValues({}, attrs), {
823
- id: node.textContent.split(" ").join("-").toLocaleLowerCase()
824
- }));
825
- modified = true;
817
+ prosePlugins: (type, ctx) => {
818
+ let lock = false;
819
+ const createId = (node) => {
820
+ return node.textContent.replace(/[\p{P}\p{S}]/gu, "").replace(/\s/g, "").trim();
821
+ };
822
+ const walkThrough = (state, callback) => {
823
+ const tr = state.tr;
824
+ state.doc.descendants((node, pos) => {
825
+ if (node.type === type && !lock) {
826
+ if (node.textContent.trim().length === 0) {
827
+ return;
828
+ }
829
+ const attrs = node.attrs;
830
+ const id22 = createId(node);
831
+ if (attrs["id"] !== id22) {
832
+ tr.setMeta(headingPluginKey, true).setNodeMarkup(pos, void 0, __spreadProps(__spreadValues({}, attrs), {
833
+ id: id22
834
+ }));
835
+ }
836
+ }
837
+ });
838
+ callback(tr);
839
+ };
840
+ return [
841
+ new Plugin({
842
+ key: headingPluginKey,
843
+ props: {
844
+ handleDOMEvents: {
845
+ compositionstart: () => {
846
+ lock = true;
847
+ return false;
848
+ },
849
+ compositionend: () => {
850
+ lock = false;
851
+ const view = ctx.get(editorViewCtx);
852
+ setTimeout(() => {
853
+ walkThrough(view.state, (tr) => view.dispatch(tr));
854
+ }, 0);
855
+ return false;
826
856
  }
827
- });
857
+ }
858
+ },
859
+ appendTransaction: (transactions, _, nextState) => {
860
+ let tr = null;
861
+ if (transactions.every((transaction) => !transaction.getMeta(headingPluginKey)) && transactions.some((transaction) => transaction.docChanged)) {
862
+ walkThrough(nextState, (t) => {
863
+ tr = t;
864
+ });
865
+ }
866
+ return tr;
828
867
  }
829
- return modified ? tr : null;
830
- }
831
- })
832
- ]
868
+ })
869
+ ];
870
+ }
833
871
  };
834
872
  });
835
873
  const id$4 = "hr";
836
- const InsertHr = createCmdKey();
874
+ const InsertHr = createCmdKey("InsertHr");
837
875
  const hr = createNode((utils) => {
838
876
  const style = utils.getStyle((themeTool, { css }) => css`
839
877
  height: ${themeTool.size.lineWidth};
@@ -889,8 +927,8 @@ const hr = createNode((utils) => {
889
927
  ]
890
928
  };
891
929
  });
892
- const ModifyImage = createCmdKey();
893
- const InsertImage = createCmdKey();
930
+ const ModifyImage = createCmdKey("ModifyImage");
931
+ const InsertImage = createCmdKey("InsertImage");
894
932
  const id$3 = "image";
895
933
  const image = createNode((utils, options) => {
896
934
  var _a, _b;
@@ -1042,16 +1080,16 @@ const image = createNode((utils, options) => {
1042
1080
  return [
1043
1081
  "img",
1044
1082
  __spreadProps(__spreadValues({}, node.attrs), {
1045
- class: utils.getClassName(node.attrs, id$3, node.attrs.failed ? "failed" : "", node.attrs.loading ? "loading" : "", style)
1083
+ class: utils.getClassName(node.attrs, id$3, node.attrs["failed"] ? "failed" : "", node.attrs["loading"] ? "loading" : "", style)
1046
1084
  })
1047
1085
  ];
1048
1086
  },
1049
1087
  parseMarkdown: {
1050
1088
  match: ({ type }) => type === id$3,
1051
1089
  runner: (state, node, type) => {
1052
- const url = node.url;
1053
- const alt = node.alt;
1054
- const title = node.title;
1090
+ const url = node["url"];
1091
+ const alt = node["alt"];
1092
+ const title = node["title"];
1055
1093
  state.addNode(type, {
1056
1094
  src: url,
1057
1095
  alt,
@@ -1063,9 +1101,9 @@ const image = createNode((utils, options) => {
1063
1101
  match: (node) => node.type.name === id$3,
1064
1102
  runner: (state, node) => {
1065
1103
  state.addNode("image", void 0, void 0, {
1066
- title: node.attrs.title,
1067
- url: node.attrs.src,
1068
- alt: node.attrs.alt
1104
+ title: node.attrs["title"],
1105
+ url: node.attrs["src"],
1106
+ alt: node.attrs["alt"]
1069
1107
  });
1070
1108
  }
1071
1109
  }
@@ -1203,9 +1241,9 @@ const image = createNode((utils, options) => {
1203
1241
  };
1204
1242
  });
1205
1243
  const id$2 = "list_item";
1206
- const SplitListItem = createCmdKey();
1207
- const SinkListItem = createCmdKey();
1208
- const LiftListItem = createCmdKey();
1244
+ const SplitListItem = createCmdKey("SplitListItem");
1245
+ const SinkListItem = createCmdKey("SinkListItem");
1246
+ const LiftListItem = createCmdKey("LiftListItem");
1209
1247
  const listItem = createNode((utils) => {
1210
1248
  const style = utils.getStyle((themeTool, { css }) => css`
1211
1249
  &,
@@ -1258,7 +1296,7 @@ const listItem = createNode((utils) => {
1258
1296
  }
1259
1297
  };
1260
1298
  });
1261
- const WrapInOrderedList = createCmdKey();
1299
+ const WrapInOrderedList = createCmdKey("WrapInOrderedList");
1262
1300
  const id$1 = "ordered_list";
1263
1301
  const orderedList = createNode((utils) => ({
1264
1302
  id: id$1,
@@ -1283,7 +1321,7 @@ const orderedList = createNode((utils) => ({
1283
1321
  ],
1284
1322
  toDOM: (node) => [
1285
1323
  "ol",
1286
- __spreadProps(__spreadValues({}, node.attrs.order === 1 ? {} : node.attrs.order), {
1324
+ __spreadProps(__spreadValues({}, node.attrs["order"] === 1 ? {} : node.attrs["order"]), {
1287
1325
  class: utils.getClassName(node.attrs, "ordered-list")
1288
1326
  }),
1289
1327
  0
@@ -1304,14 +1342,14 @@ const orderedList = createNode((utils) => ({
1304
1342
  }
1305
1343
  }),
1306
1344
  inputRules: (nodeType) => [
1307
- wrappingInputRule(/^(\d+)\.\s$/, nodeType, (match) => ({ order: Number(match[1]) }), (match, node) => node.childCount + node.attrs.order === Number(match[1]))
1345
+ wrappingInputRule(/^(\d+)\.\s$/, nodeType, (match) => ({ order: Number(match[1]) }), (match, node) => node.childCount + node.attrs["order"] === Number(match[1]))
1308
1346
  ],
1309
1347
  commands: (nodeType) => [createCmd(WrapInOrderedList, () => wrapIn(nodeType))],
1310
1348
  shortcuts: {
1311
1349
  [SupportedKeys.OrderedList]: createShortcut(WrapInOrderedList, "Mod-Alt-7")
1312
1350
  }
1313
1351
  }));
1314
- const TurnIntoText = createCmdKey();
1352
+ const TurnIntoText = createCmdKey("TurnIntoText");
1315
1353
  const id = "paragraph";
1316
1354
  const paragraph = createNode((utils) => {
1317
1355
  const style = utils.getStyle((_, { css }) => {
@@ -1335,7 +1373,7 @@ const paragraph = createNode((utils) => {
1335
1373
  if (node.children) {
1336
1374
  state.next(node.children);
1337
1375
  } else {
1338
- state.addText(node.value);
1376
+ state.addText(node["value"]);
1339
1377
  }
1340
1378
  state.closeNode();
1341
1379
  }
@@ -1362,7 +1400,7 @@ const text = createNode(() => ({
1362
1400
  parseMarkdown: {
1363
1401
  match: ({ type }) => type === "text",
1364
1402
  runner: (state, node) => {
1365
- state.addText(node.value);
1403
+ state.addText(node["value"]);
1366
1404
  }
1367
1405
  },
1368
1406
  toMarkdown: {
@@ -1395,10 +1433,16 @@ function flatMapWithDepth(ast, fn) {
1395
1433
  if (isParent(node)) {
1396
1434
  const out = [];
1397
1435
  for (let i = 0, n = node.children.length; i < n; i++) {
1398
- const xs = transform(node.children[i], i, node);
1399
- if (xs) {
1400
- for (let j = 0, m = xs.length; j < m; j++) {
1401
- out.push(xs[j]);
1436
+ const nthChild = node.children[i];
1437
+ if (nthChild) {
1438
+ const xs = transform(nthChild, i, node);
1439
+ if (xs) {
1440
+ for (let j = 0, m = xs.length; j < m; j++) {
1441
+ const item = xs[j];
1442
+ if (item) {
1443
+ out.push(item);
1444
+ }
1445
+ }
1402
1446
  }
1403
1447
  }
1404
1448
  }