@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
|
@@ -1,59 +1,63 @@
|
|
|
1
1
|
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
|
-
import { Ctx
|
|
3
|
-
import {
|
|
2
|
+
import type { Ctx } from '@milkdown/core'
|
|
3
|
+
import { editorViewCtx } from '@milkdown/core'
|
|
4
|
+
import { Plugin, PluginKey } from '@milkdown/prose/state'
|
|
4
5
|
|
|
5
|
-
import { inlineSyncConfigCtx } from './config'
|
|
6
|
-
import { getContextByState } from './context'
|
|
7
|
-
import { runReplacer } from './replacer'
|
|
6
|
+
import { inlineSyncConfigCtx } from './config'
|
|
7
|
+
import { getContextByState } from './context'
|
|
8
|
+
import { runReplacer } from './replacer'
|
|
8
9
|
|
|
9
|
-
export * from './config'
|
|
10
|
+
export * from './config'
|
|
10
11
|
|
|
11
|
-
export const inlineSyncPluginKey = new PluginKey('MILKDOWN_INLINE_SYNC')
|
|
12
|
+
export const inlineSyncPluginKey = new PluginKey('MILKDOWN_INLINE_SYNC')
|
|
12
13
|
export const getInlineSyncPlugin = (ctx: Ctx) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
14
|
+
let requestId: number | null = null
|
|
15
|
+
const inlineSyncPlugin = new Plugin<null>({
|
|
16
|
+
key: inlineSyncPluginKey,
|
|
17
|
+
state: {
|
|
18
|
+
init: () => {
|
|
19
|
+
return null
|
|
20
|
+
},
|
|
21
|
+
apply: (tr, _value, _oldState, newState) => {
|
|
22
|
+
const view = ctx.get(editorViewCtx)
|
|
23
|
+
if (!view.hasFocus?.() || !view.editable)
|
|
24
|
+
return null
|
|
23
25
|
|
|
24
|
-
|
|
26
|
+
if (!tr.docChanged)
|
|
27
|
+
return null
|
|
25
28
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
29
|
+
const meta = tr.getMeta(inlineSyncPluginKey)
|
|
30
|
+
if (meta)
|
|
31
|
+
return null
|
|
30
32
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
+
const context = getContextByState(ctx, newState)
|
|
34
|
+
if (!context)
|
|
35
|
+
return null
|
|
33
36
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
if (requestId) {
|
|
38
|
+
cancelAnimationFrame(requestId)
|
|
39
|
+
requestId = null
|
|
40
|
+
}
|
|
38
41
|
|
|
39
|
-
|
|
42
|
+
const { prevNode, nextNode, text } = context
|
|
40
43
|
|
|
41
|
-
|
|
44
|
+
const { shouldSyncNode } = ctx.get(inlineSyncConfigCtx)
|
|
42
45
|
|
|
43
|
-
|
|
46
|
+
if (!shouldSyncNode({ prevNode, nextNode, ctx, tr, text }))
|
|
47
|
+
return null
|
|
44
48
|
|
|
45
|
-
|
|
46
|
-
|
|
49
|
+
requestId = requestAnimationFrame(() => {
|
|
50
|
+
requestId = null
|
|
47
51
|
|
|
48
|
-
|
|
52
|
+
const { dispatch, state } = ctx.get(editorViewCtx)
|
|
49
53
|
|
|
50
|
-
|
|
51
|
-
|
|
54
|
+
runReplacer(ctx, inlineSyncPluginKey, state, dispatch, prevNode.attrs)
|
|
55
|
+
})
|
|
52
56
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
+
return null
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
})
|
|
57
61
|
|
|
58
|
-
|
|
59
|
-
}
|
|
62
|
+
return inlineSyncPlugin
|
|
63
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
2
|
|
|
3
|
-
export const linkRegexp = /\[(?<span>((www|https:\/\/|http:\/\/)[^\s\]]+))]\((?<url>[^\s\]]+)\)
|
|
3
|
+
export const linkRegexp = /\[(?<span>((www|https:\/\/|http:\/\/)[^\s\]]+))]\((?<url>[^\s\]]+)\)/
|
|
4
4
|
|
|
5
5
|
export const punctuationRegexp = (holePlaceholder: string) =>
|
|
6
|
-
|
|
6
|
+
new RegExp(`\\\\(?=[^\\w\\s${holePlaceholder}\\\\]|_)`, 'g')
|
|
@@ -1,43 +1,45 @@
|
|
|
1
1
|
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
|
-
import { Ctx } from '@milkdown/core'
|
|
3
|
-
import { Attrs } from '@milkdown/prose/model'
|
|
4
|
-
import { EditorState, PluginKey,
|
|
2
|
+
import type { Ctx } from '@milkdown/core'
|
|
3
|
+
import type { Attrs } from '@milkdown/prose/model'
|
|
4
|
+
import type { EditorState, PluginKey, Transaction } from '@milkdown/prose/state'
|
|
5
|
+
import { TextSelection } from '@milkdown/prose/state'
|
|
5
6
|
|
|
6
|
-
import { inlineSyncConfigCtx } from './config'
|
|
7
|
-
import { getContextByState } from './context'
|
|
8
|
-
import { calcOffset } from './utils'
|
|
7
|
+
import { inlineSyncConfigCtx } from './config'
|
|
8
|
+
import { getContextByState } from './context'
|
|
9
|
+
import { calcOffset } from './utils'
|
|
9
10
|
|
|
10
11
|
export const runReplacer = (
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
ctx: Ctx,
|
|
13
|
+
key: PluginKey,
|
|
14
|
+
state: EditorState,
|
|
15
|
+
dispatch: (tr: Transaction) => void,
|
|
16
|
+
attrs: Attrs,
|
|
16
17
|
) => {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
const { placeholderConfig } = ctx.get(inlineSyncConfigCtx)
|
|
19
|
+
const holePlaceholder = placeholderConfig.hole
|
|
20
|
+
// insert a placeholder to restore the selection
|
|
21
|
+
let tr = state.tr.setMeta(key, true).insertText(holePlaceholder, state.selection.from)
|
|
21
22
|
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
const nextState = state.apply(tr)
|
|
24
|
+
const context = getContextByState(ctx, nextState)
|
|
24
25
|
|
|
25
|
-
|
|
26
|
+
if (!context)
|
|
27
|
+
return
|
|
26
28
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
const { $from } = nextState.selection
|
|
30
|
+
const from = $from.before()
|
|
31
|
+
const to = $from.after()
|
|
30
32
|
|
|
31
|
-
|
|
33
|
+
const offset = calcOffset(context.nextNode, from, context.placeholder)
|
|
32
34
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
tr = tr
|
|
36
|
+
.replaceWith(from, to, context.nextNode)
|
|
37
|
+
.setNodeMarkup(from, undefined, attrs)
|
|
38
|
+
// delete the placeholder
|
|
39
|
+
.delete(offset + 1, offset + 2)
|
|
38
40
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
+
// restore the selection
|
|
42
|
+
tr = tr.setSelection(TextSelection.near(tr.doc.resolve(offset + 1)))
|
|
41
43
|
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
+
dispatch(tr)
|
|
45
|
+
}
|
|
@@ -1,73 +1,71 @@
|
|
|
1
1
|
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
2
|
|
|
3
|
-
import { Node } from '@milkdown/prose/model'
|
|
3
|
+
import type { Node } from '@milkdown/prose/model'
|
|
4
4
|
|
|
5
|
-
import { SyncNodePlaceholder } from './config'
|
|
6
|
-
import { linkRegexp, punctuationRegexp } from './regexp'
|
|
5
|
+
import type { SyncNodePlaceholder } from './config'
|
|
6
|
+
import { linkRegexp, punctuationRegexp } from './regexp'
|
|
7
7
|
|
|
8
8
|
export const keepLink = (str: string) => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
let text = str
|
|
10
|
+
let match = text.match(linkRegexp)
|
|
11
|
+
while (match && match.groups) {
|
|
12
|
+
const { span } = match.groups
|
|
13
|
+
text = text.replace(linkRegexp, span as string)
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
15
|
+
match = text.match(linkRegexp)
|
|
16
|
+
}
|
|
17
|
+
return text
|
|
18
|
+
}
|
|
19
19
|
|
|
20
20
|
export const swap = (text: string, first: number, last: number) => {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
21
|
+
const arr = text.split('')
|
|
22
|
+
const temp = arr[first]
|
|
23
|
+
if (arr[first] && arr[last]) {
|
|
24
|
+
arr[first] = arr[last] as string
|
|
25
|
+
arr[last] = temp as string
|
|
26
|
+
}
|
|
27
|
+
return arr.join('').toString()
|
|
28
|
+
}
|
|
29
29
|
|
|
30
30
|
export const replacePunctuation = (holePlaceholder: string) => (text: string) =>
|
|
31
|
-
|
|
31
|
+
text.replace(punctuationRegexp(holePlaceholder), '')
|
|
32
32
|
|
|
33
33
|
export const calculatePlaceholder = (placeholder: SyncNodePlaceholder) => (text: string) => {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
34
|
+
const index = text.indexOf(placeholder.hole)
|
|
35
|
+
const left = text.charAt(index - 1)
|
|
36
|
+
const right = text.charAt(index + 1)
|
|
37
|
+
const notAWord = /[^\w]|_/
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
39
|
+
// cursor on the right
|
|
40
|
+
if (!right)
|
|
41
|
+
return placeholder.punctuation
|
|
43
42
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
43
|
+
// cursor on the left
|
|
44
|
+
if (!left)
|
|
45
|
+
return placeholder.char
|
|
48
46
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
47
|
+
if (notAWord.test(left) && notAWord.test(right))
|
|
48
|
+
return placeholder.punctuation
|
|
52
49
|
|
|
53
|
-
|
|
54
|
-
}
|
|
50
|
+
return placeholder.char
|
|
51
|
+
}
|
|
55
52
|
|
|
56
53
|
export const calcOffset = (node: Node, from: number, placeholder: string) => {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
54
|
+
let offset = from
|
|
55
|
+
let find = false
|
|
56
|
+
node.descendants((n) => {
|
|
57
|
+
if (find)
|
|
58
|
+
return false
|
|
59
|
+
if (n.isText) {
|
|
60
|
+
const i = n.text?.indexOf(placeholder)
|
|
61
|
+
if (i != null && i >= 0) {
|
|
62
|
+
find = true
|
|
63
|
+
offset += i
|
|
64
|
+
return false
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
offset += n.nodeSize
|
|
68
|
+
return undefined
|
|
69
|
+
})
|
|
70
|
+
return offset
|
|
71
|
+
}
|
package/src/supported-keys.ts
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
1
|
/* Copyright 2021, Milkdown by Mirone. */
|
|
2
2
|
export const SupportedKeys = {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
} as const
|
|
3
|
+
HardBreak: 'HardBreak',
|
|
4
|
+
Blockquote: 'Blockquote',
|
|
5
|
+
BulletList: 'BulletList',
|
|
6
|
+
OrderedList: 'OrderedList',
|
|
7
|
+
CodeFence: 'CodeFence',
|
|
8
|
+
H1: 'H1',
|
|
9
|
+
H2: 'H2',
|
|
10
|
+
H3: 'H3',
|
|
11
|
+
H4: 'H4',
|
|
12
|
+
H5: 'H5',
|
|
13
|
+
H6: 'H6',
|
|
14
|
+
DowngradeHeading: 'DowngradeHeading',
|
|
15
|
+
Text: 'Text',
|
|
16
|
+
CodeInline: 'CodeInline',
|
|
17
|
+
Em: 'Em',
|
|
18
|
+
Bold: 'Bold',
|
|
19
|
+
NextListItem: 'NextListItem',
|
|
20
|
+
SinkListItem: 'SinkListItem',
|
|
21
|
+
LiftListItem: 'LiftListItem',
|
|
22
|
+
} as const
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-redeclare
|
|
25
|
+
export type SupportedKeys = typeof SupportedKeys
|