@milkdown/preset-commonmark 6.2.0 → 6.3.2
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.es.js +1036 -1144
- package/lib/index.es.js.map +1 -1
- package/lib/mark/link.d.ts.map +1 -1
- package/lib/node/code-fence.d.ts.map +1 -1
- package/lib/node/hardbreak.d.ts +5 -1
- package/lib/node/hardbreak.d.ts.map +1 -1
- package/lib/node/heading.d.ts +5 -2
- package/lib/node/heading.d.ts.map +1 -1
- package/lib/node/image.d.ts.map +1 -1
- package/lib/node/list-item.d.ts.map +1 -1
- package/lib/node/ordered-list.d.ts.map +1 -1
- package/lib/plugin/add-order-in-list.d.ts +3 -0
- package/lib/plugin/add-order-in-list.d.ts.map +1 -0
- package/lib/plugin/index.d.ts.map +1 -1
- package/lib/plugin/inline-nodes-cursor.d.ts +1 -1
- package/lib/plugin/inline-nodes-cursor.d.ts.map +1 -1
- package/lib/supported-keys.d.ts +1 -0
- package/lib/supported-keys.d.ts.map +1 -1
- package/package.json +7 -5
- package/src/mark/link.ts +3 -2
- package/src/node/code-fence.ts +9 -4
- package/src/node/hardbreak.ts +30 -1
- package/src/node/heading.ts +261 -145
- package/src/node/image.ts +2 -1
- package/src/node/list-item.ts +93 -3
- package/src/node/ordered-list.ts +2 -1
- package/src/plugin/add-order-in-list.ts +19 -0
- package/src/plugin/index.ts +4 -3
- package/src/plugin/inline-nodes-cursor.ts +76 -48
- package/src/supported-keys.ts +1 -0
package/src/node/list-item.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
2
|
import { createCmd, createCmdKey } from '@milkdown/core';
|
|
3
|
+
import { expectDomTypeError } from '@milkdown/exception';
|
|
4
|
+
import { getNodeFromSchema } from '@milkdown/prose';
|
|
3
5
|
import { wrappingInputRule } from '@milkdown/prose/inputrules';
|
|
6
|
+
import { NodeType } from '@milkdown/prose/model';
|
|
4
7
|
import { liftListItem, sinkListItem, splitListItem } from '@milkdown/prose/schema-list';
|
|
8
|
+
import { EditorState, Plugin, PluginKey, Transaction } from '@milkdown/prose/state';
|
|
5
9
|
import { createNode, createShortcut } from '@milkdown/utils';
|
|
6
10
|
|
|
7
11
|
import { SupportedKeys } from '../supported-keys';
|
|
@@ -14,18 +18,103 @@ export const SplitListItem = createCmdKey('SplitListItem');
|
|
|
14
18
|
export const SinkListItem = createCmdKey('SinkListItem');
|
|
15
19
|
export const LiftListItem = createCmdKey('LiftListItem');
|
|
16
20
|
|
|
21
|
+
const keepListOrderPluginKey = new PluginKey('MILKDOWN_KEEP_LIST_ORDER');
|
|
22
|
+
|
|
23
|
+
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
|
+
}
|
|
35
|
+
|
|
36
|
+
const base = parent?.maybeChild(0);
|
|
37
|
+
if (base && base.type === type && base.attrs['listType'] === 'ordered') {
|
|
38
|
+
const label = base.attrs['label'];
|
|
39
|
+
attrs['label'] = `${Number(label.slice(0, -1)) + index}.`;
|
|
40
|
+
changed = true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (node.attrs['label'] === '•') {
|
|
44
|
+
attrs['label'] = `${index + 1}.`;
|
|
45
|
+
changed = true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (changed) {
|
|
49
|
+
tr = tr.setNodeMarkup(pos, undefined, attrs);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
callback(tr);
|
|
54
|
+
};
|
|
55
|
+
return new Plugin({
|
|
56
|
+
key: keepListOrderPluginKey,
|
|
57
|
+
appendTransaction: (transactions, _oldState, nextState) => {
|
|
58
|
+
let tr: Transaction | null = null;
|
|
59
|
+
if (transactions.some((transaction) => transaction.docChanged)) {
|
|
60
|
+
walkThrough(nextState, (t) => {
|
|
61
|
+
tr = t;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return tr;
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
|
|
17
70
|
export const listItem = createNode<Keys>((utils) => ({
|
|
18
71
|
id,
|
|
19
72
|
schema: () => ({
|
|
20
73
|
group: 'listItem',
|
|
21
74
|
content: 'paragraph block*',
|
|
75
|
+
attrs: {
|
|
76
|
+
label: {
|
|
77
|
+
default: '•',
|
|
78
|
+
},
|
|
79
|
+
listType: {
|
|
80
|
+
default: 'bullet',
|
|
81
|
+
},
|
|
82
|
+
},
|
|
22
83
|
defining: true,
|
|
23
|
-
parseDOM: [
|
|
24
|
-
|
|
84
|
+
parseDOM: [
|
|
85
|
+
{
|
|
86
|
+
tag: 'li.list-item',
|
|
87
|
+
getAttrs: (dom) => {
|
|
88
|
+
if (!(dom instanceof HTMLElement)) {
|
|
89
|
+
throw expectDomTypeError(dom);
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
label: dom.dataset['label'],
|
|
93
|
+
listType: dom.dataset['list-type'],
|
|
94
|
+
};
|
|
95
|
+
},
|
|
96
|
+
contentElement: 'div.list-item_body',
|
|
97
|
+
},
|
|
98
|
+
{ tag: 'li' },
|
|
99
|
+
],
|
|
100
|
+
toDOM: (node) => {
|
|
101
|
+
return [
|
|
102
|
+
'li',
|
|
103
|
+
{
|
|
104
|
+
class: utils.getClassName(node.attrs, 'list-item'),
|
|
105
|
+
'data-label': node.attrs['label'],
|
|
106
|
+
'data-list-type': node.attrs['listType'],
|
|
107
|
+
},
|
|
108
|
+
['div', { class: utils.getClassName(node.attrs, 'list-item_label') }, node.attrs['label']],
|
|
109
|
+
['div', { class: utils.getClassName(node.attrs, 'list-item_body') }, 0],
|
|
110
|
+
];
|
|
111
|
+
},
|
|
25
112
|
parseMarkdown: {
|
|
26
113
|
match: ({ type, checked }) => type === 'listItem' && checked === null,
|
|
27
114
|
runner: (state, node, type) => {
|
|
28
|
-
|
|
115
|
+
const label = node['label'] != null ? `${node['label']}.` : '•';
|
|
116
|
+
const listType = node['label'] != null ? 'ordered' : 'bullet';
|
|
117
|
+
state.openNode(type, { label, listType });
|
|
29
118
|
state.next(node.children);
|
|
30
119
|
state.closeNode();
|
|
31
120
|
},
|
|
@@ -50,4 +139,5 @@ export const listItem = createNode<Keys>((utils) => ({
|
|
|
50
139
|
[SupportedKeys.SinkListItem]: createShortcut(SinkListItem, 'Mod-]'),
|
|
51
140
|
[SupportedKeys.LiftListItem]: createShortcut(LiftListItem, 'Mod-['),
|
|
52
141
|
},
|
|
142
|
+
prosePlugins: (nodeType) => [createKeepListOrderPlugin(nodeType)],
|
|
53
143
|
}));
|
package/src/node/ordered-list.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
2
|
import { createCmd, createCmdKey } from '@milkdown/core';
|
|
3
|
+
import { expectDomTypeError } from '@milkdown/exception';
|
|
3
4
|
import { wrapIn } from '@milkdown/prose/commands';
|
|
4
5
|
import { wrappingInputRule } from '@milkdown/prose/inputrules';
|
|
5
6
|
import { createNode, createShortcut } from '@milkdown/utils';
|
|
@@ -26,7 +27,7 @@ export const orderedList = createNode<Keys>((utils) => ({
|
|
|
26
27
|
tag: 'ol',
|
|
27
28
|
getAttrs: (dom) => {
|
|
28
29
|
if (!(dom instanceof HTMLElement)) {
|
|
29
|
-
throw
|
|
30
|
+
throw expectDomTypeError(dom);
|
|
30
31
|
}
|
|
31
32
|
return { order: dom.hasAttribute('start') ? Number(dom.getAttribute('start')) : 1 };
|
|
32
33
|
},
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
|
+
import { Node } from 'unist';
|
|
3
|
+
import { Parent, visit } from 'unist-util-visit';
|
|
4
|
+
|
|
5
|
+
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
|
+
}
|
|
17
|
+
|
|
18
|
+
return transformer;
|
|
19
|
+
};
|
package/src/plugin/index.ts
CHANGED
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
import { createPlugin } from '@milkdown/utils';
|
|
3
3
|
import links from 'remark-inline-links';
|
|
4
4
|
|
|
5
|
+
import { addOrderInList } from './add-order-in-list';
|
|
5
6
|
import { filterHTMLPlugin } from './filter-html';
|
|
6
|
-
import {
|
|
7
|
+
import { getInlineNodesCursorPlugin } from './inline-nodes-cursor';
|
|
7
8
|
|
|
8
9
|
export const commonmarkPlugins = [
|
|
9
10
|
createPlugin(() => ({
|
|
10
|
-
prosePlugins: () => [
|
|
11
|
-
remarkPlugins: () => [links, filterHTMLPlugin],
|
|
11
|
+
prosePlugins: () => [getInlineNodesCursorPlugin()],
|
|
12
|
+
remarkPlugins: () => [links, filterHTMLPlugin, addOrderInList],
|
|
12
13
|
}))(),
|
|
13
14
|
];
|
|
@@ -8,59 +8,87 @@ const inlineNodesCursorPluginKey = new PluginKey('MILKDOWN_INLINE_NODES_CURSOR')
|
|
|
8
8
|
* This plugin is to solve the chrome 98 bug:
|
|
9
9
|
* https://discuss.prosemirror.net/t/cursor-jumps-at-the-end-of-line-when-it-betweens-two-inline-nodes/4641
|
|
10
10
|
*/
|
|
11
|
-
export const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
apply(tr) {
|
|
18
|
-
if (!tr.selection.empty) {
|
|
11
|
+
export const getInlineNodesCursorPlugin = (): Plugin => {
|
|
12
|
+
let lock = false;
|
|
13
|
+
const inlineNodesCursorPlugin: Plugin = new Plugin({
|
|
14
|
+
key: inlineNodesCursorPluginKey,
|
|
15
|
+
state: {
|
|
16
|
+
init() {
|
|
19
17
|
return false;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
},
|
|
30
|
-
},
|
|
31
|
-
props: {
|
|
32
|
-
handleDOMEvents: {
|
|
33
|
-
beforeinput: (view, e) => {
|
|
34
|
-
const active = inlineNodesCursorPlugin.getState(view.state);
|
|
35
|
-
if (active && e instanceof InputEvent) {
|
|
36
|
-
const from = view.state.selection.from;
|
|
37
|
-
e.preventDefault();
|
|
38
|
-
view.dispatch(view.state.tr.insertText(e.data || '', from));
|
|
39
|
-
|
|
18
|
+
},
|
|
19
|
+
apply(tr) {
|
|
20
|
+
if (!tr.selection.empty) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
const pos = tr.selection.$from;
|
|
24
|
+
const left = pos.nodeBefore;
|
|
25
|
+
const right = pos.nodeAfter;
|
|
26
|
+
if (left && right && left.isInline && !left.isText && right.isInline && !right.isText) {
|
|
40
27
|
return true;
|
|
41
28
|
}
|
|
42
29
|
|
|
43
30
|
return false;
|
|
44
31
|
},
|
|
45
32
|
},
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
33
|
+
props: {
|
|
34
|
+
handleDOMEvents: {
|
|
35
|
+
compositionend: (view, e) => {
|
|
36
|
+
if (lock) {
|
|
37
|
+
lock = false;
|
|
38
|
+
requestAnimationFrame(() => {
|
|
39
|
+
const active = inlineNodesCursorPlugin.getState(view.state);
|
|
40
|
+
if (active) {
|
|
41
|
+
const from = view.state.selection.from;
|
|
42
|
+
e.preventDefault();
|
|
43
|
+
view.dispatch(view.state.tr.insertText(e.data || '', from));
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
},
|
|
51
|
+
compositionstart: (view) => {
|
|
52
|
+
const active = inlineNodesCursorPlugin.getState(view.state);
|
|
53
|
+
if (active) {
|
|
54
|
+
lock = true;
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
},
|
|
58
|
+
beforeinput: (view, e) => {
|
|
59
|
+
const active = inlineNodesCursorPlugin.getState(view.state);
|
|
60
|
+
if (active && e instanceof InputEvent && e.data && !lock) {
|
|
61
|
+
const from = view.state.selection.from;
|
|
62
|
+
e.preventDefault();
|
|
63
|
+
view.dispatch(view.state.tr.insertText(e.data || '', from));
|
|
64
|
+
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return false;
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
decorations(state) {
|
|
72
|
+
const active = inlineNodesCursorPlugin.getState(state);
|
|
73
|
+
if (active) {
|
|
74
|
+
const pos = state.selection.$from;
|
|
75
|
+
const position = pos.pos;
|
|
76
|
+
const left = document.createElement('span');
|
|
77
|
+
const leftDec = Decoration.widget(position, left, {
|
|
78
|
+
side: -1,
|
|
79
|
+
});
|
|
80
|
+
const right = document.createElement('span');
|
|
81
|
+
const rightDec = Decoration.widget(position, right);
|
|
82
|
+
setTimeout(() => {
|
|
83
|
+
left.contentEditable = 'true';
|
|
84
|
+
right.contentEditable = 'true';
|
|
85
|
+
});
|
|
86
|
+
return DecorationSet.create(state.doc, [leftDec, rightDec]);
|
|
87
|
+
}
|
|
88
|
+
return DecorationSet.empty;
|
|
89
|
+
},
|
|
64
90
|
},
|
|
65
|
-
}
|
|
66
|
-
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
return inlineNodesCursorPlugin;
|
|
94
|
+
};
|