@milkdown/preset-commonmark 7.0.0 → 7.1.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.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/node/index.ts"],"names":[],"mappings":"AAEA,cAAc,OAAO,CAAA;AACrB,cAAc,WAAW,CAAA;AACzB,cAAc,cAAc,CAAA;AAC5B,cAAc,cAAc,CAAA;AAC5B,cAAc,SAAS,CAAA;AACvB,cAAc,aAAa,CAAA;AAC3B,cAAc,MAAM,CAAA;AACpB,cAAc,eAAe,CAAA;AAC7B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,QAAQ,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/node/index.ts"],"names":[],"mappings":"AAEA,cAAc,OAAO,CAAA;AACrB,cAAc,WAAW,CAAA;AACzB,cAAc,cAAc,CAAA;AAC5B,cAAc,cAAc,CAAA;AAC5B,cAAc,SAAS,CAAA;AACvB,cAAc,aAAa,CAAA;AAC3B,cAAc,MAAM,CAAA;AACpB,cAAc,eAAe,CAAA;AAC7B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,QAAQ,CAAA;AAEtB,cAAc,QAAQ,CAAA"}
@@ -2,7 +2,7 @@ export * from './inline-sync-plugin';
2
2
  export * from './remark-add-order-in-list-plugin';
3
3
  export * from './remark-line-break';
4
4
  export * from './remark-inline-link-plugin';
5
- export * from './remark-html-filter';
5
+ export * from './remark-html-transformer';
6
6
  export * from './inline-nodes-cursor-plugin';
7
7
  export * from './hardbreak-clear-mark-plugin';
8
8
  export * from './hardbreak-filter-plugin';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/plugin/index.ts"],"names":[],"mappings":"AAEA,cAAc,sBAAsB,CAAA;AAEpC,cAAc,mCAAmC,CAAA;AACjD,cAAc,qBAAqB,CAAA;AACnC,cAAc,6BAA6B,CAAA;AAC3C,cAAc,sBAAsB,CAAA;AAEpC,cAAc,8BAA8B,CAAA;AAE5C,cAAc,+BAA+B,CAAA;AAC7C,cAAc,2BAA2B,CAAA;AAEzC,cAAc,0BAA0B,CAAA;AACxC,cAAc,0BAA0B,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/plugin/index.ts"],"names":[],"mappings":"AAEA,cAAc,sBAAsB,CAAA;AAEpC,cAAc,mCAAmC,CAAA;AACjD,cAAc,qBAAqB,CAAA;AACnC,cAAc,6BAA6B,CAAA;AAC3C,cAAc,2BAA2B,CAAA;AAEzC,cAAc,8BAA8B,CAAA;AAE5C,cAAc,+BAA+B,CAAA;AAC7C,cAAc,2BAA2B,CAAA;AAEzC,cAAc,0BAA0B,CAAA;AACxC,cAAc,0BAA0B,CAAA"}
@@ -0,0 +1,2 @@
1
+ export declare const remarkHtmlTransformer: import("@milkdown/utils").$Remark;
2
+ //# sourceMappingURL=remark-html-transformer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"remark-html-transformer.d.ts","sourceRoot":"","sources":["../../src/plugin/remark-html-transformer.ts"],"names":[],"mappings":"AAmCA,eAAO,MAAM,qBAAqB,mCAahC,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"sync-list-order-plugin.d.ts","sourceRoot":"","sources":["../../src/plugin/sync-list-order-plugin.ts"],"names":[],"mappings":"AASA,eAAO,MAAM,mBAAmB,kCA+C9B,CAAA"}
1
+ {"version":3,"file":"sync-list-order-plugin.d.ts","sourceRoot":"","sources":["../../src/plugin/sync-list-order-plugin.ts"],"names":[],"mappings":"AAUA,eAAO,MAAM,mBAAmB,kCAyE9B,CAAA"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@milkdown/preset-commonmark",
3
3
  "type": "module",
4
- "version": "7.0.0",
4
+ "version": "7.1.0",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -30,14 +30,14 @@
30
30
  "remark-inline-links": "^6.0.0",
31
31
  "tslib": "^2.4.0",
32
32
  "unist-util-visit": "^4.0.0",
33
- "@milkdown/exception": "7.0.0",
34
- "@milkdown/utils": "7.0.0"
33
+ "@milkdown/exception": "7.1.0",
34
+ "@milkdown/utils": "7.1.0"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/unist": "^2.0.6",
38
- "@milkdown/core": "7.0.0",
39
- "@milkdown/ctx": "7.0.0",
40
- "@milkdown/prose": "7.0.0"
38
+ "@milkdown/core": "7.1.0",
39
+ "@milkdown/ctx": "7.1.0",
40
+ "@milkdown/prose": "7.1.0"
41
41
  },
