@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.
- package/lib/index.d.ts +5 -3
- package/lib/index.d.ts.map +1 -1
- package/lib/mark/code-inline.d.ts +5 -1
- package/lib/mark/code-inline.d.ts.map +1 -1
- package/lib/mark/code-inline.js +16 -16
- package/lib/mark/code-inline.js.map +1 -1
- package/lib/mark/em.d.ts +5 -1
- package/lib/mark/em.d.ts.map +1 -1
- package/lib/mark/em.js +15 -15
- package/lib/mark/em.js.map +1 -1
- package/lib/mark/index.d.ts +1 -1
- package/lib/mark/index.d.ts.map +1 -1
- package/lib/mark/link.d.ts +5 -1
- package/lib/mark/link.d.ts.map +1 -1
- package/lib/mark/link.js +23 -23
- package/lib/mark/link.js.map +1 -1
- package/lib/mark/strong.d.ts +5 -1
- package/lib/mark/strong.d.ts.map +1 -1
- package/lib/mark/strong.js +15 -15
- package/lib/mark/strong.js.map +1 -1
- package/lib/node/blockquote.d.ts +5 -1
- package/lib/node/blockquote.d.ts.map +1 -1
- package/lib/node/blockquote.js +13 -13
- package/lib/node/blockquote.js.map +1 -1
- package/lib/node/bullet-list.d.ts +5 -1
- package/lib/node/bullet-list.d.ts.map +1 -1
- package/lib/node/bullet-list.js +14 -14
- package/lib/node/bullet-list.js.map +1 -1
- package/lib/node/code-fence.d.ts +6 -2
- package/lib/node/code-fence.d.ts.map +1 -1
- package/lib/node/code-fence.js +22 -20
- package/lib/node/code-fence.js.map +1 -1
- package/lib/node/doc.d.ts +5 -1
- package/lib/node/doc.d.ts.map +1 -1
- package/lib/node/doc.js +21 -19
- package/lib/node/doc.js.map +1 -1
- package/lib/node/hardbreak.d.ts +5 -1
- package/lib/node/hardbreak.d.ts.map +1 -1
- package/lib/node/hardbreak.js +50 -31
- package/lib/node/hardbreak.js.map +1 -1
- package/lib/node/heading.d.ts +5 -1
- package/lib/node/heading.d.ts.map +1 -1
- package/lib/node/heading.js +20 -20
- package/lib/node/heading.js.map +1 -1
- package/lib/node/hr.d.ts +5 -1
- package/lib/node/hr.d.ts.map +1 -1
- package/lib/node/hr.js +19 -19
- package/lib/node/hr.js.map +1 -1
- package/lib/node/image.d.ts +5 -1
- package/lib/node/image.d.ts.map +1 -1
- package/lib/node/image.js +32 -32
- package/lib/node/image.js.map +1 -1
- package/lib/node/index.d.ts +2 -2
- package/lib/node/index.d.ts.map +1 -1
- package/lib/node/list-item.d.ts +5 -1
- package/lib/node/list-item.d.ts.map +1 -1
- package/lib/node/list-item.js +17 -17
- package/lib/node/list-item.js.map +1 -1
- package/lib/node/ordered-list.d.ts +5 -1
- package/lib/node/ordered-list.d.ts.map +1 -1
- package/lib/node/ordered-list.js +15 -15
- package/lib/node/ordered-list.js.map +1 -1
- package/lib/node/paragraph.d.ts +5 -1
- package/lib/node/paragraph.d.ts.map +1 -1
- package/lib/node/paragraph.js +22 -22
- package/lib/node/paragraph.js.map +1 -1
- package/lib/node/text.d.ts +5 -1
- package/lib/node/text.d.ts.map +1 -1
- package/lib/node/text.js +12 -12
- package/lib/node/text.js.map +1 -1
- package/lib/plugin/index.d.ts +1 -1
- package/lib/plugin/index.d.ts.map +1 -1
- package/lib/plugin/index.js +6 -2
- package/lib/plugin/index.js.map +1 -1
- package/package.json +3 -3
- package/src/mark/code-inline.ts +16 -16
- package/src/mark/em.ts +15 -15
- package/src/mark/link.ts +23 -23
- package/src/mark/strong.ts +15 -15
- package/src/node/blockquote.ts +13 -13
- package/src/node/bullet-list.ts +14 -14
- package/src/node/code-fence.ts +21 -19
- package/src/node/doc.ts +21 -19
- package/src/node/hardbreak.ts +48 -32
- package/src/node/heading.ts +21 -21
- package/src/node/hr.ts +19 -19
- package/src/node/image.ts +32 -32
- package/src/node/list-item.ts +17 -17
- package/src/node/ordered-list.ts +15 -15
- package/src/node/paragraph.ts +21 -21
- package/src/node/text.ts +12 -12
- 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>((
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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((
|
|
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
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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,
|
|
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,
|
|
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 }),
|
package/src/mark/strong.ts
CHANGED
|
@@ -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>((
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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),
|
package/src/node/blockquote.ts
CHANGED
|
@@ -12,7 +12,7 @@ const id = 'blockquote';
|
|
|
12
12
|
|
|
13
13
|
export const WrapInBlockquote = createCmdKey();
|
|
14
14
|
|
|
15
|
-
export const blockquote = createNode<Keys>((
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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: {
|
package/src/node/bullet-list.ts
CHANGED
|
@@ -9,30 +9,30 @@ type Keys = SupportedKeys['BulletList'];
|
|
|
9
9
|
|
|
10
10
|
export const WrapInBulletList = createCmdKey();
|
|
11
11
|
|
|
12
|
-
const
|
|
13
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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: {
|
package/src/node/code-fence.ts
CHANGED
|
@@ -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[] }>((
|
|
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
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
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
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
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(
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
+
});
|
package/src/node/hardbreak.ts
CHANGED
|
@@ -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>((
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
41
|
-
|
|
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
|
+
});
|
package/src/node/heading.ts
CHANGED
|
@@ -20,7 +20,7 @@ type Keys =
|
|
|
20
20
|
|
|
21
21
|
export const TurnIntoHeading = createCmdKey<number>();
|
|
22
22
|
|
|
23
|
-
export const heading = createNode<Keys>((
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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: (
|
|
89
|
+
}),
|
|
90
|
+
inputRules: (type) =>
|
|
91
91
|
headingIndex.map((x) =>
|
|
92
|
-
textblockTypeInputRule(new RegExp(`^(#{1,${x}})\\s$`),
|
|
92
|
+
textblockTypeInputRule(new RegExp(`^(#{1,${x}})\\s$`), type, () => ({
|
|
93
93
|
level: x,
|
|
94
94
|
})),
|
|
95
95
|
),
|
|
96
|
-
commands: (
|
|
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((
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
parseMarkdown: {
|
|
24
|
+
match: ({ type }) => type === 'thematicBreak',
|
|
25
|
+
runner: (state, _, type) => {
|
|
26
|
+
state.addNode(type);
|
|
27
|
+
},
|
|
28
28
|
},
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
toMarkdown: {
|
|
30
|
+
match: (node) => node.type.name === id,
|
|
31
|
+
runner: (state) => {
|
|
32
|
+
state.addNode('thematicBreak');
|
|
33
|
+
},
|
|
34
34
|
},
|
|
35
|
-
},
|
|
36
|
-
inputRules: (
|
|
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,
|
|
41
|
+
tr.replaceWith(start - 1, end, type.create());
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
return tr;
|
|
45
45
|
}),
|
|
46
46
|
],
|
|
47
|
-
commands: (
|
|
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 =
|
|
52
|
+
const node = type.create();
|
|
53
53
|
if (!node) {
|
|
54
54
|
return true;
|
|
55
55
|
}
|
|
56
|
-
const _tr = tr.replaceSelectionWith(node).insert(from,
|
|
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;
|