Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown paragraph handling is broken #2205

Open
SebastianStehle opened this issue Nov 10, 2023 · 1 comment
Open

Markdown paragraph handling is broken #2205

SebastianStehle opened this issue Nov 10, 2023 · 1 comment
Labels
type: bug 🪲 Something isn't working

Comments

@SebastianStehle
Copy link
Sponsor

Summary

I think the markdown paragraph handling is broken right now. Because markdown and html in remirror handle paragraphs differently.

Steps to reproduce

Lets consider a very simple markdown example:

a

b
c

In the editor this is represented as

<p>a</p>
<p></p>
<p>b</p>
<p>c</p>

But in markdown these are 3 paragraphs, so after markdown to html conversion, this would be:

<p>a</p>
<p>b**\n**
c</p>

Therefore both conversions are actually wrong in this context.

Expected results

The markdown output should be the same as the markdown input.

Actual results

Possible Solution

I have fixed it with a custom conversion:

const turnDownService = new TurndownService();

turnDownService.addRule('paragraph2', {
    filter: 'p',
    replacement: content => {
        return `${content}\n`;
    }
});

marked.use({
    renderer: {
        paragraph: (text: string) => {
            const parts = text.split('\n');

            return parts.map(p => `<p>${p}</p>\n`).join('') + EMPTY_LINE;
        }
    }
});

const EMPTY_LINE = '<p></p>\n';

export function htmlToMarkdown(html: string) {
    return turnDownService.turndown(html);
}

export function markdownToHtml(markdown: string): string {
    let html = marked(markdown, { gfm: true });

    if (html.endsWith(EMPTY_LINE)) {
        html = html.substring(0, html.length - EMPTY_LINE.length);
    }

    return html;
}

Screenshot(s)

@SebastianStehle SebastianStehle added the type: bug 🪲 Something isn't working label Nov 10, 2023
@SebastianStehle
Copy link
Sponsor Author

Sorry, I have to revert my opinion here. I think the main problem here, is that paragraphs don't have a padding. Therefore my users did not understood that there is actually a new paragraph.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug 🪲 Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant