@milkdown/preset-commonmark 5.3.2 → 5.4.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 (83) hide show
  1. package/lib/index.d.ts +1 -34
  2. package/lib/index.es.js +130 -41
  3. package/lib/index.es.js.map +1 -1
  4. package/lib/src/index.d.ts +34 -0
  5. package/lib/src/index.d.ts.map +1 -0
  6. package/lib/{mark → src/mark}/code-inline.d.ts +1 -1
  7. package/lib/src/mark/code-inline.d.ts.map +1 -0
  8. package/lib/{mark → src/mark}/em.d.ts +1 -1
  9. package/lib/src/mark/em.d.ts.map +1 -0
  10. package/lib/src/mark/index.d.ts +6 -0
  11. package/lib/src/mark/index.d.ts.map +1 -0
  12. package/lib/{mark → src/mark}/link.d.ts +1 -1
  13. package/lib/src/mark/link.d.ts.map +1 -0
  14. package/lib/{mark → src/mark}/strong.d.ts +1 -1
  15. package/lib/src/mark/strong.d.ts.map +1 -0
  16. package/lib/{node → src/node}/blockquote.d.ts +1 -1
  17. package/lib/src/node/blockquote.d.ts.map +1 -0
  18. package/lib/{node → src/node}/bullet-list.d.ts +1 -1
  19. package/lib/src/node/bullet-list.d.ts.map +1 -0
  20. package/lib/{node → src/node}/code-fence.d.ts +1 -1
  21. package/lib/src/node/code-fence.d.ts.map +1 -0
  22. package/lib/{node → src/node}/doc.d.ts +1 -1
  23. package/lib/src/node/doc.d.ts.map +1 -0
  24. package/lib/{node → src/node}/hardbreak.d.ts +1 -1
  25. package/lib/src/node/hardbreak.d.ts.map +1 -0
  26. package/lib/{node → src/node}/heading.d.ts +3 -1
  27. package/lib/src/node/heading.d.ts.map +1 -0
  28. package/lib/{node → src/node}/hr.d.ts +1 -1
  29. package/lib/src/node/hr.d.ts.map +1 -0
  30. package/lib/{node → src/node}/image.d.ts +1 -1
  31. package/lib/src/node/image.d.ts.map +1 -0
  32. package/lib/{node → src/node}/index.d.ts +2 -2
  33. package/lib/src/node/index.d.ts.map +1 -0
  34. package/lib/{node → src/node}/list-item.d.ts +1 -1
  35. package/lib/src/node/list-item.d.ts.map +1 -0
  36. package/lib/{node → src/node}/ordered-list.d.ts +1 -1
  37. package/lib/src/node/ordered-list.d.ts.map +1 -0
  38. package/lib/{node → src/node}/paragraph.d.ts +1 -1
  39. package/lib/src/node/paragraph.d.ts.map +1 -0
  40. package/lib/{node → src/node}/text.d.ts +1 -1
  41. package/lib/src/node/text.d.ts.map +1 -0
  42. package/lib/{plugin → src/plugin}/filter-html.d.ts +0 -0
  43. package/lib/src/plugin/filter-html.d.ts.map +1 -0
  44. package/lib/src/plugin/index.d.ts +2 -0
  45. package/lib/src/plugin/index.d.ts.map +1 -0
  46. package/lib/{supported-keys.d.ts → src/supported-keys.d.ts} +0 -0
  47. package/lib/src/supported-keys.d.ts.map +1 -0
  48. package/lib/src/types.d.ts +5 -0
  49. package/package.json +12 -7
  50. package/src/mark/code-inline.ts +1 -2
  51. package/src/mark/link.ts +1 -2
  52. package/src/mark/strong.ts +1 -2
  53. package/src/node/blockquote.ts +1 -2
  54. package/src/node/code-fence.ts +4 -2
  55. package/src/node/heading.ts +119 -26
  56. package/src/node/hr.ts +1 -2
  57. package/src/node/image.ts +12 -5
  58. package/src/node/list-item.ts +1 -2
  59. package/src/node/paragraph.ts +8 -9
  60. package/lib/index.d.ts.map +0 -1
  61. package/lib/mark/code-inline.d.ts.map +0 -1
  62. package/lib/mark/em.d.ts.map +0 -1
  63. package/lib/mark/index.d.ts +0 -6
  64. package/lib/mark/index.d.ts.map +0 -1
  65. package/lib/mark/link.d.ts.map +0 -1
  66. package/lib/mark/strong.d.ts.map +0 -1
  67. package/lib/node/blockquote.d.ts.map +0 -1
  68. package/lib/node/bullet-list.d.ts.map +0 -1
  69. package/lib/node/code-fence.d.ts.map +0 -1
  70. package/lib/node/doc.d.ts.map +0 -1
  71. package/lib/node/hardbreak.d.ts.map +0 -1
  72. package/lib/node/heading.d.ts.map +0 -1
  73. package/lib/node/hr.d.ts.map +0 -1
  74. package/lib/node/image.d.ts.map +0 -1
  75. package/lib/node/index.d.ts.map +0 -1
  76. package/lib/node/list-item.d.ts.map +0 -1
  77. package/lib/node/ordered-list.d.ts.map +0 -1
  78. package/lib/node/paragraph.d.ts.map +0 -1
  79. package/lib/node/text.d.ts.map +0 -1
  80. package/lib/plugin/filter-html.d.ts.map +0 -1
  81. package/lib/plugin/index.d.ts +0 -2
  82. package/lib/plugin/index.d.ts.map +0 -1
  83. package/lib/supported-keys.d.ts.map +0 -1
