@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/bullet-list.ts
CHANGED
|
@@ -1,72 +1,72 @@
|
|
|
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['BulletList']
|
|
10
|
+
type Keys = SupportedKeys['BulletList']
|
|
11
11
|
|
|
12
|
-
export const WrapInBulletList = createCmdKey('WrapInBulletList')
|
|
12
|
+
export const WrapInBulletList = createCmdKey('WrapInBulletList')
|
|
13
13
|
|
|
14
14
|
export const bulletList = createNode<Keys>((utils) => {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
parseDOM: [
|
|
27
|
-
{
|
|
28
|
-
tag: 'ul',
|
|
29
|
-
getAttrs: (dom) => {
|
|
30
|
-
if (!(dom instanceof HTMLElement)) {
|
|
31
|
-
throw expectDomTypeError(dom);
|
|
32
|
-
}
|
|
33
|
-
return {
|
|
34
|
-
spread: dom.dataset['spread'],
|
|
35
|
-
};
|
|
36
|
-
},
|
|
37
|
-
},
|
|
38
|
-
],
|
|
39
|
-
toDOM: (node) => {
|
|
40
|
-
return [
|
|
41
|
-
'ul',
|
|
42
|
-
{
|
|
43
|
-
'data-spread': node.attrs['spread'],
|
|
44
|
-
class: utils.getClassName(node.attrs, 'bullet-list'),
|
|
45
|
-
},
|
|
46
|
-
0,
|
|
47
|
-
];
|
|
48
|
-
},
|
|
49
|
-
parseMarkdown: {
|
|
50
|
-
match: ({ type, ordered }) => type === 'list' && !ordered,
|
|
51
|
-
runner: (state, node, type) => {
|
|
52
|
-
const spread = node['spread'] != null ? `${node['spread']}` : 'false';
|
|
53
|
-
state.openNode(type, { spread }).next(node.children).closeNode();
|
|
54
|
-
},
|
|
55
|
-
},
|
|
56
|
-
toMarkdown: {
|
|
57
|
-
match: (node) => node.type.name === id,
|
|
58
|
-
runner: (state, node) => {
|
|
59
|
-
state
|
|
60
|
-
.openNode('list', undefined, { ordered: false, spread: node.attrs['spread'] === 'true' })
|
|
61
|
-
.next(node.content)
|
|
62
|
-
.closeNode();
|
|
63
|
-
},
|
|
64
|
-
},
|
|
65
|
-
}),
|
|
66
|
-
inputRules: (nodeType) => [wrappingInputRule(/^\s*([-+*])\s$/, nodeType)],
|
|
67
|
-
commands: (nodeType) => [createCmd(WrapInBulletList, () => wrapIn(nodeType))],
|
|
68
|
-
shortcuts: {
|
|
69
|
-
[SupportedKeys.BulletList]: createShortcut(WrapInBulletList, 'Mod-Alt-8'),
|
|
15
|
+
const id = 'bullet_list'
|
|
16
|
+
return {
|
|
17
|
+
id,
|
|
18
|
+
schema: () => ({
|
|
19
|
+
content: 'listItem+',
|
|
20
|
+
group: 'block',
|
|
21
|
+
attrs: {
|
|
22
|
+
spread: {
|
|
23
|
+
default: false,
|
|
70
24
|
},
|
|
71
|
-
|
|
72
|
-
|
|
25
|
+
},
|
|
26
|
+
parseDOM: [
|
|
27
|
+
{
|
|
28
|
+
tag: 'ul',
|
|
29
|
+
getAttrs: (dom) => {
|
|
30
|
+
if (!(dom instanceof HTMLElement))
|
|
31
|
+
throw expectDomTypeError(dom)
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
spread: dom.dataset.spread,
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
toDOM: (node) => {
|
|
40
|
+
return [
|
|
41
|
+
'ul',
|
|
42
|
+
{
|
|
43
|
+
'data-spread': node.attrs.spread,
|
|
44
|
+
'class': utils.getClassName(node.attrs, 'bullet-list'),
|
|
45
|
+
},
|
|
46
|
+
0,
|
|
47
|
+
]
|
|
48
|
+
},
|
|
49
|
+
parseMarkdown: {
|
|
50
|
+
match: ({ type, ordered }) => type === 'list' && !ordered,
|
|
51
|
+
runner: (state, node, type) => {
|
|
52
|
+
const spread = node.spread != null ? `${node.spread}` : 'false'
|
|
53
|
+
state.openNode(type, { spread }).next(node.children).closeNode()
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
toMarkdown: {
|
|
57
|
+
match: node => node.type.name === id,
|
|
58
|
+
runner: (state, node) => {
|
|
59
|
+
state
|
|
60
|
+
.openNode('list', undefined, { ordered: false, spread: node.attrs.spread === 'true' })
|
|
61
|
+
.next(node.content)
|
|
62
|
+
.closeNode()
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
}),
|
|
66
|
+
inputRules: nodeType => [wrappingInputRule(/^\s*([-+*])\s$/, nodeType)],
|
|
67
|
+
commands: nodeType => [createCmd(WrapInBulletList, () => wrapIn(nodeType))],
|
|
68
|
+
shortcuts: {
|
|
69
|
+
[SupportedKeys.BulletList]: createShortcut(WrapInBulletList, 'Mod-Alt-8'),
|
|
70
|
+
},
|
|
71
|
+
}
|
|
72
|
+
})
|
package/src/node/code-fence.ts
CHANGED
|
@@ -1,238 +1,245 @@
|
|
|
1
1
|
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
2
|
+
import type { ThemeCodeFenceType } from '@milkdown/core'
|
|
3
|
+
import { createCmd, createCmdKey, editorViewCtx } from '@milkdown/core'
|
|
4
|
+
import { expectDomTypeError } from '@milkdown/exception'
|
|
5
|
+
import { setBlockType } from '@milkdown/prose/commands'
|
|
6
|
+
import { textblockTypeInputRule } from '@milkdown/prose/inputrules'
|
|
7
|
+
import { Fragment } from '@milkdown/prose/model'
|
|
8
|
+
import type { NodeView } from '@milkdown/prose/view'
|
|
9
|
+
import { createNode, createShortcut } from '@milkdown/utils'
|
|
9
10
|
|
|
10
|
-
import { SupportedKeys } from '../supported-keys'
|
|
11
|
+
import { SupportedKeys } from '../supported-keys'
|
|
11
12
|
|
|
12
|
-
type Keys = SupportedKeys['CodeFence']
|
|
13
|
+
type Keys = SupportedKeys['CodeFence']
|
|
13
14
|
|
|
14
15
|
const languageOptions = [
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
]
|
|
32
|
-
|
|
33
|
-
export const backtickInputRegex = /^```(?<language>[a-z]*)?[\s\n]
|
|
34
|
-
export const tildeInputRegex = /^~~~(?<language>[a-z]*)?[\s\n]
|
|
35
|
-
|
|
36
|
-
export const TurnIntoCodeFence = createCmdKey('TurnIntoCodeFence')
|
|
37
|
-
|
|
38
|
-
const id = 'fence'
|
|
16
|
+
'',
|
|
17
|
+
'javascript',
|
|
18
|
+
'typescript',
|
|
19
|
+
'bash',
|
|
20
|
+
'sql',
|
|
21
|
+
'json',
|
|
22
|
+
'html',
|
|
23
|
+
'css',
|
|
24
|
+
'c',
|
|
25
|
+
'cpp',
|
|
26
|
+
'java',
|
|
27
|
+
'ruby',
|
|
28
|
+
'python',
|
|
29
|
+
'go',
|
|
30
|
+
'rust',
|
|
31
|
+
'markdown',
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
export const backtickInputRegex = /^```(?<language>[a-z]*)?[\s\n]$/
|
|
35
|
+
export const tildeInputRegex = /^~~~(?<language>[a-z]*)?[\s\n]$/
|
|
36
|
+
|
|
37
|
+
export const TurnIntoCodeFence = createCmdKey('TurnIntoCodeFence')
|
|
38
|
+
|
|
39
|
+
const id = 'fence'
|
|
39
40
|
export const codeFence = createNode<Keys, { languageList?: string[] }>((utils, options) => {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
);
|
|
125
|
-
};
|
|
126
|
-
return [
|
|
127
|
-
'div',
|
|
128
|
-
{
|
|
129
|
-
class: 'code-fence-container',
|
|
130
|
-
},
|
|
131
|
-
select,
|
|
132
|
-
[
|
|
133
|
-
'pre',
|
|
134
|
-
{
|
|
135
|
-
'data-language': node.attrs['language'],
|
|
136
|
-
class: utils.getClassName(node.attrs, 'code-fence'),
|
|
137
|
-
},
|
|
138
|
-
['code', { spellCheck: 'false' }, 0],
|
|
139
|
-
],
|
|
140
|
-
];
|
|
141
|
-
},
|
|
142
|
-
parseMarkdown: {
|
|
143
|
-
match: ({ type }) => type === 'code',
|
|
144
|
-
runner: (state, node, type) => {
|
|
145
|
-
const language = node['lang'] as string;
|
|
146
|
-
const value = node['value'] as string;
|
|
147
|
-
state.openNode(type, { language });
|
|
148
|
-
if (value) {
|
|
149
|
-
state.addText(value);
|
|
150
|
-
}
|
|
151
|
-
state.closeNode();
|
|
152
|
-
},
|
|
153
|
-
},
|
|
154
|
-
toMarkdown: {
|
|
155
|
-
match: (node) => node.type.name === id,
|
|
156
|
-
runner: (state, node) => {
|
|
157
|
-
state.addNode('code', undefined, node.content.firstChild?.text || '', {
|
|
158
|
-
lang: node.attrs['language'],
|
|
159
|
-
});
|
|
160
|
-
},
|
|
161
|
-
},
|
|
162
|
-
}),
|
|
163
|
-
inputRules: (nodeType) => [
|
|
164
|
-
textblockTypeInputRule(backtickInputRegex, nodeType, (match) => {
|
|
165
|
-
const [ok, language] = match;
|
|
166
|
-
if (!ok) return;
|
|
167
|
-
return { language };
|
|
168
|
-
}),
|
|
169
|
-
textblockTypeInputRule(tildeInputRegex, nodeType, (match) => {
|
|
170
|
-
const [ok, language] = match;
|
|
171
|
-
if (!ok) return;
|
|
172
|
-
return { language };
|
|
41
|
+
const languageList = options?.languageList || languageOptions
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
id,
|
|
45
|
+
schema: ctx => ({
|
|
46
|
+
content: 'text*',
|
|
47
|
+
group: 'block',
|
|
48
|
+
marks: '',
|
|
49
|
+
defining: true,
|
|
50
|
+
code: true,
|
|
51
|
+
attrs: {
|
|
52
|
+
language: {
|
|
53
|
+
default: '',
|
|
54
|
+
},
|
|
55
|
+
fold: {
|
|
56
|
+
default: true,
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
parseDOM: [
|
|
60
|
+
{
|
|
61
|
+
tag: 'div.code-fence-container',
|
|
62
|
+
preserveWhitespace: 'full',
|
|
63
|
+
getAttrs: (dom) => {
|
|
64
|
+
if (!(dom instanceof HTMLElement))
|
|
65
|
+
throw expectDomTypeError(dom)
|
|
66
|
+
|
|
67
|
+
return { language: dom.querySelector('pre')?.dataset.language }
|
|
68
|
+
},
|
|
69
|
+
getContent: (dom, schema) => {
|
|
70
|
+
if (!(dom instanceof HTMLElement))
|
|
71
|
+
throw expectDomTypeError(dom)
|
|
72
|
+
|
|
73
|
+
const text = dom.querySelector('pre')?.textContent ?? ''
|
|
74
|
+
if (!text)
|
|
75
|
+
return Fragment.empty
|
|
76
|
+
|
|
77
|
+
const textNode = schema.text(text)
|
|
78
|
+
return Fragment.from(textNode)
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
tag: 'pre',
|
|
83
|
+
preserveWhitespace: 'full',
|
|
84
|
+
getAttrs: (dom) => {
|
|
85
|
+
if (!(dom instanceof HTMLElement))
|
|
86
|
+
throw expectDomTypeError(dom)
|
|
87
|
+
|
|
88
|
+
return { language: dom.dataset.language }
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
],
|
|
92
|
+
toDOM: (node) => {
|
|
93
|
+
const select = document.createElement('select')
|
|
94
|
+
languageList.forEach((lang) => {
|
|
95
|
+
const option = document.createElement('option')
|
|
96
|
+
option.value = lang
|
|
97
|
+
option.innerText = !lang ? '--' : lang
|
|
98
|
+
if (lang === node.attrs.language)
|
|
99
|
+
option.selected = true
|
|
100
|
+
|
|
101
|
+
select.appendChild(option)
|
|
102
|
+
})
|
|
103
|
+
select.onchange = (e) => {
|
|
104
|
+
const target = e.target
|
|
105
|
+
if (!(target instanceof HTMLSelectElement))
|
|
106
|
+
return
|
|
107
|
+
|
|
108
|
+
const view = ctx.get(editorViewCtx)
|
|
109
|
+
if (!view.editable) {
|
|
110
|
+
target.value = node.attrs.language
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const { top, left } = target.getBoundingClientRect()
|
|
115
|
+
const result = view.posAtCoords({ top, left })
|
|
116
|
+
if (!result)
|
|
117
|
+
return
|
|
118
|
+
|
|
119
|
+
const { tr } = view.state
|
|
120
|
+
|
|
121
|
+
view.dispatch(
|
|
122
|
+
tr.setNodeMarkup(result.inside, undefined, {
|
|
123
|
+
...node.attrs,
|
|
124
|
+
language: target.value,
|
|
173
125
|
}),
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
126
|
+
)
|
|
127
|
+
}
|
|
128
|
+
return [
|
|
129
|
+
'div',
|
|
130
|
+
{
|
|
131
|
+
class: 'code-fence-container',
|
|
132
|
+
},
|
|
133
|
+
select,
|
|
134
|
+
[
|
|
135
|
+
'pre',
|
|
136
|
+
{
|
|
137
|
+
'data-language': node.attrs.language,
|
|
138
|
+
'class': utils.getClassName(node.attrs, 'code-fence'),
|
|
139
|
+
},
|
|
140
|
+
['code', { spellCheck: 'false' }, 0],
|
|
141
|
+
],
|
|
142
|
+
]
|
|
143
|
+
},
|
|
144
|
+
parseMarkdown: {
|
|
145
|
+
match: ({ type }) => type === 'code',
|
|
146
|
+
runner: (state, node, type) => {
|
|
147
|
+
const language = node.lang as string
|
|
148
|
+
const value = node.value as string
|
|
149
|
+
state.openNode(type, { language })
|
|
150
|
+
if (value)
|
|
151
|
+
state.addText(value)
|
|
152
|
+
|
|
153
|
+
state.closeNode()
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
toMarkdown: {
|
|
157
|
+
match: node => node.type.name === id,
|
|
158
|
+
runner: (state, node) => {
|
|
159
|
+
state.addNode('code', undefined, node.content.firstChild?.text || '', {
|
|
160
|
+
lang: node.attrs.language,
|
|
161
|
+
})
|
|
178
162
|
},
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
163
|
+
},
|
|
164
|
+
}),
|
|
165
|
+
inputRules: nodeType => [
|
|
166
|
+
textblockTypeInputRule(backtickInputRegex, nodeType, (match) => {
|
|
167
|
+
const [ok, language] = match
|
|
168
|
+
if (!ok)
|
|
169
|
+
return
|
|
170
|
+
return { language }
|
|
171
|
+
}),
|
|
172
|
+
textblockTypeInputRule(tildeInputRegex, nodeType, (match) => {
|
|
173
|
+
const [ok, language] = match
|
|
174
|
+
if (!ok)
|
|
175
|
+
return
|
|
176
|
+
return { language }
|
|
177
|
+
}),
|
|
178
|
+
],
|
|
179
|
+
commands: nodeType => [createCmd(TurnIntoCodeFence, () => setBlockType(nodeType))],
|
|
180
|
+
shortcuts: {
|
|
181
|
+
[SupportedKeys.CodeFence]: createShortcut(TurnIntoCodeFence, 'Mod-Alt-c'),
|
|
182
|
+
},
|
|
183
|
+
view: () => (node, view, getPos) => {
|
|
184
|
+
let currNode = node
|
|
185
|
+
|
|
186
|
+
const onSelectLanguage = (language: string) => {
|
|
187
|
+
const { tr } = view.state
|
|
188
|
+
view.dispatch(
|
|
189
|
+
tr.setNodeMarkup(getPos(), undefined, {
|
|
190
|
+
fold: true,
|
|
191
|
+
language,
|
|
192
|
+
}),
|
|
193
|
+
)
|
|
194
|
+
}
|
|
195
|
+
const onBlur = () => {
|
|
196
|
+
const { tr } = view.state
|
|
197
|
+
|
|
198
|
+
view.dispatch(
|
|
199
|
+
tr.setNodeMarkup(getPos(), undefined, {
|
|
200
|
+
...currNode.attrs,
|
|
201
|
+
fold: true,
|
|
202
|
+
}),
|
|
203
|
+
)
|
|
204
|
+
}
|
|
205
|
+
const onFocus = () => {
|
|
206
|
+
const { tr } = view.state
|
|
207
|
+
|
|
208
|
+
view.dispatch(
|
|
209
|
+
tr.setNodeMarkup(getPos(), undefined, {
|
|
210
|
+
...currNode.attrs,
|
|
211
|
+
fold: false,
|
|
212
|
+
}),
|
|
213
|
+
)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const renderer = utils.themeManager.get<ThemeCodeFenceType>('code-fence', {
|
|
217
|
+
view,
|
|
218
|
+
onBlur,
|
|
219
|
+
onFocus,
|
|
220
|
+
onSelectLanguage,
|
|
221
|
+
editable: () => view.editable,
|
|
222
|
+
languageList,
|
|
223
|
+
})
|
|
224
|
+
if (!renderer)
|
|
225
|
+
return {} as NodeView
|
|
226
|
+
|
|
227
|
+
const { dom, contentDOM, onUpdate, onDestroy } = renderer
|
|
228
|
+
onUpdate(currNode)
|
|
229
|
+
|
|
230
|
+
return {
|
|
231
|
+
dom,
|
|
232
|
+
contentDOM,
|
|
233
|
+
update: (updatedNode) => {
|
|
234
|
+
if (updatedNode.type.name !== id)
|
|
235
|
+
return false
|
|
236
|
+
currNode = updatedNode
|
|
237
|
+
onUpdate(currNode)
|
|
238
|
+
|
|
239
|
+
return true
|
|
236
240
|
},
|
|
237
|
-
|
|
238
|
-
}
|
|
241
|
+
destroy: onDestroy,
|
|
242
|
+
}
|
|
243
|
+
},
|
|
244
|
+
}
|
|
245
|
+
})
|
package/src/node/doc.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
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 doc = createNode(() => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
})
|
|
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
|
+
})
|