@milkdown/preset-commonmark 7.0.1 → 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.
@@ -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"}
@@ -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"}
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.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.1",
34
- "@milkdown/utils": "7.0.1"
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.1",
39
- "@milkdown/ctx": "7.0.1",
40
- "@milkdown/prose": "7.0.1"
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,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"}