@milkdown/preset-commonmark 7.0.1 → 7.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.
@@ -0,0 +1,3 @@
1
+ export declare const htmlAttr: import("@milkdown/utils").$NodeAttr;
2
+ export declare const htmlSchema: import("@milkdown/utils").$NodeSchema<"html">;
3
+ //# sourceMappingURL=html.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"html.d.ts","sourceRoot":"","sources":["../../src/node/html.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,QAAQ,qCAAoB,CAAA;AAEzC,eAAO,MAAM,UAAU,+CAyCrB,CAAA"}
@@ -10,4 +10,5 @@ export * from './ordered-list';
10
10
  export * from './list-item';
11
11
  export * from './paragraph';
12
12
  export * from './text';
13
+ export * from './html';
13
14
  //# sourceMappingURL=index.d.ts.map
@@ -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"}
@@ -1 +1 @@
1
- {"version":3,"file":"paragraph.d.ts","sourceRoot":"","sources":["../../src/node/paragraph.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,aAAa,qCAAyB,CAAA;AAGnD,eAAO,MAAM,eAAe,oDAuCzB,CAAA;AAGH,eAAO,MAAM,mBAAmB,6CAA6E,CAAA;AAI7G,eAAO,MAAM,eAAe,0EAQ1B,CAAA"}
1
+ {"version":3,"file":"paragraph.d.ts","sourceRoot":"","sources":["../../src/node/paragraph.ts"],"names":[],"mappings":"AAOA,eAAO,MAAM,aAAa,qCAAyB,CAAA;AAGnD,eAAO,MAAM,eAAe,oDA0BzB,CAAA;AAGH,eAAO,MAAM,mBAAmB,6CAA6E,CAAA;AAI7G,eAAO,MAAM,eAAe,0EAQ1B,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"}
@@ -1 +1 @@
1
- {"version":3,"file":"inline-sync-plugin.d.ts","sourceRoot":"","sources":["../../../src/plugin/inline-sync-plugin/inline-sync-plugin.ts"],"names":[],"mappings":"AAeA,eAAO,MAAM,gBAAgB,kCAkD3B,CAAA"}
1
+ {"version":3,"file":"inline-sync-plugin.d.ts","sourceRoot":"","sources":["../../../src/plugin/inline-sync-plugin/inline-sync-plugin.ts"],"names":[],"mappings":"AAeA,eAAO,MAAM,gBAAgB,kCAoD3B,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"}
@@ -0,0 +1,4 @@
1
+ import type { SerializerState } from '@milkdown/transformer';
2
+ import type { Node } from '@milkdown/prose/model';
3
+ export declare const serializeText: (state: SerializerState, node: Node) => void;
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AAE5D,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAA;AAGjD,eAAO,MAAM,aAAa,UAAW,eAAe,QAAQ,IAAI,SAe/D,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.1",
4
+ "version": "7.1.1",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -24,20 +24,23 @@
24
24
  "peerDependencies": {
25
25
  "@milkdown/core": "^7.0.0-next.0",
26
26
  "@milkdown/ctx": "^7.0.0-next.0",
27
- "@milkdown/prose": "^7.0.0-next.0"
27
+ "@milkdown/prose": "^7.0.0-next.0",
28
+ "@milkdown/transformer": "^7.0.0-next.0"
28
29
  },
29
30
  "dependencies": {
31
+ "@sindresorhus/slugify": "^2.2.0",
30
32
  "remark-inline-links": "^6.0.0",
31
- "tslib": "^2.4.0",
33
+ "tslib": "^2.5.0",
32
34
  "unist-util-visit": "^4.0.0",
33
- "@milkdown/exception": "7.0.1",
34
- "@milkdown/utils": "7.0.1"
35
+ "@milkdown/exception": "7.1.1",
36
+ "@milkdown/utils": "7.1.1"
35
37
  },
36
38
  "devDependencies": {
37
39
  "@types/unist": "^2.0.6",
38
- "@milkdown/core": "7.0.1",
39
- "@milkdown/ctx": "7.0.1",
40
- "@milkdown/prose": "7.0.1"
40
+ "@milkdown/core": "7.1.1",
41
+ "@milkdown/ctx": "7.1.1",
42
+ "@milkdown/prose": "7.1.1",
43
+ "@milkdown/transformer": "7.1.1"
41
44
  },
42
45
  "nx": {
43
46
  "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()
@@ -31,6 +31,7 @@ export const hardbreakSchema = $nodeSchema('hardbreak', ctx => ({
31
31
  state.addNode(type, { isInline: Boolean(node.data?.isInline) })
32
32
  },
33
33
  },
34
+ leafText: () => '\n',
34
35
  toMarkdown: {
35
36
  match: node => node.type.name === 'hardbreak',
36
37
  runner: (state, node) => {
@@ -4,8 +4,9 @@ import { expectDomTypeError } from '@milkdown/exception'
4
4
  import { setBlockType } from '@milkdown/prose/commands'
5
5
  import { textblockTypeInputRule } from '@milkdown/prose/inputrules'
6
6
  import type { Node } from '@milkdown/prose/model'
7
- import { Fragment } from '@milkdown/prose/model'
8
7
  import { $command, $ctx, $inputRule, $nodeAttr, $nodeSchema, $useKeymap } from '@milkdown/utils'
8
+ import slugify from '@sindresorhus/slugify'
9
+ import { serializeText } from '../utils'
9
10
  import { paragraphSchema } from './paragraph'
10
11
 
11
12
  const headingIndex = Array(6)
@@ -13,11 +14,7 @@ const headingIndex = Array(6)
13
14
  .map((_, i) => i + 1)
14
15
 
15
16
  const defaultHeadingIdGenerator = (node: Node) =>
16
- node.textContent
17
- .replace(/[\p{P}\p{S}]/gu, '')
18
- .replace(/\s/g, '-')
19
- .toLowerCase()
20
- .trim()
17
+ slugify(node.textContent)
21
18
 
22
19
  /// This is a slice contains a function to generate heading id.
23
20
  /// You can configure it to generate id in your own way.
@@ -73,20 +70,7 @@ export const headingSchema = $nodeSchema('heading', (ctx) => {
73
70
  match: node => node.type.name === 'heading',
74
71
  runner: (state, node) => {
75
72
  state.openNode('heading', undefined, { depth: node.attrs.level })
76
- const lastIsHardbreak = node.childCount >= 1 && node.lastChild?.type.name === 'hardbreak'
77
- if (lastIsHardbreak) {
78
- const contentArr: Node[] = []
79
- node.content.forEach((n, _, i) => {
80
- if (i === node.childCount - 1)
81
- return
82
-
83
- contentArr.push(n)
84
- })
85
- state.next(Fragment.fromArray(contentArr))
86
- }
87
- else {
88
- state.next(node.content)
89
- }
73
+ serializeText(state, node)
90
74
  state.closeNode()
91
75
  },
92
76
  },
@@ -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'
@@ -1,9 +1,8 @@
1
1
  /* Copyright 2021, Milkdown by Mirone. */
2
2
  import { commandsCtx } from '@milkdown/core'
3
3
  import { setBlockType } from '@milkdown/prose/commands'
4
- import type { Node } from '@milkdown/prose/model'
5
- import { Fragment } from '@milkdown/prose/model'
6
4
  import { $command, $nodeAttr, $nodeSchema, $useKeymap } from '@milkdown/utils'
5
+ import { serializeText } from '../utils'
7
6
 
8
7
  /// HTML attributes for paragraph node.
9
8
  export const paragraphAttr = $nodeAttr('paragraph')
@@ -31,20 +30,7 @@ export const paragraphSchema = $nodeSchema('paragraph', ctx => ({
31
30
  match: node => node.type.name === 'paragraph',
32
31
  runner: (state, node) => {
33
32
  state.openNode('paragraph')
34
- const lastIsHardbreak = node.childCount >= 1 && node.lastChild?.type.name === 'hardbreak'
35
- if (lastIsHardbreak) {
36
- const contentArr: Node[] = []
37
- node.content.forEach((n, _, i) => {
38
- if (i === node.childCount - 1)
39
- return
40
-
41
- contentArr.push(n)
42
- })
43
- state.next(Fragment.fromArray(contentArr))
44
- }
45
- else {
46
- state.next(node.content)
47
- }
33
+ serializeText(state, node)
48
34
  state.closeNode()
49
35
  },
50
36
  },
@@ -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
@@ -59,6 +59,8 @@ export const inlineSyncPlugin = $prose((ctx: Ctx) => {
59
59
  runReplacer(ctx, inlineSyncPluginKey, state, dispatch, prevNode.attrs)
60
60
  })
61
61
 
62
+ tr.setMeta('addToHistory', false)
63
+
62
64
  return null
63
65
  },
64
66
  },
@@ -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
  })
@@ -0,0 +1,22 @@
1
+ /* Copyright 2021, Milkdown by Mirone. */
2
+ import type { SerializerState } from '@milkdown/transformer'
3
+ import { Fragment } from '@milkdown/prose/model'
4
+ import type { Node } from '@milkdown/prose/model'
5
+ import { hardbreakSchema } from '../node'
6
+
7
+ export const serializeText = (state: SerializerState, node: Node) => {
8
+ const lastIsHardBreak = node.childCount >= 1 && node.lastChild?.type === hardbreakSchema.type()
9
+ if (!lastIsHardBreak) {
10
+ state.next(node.content)
11
+ return
12
+ }
13
+
14
+ const contentArr: Node[] = []
15
+ node.content.forEach((n, _, i) => {
16
+ if (i === node.childCount - 1)
17
+ return
18
+
19
+ contentArr.push(n)
20
+ })
21
+ state.next(Fragment.fromArray(contentArr))
22
+ }
@@ -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"}