@milkdown/preset-commonmark 6.1.3 → 6.2.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.
Files changed (52) hide show
  1. package/lib/index.d.ts +2 -6
  2. package/lib/index.d.ts.map +1 -1
  3. package/lib/index.es.js +106 -20
  4. package/lib/index.es.js.map +1 -1
  5. package/lib/mark/code-inline.d.ts +1 -5
  6. package/lib/mark/code-inline.d.ts.map +1 -1
  7. package/lib/mark/em.d.ts +1 -5
  8. package/lib/mark/em.d.ts.map +1 -1
  9. package/lib/mark/index.d.ts +1 -1
  10. package/lib/mark/index.d.ts.map +1 -1
  11. package/lib/mark/link.d.ts +1 -5
  12. package/lib/mark/link.d.ts.map +1 -1
  13. package/lib/mark/strong.d.ts +1 -5
  14. package/lib/mark/strong.d.ts.map +1 -1
  15. package/lib/node/blockquote.d.ts +1 -5
  16. package/lib/node/blockquote.d.ts.map +1 -1
  17. package/lib/node/bullet-list.d.ts +1 -5
  18. package/lib/node/bullet-list.d.ts.map +1 -1
  19. package/lib/node/code-fence.d.ts +1 -5
  20. package/lib/node/code-fence.d.ts.map +1 -1
  21. package/lib/node/doc.d.ts +1 -5
  22. package/lib/node/doc.d.ts.map +1 -1
  23. package/lib/node/hardbreak.d.ts +1 -5
  24. package/lib/node/hardbreak.d.ts.map +1 -1
  25. package/lib/node/heading.d.ts +4 -5
  26. package/lib/node/heading.d.ts.map +1 -1
  27. package/lib/node/hr.d.ts +1 -5
  28. package/lib/node/hr.d.ts.map +1 -1
  29. package/lib/node/image.d.ts +1 -5
  30. package/lib/node/image.d.ts.map +1 -1
  31. package/lib/node/index.d.ts +2 -3
  32. package/lib/node/index.d.ts.map +1 -1
  33. package/lib/node/list-item.d.ts +1 -5
  34. package/lib/node/list-item.d.ts.map +1 -1
  35. package/lib/node/ordered-list.d.ts +1 -5
  36. package/lib/node/ordered-list.d.ts.map +1 -1
  37. package/lib/node/paragraph.d.ts +1 -5
  38. package/lib/node/paragraph.d.ts.map +1 -1
  39. package/lib/node/text.d.ts +1 -5
  40. package/lib/node/text.d.ts.map +1 -1
  41. package/lib/plugin/index.d.ts +1 -1
  42. package/lib/plugin/index.d.ts.map +1 -1
  43. package/package.json +4 -4
  44. package/src/mark/link.ts +53 -8
  45. package/src/node/code-fence.ts +3 -3
  46. package/src/node/hardbreak.ts +16 -2
  47. package/src/node/heading.ts +42 -11
  48. package/src/node/hr.ts +4 -2
  49. package/src/node/image.ts +2 -2
  50. package/src/node/index.ts +3 -1
  51. package/src/node/paragraph.ts +12 -2
  52. package/src/plugin/inline-nodes-cursor.ts +1 -1
package/lib/index.d.ts CHANGED
@@ -3,13 +3,9 @@ import { commonmarkPlugins } from './plugin';
3
3
  export * from './mark';
4
4
  export * from './node';
5
5
  export * from './supported-keys';
6
- export declare const commonmarkNodes: AtomList<(import("@milkdown/utils/lib/types").Metadata<import("@milkdown/utils/lib/types").GetPlugin<"CodeFence", {
7
- languageList?: string[] | undefined;
8
- }>> & import("@milkdown/core").MilkdownPlugin) | (import("@milkdown/utils/lib/types").Metadata<import("@milkdown/utils/lib/types").GetPlugin<string, import("./node").ImageOptions>> & import("@milkdown/core").MilkdownPlugin)>;
6
+ export declare const commonmarkNodes: AtomList<import("@milkdown/utils").AtomPlugin>;
9
7
  export { commonmarkPlugins };
