@milkdown/preset-commonmark 4.14.2 → 5.1.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.
Files changed (92) hide show
  1. package/lib/index.d.ts +5 -3
  2. package/lib/index.d.ts.map +1 -1
  3. package/lib/mark/code-inline.d.ts +5 -1
  4. package/lib/mark/code-inline.d.ts.map +1 -1
  5. package/lib/mark/code-inline.js +16 -16
  6. package/lib/mark/code-inline.js.map +1 -1
  7. package/lib/mark/em.d.ts +5 -1
  8. package/lib/mark/em.d.ts.map +1 -1
  9. package/lib/mark/em.js +15 -15
  10. package/lib/mark/em.js.map +1 -1
  11. package/lib/mark/index.d.ts +1 -1
  12. package/lib/mark/index.d.ts.map +1 -1
  13. package/lib/mark/link.d.ts +5 -1
  14. package/lib/mark/link.d.ts.map +1 -1
  15. package/lib/mark/link.js +23 -23
  16. package/lib/mark/link.js.map +1 -1
  17. package/lib/mark/strong.d.ts +5 -1
  18. package/lib/mark/strong.d.ts.map +1 -1
  19. package/lib/mark/strong.js +15 -15
  20. package/lib/mark/strong.js.map +1 -1
  21. package/lib/node/blockquote.d.ts +5 -1
  22. package/lib/node/blockquote.d.ts.map +1 -1
  23. package/lib/node/blockquote.js +13 -13
  24. package/lib/node/blockquote.js.map +1 -1
  25. package/lib/node/bullet-list.d.ts +5 -1
  26. package/lib/node/bullet-list.d.ts.map +1 -1
  27. package/lib/node/bullet-list.js +14 -14
  28. package/lib/node/bullet-list.js.map +1 -1
  29. package/lib/node/code-fence.d.ts +6 -2
  30. package/lib/node/code-fence.d.ts.map +1 -1
  31. package/lib/node/code-fence.js +22 -20
  32. package/lib/node/code-fence.js.map +1 -1
  33. package/lib/node/doc.d.ts +5 -1
  34. package/lib/node/doc.d.ts.map +1 -1
  35. package/lib/node/doc.js +21 -19
  36. package/lib/node/doc.js.map +1 -1
  37. package/lib/node/hardbreak.d.ts +5 -1
  38. package/lib/node/hardbreak.d.ts.map +1 -1
  39. package/lib/node/hardbreak.js +50 -31
  40. package/lib/node/hardbreak.js.map +1 -1
  41. package/lib/node/heading.d.ts +5 -1
  42. package/lib/node/heading.d.ts.map +1 -1
  43. package/lib/node/heading.js +20 -20
  44. package/lib/node/heading.js.map +1 -1
  45. package/lib/node/hr.d.ts +5 -1
  46. package/lib/node/hr.d.ts.map +1 -1
  47. package/lib/node/hr.js +19 -19
  48. package/lib/node/hr.js.map +1 -1
  49. package/lib/node/image.d.ts +5 -1
  50. package/lib/node/image.d.ts.map +1 -1
  51. package/lib/node/image.js +32 -32
  52. package/lib/node/image.js.map +1 -1
  53. package/lib/node/index.d.ts +2 -2
  54. package/lib/node/index.d.ts.map +1 -1
  55. package/lib/node/list-item.d.ts +5 -1
  56. package/lib/node/list-item.d.ts.map +1 -1
  57. package/lib/node/list-item.js +17 -17
  58. package/lib/node/list-item.js.map +1 -1
  59. package/lib/node/ordered-list.d.ts +5 -1
  60. package/lib/node/ordered-list.d.ts.map +1 -1
  61. package/lib/node/ordered-list.js +15 -15
  62. package/lib/node/ordered-list.js.map +1 -1
  63. package/lib/node/paragraph.d.ts +5 -1
  64. package/lib/node/paragraph.d.ts.map +1 -1
  65. package/lib/node/paragraph.js +22 -22
  66. package/lib/node/paragraph.js.map +1 -1
  67. package/lib/node/text.d.ts +5 -1
  68. package/lib/node/text.d.ts.map +1 -1
  69. package/lib/node/text.js +12 -12
  70. package/lib/node/text.js.map +1 -1
  71. package/lib/plugin/index.d.ts +1 -1
  72. package/lib/plugin/index.d.ts.map +1 -1
  73. package/lib/plugin/index.js +6 -2
  74. package/lib/plugin/index.js.map +1 -1
  75. package/package.json +3 -3
  76. package/src/mark/code-inline.ts +16 -16
  77. package/src/mark/em.ts +15 -15
  78. package/src/mark/link.ts +23 -23
  79. package/src/mark/strong.ts +15 -15
  80. package/src/node/blockquote.ts +13 -13
  81. package/src/node/bullet-list.ts +14 -14
  82. package/src/node/code-fence.ts +21 -19
  83. package/src/node/doc.ts +21 -19
  84. package/src/node/hardbreak.ts +48 -32
  85. package/src/node/heading.ts +21 -21
  86. package/src/node/hr.ts +19 -19
  87. package/src/node/image.ts +32 -32
  88. package/src/node/list-item.ts +17 -17
  89. package/src/node/ordered-list.ts +15 -15
  90. package/src/node/paragraph.ts +21 -21
  91. package/src/node/text.ts +12 -12
  92. package/src/plugin/index.ts +6 -2