package/lib/index.d.ts CHANGED
@@ -1,34 +1 @@
1
- import { AtomList } from '@milkdown/utils';
2
- import { commonmarkPlugins } from './plugin';
3
- export * from './mark';
4
- export * from './node';
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)>;
9
- 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)>;
13
- export declare const commands: {
14
- readonly ToggleInlineCode: import("@milkdown/core").CmdKey<undefined>;
15
- readonly ToggleItalic: import("@milkdown/core").CmdKey<undefined>;
16
- readonly ToggleLink: import("@milkdown/core").CmdKey<string>;
17
- readonly ToggleBold: import("@milkdown/core").CmdKey<undefined>;
18
- readonly ModifyLink: import("@milkdown/core").CmdKey<string>;
19
- readonly ModifyImage: import("@milkdown/core").CmdKey<string>;
20
- readonly WrapInBlockquote: import("@milkdown/core").CmdKey<undefined>;
21
- readonly WrapInBulletList: import("@milkdown/core").CmdKey<undefined>;
22
- readonly WrapInOrderedList: import("@milkdown/core").CmdKey<undefined>;
23
- readonly TurnIntoCodeFence: import("@milkdown/core").CmdKey<undefined>;
24
- readonly TurnIntoHeading: import("@milkdown/core").CmdKey<number>;
25
- readonly TurnIntoText: import("@milkdown/core").CmdKey<undefined>;
26
- readonly InsertHardbreak: import("@milkdown/core").CmdKey<undefined>;
27
- readonly InsertHr: import("@milkdown/core").CmdKey<string>;
28
- readonly InsertImage: import("@milkdown/core").CmdKey<string>;
29
- readonly SplitListItem: import("@milkdown/core").CmdKey<undefined>;
30
- readonly SinkListItem: import("@milkdown/core").CmdKey<undefined>;
31
- readonly LiftListItem: import("@milkdown/core").CmdKey<undefined>;
32
- };
33
- export declare type Commands = typeof commands;
34
- //# sourceMappingURL=index.d.ts.map
1
+ export * from './src/index'
package/lib/index.es.js CHANGED
@@ -18,8 +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 { css } from "@emotion/css";
22
- import { createCmdKey, createCmd, schemaCtx, themeToolCtx } from "@milkdown/core";
21
+ import { createCmdKey, createCmd, schemaCtx, themeToolCtx, editorViewCtx } from "@milkdown/core";
23
22
  import { markRule, toggleMark, TextSelection, InputRule, wrappingInputRule, wrapIn, textblockTypeInputRule, setBlockType, Plugin, PluginKey, ReplaceStep, AddMarkStep, Selection, findSelectedNodeOfType, splitListItem, sinkListItem, liftListItem } from "@milkdown/prose";
24
23
  import links from "remark-inline-links";
