Skip to content

Commit 5353514

Browse files
committed
fix: πŸ› preserve empty line exception and behavior
βœ… Closes: #1811
1 parent 530ed51 commit 5353514

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

Diff for: β€Ž.prettierignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pnpm-lock.yaml
22
**/storybook-static
33
**/lib
4-
**/.storybook
4+
**/.storybook
5+
**/tests/data/*.md

Diff for: β€Že2e/tests/data/preserve-empty-line.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
#
2+
13
123
24

35
<br />
46

57
456
68

7-
<br />
9+
* <br />
810

9-
789
11+
> <br />

Diff for: β€Že2e/tests/transform/preserve-empty-line.spec.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,23 @@ test('preserve empty line', async ({ page }) => {
1616
expect(await page.locator('p').nth(1).textContent()).toBe('')
1717
expect(await page.locator('p').nth(2).textContent()).toBe('456')
1818
expect(await page.locator('p').nth(3).textContent()).toBe('')
19-
expect(await page.locator('p').nth(4).textContent()).toBe('789')
19+
expect(await page.locator('li').nth(0).textContent()).toBe('')
20+
expect(await page.locator('blockquote').nth(0).textContent()).toBe('')
2021

2122
const markdownOutput = await getMarkdown(page)
2223
expect(markdownOutput.trim()).toBe(markdown.trim())
2324
})
25+
26+
test('should not preserve last empty line', async ({ page }) => {
27+
await focusEditor(page)
28+
29+
await page.keyboard.press('Enter')
30+
await page.keyboard.press('Backspace')
31+
32+
let markdownOutput = await getMarkdown(page)
33+
expect(markdownOutput.trim()).toBe('')
34+
35+
await page.keyboard.press('Enter')
36+
markdownOutput = await getMarkdown(page)
37+
expect(markdownOutput.trim()).toBe('<br />')
38+
})

Diff for: β€Žpackages/plugins/preset-commonmark/src/node/paragraph.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Ctx } from '@milkdown/ctx'
22

3-
import { commandsCtx } from '@milkdown/core'
3+
import { commandsCtx, editorViewCtx } from '@milkdown/core'
44
import { setBlockType } from '@milkdown/prose/commands'
55
import { $command, $nodeAttr, $nodeSchema, $useKeymap } from '@milkdown/utils'
66

@@ -34,9 +34,13 @@ export const paragraphSchema = $nodeSchema('paragraph', (ctx) => ({
3434
toMarkdown: {
3535
match: (node) => node.type.name === 'paragraph',
3636
runner: (state, node) => {
37+
const view = ctx.get(editorViewCtx)
38+
const lastNode = view.state?.doc.lastChild
39+
3740
state.openNode('paragraph')
3841
if (
3942
(!node.content || node.content.size === 0) &&
43+
node !== lastNode &&
4044
shouldPreserveEmptyLine(ctx)
4145
) {
4246
state.addNode('html', undefined, '<br />')

Diff for: β€Žpackages/plugins/preset-commonmark/src/plugin/remark-preserve-empty-line.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { visit } from 'unist-util-visit'
55

66
import { withMeta } from '../__internal__'
77

8-
function visitImage(ast: Node) {
9-
return visit(ast, 'paragraph', (node: Node & { children?: Node[] }) => {
8+
function visitEmptyLine(ast: Node) {
9+
return visit(ast, (node: Node & { children?: Node[] }) => {
1010
if (node.children?.length !== 1) return
1111
const firstChild = node.children?.[0]
1212
if (!firstChild || firstChild.type !== 'html') return
@@ -30,7 +30,7 @@ function visitImage(ast: Node) {
3030
/// This plugin should be used with `linebreakSchema` to work.
3131
export const remarkPreserveEmptyLinePlugin = $remark(
3232
'remark-preserve-empty-line',
33-
() => () => visitImage
33+
() => () => visitEmptyLine
3434
)
3535

3636
withMeta(remarkPreserveEmptyLinePlugin.plugin, {

0 commit comments

Comments
Β (0)