Unabled to convert empty lines into the correct HTML element #591

Closed
opened 2026-01-29 14:40:30 +00:00 by claunia · 6 comments
Owner

Originally created by @tiagolima97 on GitHub (Feb 16, 2023).

Here's an image showing what the markdown text I'm trying to convert looks like:
image

Here's the value I have relative to the previous image:
TEST 1\n\nTEST 2\n\nTEST 3\n\n- TEST 4\n- TEST 5

Here's the output I'm getting from the Markdown.ToHtml method:
<p>TEST 1</p>\n<p>TEST 2</p>\n<p>TEST 3</p>\n<ul>\n<li>TEST 4</li>\n<li>TEST 5</li>\n</ul>\n

The output is returned with a bunch of \n that aren't converted successfully to HTML tags.

Here's the output I was expecting to get:
<p>TEST 1</p><br><p>TEST 2</p><br><p>TEST 3</p><br><ul><li>TEST 4</li><li>TEST 5</li></ul>

Originally created by @tiagolima97 on GitHub (Feb 16, 2023). Here's an image showing what the markdown text I'm trying to convert looks like: ![image](https://user-images.githubusercontent.com/125599062/219459185-180faa55-4c05-450e-8777-7c1585023235.png) Here's the value I have relative to the previous image: `TEST 1\n\nTEST 2\n\nTEST 3\n\n- TEST 4\n- TEST 5` Here's the output I'm getting from the `Markdown.ToHtml` method: `<p>TEST 1</p>\n<p>TEST 2</p>\n<p>TEST 3</p>\n<ul>\n<li>TEST 4</li>\n<li>TEST 5</li>\n</ul>\n` The output is returned with a bunch of `\n` that aren't converted successfully to HTML tags. Here's the output I was expecting to get: `<p>TEST 1</p><br><p>TEST 2</p><br><p>TEST 3</p><br><ul><li>TEST 4</li><li>TEST 5</li></ul>`
claunia added the question label 2026-01-29 14:40:30 +00:00
Author
Owner

@xoofx commented on GitHub (Feb 16, 2023):

The output is returned with a bunch of \n that aren't converted successfully to HTML tags.

It depends on the options you are using. The default is that it should be CommonMark compliant, and it means that you don't get <br> for multiple lines. There is an extension in Markdig to create hard break lines, but it's not outside of a paragraph as you expect (afair, <br> has to be inside a <p> as per the HTML specs)

@xoofx commented on GitHub (Feb 16, 2023): > The output is returned with a bunch of \n that aren't converted successfully to HTML tags. It depends on the options you are using. The default is that it should be CommonMark compliant, and it means that you don't get `<br>` for multiple lines. There is an extension in Markdig to [create hard break lines](https://github.com/xoofx/markdig/blob/master/src/Markdig.Tests/Specs/HardlineBreakSpecs.md), but it's not outside of a paragraph as you expect (afair, `<br>` has to be inside a `<p>` as per the HTML specs)
Author
Owner

@tiagolima97 commented on GitHub (Feb 17, 2023):

Thanks for the quick response @xoofx!

In regard to the <br> being inside the <p>, you're correct, that's how it should be returned. My mistake.

However, I've tried adding the extension needed to create hard break lines but I still get the same output.
It seems that Markdig is struggling to understand what these \n mean and leaves them unparsed.

Here's the implementation I'm testing:
image

Here's the output I'm getting:
image

@tiagolima97 commented on GitHub (Feb 17, 2023): Thanks for the quick response @xoofx! In regard to the `<br>` being inside the `<p>`, you're correct, that's how it should be returned. My mistake. However, I've tried adding the extension needed to create hard break lines but I still get the same output. It seems that Markdig is struggling to understand what these `\n` mean and leaves them unparsed. Here's the implementation I'm testing: ![image](https://user-images.githubusercontent.com/125599062/219680648-d0268482-e9b2-4c9f-ace6-8e56535f89f5.png) Here's the output I'm getting: ![image](https://user-images.githubusercontent.com/125599062/219681014-6521543b-c2c2-4469-84f3-fc6c404cd9dc.png)
Author
Owner

@xoofx commented on GitHub (Feb 17, 2023):

It seems that Markdig is struggling to understand what these \n mean and leaves them unparsed.

Yep, but the extension is for lines within a paragraph, not trailing blank lines. The problem is that blank lines are used to break paragraph already. It's part of CommonMark parsing rules and this is not something that can be changed.

@xoofx commented on GitHub (Feb 17, 2023): > It seems that Markdig is struggling to understand what these \n mean and leaves them unparsed. Yep, but the extension is for lines within a paragraph, not trailing blank lines. The problem is that blank lines are used to break paragraph already. It's part of [CommonMark parsing rules](https://spec.commonmark.org/0.30/#blank-lines) and this is not something that can be changed.
Author
Owner

@CraigComeOnThisNameCantBeTaken commented on GitHub (May 18, 2023):

I am also finding this a problem

@CraigComeOnThisNameCantBeTaken commented on GitHub (May 18, 2023): I am also finding this a problem
Author
Owner

@angezid commented on GitHub (May 26, 2023):

It's part of CommonMark parsing rules and this is not something that can be changed.

Why Github

not following this stupid

rule?

Markdown is a plain text format for writing structured documents.

How to keep structure of documents then?

@angezid commented on GitHub (May 26, 2023): >It's part of CommonMark parsing rules and this is not something that can be changed. Why Github not following this stupid rule? > Markdown is a plain text format for writing structured documents. How to keep structure of documents then?
Author
Owner

@xoofx commented on GitHub (May 26, 2023):

How to keep structure of documents then?

A blank line is not considered as a structure. Don't use Markdown in that case.

@xoofx commented on GitHub (May 26, 2023): > How to keep structure of documents then? A blank line is not considered as a structure. Don't use Markdown in that case.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#591