Skip to content

Commit

Permalink
fix: remove marks from breaks that are first node inside that mark (#378
Browse files Browse the repository at this point in the history
)
  • Loading branch information
d3m1d0v authored Sep 24, 2024
1 parent b6b61f8 commit 6b148fd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/core/markdown/Markdown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ describe('markdown', () => {

it('drops trailing hard breaks', () => serialize(doc(p('a', br(), br())), 'a'));

it('should remove marks from edge break', () =>
it('should remove marks from edge break (before)', () =>
serialize(doc(p('text', strong(br(), 'text2'))), 'text\\\n**text2**'));

it('should remove marks from edge break (after)', () =>
serialize(doc(p(strong('text', br()), 'text2')), '**text**\\\ntext2'));

it('expels enclosing whitespace from inside emphasis', () =>
Expand Down
11 changes: 8 additions & 3 deletions src/core/markdown/MarkdownSerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,19 @@ export class MarkdownSerializerState {
const progress = (node, _, index) => {
let marks = node ? node.marks : [];

// Remove marks from breaks (hard_break or soft_break) that are the last node inside
// Remove marks from breaks (hard_break or soft_break) that are the edge node inside
// that mark to prevent parser edge cases with new lines just
// before closing marks.
// before closing or after opening marks.
if (node && node.type.spec.isBreak) {
marks = marks.filter(m => {
if (index === 0) return false;
if (index + 1 == parent.childCount) return false;
const prev = parent.child(index - 1);
const next = parent.child(index + 1);
return m.isInSet(next.marks) && (!next.isText || /\S/.test(next.text));
return (
(m.isInSet(prev.marks) && (!prev.isText || /\S/.test(prev.text))) &&
(m.isInSet(next.marks) && (!next.isText || /\S/.test(next.text)))
);
});
}

Expand Down
12 changes: 12 additions & 0 deletions src/extensions/markdown/Breaks/Breaks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,24 @@ describe('Breaks extension', () => {
'Lorem **ipsum**\\\ndolor sit amet',
));

it('should serialize hard break 2', () =>
serialize(
doc(p('Lorem', bold(hb(), 'ipsum'), ' dolor sit amet')),
'Lorem\\\n**ipsum** dolor sit amet',
));

it('should serialize soft break', () =>
serialize(
doc(p('Lorem ', bold('ipsum', sb()), 'dolor sit amet')),
'Lorem **ipsum**\ndolor sit amet',
));

it('should serialize soft break 2', () =>
serialize(
doc(p('Lorem', bold(sb(), 'ipsum'), ' dolor sit amet')),
'Lorem\n**ipsum** dolor sit amet',
));

it('should parse html - br tag', () => {
parseDOM(schema, '<div>hello<br>world!</div>', doc(p('hello', hb(), 'world!')));
});
Expand Down

0 comments on commit 6b148fd

Please sign in to comment.