Skip to content

Commit

Permalink
Merge pull request #45 from aguingand/fix-br-extra-spaces
Browse files Browse the repository at this point in the history
Fix unwanted extra spaces
  • Loading branch information
aguingand authored Nov 1, 2023
2 parents 0fa8eec + 0d63304 commit 6fbf024
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
20 changes: 16 additions & 4 deletions __tests__/__snapshots__/parse.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,22 @@ exports[`parse > nodes > hard break > markdown 1`] = `
]
`;

exports[`parse > nodes > hard break > markdown with breaks option + inline 1`] = `
[
{
"text": "example1",
"type": "text",
},
{
"type": "hardBreak",
},
{
"text": "example2",
"type": "text",
},
]
`;

exports[`parse > nodes > hard break > markdown with breaks option 1`] = `
[
{
Expand Down Expand Up @@ -859,10 +875,6 @@ exports[`parse > nodes > paragraph > markdown 1`] = `
],
"type": "paragraph",
},
{
"text": " ",
"type": "text",
},
{
"content": [
{
Expand Down
3 changes: 3 additions & 0 deletions __tests__/parse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ describe('parse', () => {
test('markdown with breaks option', () => {
expect(parse('example1\nexample2', { breaks: true })).toMatchSnapshot();
})
test('markdown with breaks option + inline', () => {
expect(parse('example1\nexample2', { breaks: true, inline: true })).toMatchSnapshot();
})
test('html', () => {
expect(parse('example1<br>example2')).toMatchSnapshot();
});
Expand Down
5 changes: 4 additions & 1 deletion src/extensions/tiptap/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export const MarkdownClipboard = Extension.create({
}
const parsed = this.editor.storage.markdown.parser.parse(text, { inline: true });
return DOMParser.fromSchema(this.editor.schema)
.parseSlice(elementFromString(parsed), { preserveWhitespace: true });
.parseSlice(elementFromString(parsed), {
preserveWhitespace: true,
context,
});
},
/**
* @param {import('prosemirror-model').Slice} slice
Expand Down
13 changes: 8 additions & 5 deletions src/parse/MarkdownParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@ export class MarkdownParser {
return content;
}

normalizeDOM(node, { inline, content } = {}) {
normalizeDOM(node, { inline, content }) {
this.normalizeBlocks(node);

// remove all \n appended by markdown-it
node.querySelectorAll('*').forEach(el => {
if(el.nextSibling?.nodeType === Node.TEXT_NODE && !el.closest('pre')) {
el.nextSibling.textContent = el.nextSibling.textContent.replace(/^\n/, '');
}
});

if(inline) {
this.normalizeInline(node, content);
}
Expand Down Expand Up @@ -85,10 +92,6 @@ export class MarkdownParser {
? content.match(/\s+$/)?.[0] ?? ''
: '';

if(nextSibling?.nodeType === Node.TEXT_NODE) {
nextSibling.textContent = nextSibling.textContent.replace(/^\n/, '');
}

if(content.match(/^\n\n/)) {
firstParagraph.innerHTML = `${firstParagraph.innerHTML}${endSpaces}`;
return;
Expand Down

0 comments on commit 6fbf024

Please sign in to comment.