25
24
  const SupportedKeys = {
@@ -45,7 +44,7 @@ const SupportedKeys = {
45
44
  const id$a = "code_inline";
46
45
  const ToggleInlineCode = createCmdKey();
47
46
  const codeInline = createMark((utils) => {
48
- const style = utils.getStyle(({ palette, size, font }) => css`
47
+ const style = utils.getStyle(({ palette, size, font }, { css }) => css`
49
48
  background-color: ${palette("neutral")};
50
49
  color: ${palette("background")};
51
50
  border-radius: ${size.radius};
@@ -122,7 +121,7 @@ const ToggleLink = createCmdKey();
122
121
  const ModifyLink = createCmdKey();
123
122
  const id$8 = "link";
124
123
  const link = createMark((utils) => {
125
- const style = utils.getStyle((themeTool) => {
124
+ const style = utils.getStyle((themeTool, { css }) => {
126
125
  const lineColor = themeTool.palette("line");
127
126
  return css`
128
127
  color: ${themeTool.palette("secondary")};
@@ -221,7 +220,7 @@ const link = createMark((utils) => {
221
220
  const id$7 = "strong";
222
221
  const ToggleBold = createCmdKey();
223
222
  const strong = createMark((utils) => {
224
- const style = utils.getStyle(() => css`
223
+ const style = utils.getStyle((_, { css }) => css`
225
224
  font-weight: 600;
226
225
  `);
227
226
  return {
@@ -262,7 +261,7 @@ const marks = [codeInline(), em(), strong(), link()];
262
261
  const id$6 = "blockquote";
263
262
  const WrapInBlockquote = createCmdKey();
264
263
  const blockquote = createNode((utils) => {
265
- const style = utils.getStyle((themeTool) => css`
264
+ const style = utils.getStyle((themeTool, { css }) => css`
266
265
  padding-left: 1.875rem;
267
266
  line-height: 1.75rem;
268
267
  border-left: 4px solid ${themeTool.palette("primary")};
@@ -354,7 +353,7 @@ const tildeInputRegex = /^~~~(?<language>[a-z]*)?[\s\n]$/;
354
353
  const TurnIntoCodeFence = createCmdKey();
355
354
  const id$5 = "fence";
356
355
  const codeFence = createNode((utils, options) => {
357
- const style = utils.getStyle(({ palette, mixin, size, font }) => {
356
+ const style = utils.getStyle(({ palette, mixin, size, font }, { css }) => {
358
357
  const { shadow, scrollbar, border } = mixin;
359
358
  const { lineWidth, radius } = size;
360
359
  return css`
@@ -457,6 +456,9 @@ const codeFence = createNode((utils, options) => {
457
456
  pre {
458
457
  font-family: ${font.code};
459
458
  margin: 0 1.2rem !important;
459
+ white-space: pre;
460
+ overflow: auto;
461
+ ${scrollbar("x")};
460
462
  }
461
463
  `;
462
464
  });
@@ -711,27 +713,42 @@ const hardbreak = createNode((utils) => {
711
713
  });
712
714
  const headingIndex = Array(6).fill(0).map((_, i) => i + 1);
713
715
  const TurnIntoHeading = createCmdKey();
714
- const heading = createNode((utils, options) => {
716
+ const headingPluginKey = new PluginKey("MILKDOWN_PLUGIN_ID");
717
+ const heading = createNode((utils) => {
715
718
  const id2 = "heading";
716
- const headingMap = {
717
- 1: css`
718
- font-size: 3rem;
719
- line-height: 3.5rem;
720
- `,
721
- 2: css`
722
- font-size: 2.125rem;
723
- line-height: 2.25rem;
724
- `,
725
- 3: css`
726
- font-size: 1.5rem;
727
- line-height: 1.5rem;
728
- `
729
- };
730
- const style = (level) => (options == null ? void 0 : options.headless) ? null : css`
731
- ${headingMap[level] || ""}
732
- margin: 2.5rem 0 !important;
733
- font-weight: 400;
734
- `;
719
+ const style = (level) => utils.getStyle((_, { css }) => {
720
+ const headingMap = {
721
+ 1: css`
722
+ font-size: 3rem;
723
+ line-height: 3.5rem;
724
+ `,
725
+ 2: css`
726
+ font-size: 2.5rem;
727
+ line-height: 3rem;
728
+ `,
729
+ 3: css`
730
+ font-size: 2.125rem;
731
+ line-height: 2.25rem;
732
+ `,
733
+ 4: css`
734
+ font-size: 1.75rem;
735
+ line-height: 2rem;
736
+ `,
737
+ 5: css`
738
+ font-size: 1.5rem;
739
+ line-height: 1.5rem;
740
+ `,
741
+ 6: css`
742
+ font-size: 1.25rem;
743
+ line-height: 1.25rem;
744
+ `
745
+ };
746
+ return css`
747
+ ${headingMap[level] || ""}
748
+ margin: 2.5rem 0 !important;
749
+ font-weight: 400;
750
+ `;
751
+ });
735
752
  return {
736
753
  id: id2,
737
754
  schema: () => ({
@@ -739,15 +756,27 @@ const heading = createNode((utils, options) => {
739
756
  group: "block",
740
757
  defining: true,
741
758
  attrs: {
759
+ id: {
760
+ default: ""
761
+ },
742
762
  level: {
743
763
  default: 1
744
764
  }
745
765
  },
746
- parseDOM: headingIndex.map((x) => ({ tag: `h${x}`, attrs: { level: x } })),
766
+ parseDOM: headingIndex.map((x) => ({
767
+ tag: `h${x}`,
768
+ getAttrs: (node) => {
769
+ if (!(node instanceof HTMLElement)) {
770
+ throw new Error();
771
+ }
772
+ return { level: x, id: node.id };
773
+ }
774
+ })),
747
775
  toDOM: (node) => {
748
776
  return [
749
777
  `h${node.attrs.level}`,
750
778
  {
779
+ id: node.attrs.id || node.textContent.split(" ").join("-").toLocaleLowerCase(),
751
780
  class: utils.getClassName(node.attrs, `heading h${node.attrs.level}`, style(node.attrs.level))
752
781
  },
753
782
  0
@@ -782,13 +811,65 @@ const heading = createNode((utils, options) => {
782
811
  [SupportedKeys.H4]: createShortcut(TurnIntoHeading, "Mod-Alt-4", 4),
783
812
  [SupportedKeys.H5]: createShortcut(TurnIntoHeading, "Mod-Alt-5", 5),
784
813
  [SupportedKeys.H6]: createShortcut(TurnIntoHeading, "Mod-Alt-6", 6)
814
+ },
815
+ prosePlugins: (type, ctx) => {
816
+ let lock = false;
817
+ const createId = (node) => {
818
+ return node.textContent.replace(/[\p{P}\p{S}]/gu, "").replace(/\s/g, "").trim();
819
+ };
820
+ const walkThrough = (state, callback) => {
821
+ const tr = state.tr;
822
+ state.doc.descendants((node, pos) => {
823
+ if (node.type === type && !lock) {
824
+ if (node.textContent.trim().length === 0) {
825
+ return;
826
+ }
827
+ const attrs = node.attrs;
828
+ const id22 = createId(node);
829
+ if (attrs.id !== id22) {
830
+ tr.setNodeMarkup(pos, void 0, __spreadProps(__spreadValues({}, attrs), {
831
+ id: id22
832
+ }));
833
+ }
834
+ }
835
+ });
836
+ callback(tr);
837
+ };
838
+ return [
839
+ new Plugin({
840
+ key: headingPluginKey,
841
+ props: {
842
+ handleDOMEvents: {
843
+ compositionstart: () => {
844
+ lock = true;
845
+ return false;
846
+ },
847
+ compositionend: () => {
848
+ lock = false;
849
+ const view = ctx.get(editorViewCtx);
850
+ walkThrough(view.state, (tr) => view.dispatch(tr));
851
+ return false;
852
+ }
853
+ }
854
+ },
855
+ appendTransaction: (transactions, _, nextState) => {
856
+ let tr = null;
857
+ if (transactions.some((transaction) => transaction.docChanged)) {
858
+ walkThrough(nextState, (t) => {
859
+ tr = t;
860
+ });
861
+ }
862
+ return tr;
863
+ }
864
+ })
865
+ ];
785
866
  }
786
867
  };
787
868
  });
788
869
  const id$4 = "hr";
789
870
  const InsertHr = createCmdKey();
790
871
  const hr = createNode((utils) => {
791
- const style = utils.getStyle((themeTool) => css`
872
+ const style = utils.getStyle((themeTool, { css }) => css`
792
873
  height: ${themeTool.size.lineWidth};
793
874
  background-color: ${themeTool.palette("line")};
794
875
  border-width: 0;
@@ -853,7 +934,7 @@ const image = createNode((utils, options) => {
853
934
  failed: "Image loads failed"
854
935
  }, (_a = options == null ? void 0 : options.placeholder) != null ? _a : {});
855
936
  const isBlock = (_b = options == null ? void 0 : options.isBlock) != null ? _b : false;
856
- const containerStyle = utils.getStyle((themeTool) => css`
937
+ const containerStyle = utils.getStyle((themeTool, { css }) => css`
857
938
  display: inline-block;
858
939
  position: relative;
859
940
  text-align: center;
@@ -945,7 +1026,7 @@ const image = createNode((utils, options) => {
945
1026
  }
946
1027
  }
947
1028
  `);
948
- const style = utils.getStyle(() => css`
1029
+ const style = utils.getStyle((_, { css }) => css`
949
1030
  display: inline-block;
950
1031
  margin: 0 auto;
951
1032
  object-fit: contain;
@@ -1077,8 +1158,11 @@ const image = createNode((utils, options) => {
1077
1158
  const img = document.createElement("img");
1078
1159
  img.src = src2;
1079
1160
  img.onerror = () => {
1161
+ const pos = getPos();
1162
+ if (!pos)
1163
+ return;
1080
1164
  const { tr } = view.state;
1081
- const _tr = tr.setNodeMarkup(getPos(), nodeType, __spreadProps(__spreadValues({}, node.attrs), {
1165
+ const _tr = tr.setNodeMarkup(pos, nodeType, __spreadProps(__spreadValues({}, node.attrs), {
1082
1166
  src: src2,
1083
1167
  loading: false,
1084
1168
  failed: true
@@ -1087,7 +1171,10 @@ const image = createNode((utils, options) => {
1087
1171
  };
1088
1172
  img.onload = () => {
1089
1173
  const { tr } = view.state;
1090
- const _tr = tr.setNodeMarkup(getPos(), nodeType, __spreadProps(__spreadValues({}, node.attrs), {
1174
+ const pos = getPos();
1175
+ if (!pos)
1176
+ return;
1177
+ const _tr = tr.setNodeMarkup(pos, nodeType, __spreadProps(__spreadValues({}, node.attrs), {
1091
1178
  width: img.width,
1092
1179
  src: src2,
1093
1180
  loading: false,
@@ -1154,7 +1241,7 @@ const SplitListItem = createCmdKey();
1154
1241
  const SinkListItem = createCmdKey();
1155
1242
  const LiftListItem = createCmdKey();
1156
1243
  const listItem = createNode((utils) => {
1157
- const style = utils.getStyle((themeTool) => css`
1244
+ const style = utils.getStyle((themeTool, { css }) => css`
1158
1245
  &,
1159
1246
  & > * {
1160
1247
  margin: 0.5rem 0;
@@ -1260,12 +1347,14 @@ const orderedList = createNode((utils) => ({
1260
1347
  }));
1261
1348
  const TurnIntoText = createCmdKey();
1262
1349
  const id = "paragraph";
1263
- const paragraph = createNode((utils, options) => {
1264
- const style = (options == null ? void 0 : options.headless) ? null : css`
1265
- font-size: 1rem;
1266
- line-height: 1.5;
1267
- letter-spacing: 0.5px;
1268
- `;
1350
+ const paragraph = createNode((utils) => {
1351
+ const style = utils.getStyle((_, { css }) => {
1352
+ return css`
1353
+ font-size: 1rem;
1354
+ line-height: 1.5;
1355
+ letter-spacing: 0.5px;
1356
+ `;
1357
+ });
1269
1358
  return {
1270
1359
  id,
1271
1360
  schema: () => ({
@@ -1390,5 +1479,5 @@ const commands = {
1390
1479
  SinkListItem,
1391
1480
  LiftListItem
1392
1481
  };
1393
- export { InsertHardbreak, InsertHr, InsertImage, LiftListItem, ModifyImage, ModifyLink, SinkListItem, SplitListItem, SupportedKeys, ToggleBold, ToggleInlineCode, ToggleItalic, ToggleLink, TurnIntoCodeFence, TurnIntoHeading, TurnIntoText, WrapInBlockquote, WrapInBulletList, WrapInOrderedList, backtickInputRegex, blockquote, bulletList, codeFence, codeInline, commands, commonmark, commonmarkNodes, commonmarkPlugins, doc, em, hardbreak, heading, hr, image, link, listItem, marks, nodes, orderedList, paragraph, strong, text, tildeInputRegex };
1482
+ export { InsertHardbreak, InsertHr, InsertImage, LiftListItem, ModifyImage, ModifyLink, SinkListItem, SplitListItem, SupportedKeys, ToggleBold, ToggleInlineCode, ToggleItalic, ToggleLink, TurnIntoCodeFence, TurnIntoHeading, TurnIntoText, WrapInBlockquote, WrapInBulletList, WrapInOrderedList, backtickInputRegex, blockquote, bulletList, codeFence, codeInline, commands, commonmark, commonmarkNodes, commonmarkPlugins, doc, em, hardbreak, heading, headingPluginKey, hr, image, link, listItem, marks, nodes, orderedList, paragraph, strong, text, tildeInputRegex };
1394
1483
  //# sourceMappingURL=index.es.js.map