@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.
- package/lib/index.d.ts +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.es.js +228 -219
- package/lib/index.es.js.map +1 -1
- package/lib/mark/code-inline.d.ts +1 -1
- package/lib/mark/code-inline.d.ts.map +1 -1
- package/lib/mark/em.d.ts +1 -1
- package/lib/mark/em.d.ts.map +1 -1
- package/lib/mark/index.d.ts.map +1 -1
- package/lib/mark/link.d.ts +4 -3
- package/lib/mark/link.d.ts.map +1 -1
- package/lib/mark/strong.d.ts +1 -1
- package/lib/mark/strong.d.ts.map +1 -1
- package/lib/node/blockquote.d.ts +1 -1
- package/lib/node/blockquote.d.ts.map +1 -1
- package/lib/node/bullet-list.d.ts +1 -1
- package/lib/node/bullet-list.d.ts.map +1 -1
- package/lib/node/code-fence.d.ts.map +1 -1
- package/lib/node/doc.d.ts +1 -1
- package/lib/node/doc.d.ts.map +1 -1
- package/lib/node/hardbreak.d.ts.map +1 -1
- package/lib/node/heading.d.ts +2 -2
- package/lib/node/heading.d.ts.map +1 -1
- package/lib/node/hr.d.ts +1 -1
- package/lib/node/hr.d.ts.map +1 -1
- package/lib/node/image.d.ts +2 -2
- package/lib/node/image.d.ts.map +1 -1
- package/lib/node/index.d.ts +1 -1
- package/lib/node/index.d.ts.map +1 -1
- package/lib/node/list-item.d.ts +2 -2
- package/lib/node/list-item.d.ts.map +1 -1
- package/lib/node/ordered-list.d.ts +1 -1
- package/lib/node/ordered-list.d.ts.map +1 -1
- package/lib/node/paragraph.d.ts +1 -1
- package/lib/node/paragraph.d.ts.map +1 -1
- package/lib/node/text.d.ts +1 -1
- package/lib/node/text.d.ts.map +1 -1
- package/lib/plugin/add-order-in-list.d.ts +1 -1
- package/lib/plugin/add-order-in-list.d.ts.map +1 -1
- package/lib/plugin/filter-html.d.ts +1 -1
- package/lib/plugin/filter-html.d.ts.map +1 -1
- package/lib/plugin/index.d.ts +1 -1
- package/lib/plugin/index.d.ts.map +1 -1
- package/lib/plugin/inline-nodes-cursor.d.ts.map +1 -1
- package/lib/plugin/inline-sync/config.d.ts +8 -8
- package/lib/plugin/inline-sync/config.d.ts.map +1 -1
- package/lib/plugin/inline-sync/context.d.ts +5 -5
- package/lib/plugin/inline-sync/context.d.ts.map +1 -1
- package/lib/plugin/inline-sync/index.d.ts +1 -1
- package/lib/plugin/inline-sync/index.d.ts.map +1 -1
- package/lib/plugin/inline-sync/regexp.d.ts.map +1 -1
- package/lib/plugin/inline-sync/replacer.d.ts +3 -3
- package/lib/plugin/inline-sync/replacer.d.ts.map +1 -1
- package/lib/plugin/inline-sync/utils.d.ts +2 -2
- package/lib/plugin/inline-sync/utils.d.ts.map +1 -1
- package/lib/supported-keys.d.ts +1 -1
- package/lib/supported-keys.d.ts.map +1 -1
- package/package.json +17 -17
- package/src/index.ts +52 -52
- package/src/mark/code-inline.ts +56 -55
- package/src/mark/em.ts +36 -36
- package/src/mark/index.ts +9 -9
- package/src/mark/link.ts +250 -223
- package/src/mark/strong.ts +37 -37
- package/src/node/blockquote.ts +35 -35
- package/src/node/bullet-list.ts +65 -65
- package/src/node/code-fence.ts +236 -229
- package/src/node/doc.ts +21 -21
- package/src/node/hardbreak.ts +108 -107
- package/src/node/heading.ts +275 -272
- package/src/node/hr.ts +55 -55
- package/src/node/image.ts +216 -210
- package/src/node/index.ts +38 -38
- package/src/node/list-item.ts +139 -140
- package/src/node/ordered-list.ts +71 -71
- package/src/node/paragraph.ts +55 -53
- package/src/node/text.ts +18 -18
- package/src/plugin/add-order-in-list.ts +15 -15
- package/src/plugin/filter-html.ts +31 -33
- package/src/plugin/index.ts +13 -13
- package/src/plugin/inline-nodes-cursor.ts +78 -79
- package/src/plugin/inline-sync/config.ts +46 -45
- package/src/plugin/inline-sync/context.ts +88 -83
- package/src/plugin/inline-sync/index.ts +46 -42
- package/src/plugin/inline-sync/regexp.ts +2 -2
- package/src/plugin/inline-sync/replacer.ts +33 -31
- package/src/plugin/inline-sync/utils.ts +53 -55
- package/src/supported-keys.ts +22 -21
package/src/node/list-item.ts
CHANGED
|
@@ -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,
|
|
9
|
-
import {
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
if (node.attrs.label === '•') {
|
|
44
|
+
attrs.label = `${index + 1}.`
|
|
45
|
+
changed = true
|
|
46
|
+
}
|
|
46
47
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
64
|
+
return tr
|
|
65
|
+
},
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
68
|
|
|
69
|
-
export const listItem = createNode<Keys>(
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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
|
-
|
|
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
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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
|
-
|
|
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
|
+
}))
|
package/src/node/ordered-list.ts
CHANGED
|
@@ -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>(
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
+
}))
|
package/src/node/paragraph.ts
CHANGED
|
@@ -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 {
|
|
5
|
-
import {
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
19
|
-
}
|
|
18
|
+
return transformer
|
|
19
|
+
}
|