42
42
  "nx": {
43
43
  "targets": {
@@ -1,7 +1,20 @@
1
1
  /* Copyright 2021, Milkdown by Mirone. */
2
2
 
3
3
  import type { MilkdownPlugin } from '@milkdown/ctx'
4
- import { hardbreakClearMarkPlugin, hardbreakFilterNodes, hardbreakFilterPlugin, inlineNodesCursorPlugin, inlineSyncConfig, inlineSyncPlugin, remarkAddOrderInListPlugin, remarkHTMLFilter, remarkInlineLinkPlugin, remarkLineBreak, syncHeadingIdPlugin, syncListOrderPlugin } from '../plugin'
4
+ import {
5
+ hardbreakClearMarkPlugin,
6
+ hardbreakFilterNodes,
7
+ hardbreakFilterPlugin,
8
+ inlineNodesCursorPlugin,
9
+ inlineSyncConfig,
10
+ inlineSyncPlugin,
11
+ remarkAddOrderInListPlugin,
12
+ remarkHtmlTransformer,
13
+ remarkInlineLinkPlugin,
14
+ remarkLineBreak,
15
+ syncHeadingIdPlugin,
16
+ syncListOrderPlugin,
17
+ } from '../plugin'
5
18
 
6
19
  /// @internal
7
20
  export const plugins: MilkdownPlugin[] = [
@@ -17,7 +30,7 @@ export const plugins: MilkdownPlugin[] = [
17
30
  remarkAddOrderInListPlugin,
18
31
  remarkInlineLinkPlugin,
19
32
  remarkLineBreak,
20
- remarkHTMLFilter,
33
+ remarkHtmlTransformer,
21
34
 
22
35
  syncHeadingIdPlugin,
23
36
  syncListOrderPlugin,
@@ -2,7 +2,32 @@
2
2
 
3
3
  import type { MilkdownPlugin } from '@milkdown/ctx'
4
4
  import { emphasisAttr, emphasisSchema, inlineCodeAttr, inlineCodeSchema, linkAttr, linkSchema, strongAttr, strongSchema } from '../mark'
5
- import { blockquoteAttr, blockquoteSchema, bulletListAttr, bulletListSchema, codeBlockAttr, codeBlockSchema, docSchema, hardbreakAttr, hardbreakSchema, headingAttr, headingIdGenerator, headingSchema, hrAttr, hrSchema, imageAttr, imageSchema, listItemAttr, listItemSchema, orderedListAttr, orderedListSchema, paragraphAttr, paragraphSchema, textSchema } from '../node'
5
+ import {
6
+ blockquoteAttr,
7
+ blockquoteSchema,
8
+ bulletListAttr,
9
+ bulletListSchema,
10
+ codeBlockAttr,
11
+ codeBlockSchema,
12
+ docSchema,
13
+ hardbreakAttr,
14
+ hardbreakSchema,
15
+ headingAttr,
16
+ headingIdGenerator,
17
+ headingSchema,
18
+ hrAttr,
19
+ hrSchema, htmlAttr,
20
+ htmlSchema,
21
+ imageAttr,
22
+ imageSchema,
23
+ listItemAttr,
24
+ listItemSchema,
25
+ orderedListAttr,
26
+ orderedListSchema,
27
+ paragraphAttr,
28
+ paragraphSchema,
29
+ textSchema,
30
+ } from '../node'
6
31
 
7
32
  /// @internal
8
33
  export const schema: MilkdownPlugin[] = [
@@ -51,5 +76,8 @@ export const schema: MilkdownPlugin[] = [
51
76
  linkAttr,
52
77
  linkSchema,
53
78
 
79
+ htmlAttr,
80
+ htmlSchema,
81
+
54
82
  textSchema,
55
83
  ].flat()
@@ -0,0 +1,47 @@
1
+ /* Copyright 2021, Milkdown by Mirone. */
2
+ import { $nodeAttr, $nodeSchema } from '@milkdown/utils'
3
+
4
+ export const htmlAttr = $nodeAttr('html')
5
+
6
+ export const htmlSchema = $nodeSchema('html', (ctx) => {
7
+ return {
8
+ atom: true,
9
+ group: 'inline',
10
+ inline: true,
11
+ attrs: {
12
+ value: {
13
+ default: '',
14
+ },
15
+ },
16
+ toDOM: (node) => {
17
+ const span = document.createElement('span')
18
+ const attr = {
19
+ ...ctx.get(htmlAttr.key)(node),
20
+ 'data-value': node.attrs.value,
21
+ 'data-type': 'html',
22
+ }
23
+ span.textContent = node.attrs.value
24
+ return ['span', attr, node.attrs.value]
25
+ },
26
+ parseDOM: [{
27
+ tag: 'span[data-type="html"]',
28
+ getAttrs: (dom) => {
29
+ return {
30
+ value: (dom as HTMLElement).dataset.value ?? '',
31
+ }
32
+ },
33
+ }],
34
+ parseMarkdown: {
35
+ match: ({ type }) => Boolean(type === 'html'),
36
+ runner: (state, node, type) => {
37
+ state.addNode(type, { value: node.value as string })
38
+ },
39
+ },
40
+ toMarkdown: {
41
+ match: node => node.type.name === 'html',
42
+ runner: (state, node) => {
43
+ state.addNode('html', undefined, node.attrs.value)
44
+ },
45
+ },
46
+ }
47
+ })
package/src/node/index.ts CHANGED
@@ -12,3 +12,5 @@ export * from './ordered-list'
12
12
  export * from './list-item'
13
13
  export * from './paragraph'
14
14
  export * from './text'
15
+
16
+ export * from './html'
@@ -5,7 +5,7 @@ export * from './inline-sync-plugin'
5
5
  export * from './remark-add-order-in-list-plugin'
6
6
  export * from './remark-line-break'
7
7
  export * from './remark-inline-link-plugin'
8
- export * from './remark-html-filter'
8
+ export * from './remark-html-transformer'
9
9
 
10
10
  export * from './inline-nodes-cursor-plugin'
11
11
 
@@ -16,11 +16,7 @@ export interface InlineSyncContext {
16
16
  }
17
17
 
18
18
  const getNodeFromSelection = (state: EditorState) => {
19
- const { selection } = state
20
- const { $from } = selection
21
- const node = $from.node()
22
-
23
- return node
19
+ return state.selection.$from.node()
24
20
  }
25
21
 
26
22
  const getMarkdown = (ctx: Ctx, state: EditorState, node: Node, globalNode: Node[]) => {
@@ -80,6 +76,10 @@ const collectGlobalNodes = (ctx: Ctx, state: EditorState) => {
80
76
 
81
77
  const removeGlobalFromText = (text: string) => text.split('\n\n')[0] || ''
82
78
 
79
+ const onlyHTML = (node: Node) => {
80
+ return node.childCount === 1 && node.child(0).type.name === 'html'
81
+ }
82
+
83
83
  export const getContextByState = (ctx: Ctx, state: EditorState): InlineSyncContext | null => {
84
84
  try {
85
85
  const globalNode = collectGlobalNodes(ctx, state)
@@ -90,7 +90,7 @@ export const getContextByState = (ctx: Ctx, state: EditorState): InlineSyncConte
90
90
 
91
91
  const newNode = getNewNode(ctx, text)
92
92
 
93
- if (!newNode || node.type !== newNode.type)
93
+ if (!newNode || node.type !== newNode.type || onlyHTML(newNode))
94
94
  return null
95
95
 
96
96
  // @ts-expect-error hijack the node attribute
@@ -35,7 +35,7 @@ export const runReplacer = (
35
35
  tr = tr
36
36
  .replaceWith(from, to, context.nextNode)
37
37
  .setNodeMarkup(from, undefined, attrs)
38
- // delete the placeholder
38
+ // delete the placeholder
39
39
  .delete(offset + 1, offset + 2)
40
40
 
41
41
  // restore the selection
@@ -33,11 +33,17 @@ function flatMapWithDepth(ast: Node, fn: (node: Node, index: number, parent: Nod
33
33
 
34
34
  /// @internal
35
35
  /// This plugin should be deprecated after we support HTML.
36
- export const remarkHTMLFilter = $remark(() => () => (tree: Node) => {
37
- flatMapWithDepth(tree, (node) => {
36
+ export const remarkHtmlTransformer = $remark(() => () => (tree: Node) => {
37
+ flatMapWithDepth(tree, (node, _index, parent) => {
38
38
  if (!isHTML(node))
39
39
  return [node]
40
40
 
41
- return []
41
+ if (parent?.type === 'root') {
42
+ (node as Literal & { children: Literal[] }).children = [{ ...node }]
43
+ delete (node as Literal).value
44
+ node.type = 'paragraph'
45
+ }
46
+
47
+ return [node]
42
48
  })
43
49
  })
@@ -1,57 +1,84 @@
1
1
  /* Copyright 2021, Milkdown by Mirone. */
2
- import type { EditorState, Transaction } from '@milkdown/prose/state'
3
2
  import { Plugin, PluginKey } from '@milkdown/prose/state'
4
3
  import { $prose } from '@milkdown/utils'
4
+ import type { EditorView } from '@milkdown/prose/view'
5
5
  import { listItemSchema } from '../node/list-item'
6
6
 
7
7
  import { orderedListSchema } from '../node/ordered-list'
8
+ import { bulletListSchema } from '../node'
8
9
 
9
10
  /// This plugin is used to keep the label of list item up to date in ordered list.
10
11
  export const syncListOrderPlugin = $prose(() => {
11
- const walkThrough = (state: EditorState, callback: (tr: Transaction) => void) => {
12
+ const syncOrderLabel = (view: EditorView) => {
13
+ if (view.composing || !view.editable)
14
+ return
15
+
12
16
  const orderedListType = orderedListSchema.type()
17
+ const bulletListType = bulletListSchema.type()
13
18
  const listItemType = listItemSchema.type()
19
+ const state = view.state
20
+ const handleNodeItem = (attrs: Record<string, any>, index: number): boolean => {
21
+ let changed = false
22
+ const expectedLabel = `${index + 1}.`
23
+ if (attrs.label !== expectedLabel) {
24
+ attrs.label = expectedLabel
25
+ changed = true
26
+ }
27
+
28
+ return changed
29
+ }
30
+
14
31
  let tr = state.tr
32
+ let needDispatch = false
15
33
  state.doc.descendants((node, pos, parent, index) => {
16
- if (node.type === listItemType && parent?.type === orderedListType) {
17
- let changed = false
34
+ if (node.type === bulletListType) {
35
+ const base = node.maybeChild(0)
36
+ if (base?.type === listItemType && base.attrs.listType === 'ordered') {
37
+ needDispatch = true
38
+ tr.setNodeMarkup(pos, orderedListType, { spread: 'true' })
39
+
40
+ node.descendants((child, pos, _parent, index) => {
41
+ if (child.type === listItemType) {
42
+ const attrs = { ...child.attrs }
43
+ const changed = handleNodeItem(attrs, index)
44
+ if (changed)
45
+ tr = tr.setNodeMarkup(pos, undefined, attrs)
46
+ }
47
+ return false
48
+ })
49
+ }
50
+ }
51
+ else if (node.type === listItemType && parent?.type === orderedListType) {
18
52
  const attrs = { ...node.attrs }
19
- if (node.attrs.listType !== 'ordered') {
53
+ let changed = false
54
+ if (attrs.listType !== 'ordered') {
20
55
  attrs.listType = 'ordered'
21
56
  changed = true
22
57
  }
23
58
 
24
59
  const base = parent?.maybeChild(0)
25
- if (base === node) {
26
- attrs.label = '1.'
27
- changed = true
28
- }
29
- else if (base?.type === listItemType && base.attrs.listType === 'ordered') {
30
- attrs.label = `${index + 1}.`
31
- changed = true
32
- }
33
- else if (node.attrs.label === '•') {
34
- attrs.label = `${index + 1}.`
35
- changed = true
36
- }
60
+ if (base)
61
+ changed = handleNodeItem(attrs, index)
37
62
 
38
- if (changed)
63
+ if (changed) {
39
64
  tr = tr.setNodeMarkup(pos, undefined, attrs)
65
+ needDispatch = true
66
+ }
40
67
  }
41
68
  })
42
- callback(tr)
69
+
70
+ if (needDispatch)
71
+ view.dispatch(tr.setMeta('addToHistory', false))
43
72
  }
44
73
  return new Plugin({
45
74
  key: new PluginKey('MILKDOWN_KEEP_LIST_ORDER'),
46
- appendTransaction: (transactions, _oldState, nextState) => {
47
- let tr: Transaction | null = null
48
- if (transactions.some(transaction => transaction.docChanged)) {
49
- walkThrough(nextState, (t) => {
50
- tr = t
51
- })
75
+ view: (view) => {
76
+ syncOrderLabel(view)
77
+ return {
78
+ update: (view) => {
79
+ syncOrderLabel(view)
80
+ },
52
81
  }
53
-
54
- return tr
55
82
  },
56
83
  })
57
84
  })
@@ -1,2 +0,0 @@
1
- export declare const remarkHTMLFilter: import("@milkdown/utils").$Remark;
2
- //# sourceMappingURL=remark-html-filter.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"remark-html-filter.d.ts","sourceRoot":"","sources":["../../src/plugin/remark-html-filter.ts"],"names":[],"mappings":"AAmCA,eAAO,MAAM,gBAAgB,mCAO3B,CAAA"}