@jvs-milkdown/preset-commonmark 1.0.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/LICENSE +21 -0
- package/README.md +11 -0
- package/lib/__internal__/index.d.ts +3 -0
- package/lib/__internal__/index.d.ts.map +1 -0
- package/lib/__internal__/serialize-text.d.ts +4 -0
- package/lib/__internal__/serialize-text.d.ts.map +1 -0
- package/lib/__internal__/with-meta.d.ts +3 -0
- package/lib/__internal__/with-meta.d.ts.map +1 -0
- package/lib/__test__/html.spec.d.ts +2 -0
- package/lib/__test__/html.spec.d.ts.map +1 -0
- package/lib/__test__/trailing-space.spec.d.ts +2 -0
- package/lib/__test__/trailing-space.spec.d.ts.map +1 -0
- package/lib/__test__/vitest.setup.d.ts +1 -0
- package/lib/__test__/vitest.setup.d.ts.map +1 -0
- package/lib/commands/index.d.ts +20 -0
- package/lib/commands/index.d.ts.map +1 -0
- package/lib/composed/commands.d.ts +3 -0
- package/lib/composed/commands.d.ts.map +1 -0
- package/lib/composed/index.d.ts +6 -0
- package/lib/composed/index.d.ts.map +1 -0
- package/lib/composed/inputrules.d.ts +4 -0
- package/lib/composed/inputrules.d.ts.map +1 -0
- package/lib/composed/keymap.d.ts +3 -0
- package/lib/composed/keymap.d.ts.map +1 -0
- package/lib/composed/plugins.d.ts +3 -0
- package/lib/composed/plugins.d.ts.map +1 -0
- package/lib/composed/schema.d.ts +3 -0
- package/lib/composed/schema.d.ts.map +1 -0
- package/lib/index.d.ts +8 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +2153 -0
- package/lib/index.js.map +1 -0
- package/lib/mark/emphasis.d.ts +7 -0
- package/lib/mark/emphasis.d.ts.map +1 -0
- package/lib/mark/index.d.ts +5 -0
- package/lib/mark/index.d.ts.map +1 -0
- package/lib/mark/inline-code.d.ts +6 -0
- package/lib/mark/inline-code.d.ts.map +1 -0
- package/lib/mark/link.d.ts +9 -0
- package/lib/mark/link.d.ts.map +1 -0
- package/lib/mark/strong.d.ts +6 -0
- package/lib/mark/strong.d.ts.map +1 -0
- package/lib/node/blockquote.d.ts +7 -0
- package/lib/node/blockquote.d.ts.map +1 -0
- package/lib/node/bullet-list.d.ts +6 -0
- package/lib/node/bullet-list.d.ts.map +1 -0
- package/lib/node/code-block.d.ts +10 -0
- package/lib/node/code-block.d.ts.map +1 -0
- package/lib/node/doc.d.ts +2 -0
- package/lib/node/doc.d.ts.map +1 -0
- package/lib/node/hardbreak.d.ts +5 -0
- package/lib/node/hardbreak.d.ts.map +1 -0
- package/lib/node/heading.d.ts +11 -0
- package/lib/node/heading.d.ts.map +1 -0
- package/lib/node/hr.d.ts +5 -0
- package/lib/node/hr.d.ts.map +1 -0
- package/lib/node/html.d.ts +3 -0
- package/lib/node/html.d.ts.map +1 -0
- package/lib/node/image.d.ts +11 -0
- package/lib/node/image.d.ts.map +1 -0
- package/lib/node/index.d.ts +14 -0
- package/lib/node/index.d.ts.map +1 -0
- package/lib/node/list-item.d.ts +8 -0
- package/lib/node/list-item.d.ts.map +1 -0
- package/lib/node/ordered-list.d.ts +6 -0
- package/lib/node/ordered-list.d.ts.map +1 -0
- package/lib/node/paragraph.d.ts +5 -0
- package/lib/node/paragraph.d.ts.map +1 -0
- package/lib/node/text.d.ts +2 -0
- package/lib/node/text.d.ts.map +1 -0
- package/lib/plugin/hardbreak-clear-mark-plugin.d.ts +2 -0
- package/lib/plugin/hardbreak-clear-mark-plugin.d.ts.map +1 -0
- package/lib/plugin/hardbreak-filter-plugin.d.ts +3 -0
- package/lib/plugin/hardbreak-filter-plugin.d.ts.map +1 -0
- package/lib/plugin/index.d.ts +12 -0
- package/lib/plugin/index.d.ts.map +1 -0
- package/lib/plugin/inline-nodes-cursor-plugin.d.ts +2 -0
- package/lib/plugin/inline-nodes-cursor-plugin.d.ts.map +1 -0
- package/lib/plugin/remark-add-order-in-list-plugin.d.ts +2 -0
- package/lib/plugin/remark-add-order-in-list-plugin.d.ts.map +1 -0
- package/lib/plugin/remark-html-transformer.d.ts +2 -0
- package/lib/plugin/remark-html-transformer.d.ts.map +1 -0
- package/lib/plugin/remark-inline-link-plugin.d.ts +2 -0
- package/lib/plugin/remark-inline-link-plugin.d.ts.map +1 -0
- package/lib/plugin/remark-line-break.d.ts +2 -0
- package/lib/plugin/remark-line-break.d.ts.map +1 -0
- package/lib/plugin/remark-marker-plugin.d.ts +2 -0
- package/lib/plugin/remark-marker-plugin.d.ts.map +1 -0
- package/lib/plugin/remark-preserve-empty-line.d.ts +2 -0
- package/lib/plugin/remark-preserve-empty-line.d.ts.map +1 -0
- package/lib/plugin/sync-heading-id-plugin.d.ts +2 -0
- package/lib/plugin/sync-heading-id-plugin.d.ts.map +1 -0
- package/lib/plugin/sync-list-order-plugin.d.ts +2 -0
- package/lib/plugin/sync-list-order-plugin.d.ts.map +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/package.json +44 -0
- package/src/__internal__/index.ts +2 -0
- package/src/__internal__/serialize-text.ts +21 -0
- package/src/__internal__/with-meta.ts +15 -0
- package/src/__test__/html.spec.ts +46 -0
- package/src/__test__/trailing-space.spec.ts +27 -0
- package/src/__test__/vitest.setup.ts +65 -0
- package/src/commands/index.ts +140 -0
- package/src/composed/commands.ts +72 -0
- package/src/composed/index.ts +5 -0
- package/src/composed/inputrules.ts +34 -0
- package/src/composed/keymap.ts +29 -0
- package/src/composed/plugins.ts +35 -0
- package/src/composed/schema.ts +92 -0
- package/src/index.ts +26 -0
- package/src/mark/emphasis.ts +130 -0
- package/src/mark/index.ts +4 -0
- package/src/mark/inline-code.ts +123 -0
- package/src/mark/link.ts +134 -0
- package/src/mark/strong.ts +130 -0
- package/src/node/blockquote.ts +100 -0
- package/src/node/bullet-list.ts +129 -0
- package/src/node/code-block.ts +176 -0
- package/src/node/doc.ts +26 -0
- package/src/node/hardbreak.ts +134 -0
- package/src/node/heading.ts +271 -0
- package/src/node/hr.ts +87 -0
- package/src/node/html.ts +66 -0
- package/src/node/image.ts +173 -0
- package/src/node/index.ts +14 -0
- package/src/node/list-item.ts +244 -0
- package/src/node/ordered-list.ts +141 -0
- package/src/node/paragraph.ts +136 -0
- package/src/node/text.ts +25 -0
- package/src/plugin/hardbreak-clear-mark-plugin.ts +58 -0
- package/src/plugin/hardbreak-filter-plugin.ts +46 -0
- package/src/plugin/index.ts +14 -0
- package/src/plugin/inline-nodes-cursor-plugin.ts +103 -0
- package/src/plugin/remark-add-order-in-list-plugin.ts +29 -0
- package/src/plugin/remark-html-transformer.ts +74 -0
- package/src/plugin/remark-inline-link-plugin.ts +20 -0
- package/src/plugin/remark-line-break.ts +69 -0
- package/src/plugin/remark-marker-plugin.ts +33 -0
- package/src/plugin/remark-preserve-empty-line.ts +49 -0
- package/src/plugin/sync-heading-id-plugin.ts +67 -0
- package/src/plugin/sync-list-order-plugin.ts +112 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { Node } from '@jvs-milkdown/prose/model'
|
|
2
|
+
import type { EditorState, Transaction } from '@jvs-milkdown/prose/state'
|
|
3
|
+
|
|
4
|
+
import { Plugin, PluginKey } from '@jvs-milkdown/prose/state'
|
|
5
|
+
import { $prose } from '@jvs-milkdown/utils'
|
|
6
|
+
|
|
7
|
+
import { withMeta } from '../__internal__'
|
|
8
|
+
import { bulletListSchema } from '../node'
|
|
9
|
+
import { listItemSchema } from '../node/list-item'
|
|
10
|
+
import { orderedListSchema } from '../node/ordered-list'
|
|
11
|
+
|
|
12
|
+
/// This plugin is used to keep the label of list item up to date in ordered list.
|
|
13
|
+
export const syncListOrderPlugin = $prose((ctx) => {
|
|
14
|
+
const syncOrderLabel = (
|
|
15
|
+
transactions: readonly Transaction[],
|
|
16
|
+
_oldState: EditorState,
|
|
17
|
+
newState: EditorState
|
|
18
|
+
) => {
|
|
19
|
+
// Skip if composing or not editable
|
|
20
|
+
if (
|
|
21
|
+
!newState.selection ||
|
|
22
|
+
transactions.some(
|
|
23
|
+
(tr) => tr.getMeta('addToHistory') === false || !tr.isGeneric
|
|
24
|
+
)
|
|
25
|
+
)
|
|
26
|
+
return null
|
|
27
|
+
|
|
28
|
+
const orderedListType = orderedListSchema.type(ctx)
|
|
29
|
+
const bulletListType = bulletListSchema.type(ctx)
|
|
30
|
+
const listItemType = listItemSchema.type(ctx)
|
|
31
|
+
|
|
32
|
+
const handleNodeItem = (
|
|
33
|
+
attrs: Record<string, any>,
|
|
34
|
+
index: number,
|
|
35
|
+
order: number = 1
|
|
36
|
+
): boolean => {
|
|
37
|
+
let changed = false
|
|
38
|
+
const expectedLabel = `${index + order}.`
|
|
39
|
+
if (attrs.label !== expectedLabel) {
|
|
40
|
+
attrs.label = expectedLabel
|
|
41
|
+
changed = true
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return changed
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let tr = newState.tr
|
|
48
|
+
let needDispatch = false
|
|
49
|
+
|
|
50
|
+
newState.doc.descendants(
|
|
51
|
+
(node: Node, pos: number, parent: Node | null, index: number) => {
|
|
52
|
+
if (node.type === bulletListType) {
|
|
53
|
+
const base = node.maybeChild(0)
|
|
54
|
+
if (
|
|
55
|
+
base?.type === listItemType &&
|
|
56
|
+
base.attrs.listType === 'ordered'
|
|
57
|
+
) {
|
|
58
|
+
needDispatch = true
|
|
59
|
+
tr.setNodeMarkup(pos, orderedListType, { spread: 'true' })
|
|
60
|
+
|
|
61
|
+
node.descendants(
|
|
62
|
+
(
|
|
63
|
+
child: Node,
|
|
64
|
+
pos: number,
|
|
65
|
+
_parent: Node | null,
|
|
66
|
+
index: number
|
|
67
|
+
) => {
|
|
68
|
+
if (child.type === listItemType) {
|
|
69
|
+
const attrs = { ...child.attrs }
|
|
70
|
+
const changed = handleNodeItem(attrs, index)
|
|
71
|
+
if (changed) tr = tr.setNodeMarkup(pos, undefined, attrs)
|
|
72
|
+
}
|
|
73
|
+
return false
|
|
74
|
+
}
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
} else if (
|
|
78
|
+
node.type === listItemType &&
|
|
79
|
+
parent?.type === orderedListType
|
|
80
|
+
) {
|
|
81
|
+
const attrs = { ...node.attrs }
|
|
82
|
+
let changed = false
|
|
83
|
+
if (attrs.listType !== 'ordered') {
|
|
84
|
+
attrs.listType = 'ordered'
|
|
85
|
+
changed = true
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const base = parent?.maybeChild(0)
|
|
89
|
+
if (base)
|
|
90
|
+
changed = handleNodeItem(attrs, index, parent?.attrs.order ?? 1)
|
|
91
|
+
|
|
92
|
+
if (changed) {
|
|
93
|
+
tr = tr.setNodeMarkup(pos, undefined, attrs)
|
|
94
|
+
needDispatch = true
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
return needDispatch ? tr.setMeta('addToHistory', false) : null
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return new Plugin({
|
|
104
|
+
key: new PluginKey('MILKDOWN_KEEP_LIST_ORDER'),
|
|
105
|
+
appendTransaction: syncOrderLabel,
|
|
106
|
+
})
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
withMeta(syncListOrderPlugin, {
|
|
110
|
+
displayName: 'Prose<syncListOrderPlugin>',
|
|
111
|
+
group: 'Prose',
|
|
112
|
+
})
|