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

two lines breaks after headlines #12

Open
DavidWiesner opened this issue Jan 26, 2016 · 4 comments
Open

two lines breaks after headlines #12

DavidWiesner opened this issue Jan 26, 2016 · 4 comments

Comments

@DavidWiesner
Copy link

Hi,

thanks for your project!
I found a bug when using micromarkdown.

When I use # to mark a headline the parser add to line breaks behind the headline.

Expected behaviour

Text

Hello
# new
world

rendered

Hello
<h1>new</h1>
world

Actual behaviour

rendered

Hello
<h1>new</h1>
<br>
<br>
world

Test

    var text='hello\n###new\nworld';
    var expected='hello<h3>new</h3>world';
    var result=micromarkdown.parse(text);
    // remove newlines in result (don't get rendered) 
    result=result.replace(/\n/g, '');
    if(result != expected){
        throw new Error('expected: ' + expected + " not equal to result: "+ result);
    }

Maybe this bug also affect other block elements too.

@mikegioia
Copy link
Contributor

@DavidWiesner This is because during header parsing, the following is used:

str = str.replace(stra[0], '<h' + count + '>' + stra[2] + '</h' + count + '>' + '\n');

Later down in the code, newlines are converted to <br> tags:

str = str.replace(/ {2,}[\n]{1,}/gmi, '<br/>');
str = str.replace(/[\n]{2,}/gmi, '<br/><br/>');

Since that newline is appended after the header, I'm assuming that its getting picked up in the
replacing. I haven't had a chance to test it yet, but the solution could just be to remove the "\n" injected after the headers. That's not needed since H1,H2.. are block level elements.

@mikegioia
Copy link
Contributor

@DavidWiesner I have a PR out that should resolve this. It's branch "test_fix". If you could pull down and test it I'd appreciate the help. I ran your example and it worked okay (FYI).

@DavidWiesner
Copy link
Author

@mikegioia ko i will check that. thx for your PR

@DavidWiesner
Copy link
Author

@mikegioia I still got the wrong result. The test written in the Issue description still failed. In general converting two newlines to lines breaks, is not the problem in general. Just newlines after or before a HTML-blockelement (like h1-h6) should't be add new <br>'s to the parsed html.
Here is the test as mocha-test

    it("headline with newlines", function (done) {
        var text='hello\n###new\nworld';
        var expected='hello<h3>new</h3>world';
        var result=micromarkdown.parse(text);
        // remove newlines in result (don't get rendered)
        result=result.replace(/\n/g, '');
        expect(result).to.equal(expected);
    });

regards,
David

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants