@milkdown/preset-commonmark 6.5.2 → 6.5.3

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 (88) hide show
  1. package/lib/index.d.ts +1 -1
  2. package/lib/index.d.ts.map +1 -1
  3. package/lib/index.es.js +228 -219
  4. package/lib/index.es.js.map +1 -1
  5. package/lib/mark/code-inline.d.ts +1 -1
  6. package/lib/mark/code-inline.d.ts.map +1 -1
  7. package/lib/mark/em.d.ts +1 -1
  8. package/lib/mark/em.d.ts.map +1 -1
  9. package/lib/mark/index.d.ts.map +1 -1
  10. package/lib/mark/link.d.ts +4 -3
  11. package/lib/mark/link.d.ts.map +1 -1
  12. package/lib/mark/strong.d.ts +1 -1
  13. package/lib/mark/strong.d.ts.map +1 -1
  14. package/lib/node/blockquote.d.ts +1 -1
  15. package/lib/node/blockquote.d.ts.map +1 -1
  16. package/lib/node/bullet-list.d.ts +1 -1
  17. package/lib/node/bullet-list.d.ts.map +1 -1
  18. package/lib/node/code-fence.d.ts.map +1 -1
  19. package/lib/node/doc.d.ts +1 -1
  20. package/lib/node/doc.d.ts.map +1 -1
  21. package/lib/node/hardbreak.d.ts.map +1 -1
  22. package/lib/node/heading.d.ts +2 -2
  23. package/lib/node/heading.d.ts.map +1 -1
  24. package/lib/node/hr.d.ts +1 -1
  25. package/lib/node/hr.d.ts.map +1 -1
  26. package/lib/node/image.d.ts +2 -2
  27. package/lib/node/image.d.ts.map +1 -1
  28. package/lib/node/index.d.ts +1 -1
  29. package/lib/node/index.d.ts.map +1 -1
  30. package/lib/node/list-item.d.ts +2 -2
  31. package/lib/node/list-item.d.ts.map +1 -1
  32. package/lib/node/ordered-list.d.ts +1 -1
  33. package/lib/node/ordered-list.d.ts.map +1 -1
  34. package/lib/node/paragraph.d.ts +1 -1
  35. package/lib/node/paragraph.d.ts.map +1 -1
  36. package/lib/node/text.d.ts +1 -1
  37. package/lib/node/text.d.ts.map +1 -1
  38. package/lib/plugin/add-order-in-list.d.ts +1 -1
  39. package/lib/plugin/add-order-in-list.d.ts.map +1 -1
  40. package/lib/plugin/filter-html.d.ts +1 -1
  41. package/lib/plugin/filter-html.d.ts.map +1 -1
  42. package/lib/plugin/index.d.ts +1 -1
  43. package/lib/plugin/index.d.ts.map +1 -1
  44. package/lib/plugin/inline-nodes-cursor.d.ts.map +1 -1
  45. package/lib/plugin/inline-sync/config.d.ts +8 -8
  46. package/lib/plugin/inline-sync/config.d.ts.map +1 -1
  47. package/lib/plugin/inline-sync/context.d.ts +5 -5
  48. package/lib/plugin/inline-sync/context.d.ts.map +1 -1
  49. package/lib/plugin/inline-sync/index.d.ts +1 -1
  50. package/lib/plugin/inline-sync/index.d.ts.map +1 -1
  51. package/lib/plugin/inline-sync/regexp.d.ts.map +1 -1
  52. package/lib/plugin/inline-sync/replacer.d.ts +3 -3
  53. package/lib/plugin/inline-sync/replacer.d.ts.map +1 -1
  54. package/lib/plugin/inline-sync/utils.d.ts +2 -2
  55. package/lib/plugin/inline-sync/utils.d.ts.map +1 -1
  56. package/lib/supported-keys.d.ts +1 -1
  57. package/lib/supported-keys.d.ts.map +1 -1
  58. package/package.json +17 -17
  59. package/src/index.ts +52 -52
  60. package/src/mark/code-inline.ts +56 -55
  61. package/src/mark/em.ts +36 -36
  62. package/src/mark/index.ts +9 -9
  63. package/src/mark/link.ts +250 -223
  64. package/src/mark/strong.ts +37 -37
  65. package/src/node/blockquote.ts +35 -35
  66. package/src/node/bullet-list.ts +65 -65
  67. package/src/node/code-fence.ts +236 -229
  68. package/src/node/doc.ts +21 -21
  69. package/src/node/hardbreak.ts +108 -107
  70. package/src/node/heading.ts +275 -272
  71. package/src/node/hr.ts +55 -55
  72. package/src/node/image.ts +216 -210
  73. package/src/node/index.ts +38 -38
  74. package/src/node/list-item.ts +139 -140
  75. package/src/node/ordered-list.ts +71 -71
  76. package/src/node/paragraph.ts +55 -53
  77. package/src/node/text.ts +18 -18
  78. package/src/plugin/add-order-in-list.ts +15 -15
  79. package/src/plugin/filter-html.ts +31 -33
  80. package/src/plugin/index.ts +13 -13
  81. package/src/plugin/inline-nodes-cursor.ts +78 -79
  82. package/src/plugin/inline-sync/config.ts +46 -45
  83. package/src/plugin/inline-sync/context.ts +88 -83
  84. package/src/plugin/inline-sync/index.ts +46 -42
  85. package/src/plugin/inline-sync/regexp.ts +2 -2
  86. package/src/plugin/inline-sync/replacer.ts +33 -31
  87. package/src/plugin/inline-sync/utils.ts +53 -55
  88. package/src/supported-keys.ts +22 -21
@@ -1,158 +1,157 @@
1
1
  /* Copyright 2021, Milkdown by Mirone. */
2
- import { createCmd, createCmdKey } from '@milkdown/core';
3
- import { expectDomTypeError } from '@milkdown/exception';
4
- import { getNodeFromSchema } from '@milkdown/prose';
5
- import { wrappingInputRule } from '@milkdown/prose/inputrules';
6
- import { NodeType } from '@milkdown/prose/model';
7
- import { liftListItem, sinkListItem, splitListItem } from '@milkdown/prose/schema-list';
8
- import { EditorState, Plugin, PluginKey, Transaction } from '@milkdown/prose/state';
9
- import { createNode, createShortcut } from '@milkdown/utils';
2
+ import { createCmd, createCmdKey } from '@milkdown/core'
3
+ import { expectDomTypeError } from '@milkdown/exception'
4
+ import { getNodeFromSchema } from '@milkdown/prose'
5
+ import { wrappingInputRule } from '@milkdown/prose/inputrules'
6
+ import type { NodeType } from '@milkdown/prose/model'
7
+ import { liftListItem, sinkListItem, splitListItem } from '@milkdown/prose/schema-list'
8
+ import type { EditorState, Transaction } from '@milkdown/prose/state'
9
+ import { Plugin, PluginKey } from '@milkdown/prose/state'
10
+ import { createNode, createShortcut } from '@milkdown/utils'
10
11
 
11
- import { SupportedKeys } from '../supported-keys';
12
+ import { SupportedKeys } from '../supported-keys'
12
13
 
13
- type Keys = SupportedKeys['SinkListItem'] | SupportedKeys['LiftListItem'] | SupportedKeys['NextListItem'];
14
+ type Keys = SupportedKeys['SinkListItem'] | SupportedKeys['LiftListItem'] | SupportedKeys['NextListItem']
14
15
 
15
- const id = 'list_item';
16
+ const id = 'list_item'
16
17
 
17
- export const SplitListItem = createCmdKey('SplitListItem');
18
- export const SinkListItem = createCmdKey('SinkListItem');
19
- export const LiftListItem = createCmdKey('LiftListItem');
18
+ export const SplitListItem = createCmdKey('SplitListItem')
19
+ export const SinkListItem = createCmdKey('SinkListItem')
20
+ export const LiftListItem = createCmdKey('LiftListItem')
20
21
 
21
- const keepListOrderPluginKey = new PluginKey('MILKDOWN_KEEP_LIST_ORDER');
22
+ const keepListOrderPluginKey = new PluginKey('MILKDOWN_KEEP_LIST_ORDER')
22
23
 
23
24
  const createKeepListOrderPlugin = (type: NodeType) => {
24
- const walkThrough = (state: EditorState, callback: (tr: Transaction) => void) => {
25
- const orderedListType = getNodeFromSchema('ordered_list', state.schema);
26
- let tr = state.tr;
27
- state.doc.descendants((node, pos, parent, index) => {
28
- if (node.type === type && parent?.type === orderedListType) {
29
- let changed = false;
30
- const attrs = { ...node.attrs };
31
- if (node.attrs['listType'] !== 'ordered') {
32
- attrs['listType'] = 'ordered';
33
- changed = true;
34
- }
25
+ const walkThrough = (state: EditorState, callback: (tr: Transaction) => void) => {
26
+ const orderedListType = getNodeFromSchema('ordered_list', state.schema)
27
+ let tr = state.tr
28
+ state.doc.descendants((node, pos, parent, index) => {
29
+ if (node.type === type && parent?.type === orderedListType) {
30
+ let changed = false
31
+ const attrs = { ...node.attrs }
32
+ if (node.attrs.listType !== 'ordered') {
33
+ attrs.listType = 'ordered'
34
+ changed = true
35
+ }
35
36
 
36
- const base = parent?.maybeChild(0);
37
- if (base && base.type === type && base.attrs['listType'] === 'ordered') {
38
- attrs['label'] = `${index + 1}.`;
39
- changed = true;
40
- }
37
+ const base = parent?.maybeChild(0)
38
+ if (base && base.type === type && base.attrs.listType === 'ordered') {
39
+ attrs.label = `${index + 1}.`
40
+ changed = true
41
+ }
41
42
 
42
- if (node.attrs['label'] === '•') {
43
- attrs['label'] = `${index + 1}.`;
44
- changed = true;
45
- }
43
+ if (node.attrs.label === '•') {
44
+ attrs.label = `${index + 1}.`
45
+ changed = true
46
+ }
46
47
 
47
- if (changed) {
48
- tr = tr.setNodeMarkup(pos, undefined, attrs);
49
- }
50
- }
51
- });
52
- callback(tr);
53
- };
54
- return new Plugin({
55
- key: keepListOrderPluginKey,
56
- appendTransaction: (transactions, _oldState, nextState) => {
57
- let tr: Transaction | null = null;
58
- if (transactions.some((transaction) => transaction.docChanged)) {
59
- walkThrough(nextState, (t) => {
60
- tr = t;
61
- });
62
- }
48
+ if (changed)
49
+ tr = tr.setNodeMarkup(pos, undefined, attrs)
50
+ }
51
+ })
52
+ callback(tr)
53
+ }
54
+ return new Plugin({
55
+ key: keepListOrderPluginKey,
56
+ appendTransaction: (transactions, _oldState, nextState) => {
57
+ let tr: Transaction | null = null
58
+ if (transactions.some(transaction => transaction.docChanged)) {
59
+ walkThrough(nextState, (t) => {
60
+ tr = t
61
+ })
62
+ }
63
63
 
64
- return tr;
65
- },
66
- });
67
- };
64
+ return tr
65
+ },
66
+ })
67
+ }
68
68
 
