@milkdown/preset-commonmark 6.2.0 → 6.3.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.
- package/lib/index.es.js +1012 -1153
- 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/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/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 +4 -3
- 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 +2 -1
- 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 prev = parent?.maybeChild(index - 1);
|
|
37
|
+
if (prev && prev.type === type && prev.attrs['listType'] === 'ordered') {
|
|
38
|
+
const label = prev.attrs['label'];
|
|
39
|
+
attrs['label'] = `${Number(label.slice(0, -1)) + 1}.`;
|
|
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
7
|
import { inlineNodesCursorPlugin } from './inline-nodes-cursor';
|
|
7
8
|
|
|
8
9
|
export const commonmarkPlugins = [
|
|
9
10
|
createPlugin(() => ({
|
|
10
11
|
prosePlugins: () => [inlineNodesCursorPlugin],
|
|
11
|
-
remarkPlugins: () => [links, filterHTMLPlugin],
|
|
12
|
+
remarkPlugins: () => [links, filterHTMLPlugin, addOrderInList],
|
|
12
13
|
}))(),
|
|
13
14
|
];
|