10
- export declare const commonmark: AtomList<(import("@milkdown/utils/lib/types").Metadata<import("@milkdown/utils/lib/types").GetPlugin<"CodeFence", {
11
- languageList?: string[] | undefined;
12
- }>> & import("@milkdown/core").MilkdownPlugin) | (import("@milkdown/utils/lib/types").Metadata<import("@milkdown/utils/lib/types").GetPlugin<string, import("./node").ImageOptions>> & import("@milkdown/core").MilkdownPlugin)>;
8
+ export declare const commonmark: AtomList<import("@milkdown/utils").AtomPlugin>;
13
9
  export declare const commands: {
14
10
  readonly ToggleInlineCode: import("@milkdown/core").CmdKey<undefined>;
15
11
  readonly ToggleItalic: import("@milkdown/core").CmdKey<undefined>;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAmB3C,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAE7C,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,kBAAkB,CAAC;AAEjC,eAAO,MAAM,eAAe;;gOAAwC,CAAC;AACrE,OAAO,EAAE,iBAAiB,EAAE,CAAC;AAC7B,eAAO,MAAM,UAAU;;gOAA8D,CAAC;AAEtF,eAAO,MAAM,QAAQ;;;;;;;;;;;;;;;;;;;CAwBX,CAAC;AACX,oBAAY,QAAQ,GAAG,OAAO,QAAQ,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAmB3C,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAE7C,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,kBAAkB,CAAC;AAEjC,eAAO,MAAM,eAAe,gDAAwC,CAAC;AACrE,OAAO,EAAE,iBAAiB,EAAE,CAAC;AAC7B,eAAO,MAAM,UAAU,gDAA8D,CAAC;AAEtF,eAAO,MAAM,QAAQ;;;;;;;;;;;;;;;;;;;CAwBX,CAAC;AACX,oBAAY,QAAQ,GAAG,OAAO,QAAQ,CAAC"}
package/lib/index.es.js CHANGED
@@ -19,10 +19,10 @@ var __spreadValues = (a, b) => {
19
19
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
20
  import { createMark, createShortcut, createNode, createPlugin, AtomList } from "@milkdown/utils";
21
21
  import { createCmdKey, createCmd, schemaCtx, commandsCtx, editorViewCtx } from "@milkdown/core";
22
- import { markRule, findSelectedNodeOfType } from "@milkdown/prose";
22
+ import { markRule, calculateTextPosition, findSelectedNodeOfType } from "@milkdown/prose";
23
23
  import { toggleMark, wrapIn, setBlockType } from "@milkdown/prose/commands";
24
24
  import { InputRule, wrappingInputRule, textblockTypeInputRule } from "@milkdown/prose/inputrules";
25
- import { PluginKey, TextSelection, Plugin, Selection } from "@milkdown/prose/state";
25
+ import { PluginKey, TextSelection, Plugin, NodeSelection, Selection } from "@milkdown/prose/state";
26
26
  import { Fragment } from "@milkdown/prose/model";
27
27
  import { ReplaceStep, AddMarkStep } from "@milkdown/prose/transform";
28
28
  import { splitListItem, sinkListItem, liftListItem } from "@milkdown/prose/schema-list";
@@ -164,14 +164,17 @@ const link = createMark((utils, options) => {
164
164
  commands: (markType) => [
165
165
  createCmd(ToggleLink, (href = "") => toggleMark(markType, { href })),
166
166
  createCmd(ModifyLink, (href = "") => (state, dispatch) => {
167
+ var _a;
167
168
  if (!dispatch)
168
169
  return false;
169
170
  const { marks: marks2 } = state.schema;
170
171
  let node;
171
172
  let pos = -1;
172
173
  const { selection } = state;
173
- state.doc.nodesBetween(selection.from, selection.to, (n, p) => {
174
- if (marks2.link.isInSet(n.marks)) {
174
+ const { from, to } = selection;
175
+ state.doc.nodesBetween(from, from === to ? to + 1 : to, (n, p) => {
176
+ var _a2;
177
+ if ((_a2 = marks2["link"]) == null ? void 0 : _a2.isInSet(n.marks)) {
175
178
  node = n;
176
179
  pos = p;
177
180
  return false;
@@ -186,7 +189,9 @@ const link = createMark((utils, options) => {
186
189
  const start = pos;
187
190
  const end = pos + node.nodeSize;
188
191
  const { tr } = state;
189
- const linkMark = marks2.link.create(__spreadProps(__spreadValues({}, mark.attrs), { href }));
192
+ const linkMark = (_a = marks2["link"]) == null ? void 0 : _a.create(__spreadProps(__spreadValues({}, mark.attrs), { href }));
193
+ if (!linkMark)
194
+ return false;
190
195
  dispatch(tr.removeMark(start, end, mark).addMark(start, end, linkMark).setSelection(new TextSelection(tr.selection.$anchor)).scrollIntoView());
191
196
  return true;
192
197
  })
@@ -203,6 +208,7 @@ const link = createMark((utils, options) => {
203
208
  })
204
209
  ],
205
210
  prosePlugins: (type, ctx) => {
211
+ let renderOnTop = false;
206
212
  return [
207
213
  new Plugin({
208
214
  key: key$1,
@@ -213,6 +219,23 @@ const link = createMark((utils, options) => {
213
219
  buttonText: (_c = options == null ? void 0 : options.input) == null ? void 0 : _c.buttonText,
214
220
  onUpdate: (value) => {
215
221
  ctx.get(commandsCtx).call(ModifyLink, value);
222
+ },
223
+ calculatePosition: (view, input) => {
224
+ calculateTextPosition(view, input, (start, end, target, parent) => {
225
+ const $editor = view.dom.parentElement;
226
+ if (!$editor) {
227
+ throw new Error();
228
+ }
229
+ const selectionWidth = end.left - start.left;
230
+ let left = start.left - parent.left - (target.width - selectionWidth) / 2;
231
+ let top = start.bottom - parent.top + 14 + $editor.scrollTop;
232
+ if (renderOnTop) {
233
+ top = start.top - parent.top - target.height - 14 + $editor.scrollTop;
234
+ }
235
+ if (left < 0)
236
+ left = 0;
237
+ return [top, left];
238
+ });
216
239
  }
217
240
  });
218
241
  if (!inputChipRenderer)
@@ -220,7 +243,21 @@ const link = createMark((utils, options) => {
220
243
  const shouldDisplay = (view) => {
221
244
  const { selection, doc: doc2 } = view.state;
222
245
  const { from, to } = selection;
223
- return view.hasFocus() && selection.empty && selection instanceof TextSelection && to < doc2.content.size && from < doc2.content.size && doc2.rangeHasMark(from, from === to ? to + 1 : to, type);
246
+ if (!view.hasFocus()) {
247
+ return false;
248
+ }
249
+ if (selection.empty && selection instanceof TextSelection && to < doc2.content.size && from < doc2.content.size && doc2.rangeHasMark(from, from === to ? to + 1 : to, type)) {
250
+ renderOnTop = false;
251
+ return true;
252
+ }
253
+ if (selection instanceof NodeSelection) {
254
+ const { node } = selection;
255
+ if (node.type.name === "image" && node.marks.findIndex((mark) => mark.type.name === id$8) > -1) {
256
+ renderOnTop = true;
257
+ return true;
258
+ }
259
+ }
260
+ return false;
224
261
  };
225
262
  const getCurrentLink = (view) => {
226
263
  const { selection } = view.state;
@@ -260,7 +297,9 @@ const link = createMark((utils, options) => {
260
297
  const isEqualSelection = (prevState == null ? void 0 : prevState.doc.eq(view.state.doc)) && prevState.selection.eq(view.state.selection);
261
298
  if (isEqualSelection)
262
299
  return;
263
- renderByView(view);
300
+ requestAnimationFrame(() => {
301
+ renderByView(view);
302
+ });
264
303
  },
265
304
  destroy: () => {
266
305
  inputChipRenderer.destroy();
@@ -623,7 +662,16 @@ const hardbreak = createNode((utils) => {
623
662
  }),
624
663
  commands: (type) => [
625
664
  createCmd(InsertHardbreak, () => (state, dispatch) => {
626
- dispatch == null ? void 0 : dispatch(state.tr.setMeta("hardbreak", true).replaceSelectionWith(type.create()).scrollIntoView());
665
+ var _a;
666
+ const { selection, tr } = state;
667
+ if (selection.empty) {
668
+ const node = selection.$from.node();
669
+ if (node.childCount > 0 && ((_a = node.lastChild) == null ? void 0 : _a.type.name) === "hardbreak") {
670
+ dispatch == null ? void 0 : dispatch(tr.replaceRangeWith(selection.to - 1, selection.to, state.schema.node("paragraph")).setSelection(Selection.near(tr.doc.resolve(selection.to))).scrollIntoView());
671
+ return true;
672
+ }
673
+ }
674
+ dispatch == null ? void 0 : dispatch(tr.setMeta("hardbreak", true).replaceSelectionWith(type.create()).scrollIntoView());
627
675
  return true;
628
676
  })
629
677
  ],
@@ -668,8 +716,11 @@ const hardbreak = createNode((utils) => {
668
716
  const headingIndex = Array(6).fill(0).map((_, i) => i + 1);
669
717
  const TurnIntoHeading = createCmdKey("TurnIntoHeading");
670
718
  const headingPluginKey = new PluginKey("MILKDOWN_ID");
671
- const heading = createNode((utils) => {
719
+ const createId = (node) => node.textContent.replace(/[\p{P}\p{S}]/gu, "").replace(/\s/g, "-").toLowerCase().trim();
720
+ const heading = createNode((utils, options) => {
721
+ var _a;
672
722
  const id2 = "heading";
723
+ const getId = (_a = options == null ? void 0 : options.getId) != null ? _a : createId;
673
724
  return {
674
725
  id: id2,
675
726
  schema: () => ({
@@ -697,7 +748,7 @@ const heading = createNode((utils) => {
697
748
  return [
698
749
  `h${node.attrs["level"]}`,
699
750
  {
700
- id: node.attrs["id"] || node.textContent.split(" ").join("-").toLocaleLowerCase(),
751
+ id: node.attrs["id"] || getId(node),
701
752
  class: utils.getClassName(node.attrs, `heading h${node.attrs["level"]}`)
702
753
  },
703
754
  0
@@ -715,8 +766,21 @@ const heading = createNode((utils) => {
715
766
  toMarkdown: {
716
767
  match: (node) => node.type.name === id2,
717
768
  runner: (state, node) => {
769
+ var _a2;
718
770
  state.openNode("heading", void 0, { depth: node.attrs["level"] });
719
- state.next(node.content);
771
+ const lastIsHardbreak = node.childCount >= 1 && ((_a2 = node.lastChild) == null ? void 0 : _a2.type.name) === "hardbreak";
772
+ if (lastIsHardbreak) {
773
+ const contentArr = [];
774
+ node.content.forEach((n, _, i) => {
775
+ if (i === node.childCount - 1) {
776
+ return;
777
+ }
778
+ contentArr.push(n);
779
+ });
780
+ state.next(Fragment.fromArray(contentArr));
781
+ } else {
782
+ state.next(node.content);
783
+ }
720
784
  state.closeNode();
721
785
  }
722
786
  }
@@ -735,9 +799,6 @@ const heading = createNode((utils) => {
735
799
  },
736
800
  prosePlugins: (type, ctx) => {
737
801
  let lock = false;
738
- const createId = (node) => {
739
- return node.textContent.replace(/[\p{P}\p{S}]/gu, "").replace(/\s/g, "").trim();
740
- };
741
802
  const walkThrough = (state, callback) => {
742
803
  const tr = state.tr;
743
804
  state.doc.descendants((node, pos) => {
@@ -746,7 +807,7 @@ const heading = createNode((utils) => {
746
807
  return;
747
808
  }
748
809
  const attrs = node.attrs;
749
- const id22 = createId(node);
810
+ const id22 = getId(node);
750
811
  if (attrs["id"] !== id22) {
751
812
  tr.setMeta(headingPluginKey, true).setNodeMarkup(pos, void 0, __spreadProps(__spreadValues({}, attrs), {
752
813
  id: id22
@@ -783,6 +844,21 @@ const heading = createNode((utils) => {
783
844
  });
784
845
  }
785
846
  return tr;
847
+ },
848
+ view: (view) => {
849
+ const doc2 = view.state.doc;
850
+ let tr = view.state.tr;
851
+ doc2.descendants((node, pos) => {
852
+ if (node.type.name === "heading" && node.attrs["level"]) {
853
+ if (!node.attrs["id"]) {
854
+ tr = tr.setNodeMarkup(pos, void 0, __spreadProps(__spreadValues({}, node.attrs), {
855
+ id: getId(node)
856
+ }));
857
+ }
858
+ }
859
+ });
860
+ view.dispatch(tr);
861
+ return {};
786
862
  }
787
863
  })
788
864
  ];
@@ -824,13 +900,14 @@ const hr = createNode((utils) => {
824
900
  createCmd(InsertHr, () => (state, dispatch) => {
825
901
  if (!dispatch)
826
902
  return true;
903
+ const paragraph2 = ctx.get(schemaCtx).node("paragraph");
827
904
  const { tr, selection } = state;
828
- const from = selection.from;
905
+ const { from } = selection;
829
906
  const node = type.create();
830
907
  if (!node) {
831
908
  return true;
832
909
  }
833
- const _tr = tr.replaceSelectionWith(node).insert(from, ctx.get(schemaCtx).node("paragraph"));
910
+ const _tr = tr.replaceSelectionWith(node).insert(from, paragraph2);
834
911
  const sel = Selection.findFrom(_tr.doc.resolve(from), 1, true);
835
912
  if (!sel) {
836
913
  return true;
@@ -1150,8 +1227,17 @@ const paragraph = createNode((utils) => {
1150
1227
  runner: (state, node) => {
1151
1228
  var _a;
1152
1229
  state.openNode("paragraph");
1153
- const onlyHardbreak = node.childCount === 1 && ((_a = node.firstChild) == null ? void 0 : _a.type.name) === "hardbreak";
1154
- if (!onlyHardbreak) {
1230
+ const lastIsHardbreak = node.childCount >= 1 && ((_a = node.lastChild) == null ? void 0 : _a.type.name) === "hardbreak";
1231
+ if (lastIsHardbreak) {
1232
+ const contentArr = [];
1233
+ node.content.forEach((n, _, i) => {
1234
+ if (i === node.childCount - 1) {
1235
+ return;
1236
+ }
1237
+ contentArr.push(n);
1238
+ });
1239
+ state.next(Fragment.fromArray(contentArr));
1240
+ } else {
1155
1241
  state.next(node.content);
1156
1242
  }
1157
1243
  state.closeNode();
@@ -1257,7 +1343,7 @@ const inlineNodesCursorPlugin = new Plugin({
1257
1343
  handleDOMEvents: {
1258
1344
  beforeinput: (view, e) => {
1259
1345
  const active = inlineNodesCursorPlugin.getState(view.state);
1260
- if (active) {
1346
+ if (active && e instanceof InputEvent) {
1261
1347
  const from = view.state.selection.from;
1262
1348
  e.preventDefault();
1263
1349
  view.dispatch(view.state.tr.insertText(e.data || "", from));