69
- export const listItem = createNode<Keys>((utils) => ({
70
- id,
71
- schema: () => ({
72
- group: 'listItem',
73
- content: 'paragraph block*',
74
- attrs: {
75
- label: {
76
- default: '•',
77
- },
78
- listType: {
79
- default: 'bullet',
80
- },
81
- spread: {
82
- default: 'true',
83
- },
84
- },
85
- defining: true,
86
- parseDOM: [
87
- {
88
- tag: 'li.list-item',
89
- getAttrs: (dom) => {
90
- if (!(dom instanceof HTMLElement)) {
91
- throw expectDomTypeError(dom);
92
- }
93
- return {
94
- label: dom.dataset['label'],
95
- listType: dom.dataset['list-type'],
96
- spread: dom.dataset['spread'],
97
- };
98
- },
99
- contentElement: (dom) => {
100
- if (!(dom instanceof HTMLElement)) {
101
- throw expectDomTypeError(dom);
102
- }
69
+ export const listItem = createNode<Keys>(utils => ({
70
+ id,
71
+ schema: () => ({
72
+ group: 'listItem',
73
+ content: 'paragraph block*',
74
+ attrs: {
75
+ label: {
76
+ default: '•',
77
+ },
78
+ listType: {
79
+ default: 'bullet',
80
+ },
81
+ spread: {
82
+ default: 'true',
83
+ },
84
+ },
85
+ defining: true,
86
+ parseDOM: [
87
+ {
88
+ tag: 'li.list-item',
89
+ getAttrs: (dom) => {
90
+ if (!(dom instanceof HTMLElement))
91
+ throw expectDomTypeError(dom)
103
92
 
104
- const body = dom.querySelector<HTMLElement>('.list-item_body');
105
- if (!body) {
106
- return dom;
107
- }
108
- return body;
109
- },
110
- },
111
- { tag: 'li' },
112
- ],
113
- toDOM: (node) => {
114
- return [
115
- 'li',
116
- {
117
- class: utils.getClassName(node.attrs, 'list-item'),
118
- 'data-label': node.attrs['label'],
119
- 'data-list-type': node.attrs['listType'],
120
- 'data-spread': node.attrs['spread'],
121
- },
122
- ['div', { class: utils.getClassName(node.attrs, 'list-item_label') }, node.attrs['label']],
123
- ['div', { class: utils.getClassName(node.attrs, 'list-item_body') }, 0],
124
- ];
125
- },
126
- parseMarkdown: {
127
- match: ({ type, checked }) => type === 'listItem' && checked === null,
128
- runner: (state, node, type) => {
129
- const label = node['label'] != null ? `${node['label']}.` : '•';
130
- const listType = node['label'] != null ? 'ordered' : 'bullet';
131
- const spread = node['spread'] != null ? `${node['spread']}` : 'true';
132
- state.openNode(type, { label, listType, spread });
133
- state.next(node.children);
134
- state.closeNode();
135
- },
93
+ return {
94
+ label: dom.dataset.label,
95
+ listType: dom.dataset['list-type'],
96
+ spread: dom.dataset.spread,
97
+ }
136
98
  },
137
- toMarkdown: {
138
- match: (node) => node.type.name === id,
139
- runner: (state, node) => {
140
- state.openNode('listItem', undefined, { spread: node.attrs['spread'] === 'true' });
141
- state.next(node.content);
142
- state.closeNode();
143
- },
99
+ contentElement: (dom) => {
100
+ if (!(dom instanceof HTMLElement))
101
+ throw expectDomTypeError(dom)
102
+
103
+ const body = dom.querySelector<HTMLElement>('.list-item_body')
104
+ if (!body)
105
+ return dom
106
+
107
+ return body
144
108
  },
145
- }),
146
- inputRules: (nodeType) => [wrappingInputRule(/^\s*([-+*])\s$/, nodeType)],
147
- commands: (nodeType) => [
148
- createCmd(SplitListItem, () => splitListItem(nodeType)),
149
- createCmd(SinkListItem, () => sinkListItem(nodeType)),
150
- createCmd(LiftListItem, () => liftListItem(nodeType)),
109
+ },
110
+ { tag: 'li' },
151
111
  ],
152
- shortcuts: {
153
- [SupportedKeys.NextListItem]: createShortcut(SplitListItem, 'Enter'),
154
- [SupportedKeys.SinkListItem]: createShortcut(SinkListItem, 'Mod-]'),
155
- [SupportedKeys.LiftListItem]: createShortcut(LiftListItem, 'Mod-['),
112
+ toDOM: (node) => {
113
+ return [
114
+ 'li',
115
+ {
116
+ 'class': utils.getClassName(node.attrs, 'list-item'),
117
+ 'data-label': node.attrs.label,
118
+ 'data-list-type': node.attrs.listType,
119
+ 'data-spread': node.attrs.spread,
120
+ },
121
+ ['div', { class: utils.getClassName(node.attrs, 'list-item_label') }, node.attrs.label],
122
+ ['div', { class: utils.getClassName(node.attrs, 'list-item_body') }, 0],
123
+ ]
124
+ },
125
+ parseMarkdown: {
126
+ match: ({ type, checked }) => type === 'listItem' && checked === null,
127
+ runner: (state, node, type) => {
128
+ const label = node.label != null ? `${node.label}.` : '•'
129
+ const listType = node.label != null ? 'ordered' : 'bullet'
130
+ const spread = node.spread != null ? `${node.spread}` : 'true'
131
+ state.openNode(type, { label, listType, spread })
132
+ state.next(node.children)
133
+ state.closeNode()
134
+ },
135
+ },
136
+ toMarkdown: {
137
+ match: node => node.type.name === id,
138
+ runner: (state, node) => {
139
+ state.openNode('listItem', undefined, { spread: node.attrs.spread === 'true' })
140
+ state.next(node.content)
141
+ state.closeNode()
142
+ },
156
143
  },
157
- prosePlugins: (nodeType) => [createKeepListOrderPlugin(nodeType)],
158
- }));
144
+ }),
145
+ inputRules: nodeType => [wrappingInputRule(/^\s*([-+*])\s$/, nodeType)],
146
+ commands: nodeType => [
147
+ createCmd(SplitListItem, () => splitListItem(nodeType)),
148
+ createCmd(SinkListItem, () => sinkListItem(nodeType)),
149
+ createCmd(LiftListItem, () => liftListItem(nodeType)),
150
+ ],
151
+ shortcuts: {
152
+ [SupportedKeys.NextListItem]: createShortcut(SplitListItem, 'Enter'),
153
+ [SupportedKeys.SinkListItem]: createShortcut(SinkListItem, 'Mod-]'),
154
+ [SupportedKeys.LiftListItem]: createShortcut(LiftListItem, 'Mod-['),
155
+ },
156
+ prosePlugins: nodeType => [createKeepListOrderPlugin(nodeType)],
157
+ }))
@@ -1,79 +1,79 @@
1
1
  /* Copyright 2021, Milkdown by Mirone. */
2
- import { createCmd, createCmdKey } from '@milkdown/core';
3
- import { expectDomTypeError } from '@milkdown/exception';
4
- import { wrapIn } from '@milkdown/prose/commands';
5
- import { wrappingInputRule } from '@milkdown/prose/inputrules';
6
- import { createNode, createShortcut } from '@milkdown/utils';
2
+ import { createCmd, createCmdKey } from '@milkdown/core'
3
+ import { expectDomTypeError } from '@milkdown/exception'
4
+ import { wrapIn } from '@milkdown/prose/commands'
5
+ import { wrappingInputRule } from '@milkdown/prose/inputrules'
6
+ import { createNode, createShortcut } from '@milkdown/utils'
7
7
 
8
- import { SupportedKeys } from '../supported-keys';
8
+ import { SupportedKeys } from '../supported-keys'
9
9
 
10
- type Keys = SupportedKeys['OrderedList'];
10
+ type Keys = SupportedKeys['OrderedList']
11
11
 
12
- export const WrapInOrderedList = createCmdKey('WrapInOrderedList');
12
+ export const WrapInOrderedList = createCmdKey('WrapInOrderedList')
13
13
 
14
- const id = 'ordered_list';
15
- export const orderedList = createNode<Keys>((utils) => ({
16
- id,
17
- schema: () => ({
18
- content: 'listItem+',
19
- group: 'block',
20
- attrs: {
21
- order: {
22
- default: 1,
23
- },
24
- spread: {
25
- default: false,
26
- },
27
- },
28
- parseDOM: [
29
- {
30
- tag: 'ol',
31
- getAttrs: (dom) => {
32
- if (!(dom instanceof HTMLElement)) {
33
- throw expectDomTypeError(dom);
34
- }
35
- return {
36
- spread: dom.dataset['spread'],
37
- order: dom.hasAttribute('start') ? Number(dom.getAttribute('start')) : 1,
38
- };
39
- },
40
- },
41
- ],
42
- toDOM: (node) => [
43
- 'ol',
44
- {
45
- ...(node.attrs['order'] === 1 ? {} : node.attrs['order']),
46
- 'data-spread': node.attrs['spread'],
47
- class: utils.getClassName(node.attrs, 'ordered-list'),
48
- },
49
- 0,
50
- ],
51
- parseMarkdown: {
52
- match: ({ type, ordered }) => type === 'list' && !!ordered,
53
- runner: (state, node, type) => {
54
- const spread = node['spread'] != null ? `${node['spread']}` : 'true';
55
- state.openNode(type, { spread }).next(node.children).closeNode();
56
- },
57
- },
58
- toMarkdown: {
59
- match: (node) => node.type.name === id,
60
- runner: (state, node) => {
61
- state.openNode('list', undefined, { ordered: true, start: 1, spread: node.attrs['spread'] === 'true' });
62
- state.next(node.content);
63
- state.closeNode();
64
- },
14
+ const id = 'ordered_list'
15
+ export const orderedList = createNode<Keys>(utils => ({
16
+ id,
17
+ schema: () => ({
18
+ content: 'listItem+',
19
+ group: 'block',
20
+ attrs: {
21
+ order: {
22
+ default: 1,
23
+ },
24
+ spread: {
25
+ default: false,
26
+ },
27
+ },
28
+ parseDOM: [
29
+ {
30
+ tag: 'ol',
31
+ getAttrs: (dom) => {
32
+ if (!(dom instanceof HTMLElement))
33
+ throw expectDomTypeError(dom)
34
+
35
+ return {
36
+ spread: dom.dataset.spread,
37
+ order: dom.hasAttribute('start') ? Number(dom.getAttribute('start')) : 1,
38
+ }
65
39
  },
66
- }),
67
- inputRules: (nodeType) => [
68
- wrappingInputRule(
69
- /^(\d+)\.\s$/,
70
- nodeType,
71
- (match) => ({ order: Number(match[1]) }),
72
- (match, node) => node.childCount + node.attrs['order'] === Number(match[1]),
73
- ),
40
+ },
41
+ ],
42
+ toDOM: node => [
43
+ 'ol',
44
+ {
45
+ ...(node.attrs.order === 1 ? {} : node.attrs.order),
46
+ 'data-spread': node.attrs.spread,
47
+ 'class': utils.getClassName(node.attrs, 'ordered-list'),
48
+ },
49
+ 0,
74
50
  ],
75
- commands: (nodeType) => [createCmd(WrapInOrderedList, () => wrapIn(nodeType))],
76
- shortcuts: {
77
- [SupportedKeys.OrderedList]: createShortcut(WrapInOrderedList, 'Mod-Alt-7'),
51
+ parseMarkdown: {
52
+ match: ({ type, ordered }) => type === 'list' && !!ordered,
53
+ runner: (state, node, type) => {
54
+ const spread = node.spread != null ? `${node.spread}` : 'true'
55
+ state.openNode(type, { spread }).next(node.children).closeNode()
56
+ },
57
+ },
58
+ toMarkdown: {
59
+ match: node => node.type.name === id,
60
+ runner: (state, node) => {
61
+ state.openNode('list', undefined, { ordered: true, start: 1, spread: node.attrs.spread === 'true' })
62
+ state.next(node.content)
63
+ state.closeNode()
64
+ },
78
65
  },
79
- }));
66
+ }),
67
+ inputRules: nodeType => [
68
+ wrappingInputRule(
69
+ /^(\d+)\.\s$/,
70
+ nodeType,
71
+ match => ({ order: Number(match[1]) }),
72
+ (match, node) => node.childCount + node.attrs.order === Number(match[1]),
73
+ ),
74
+ ],
75
+ commands: nodeType => [createCmd(WrapInOrderedList, () => wrapIn(nodeType))],
76
+ shortcuts: {
77
+ [SupportedKeys.OrderedList]: createShortcut(WrapInOrderedList, 'Mod-Alt-7'),
78
+ },
79
+ }))
@@ -1,60 +1,62 @@
1
1
  /* Copyright 2021, Milkdown by Mirone. */
2
- import { createCmd, createCmdKey } from '@milkdown/core';
3
- import { setBlockType } from '@milkdown/prose/commands';
4
- import { Fragment, Node } from '@milkdown/prose/model';
5
- import { createNode, createShortcut } from '@milkdown/utils';
2
+ import { createCmd, createCmdKey } from '@milkdown/core'
3
+ import { setBlockType } from '@milkdown/prose/commands'
4
+ import type { Node } from '@milkdown/prose/model'
5
+ import { Fragment } from '@milkdown/prose/model'
6
+ import { createNode, createShortcut } from '@milkdown/utils'
6
7
 
7
- import { SupportedKeys } from '../supported-keys';
8
+ import { SupportedKeys } from '../supported-keys'
8
9
 
9
- type Keys = SupportedKeys['Text'];
10
+ type Keys = SupportedKeys['Text']
10
11
 
11
- export const TurnIntoText = createCmdKey('TurnIntoText');
12
+ export const TurnIntoText = createCmdKey('TurnIntoText')
12
13
 
13
- const id = 'paragraph';
14
+ const id = 'paragraph'
14
15
  export const paragraph = createNode<Keys>((utils) => {
15
- return {
16
- id,
17
- schema: () => ({
18
- content: 'inline*',
19
- group: 'block',
20
- parseDOM: [{ tag: 'p' }],
21
- toDOM: (node) => ['p', { class: utils.getClassName(node.attrs, id) }, 0],
22
- parseMarkdown: {
23
- match: (node) => node.type === 'paragraph',
24
- runner: (state, node, type) => {
25
- state.openNode(type);
26
- if (node.children) {
27
- state.next(node.children);
28
- } else {
29
- state.addText(node['value'] as string);
30
- }
31
- state.closeNode();
32
- },
33
- },
34
- toMarkdown: {
35
- match: (node) => node.type.name === 'paragraph',
36
- runner: (state, node) => {
37
- state.openNode('paragraph');
38
- const lastIsHardbreak = node.childCount >= 1 && node.lastChild?.type.name === 'hardbreak';
39
- if (lastIsHardbreak) {
40
- const contentArr: Node[] = [];
41
- node.content.forEach((n, _, i) => {
42
- if (i === node.childCount - 1) {
43
- return;
44
- }
45
- contentArr.push(n);
46
- });
47
- state.next(Fragment.fromArray(contentArr));
48
- } else {
49
- state.next(node.content);
50
- }
51
- state.closeNode();
52
- },
53
- },
54
- }),
55
- commands: (nodeType) => [createCmd(TurnIntoText, () => setBlockType(nodeType))],
56
- shortcuts: {
57
- [SupportedKeys.Text]: createShortcut(TurnIntoText, 'Mod-Alt-0'),
16
+ return {
17
+ id,
18
+ schema: () => ({
19
+ content: 'inline*',
20
+ group: 'block',
21
+ parseDOM: [{ tag: 'p' }],
22
+ toDOM: node => ['p', { class: utils.getClassName(node.attrs, id) }, 0],
23
+ parseMarkdown: {
24
+ match: node => node.type === 'paragraph',
25
+ runner: (state, node, type) => {
26
+ state.openNode(type)
27
+ if (node.children)
28
+ state.next(node.children)
29
+ else
30
+ state.addText(node.value as string)
31
+
32
+ state.closeNode()
33
+ },
34
+ },
35
+ toMarkdown: {
36
+ match: node => node.type.name === 'paragraph',
37
+ runner: (state, node) => {
38
+ state.openNode('paragraph')
39
+ const lastIsHardbreak = node.childCount >= 1 && node.lastChild?.type.name === 'hardbreak'
40
+ if (lastIsHardbreak) {
41
+ const contentArr: Node[] = []
42
+ node.content.forEach((n, _, i) => {
43
+ if (i === node.childCount - 1)
44
+ return
45
+
46
+ contentArr.push(n)
47
+ })
48
+ state.next(Fragment.fromArray(contentArr))
49
+ }
50
+ else {
51
+ state.next(node.content)
52
+ }
53
+ state.closeNode()
58
54
  },
59
- };
60
- });
55
+ },
56
+ }),
57
+ commands: nodeType => [createCmd(TurnIntoText, () => setBlockType(nodeType))],
58
+ shortcuts: {
59
+ [SupportedKeys.Text]: createShortcut(TurnIntoText, 'Mod-Alt-0'),
60
+ },
61
+ }
62
+ })
package/src/node/text.ts CHANGED
@@ -1,21 +1,21 @@
1
1
  /* Copyright 2021, Milkdown by Mirone. */
2
- import { createNode } from '@milkdown/utils';
2
+ import { createNode } from '@milkdown/utils'
3
3
 
4
4
  export const text = createNode(() => ({
5
- id: 'text',
6
- schema: () => ({
7
- group: 'inline',
8
- parseMarkdown: {
9
- match: ({ type }) => type === 'text',
10
- runner: (state, node) => {
11
- state.addText(node['value'] as string);
12
- },
13
- },
14
- toMarkdown: {
15
- match: (node) => node.type.name === 'text',
16
- runner: (state, node) => {
17
- state.addNode('text', undefined, node.text as string);
18
- },
19
- },
20
- }),
21
- }));
5
+ id: 'text',
6
+ schema: () => ({
7
+ group: 'inline',
8
+ parseMarkdown: {
9
+ match: ({ type }) => type === 'text',
10
+ runner: (state, node) => {
11
+ state.addText(node.value as string)
12
+ },
13
+ },
14
+ toMarkdown: {
15
+ match: node => node.type.name === 'text',
16
+ runner: (state, node) => {
17
+ state.addNode('text', undefined, node.text as string)
18
+ },
19
+ },
20
+ }),
21
+ }))
@@ -1,19 +1,19 @@
1
1
  /* Copyright 2021, Milkdown by Mirone. */
2
- import { Node } from 'unist';
3
- import { Parent, visit } from 'unist-util-visit';
2
+ import type { Node } from 'unist'
3
+ import type { Parent } from 'unist-util-visit'
4
+ import { visit } from 'unist-util-visit'
4
5
 
5
6
  export const addOrderInList = () => {
6
- function transformer(ast: Node) {
7
- visit(ast, 'list', (node: Parent & { ordered?: boolean; start?: number }) => {
8
- if (node.ordered) {
9
- const start = node.start ?? 1;
10
- node.children.forEach((child, index) => {
11
- (child as Node & { label: number }).label = index + start;
12
- });
13
- return;
14
- }
15
- });
16
- }
7
+ function transformer(ast: Node) {
8
+ visit(ast, 'list', (node: Parent & { ordered?: boolean; start?: number }) => {
9
+ if (node.ordered) {
10
+ const start = node.start ?? 1
11
+ node.children.forEach((child, index) => {
12
+ (child as Node & { label: number }).label = index + start
13
+ })
14
+ }
15
+ })
16
+ }
17
17
 
18
- return transformer;
19
- };
18
+ return transformer
19
+ }