package/src/mark/em.ts CHANGED
@@ -11,30 +11,30 @@ const id = 'em';
11
11
 
12
12
  export const ToggleItalic = createCmdKey();
13
13
 
14
- export const em = createMark<Keys>((_, utils) => ({
14
+ export const em = createMark<Keys>((utils) => ({
15
15
  id,
16
- schema: {
16
+ schema: () => ({
17
17
  parseDOM: [
18
18
  { tag: 'i' },
19
19
  { tag: 'em' },
20
20
  { style: 'font-style', getAttrs: (value) => (value === 'italic') as false },
21
21
  ],
22
22
  toDOM: (mark) => ['em', { class: utils.getClassName(mark.attrs, id) }],
23
- },
24
- parser: {
25
- match: (node) => node.type === 'emphasis',
26
- runner: (state, node, markType) => {
27
- state.openMark(markType);
28
- state.next(node.children);
29
- state.closeMark(markType);
23
+ parseMarkdown: {
24
+ match: (node) => node.type === 'emphasis',
25
+ runner: (state, node, markType) => {
26
+ state.openMark(markType);
27
+ state.next(node.children);
28
+ state.closeMark(markType);
29
+ },
30
30
  },
31
- },
32
- serializer: {
33
- match: (mark) => mark.type.name === id,
34
- runner: (state, mark) => {
35
- state.withMark(mark, 'emphasis');
31
+ toMarkdown: {
32
+ match: (mark) => mark.type.name === id,
33
+ runner: (state, mark) => {
34
+ state.withMark(mark, 'emphasis');
35
+ },
36
36
  },
37
- },
37
+ }),
38
38
  inputRules: (markType) => [
39
39
  markRule(/(?:^|[^_])(_([^_]+)_)$/, markType),
40
40
  markRule(/(?:^|[^*])(\*([^*]+)\*)$/, markType),
package/src/mark/link.ts CHANGED
@@ -1,13 +1,13 @@
1
1
  /* Copyright 2021, Milkdown by Mirone. */
2
2
  import { css } from '@emotion/css';
3
- import { createCmd, createCmdKey } from '@milkdown/core';
3
+ import { createCmd, createCmdKey, schemaCtx } from '@milkdown/core';
4
4
  import { InputRule, Node as ProseNode, TextSelection, toggleMark } from '@milkdown/prose';
5
5
  import { createMark } from '@milkdown/utils';
6
6
 
7
7
  export const ToggleLink = createCmdKey<string>();
8
8
  export const ModifyLink = createCmdKey<string>();
9
9
  const id = 'link';
10
- export const link = createMark((_, utils) => {
10
+ export const link = createMark((utils) => {
11
11
  const style = utils.getStyle((themeTool) => {
12
12
  const lineColor = themeTool.palette('line');
13
13
 
@@ -24,7 +24,7 @@ export const link = createMark((_, utils) => {
24
24
  });
25
25
  return {
26
26
  id,
27
- schema: {
27
+ schema: () => ({
28
28
  attrs: {
29
29
  href: {},
30
30
  title: { default: null },
@@ -42,26 +42,26 @@ export const link = createMark((_, utils) => {
42
42
  },
43
43
  ],
44
44
  toDOM: (mark) => ['a', { ...mark.attrs, class: utils.getClassName(mark.attrs, id, style) }],
45
- },
46
- parser: {
47
- match: (node) => node.type === 'link',
48
- runner: (state, node, markType) => {
49
- const url = node.url as string;
50
- const title = node.title as string;
51
- state.openMark(markType, { href: url, title });
52
- state.next(node.children);
53
- state.closeMark(markType);
45
+ parseMarkdown: {
46
+ match: (node) => node.type === 'link',
47
+ runner: (state, node, markType) => {
48
+ const url = node.url as string;
49
+ const title = node.title as string;
50
+ state.openMark(markType, { href: url, title });
51
+ state.next(node.children);
52
+ state.closeMark(markType);
53
+ },
54
54
  },
55
- },
56
- serializer: {
57
- match: (mark) => mark.type.name === id,
58
- runner: (state, mark) => {
59
- state.withMark(mark, 'link', undefined, {
60
- title: mark.attrs.title,
61
- url: mark.attrs.href,
62
- });
55
+ toMarkdown: {
56
+ match: (mark) => mark.type.name === id,
57
+ runner: (state, mark) => {
58
+ state.withMark(mark, 'link', undefined, {
59
+ title: mark.attrs.title,
60
+ url: mark.attrs.href,
61
+ });
62
+ },
63
63
  },
64
- },
64
+ }),
65
65
  commands: (markType) => [
66
66
  createCmd(ToggleLink, (href = '') => toggleMark(markType, { href })),
67
67
  createCmd(ModifyLink, (href = '') => (state, dispatch) => {
@@ -100,13 +100,13 @@ export const link = createMark((_, utils) => {
100
100
  return true;
101
101
  }),
102
102
  ],
103
- inputRules: (markType, schema) => [
103
+ inputRules: (markType, ctx) => [
104
104
  new InputRule(/\[(?<text>.*?)]\((?<href>.*?)(?=“|\))"?(?<title>[^"]+)?"?\)/, (state, match, start, end) => {
105
105
  const [okay, text = '', href, title] = match;
106
106
  const { tr } = state;
107
107
  if (okay) {
108
108
  const content = text || 'link';
109
- tr.replaceWith(start, end, schema.text(content)).addMark(
109
+ tr.replaceWith(start, end, ctx.get(schemaCtx).text(content)).addMark(
110
110
  start,
111
111
  content.length + start,
112
112
  markType.create({ title, href }),
@@ -9,7 +9,7 @@ import { SupportedKeys } from '../supported-keys';
9
9
  type Keys = SupportedKeys['Bold'];
10
10
  const id = 'strong';
11
11
  export const ToggleBold = createCmdKey();
12
- export const strong = createMark<Keys>((_, utils) => {
12
+ export const strong = createMark<Keys>((utils) => {
13
13
  const style = utils.getStyle(
14
14
  () =>
15
15
  css`
@@ -18,28 +18,28 @@ export const strong = createMark<Keys>((_, utils) => {
18
18
  );
19
19
  return {
20
20
  id,
21
- schema: {
21
+ schema: () => ({
22
22
  parseDOM: [
23
23
  { tag: 'b' },
24
24
  { tag: 'strong' },
25
25
  { style: 'font-style', getAttrs: (value) => (value === 'bold') as false },
26
26
  ],
27
27
  toDOM: (mark) => ['strong', { class: utils.getClassName(mark.attrs, id, style) }],
28
- },
29
- parser: {
30
- match: (node) => node.type === 'strong',
31
- runner: (state, node, markType) => {
32
- state.openMark(markType);
33
- state.next(node.children);
34
- state.closeMark(markType);
28
+ parseMarkdown: {
29
+ match: (node) => node.type === 'strong',
30
+ runner: (state, node, markType) => {
31
+ state.openMark(markType);
32
+ state.next(node.children);
33
+ state.closeMark(markType);
34
+ },
35
35
  },
36
- },
37
- serializer: {
38
- match: (mark) => mark.type.name === id,
39
- runner: (state, mark) => {
40
- state.withMark(mark, 'strong');
36
+ toMarkdown: {
37
+ match: (mark) => mark.type.name === id,
38
+ runner: (state, mark) => {
39
+ state.withMark(mark, 'strong');
40
+ },
41
41
  },
42
- },
42
+ }),
43
43
  inputRules: (markType) => [
44
44
  markRule(/(?:__)([^_]+)(?:__)$/, markType),
45
45
  markRule(/(?:\*\*)([^*]+)(?:\*\*)$/, markType),
@@ -12,7 +12,7 @@ const id = 'blockquote';
12
12
 
13
13
  export const WrapInBlockquote = createCmdKey();
14
14
 
15
- export const blockquote = createNode<Keys>((_, utils) => {
15
+ export const blockquote = createNode<Keys>((utils) => {
16
16
  const style = utils.getStyle(
17
17
  (themeTool) =>
18
18
  css`
@@ -28,25 +28,25 @@ export const blockquote = createNode<Keys>((_, utils) => {
28
28
 
29
29
  return {
30
30
  id,
31
- schema: {
31
+ schema: () => ({
32
32
  content: 'block+',
33
33
  group: 'block',
34
34
  defining: true,
35
35
  parseDOM: [{ tag: 'blockquote' }],
36
36
  toDOM: (node) => ['blockquote', { class: utils.getClassName(node.attrs, id, style) }, 0],
37
- },
38
- parser: {
39
- match: ({ type }) => type === id,
40
- runner: (state, node, type) => {
41
- state.openNode(type).next(node.children).closeNode();
37
+ parseMarkdown: {
38
+ match: ({ type }) => type === id,
39
+ runner: (state, node, type) => {
40
+ state.openNode(type).next(node.children).closeNode();
41
+ },
42
42
  },
43
- },
44
- serializer: {
45
- match: (node) => node.type.name === id,
46
- runner: (state, node) => {
47
- state.openNode('blockquote').next(node.content).closeNode();
43
+ toMarkdown: {
44
+ match: (node) => node.type.name === id,
45
+ runner: (state, node) => {
46
+ state.openNode('blockquote').next(node.content).closeNode();
47
+ },
48
48
  },
49
- },
49
+ }),
50
50
  inputRules: (nodeType) => [wrappingInputRule(/^\s*>\s$/, nodeType)],
51
51
  commands: (nodeType) => [createCmd(WrapInBlockquote, () => wrapIn(nodeType))],
52
52
  shortcuts: {
@@ -9,30 +9,30 @@ type Keys = SupportedKeys['BulletList'];
9
9
 
10
10
  export const WrapInBulletList = createCmdKey();
11
11
 
12
- const id = 'bullet_list';
13
- export const bulletList = createNode<Keys>((_, utils) => {
12
+ export const bulletList = createNode<Keys>((utils) => {
13
+ const id = 'bullet_list';
14
14
  return {
15
15
  id,
16
- schema: {
16
+ schema: () => ({
17
17
  content: 'listItem+',
18
18
  group: 'block',
19
19
  parseDOM: [{ tag: 'ul' }],
20
20
  toDOM: (node) => {
21
21
  return ['ul', { class: utils.getClassName(node.attrs, 'bullet-list') }, 0];
22
22
  },
23
- },
24
- parser: {
25
- match: ({ type, ordered }) => type === 'list' && !ordered,
26
- runner: (state, node, type) => {
27
- state.openNode(type).next(node.children).closeNode();
23
+ parseMarkdown: {
24
+ match: ({ type, ordered }) => type === 'list' && !ordered,
25
+ runner: (state, node, type) => {
26
+ state.openNode(type).next(node.children).closeNode();
27
+ },
28
28
  },
29
- },
30
- serializer: {
31
- match: (node) => node.type.name === id,
32
- runner: (state, node) => {
33
- state.openNode('list', undefined, { ordered: false }).next(node.content).closeNode();
29
+ toMarkdown: {
30
+ match: (node) => node.type.name === id,
31
+ runner: (state, node) => {
32
+ state.openNode('list', undefined, { ordered: false }).next(node.content).closeNode();
33
+ },
34
34
  },
35
- },
35
+ }),
36
36
  inputRules: (nodeType) => [wrappingInputRule(/^\s*([-+*])\s$/, nodeType)],
37
37
  commands: (nodeType) => [createCmd(WrapInBulletList, () => wrapIn(nodeType))],
38
38
  shortcuts: {
@@ -32,7 +32,7 @@ const inputRegex = /^```(?<language>[a-z]*)? $/;
32
32
  export const TurnIntoCodeFence = createCmdKey();
33
33
 
34
34
  const id = 'fence';
35
- export const codeFence = createNode<Keys, { languageList?: string[] }>((options, utils) => {
35
+ export const codeFence = createNode<Keys, { languageList?: string[] }>((utils, options) => {
36
36
  const style = utils.getStyle(({ palette, mixin, size, font }) => {
37
37
  const { shadow, scrollbar, border } = mixin;
38
38
  const { lineWidth, radius } = size;
@@ -148,7 +148,7 @@ export const codeFence = createNode<Keys, { languageList?: string[] }>((options,
148
148
 
149
149
  return {
150
150
  id,
151
- schema: {
151
+ schema: () => ({
152
152
  content: 'text*',
153
153
  group: 'block',
154
154
  marks: '',
@@ -184,23 +184,25 @@ export const codeFence = createNode<Keys, { languageList?: string[] }>((options,
184
184
  ['pre', ['code', { spellCheck: 'false' }, 0]],
185
185
  ];
186
186
  },
187
- },
188
- parser: {
189
- match: ({ type }) => type === 'code',
190
- runner: (state, node, type) => {
191
- const language = node.lang as string;
192
- const value = node.value as string;
193
- state.openNode(type, { language });
194
- state.addText(value);
195
- state.closeNode();
187
+ parseMarkdown: {
188
+ match: ({ type }) => type === 'code',
189
+ runner: (state, node, type) => {
190
+ const language = node.lang as string;
191
+ const value = node.value as string;
192
+ state.openNode(type, { language });
193
+ state.addText(value);
194
+ state.closeNode();
195
+ },
196
196
  },
197
- },
198
- serializer: {
199
- match: (node) => node.type.name === id,
200
- runner: (state, node) => {
201
- state.addNode('code', undefined, node.content.firstChild?.text || '', { lang: node.attrs.language });
197
+ toMarkdown: {
198
+ match: (node) => node.type.name === id,
199
+ runner: (state, node) => {
200
+ state.addNode('code', undefined, node.content.firstChild?.text || '', {
201
+ lang: node.attrs.language,
202
+ });
203
+ },
202
204
  },
203
- },
205
+ }),
204
206
  inputRules: (nodeType) => [
205
207
  textblockTypeInputRule(inputRegex, nodeType, ([ok, language]) => {
206
208
  if (!ok) return;
@@ -211,7 +213,7 @@ export const codeFence = createNode<Keys, { languageList?: string[] }>((options,
211
213
  shortcuts: {
212
214
  [SupportedKeys.CodeFence]: createShortcut(TurnIntoCodeFence, 'Mod-Alt-c'),
213
215
  },
214
- view: (node, view, getPos) => {
216
+ view: (ctx) => (node, view, getPos) => {
215
217
  const container = document.createElement('div');
216
218
  const selectWrapper = document.createElement('div');
217
219
  const select = document.createElement('ul');
@@ -222,7 +224,7 @@ export const codeFence = createNode<Keys, { languageList?: string[] }>((options,
222
224
  valueWrapper.className = 'code-fence_value';
223
225
  const value = document.createElement('span');
224
226
  valueWrapper.appendChild(value);
225
- valueWrapper.appendChild(utils.ctx.get(themeToolCtx).slots.icon('downArrow'));
227
+ valueWrapper.appendChild(ctx.get(themeToolCtx).slots.icon('downArrow'));
226
228
 
227
229
  select.className = 'code-fence_select';
228
230
  select.addEventListener('mousedown', (e) => {
package/src/node/doc.ts CHANGED
@@ -1,22 +1,24 @@
1
1
  /* Copyright 2021, Milkdown by Mirone. */
2
2
  import { createNode } from '@milkdown/utils';
3
3
 
4
- export const doc = createNode(() => ({
5
- id: 'doc',
6
- schema: {
7
- content: 'block+',
8
- },
9
- parser: {
10
- match: ({ type }) => type === 'root',
11
- runner: (state, node, type) => {
12
- state.injectRoot(node, type);
13
- },
14
- },
15
- serializer: {
16
- match: (node) => node.type.name === 'doc',
17
- runner: (state, node) => {
18
- state.openNode('root');
19
- state.next(node.content);
20
- },
21
- },
22
- }));
4
+ export const doc = createNode(() => {
5
+ return {
6
+ id: 'doc',
7
+ schema: () => ({
8
+ content: 'block+',
9
+ parseMarkdown: {
10
+ match: ({ type }) => type === 'root',
11
+ runner: (state, node, type) => {
12
+ state.injectRoot(node, type);
13
+ },
14
+ },
15
+ toMarkdown: {
16
+ match: (node) => node.type.name === 'doc',
17
+ runner: (state, node) => {
18
+ state.openNode('root');
19
+ state.next(node.content);
20
+ },
21
+ },
22
+ }),
23
+ };
24
+ });
@@ -1,43 +1,59 @@
1
1
  /* Copyright 2021, Milkdown by Mirone. */
2
2
  import { createCmd, createCmdKey } from '@milkdown/core';
3
+ import { Plugin, ReplaceStep, Slice } from '@milkdown/prose';
3
4
  import { createNode, createShortcut } from '@milkdown/utils';
4
5
 
5
6
  import { SupportedKeys } from '../supported-keys';
6
7
 
7
8
  type Keys = SupportedKeys['HardBreak'];
8
9
 
9
- const id = 'hardbreak';
10
-
11
10
  export const InsertHardbreak = createCmdKey();
12
11
 
13
- export const hardbreak = createNode<Keys>((_, utils) => ({
14
- id,
15
- schema: {
16
- inline: true,
17
- group: 'inline',
18
- selectable: false,
19
- parseDOM: [{ tag: 'br' }],
20
- toDOM: (node) => ['br', { class: utils.getClassName(node.attrs, id) }],
21
- },
22
- parser: {
23
- match: ({ type }) => type === 'break',
24
- runner: (state, _, type) => {
25
- state.addNode(type);
26
- },
27
- },
28
- serializer: {
29
- match: (node) => node.type.name === id,
30
- runner: (state) => {
31
- state.addNode('break');
32
- },
33
- },
34
- commands: (nodeType) => [
35
- createCmd(InsertHardbreak, () => (state, dispatch) => {
36
- dispatch?.(state.tr.replaceSelectionWith(nodeType.create()).scrollIntoView());
37
- return true;
12
+ export const hardbreak = createNode<Keys>((utils) => {
13
+ return {
14
+ id: 'hardbreak',
15
+ schema: () => ({
16
+ inline: true,
17
+ group: 'inline',
18
+ selectable: false,
19
+ parseDOM: [{ tag: 'br' }],
20
+ toDOM: (node) => ['br', { class: utils.getClassName(node.attrs, 'hardbreak') }],
21
+ parseMarkdown: {
22
+ match: ({ type }) => type === 'break',
23
+ runner: (state, _, type) => {
24
+ state.addNode(type);
25
+ },
26
+ },
27
+ toMarkdown: {
28
+ match: (node) => node.type.name === 'hardbreak',
29
+ runner: (state) => {
30
+ state.addNode('break');
31
+ },
32
+ },
38
33
  }),
39
- ],
40
- shortcuts: {
41
- [SupportedKeys.HardBreak]: createShortcut(InsertHardbreak, 'Shift-Enter'),
42
- },
43
- }));
34
+ commands: (type) => [
35
+ createCmd(InsertHardbreak, () => (state, dispatch) => {
36
+ dispatch?.(state.tr.setMeta('hardbreak', true).replaceSelectionWith(type.create()).scrollIntoView());
37
+ return true;
38
+ }),
39
+ ],
40
+ shortcuts: {
41
+ [SupportedKeys.HardBreak]: createShortcut(InsertHardbreak, 'Shift-Enter'),
42
+ },
43
+ prosePlugins: (type) => [
44
+ new Plugin({
45
+ appendTransaction: (trs, _oldState, newState) => {
46
+ if (!trs.length) return;
47
+ const [tr] = trs;
48
+ if (!tr.getMeta('hardbreak')) return;
49
+ const [step] = tr.steps;
50
+ if (!(step instanceof ReplaceStep)) {
51
+ return;
52
+ }
53
+ const { from } = step as unknown as { slice: Slice; from: number; to: number };
54
+ return newState.tr.setNodeMarkup(from, type, undefined, []);
55
+ },
56
+ }),
57
+ ],
58
+ };
59
+ });
@@ -20,7 +20,7 @@ type Keys =
20
20
 
21
21
  export const TurnIntoHeading = createCmdKey<number>();
22
22
 
23
- export const heading = createNode<Keys>((options, utils) => {
23
+ export const heading = createNode<Keys>((utils, options) => {
24
24
  const id = 'heading';
25
25
  const headingMap: Record<number, string> = {
26
26
  1: css`
@@ -48,7 +48,7 @@ export const heading = createNode<Keys>((options, utils) => {
48
48
 
49
49
  return {
50
50
  id,
51
- schema: {
51
+ schema: () => ({
52
52
  content: 'inline*',
53
53
  group: 'block',
54
54
  attrs: {
@@ -69,31 +69,31 @@ export const heading = createNode<Keys>((options, utils) => {
69
69
  0,
70
70
  ];
71
71
  },
72
- },
73
- parser: {
74
- match: ({ type }) => type === id,
75
- runner: (state, node, type) => {
76
- const depth = node.depth as number;
77
- state.openNode(type, { level: depth });
78
- state.next(node.children);
79
- state.closeNode();
72
+ parseMarkdown: {
73
+ match: ({ type }) => type === id,
74
+ runner: (state, node, type) => {
75
+ const depth = node.depth as number;
76
+ state.openNode(type, { level: depth });
77
+ state.next(node.children);
78
+ state.closeNode();
79
+ },
80
80
  },
81
- },
82
- serializer: {
83
- match: (node) => node.type.name === id,
84
- runner: (state, node) => {
85
- state.openNode('heading', undefined, { depth: node.attrs.level });
86
- state.next(node.content);
87
- state.closeNode();
81
+ toMarkdown: {
82
+ match: (node) => node.type.name === id,
83
+ runner: (state, node) => {
84
+ state.openNode('heading', undefined, { depth: node.attrs.level });
85
+ state.next(node.content);
86
+ state.closeNode();
87
+ },
88
88
  },
89
- },
90
- inputRules: (nodeType) =>
89
+ }),
90
+ inputRules: (type) =>
91
91
  headingIndex.map((x) =>
92
- textblockTypeInputRule(new RegExp(`^(#{1,${x}})\\s$`), nodeType, () => ({
92
+ textblockTypeInputRule(new RegExp(`^(#{1,${x}})\\s$`), type, () => ({
93
93
  level: x,
94
94
  })),
95
95
  ),
96
- commands: (nodeType) => [createCmd(TurnIntoHeading, (level = 1) => setBlockType(nodeType, { level }))],
96
+ commands: (type) => [createCmd(TurnIntoHeading, (level = 1) => setBlockType(type, { level }))],
97
97
  shortcuts: {
98
98
  [SupportedKeys.H1]: createShortcut(TurnIntoHeading, 'Mod-Alt-1', 1),
99
99
  [SupportedKeys.H2]: createShortcut(TurnIntoHeading, 'Mod-Alt-2', 2),
package/src/node/hr.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  /* Copyright 2021, Milkdown by Mirone. */
2
2
  import { css } from '@emotion/css';
3
- import { createCmd, createCmdKey } from '@milkdown/core';
3
+ import { createCmd, createCmdKey, schemaCtx } from '@milkdown/core';
4
4
  import { InputRule, Selection } from '@milkdown/prose';
5
5
  import { createNode } from '@milkdown/utils';
6
6
 
7
7
  const id = 'hr';
8
8
  export const InsertHr = createCmdKey<string>();
9
- export const hr = createNode((_, utils) => {
9
+ export const hr = createNode((utils) => {
10
10
  const style = utils.getStyle(
11
11
  (themeTool) => css`
12
12
  height: ${themeTool.size.lineWidth};
@@ -16,44 +16,44 @@ export const hr = createNode((_, utils) => {
16
16
  );
17
17
  return {
18
18
  id,
19
- schema: {
19
+ schema: () => ({
20
20
  group: 'block',
21
21
  parseDOM: [{ tag: 'hr' }],
22
22
  toDOM: (node) => ['hr', { class: utils.getClassName(node.attrs, id, style) }],
23
- },
24
- parser: {
25
- match: ({ type }) => type === 'thematicBreak',
26
- runner: (state, _, type) => {
27
- state.addNode(type);
23
+ parseMarkdown: {
24
+ match: ({ type }) => type === 'thematicBreak',
25
+ runner: (state, _, type) => {
26
+ state.addNode(type);
27
+ },
28
28
  },
29
- },
30
- serializer: {
31
- match: (node) => node.type.name === id,
32
- runner: (state) => {
33
- state.addNode('thematicBreak');
29
+ toMarkdown: {
30
+ match: (node) => node.type.name === id,
31
+ runner: (state) => {
32
+ state.addNode('thematicBreak');
33
+ },
34
34
  },
35
- },
36
- inputRules: (nodeType) => [
35
+ }),
36
+ inputRules: (type) => [
37
37
  new InputRule(/^(?:---|___\s|\*\*\*\s)$/, (state, match, start, end) => {
38
38
  const { tr } = state;
39
39
 
40
40
  if (match[0]) {
41
- tr.replaceWith(start, end, nodeType.create({}));
41
+ tr.replaceWith(start - 1, end, type.create());
42
42
  }
43
43
 
44
44
  return tr;
45
45
  }),
46
46
  ],
47
- commands: (nodeType, schema) => [
47
+ commands: (type, ctx) => [
48
48
  createCmd(InsertHr, () => (state, dispatch) => {
49
49
  if (!dispatch) return true;
50
50
  const { tr, selection } = state;
51
51
  const from = selection.from;
52
- const node = nodeType.create();
52
+ const node = type.create();
53
53
  if (!node) {
54
54
  return true;
55
55
  }
56
- const _tr = tr.replaceSelectionWith(node).insert(from, schema.node('paragraph'));
56
+ const _tr = tr.replaceSelectionWith(node).insert(from, ctx.get(schemaCtx).node('paragraph'));
57
57
  const sel = Selection.findFrom(_tr.doc.resolve(from), 1, true);
58
58
  if (!sel) {
59
59
  return true;