@milkdown/preset-commonmark 7.6.3 → 7.7.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":"paragraph.d.ts","sourceRoot":"","sources":["../../src/node/paragraph.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,aAAa,qCAAyB,CAAA;AAQnD,eAAO,MAAM,eAAe,oDAuBzB,CAAA;AAYH,eAAO,MAAM,mBAAmB,6CAG/B,CAAA;AASD,eAAO,MAAM,eAAe,0EAQ1B,CAAA"}
1
+ {"version":3,"file":"paragraph.d.ts","sourceRoot":"","sources":["../../src/node/paragraph.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,aAAa,qCAAyB,CAAA;AAQnD,eAAO,MAAM,eAAe,oDA8BzB,CAAA;AAuBH,eAAO,MAAM,mBAAmB,6CAG/B,CAAA;AASD,eAAO,MAAM,eAAe,0EAQ1B,CAAA"}
@@ -3,6 +3,7 @@ export * from './remark-line-break';
3
3
  export * from './remark-inline-link-plugin';
4
4
  export * from './remark-html-transformer';
5
5
  export * from './remark-marker-plugin';
6
+ export * from './remark-preserve-empty-line';
6
7
  export * from './inline-nodes-cursor-plugin';
7
8
  export * from './hardbreak-clear-mark-plugin';
8
9
  export * from './hardbreak-filter-plugin';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/plugin/index.ts"],"names":[],"mappings":"AAAA,cAAc,mCAAmC,CAAA;AACjD,cAAc,qBAAqB,CAAA;AACnC,cAAc,6BAA6B,CAAA;AAC3C,cAAc,2BAA2B,CAAA;AACzC,cAAc,wBAAwB,CAAA;AAEtC,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":"AAAA,cAAc,mCAAmC,CAAA;AACjD,cAAc,qBAAqB,CAAA;AACnC,cAAc,6BAA6B,CAAA;AAC3C,cAAc,2BAA2B,CAAA;AACzC,cAAc,wBAAwB,CAAA;AACtC,cAAc,8BAA8B,CAAA;AAE5C,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 remarkPreserveEmptyLinePlugin: import("@milkdown/utils").$Remark<"remark-preserve-empty-line", unknown>;
2
+ //# sourceMappingURL=remark-preserve-empty-line.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"remark-preserve-empty-line.d.ts","sourceRoot":"","sources":["../../src/plugin/remark-preserve-empty-line.ts"],"names":[],"mappings":"AA4BA,eAAO,MAAM,6BAA6B,0EAGzC,CAAA"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@milkdown/preset-commonmark",
3
3
  "type": "module",
4
- "version": "7.6.3",
4
+ "version": "7.7.0",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -36,14 +36,14 @@
36
36
  "remark-inline-links": "^7.0.0",
37
37
  "tslib": "^2.8.1",
38
38
  "unist-util-visit": "^5.0.0",
39
- "@milkdown/exception": "7.6.3",
40
- "@milkdown/utils": "7.6.3"
39
+ "@milkdown/utils": "7.7.0",
40
+ "@milkdown/exception": "7.7.0"
41
41
  },
42
42
  "devDependencies": {
43
- "@milkdown/core": "7.6.3",
44
- "@milkdown/ctx": "7.6.3",
45
- "@milkdown/transformer": "7.6.3",
46
- "@milkdown/prose": "7.6.3"
43
+ "@milkdown/ctx": "7.7.0",
44
+ "@milkdown/core": "7.7.0",
45
+ "@milkdown/prose": "7.7.0",
46
+ "@milkdown/transformer": "7.7.0"
47
47
  },
48
48
  "nx": {
49
49
  "targets": {
@@ -9,6 +9,7 @@ import {
9
9
  remarkInlineLinkPlugin,
10
10
  remarkLineBreak,
11
11
  remarkMarker,
12
+ remarkPreserveEmptyLinePlugin,
12
13
  syncHeadingIdPlugin,
13
14
  syncListOrderPlugin,
14
15
  } from '../plugin'
@@ -26,6 +27,7 @@ export const plugins: MilkdownPlugin[] = [
26
27
  remarkLineBreak,
27
28
  remarkHtmlTransformer,
28
29
  remarkMarker,
30
+ remarkPreserveEmptyLinePlugin,
29
31
 
30
32
  syncHeadingIdPlugin,
31
33
  syncListOrderPlugin,
@@ -21,7 +21,7 @@ withMeta(listItemAttr, {
21
21
  /// Schema for list item node.
22
22
  export const listItemSchema = $nodeSchema('list_item', (ctx) => ({
23
23
  group: 'listItem',
24
- content: '(paragraph|blockquote) block*',
24
+ content: 'paragraph block*',
25
25
  attrs: {
26
26
  label: {
27
27
  default: '•',
@@ -2,6 +2,8 @@ import { commandsCtx } from '@milkdown/core'
2
2
  import { setBlockType } from '@milkdown/prose/commands'
3
3
  import { $command, $nodeAttr, $nodeSchema, $useKeymap } from '@milkdown/utils'
4
4
  import { serializeText, withMeta } from '../__internal__'
5
+ import { remarkPreserveEmptyLinePlugin } from '../plugin/remark-preserve-empty-line'
6
+ import type { Ctx } from '@milkdown/ctx'
5
7
 
6
8
  /// HTML attributes for paragraph node.
7
9
  export const paragraphAttr = $nodeAttr('paragraph')
@@ -31,12 +33,30 @@ export const paragraphSchema = $nodeSchema('paragraph', (ctx) => ({
31
33
  match: (node) => node.type.name === 'paragraph',
32
34
  runner: (state, node) => {
33
35
  state.openNode('paragraph')
34
- serializeText(state, node)
36
+ if (
37
+ (!node.content || node.content.size === 0) &&
38
+ shouldPreserveEmptyLine(ctx)
39
+ ) {
40
+ state.addNode('html', undefined, '<br />')
41
+ } else {
42
+ serializeText(state, node)
43
+ }
35
44
  state.closeNode()
36
45
  },
37
46
  },
38
47
  }))
39
48
 
49
+ function shouldPreserveEmptyLine(ctx: Ctx) {
50
+ let shouldPreserveEmptyLine = false
51
+ try {
52
+ ctx.get(remarkPreserveEmptyLinePlugin.id)
53
+ shouldPreserveEmptyLine = true
54
+ } catch {
55
+ shouldPreserveEmptyLine = false
56
+ }
57
+ return shouldPreserveEmptyLine
58
+ }
59
+
40
60
  withMeta(paragraphSchema.node, {
41
61
  displayName: 'NodeSchema<paragraph>',
42
62
  group: 'Paragraph',
@@ -3,6 +3,7 @@ export * from './remark-line-break'
3
3
  export * from './remark-inline-link-plugin'
4
4
  export * from './remark-html-transformer'
5
5
  export * from './remark-marker-plugin'
6
+ export * from './remark-preserve-empty-line'
6
7
 
7
8
  export * from './inline-nodes-cursor-plugin'
8
9
 
@@ -0,0 +1,42 @@
1
+ import type { Node } from '@milkdown/transformer'
2
+ import { visit } from 'unist-util-visit'
3
+ import { $remark } from '@milkdown/utils'
4
+ import { withMeta } from '../__internal__'
5
+
6
+ function visitImage(ast: Node) {
7
+ return visit(ast, 'paragraph', (node: Node & { children?: Node[] }) => {
8
+ if (node.children?.length !== 1) return
9
+ const firstChild = node.children?.[0]
10
+ if (!firstChild || firstChild.type !== 'html') return
11
+
12
+ const { value } = firstChild as Node & {
13
+ value: string
14
+ }
15
+
16
+ if (!['<br />', '<br>', '<br/>'].includes(value)) {
17
+ return
18
+ }
19
+
20
+ node.children.splice(0, 1)
21
+ })
22
+ }
23
+
24
+ /// @internal
25
+ /// This plugin is used to preserve the empty line.
26
+ /// Markdown will fold the empty line into the previous line by default.
27
+ /// This plugin will preserve the empty line by converting `<br />` to `line-break`.
28
+ /// This plugin should be used with `linebreakSchema` to work.
29
+ export const remarkPreserveEmptyLinePlugin = $remark(
30
+ 'remark-preserve-empty-line',
31
+ () => () => visitImage
32
+ )
33
+
34
+ withMeta(remarkPreserveEmptyLinePlugin.plugin, {
35
+ displayName: 'Remark<remarkPreserveEmptyLine>',
36
+ group: 'Remark',
37
+ })
38
+
39
+ withMeta(remarkPreserveEmptyLinePlugin.options, {
40
+ displayName: 'RemarkConfig<remarkPreserveEmptyLine>',
41
+ group: 'Remark',
42
+ })