Bug? Inline HTML is converted to wrong result
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 5.3k
- Forks
- 510
- Avg merge
- 8d 5h
- Merged PRs (30d)
- 5
Description
I work with a complicated environment, where we need to render a lot of documents with MD and HTML mixed. Some HTML <pre> blocks are converted to wrong result.
Problem
Take the following document as example:
## MD Heading
MD Paragraph
<p>HTML Paragraph</p>
<pre class="code"><span style="color: blue;">public partial class </span><span style="color: rgb(43, 145, 175);">AdventureWorks </span><span style="color: black;">: </span><span style="color: rgb(43, 145, 175);">DbContext
</span><span style="color: black;">{
</span><span style="color: blue;">protected override void </span><span style="color: black;">OnModelCreating(</span><span style="color: rgb(43, 145, 175);">DbModelBuilder </span><span style="color: black;">modelBuilder)
{
</span><span style="color: blue;">base</span><span style="color: black;">.OnModelCreating(modelBuilder);
</span><span style="color: green;">// Add functions on AdventureWorks to entity model.
</span><span style="color: black;">modelBuilder.Conventions.Add(</span><span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">FunctionConvention</span><span style="color: black;"><</span><span style="color: rgb(43, 145, 175);">AdventureWorks</span><span style="color: black;">>());
</span><span style="color: green;">// Add all complex types used by functions.
</span><span style="color: black;">modelBuilder.ComplexType<</span><span style="color: rgb(43, 145, 175);">ContactInformation</span><span style="color: black;">>();
modelBuilder.ComplexType<</span><span style="color: rgb(43, 145, 175);">ManagerEmployee</span><span style="color: black;">>();
</span><span style="color: green;">// ...
</span><span style="color: black;">}
}</span></pre>
it includes <pre> block, which is correctly rendered as:

(See: https://jsfiddle.net/dixin/0dj2b81x/)
Then I tried to process it with MarkDig:
static void Main()
{
MarkdownPipelineBuilder builder = new MarkdownPipelineBuilder().UseAdvancedExtensions();
MarkdownPipeline pipeline = builder.Build();
string html = Markdown.ToHtml(File.ReadAllText(@"d:\md.txt"), pipeline);
File.WriteAllText(@"d:\html.txt", html);
}
The HTML result is messed up:
<h2 id="md-heading">MD Heading</h2>
<p>MD Paragraph</p>
<p>HTML Paragraph</p>
<pre class="code"><span style="color: blue;">public partial class </span><span style="color: rgb(43, 145, 175);">AdventureWorks </span><span style="color: black;">: </span><span style="color: rgb(43, 145, 175);">DbContext
</span><span style="color: black;">{
</span><span style="color: blue;">protected override void </span><span style="color: black;">OnModelCreating(</span><span style="color: rgb(43, 145, 175);">DbModelBuilder </span><span style="color: black;">modelBuilder)
{
</span><span style="color: blue;">base</span><span style="color: black;">.OnModelCreating(modelBuilder);
<pre><code> </span><span style="color: green;">// Add functions on AdventureWorks to entity model.
</span><span style="color: black;">modelBuilder.Conventions.Add(</span><span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">FunctionConvention</span><span style="color: black;">&lt;</span><span style="color: rgb(43, 145, 175);">AdventureWorks</span><span style="color: black;">&gt;());
</span><span style="color: green;">// Add all complex types used by functions.
</span><span style="color: black;">modelBuilder.ComplexType&lt;</span><span style="color: rgb(43, 145, 175);">ContactInformation</span><span style="color: black;">&gt;();
modelBuilder.ComplexType&lt;</span><span style="color: rgb(43, 145, 175);">ManagerEmployee</span><span style="color: black;">&gt;();
</span><span style="color: green;">// ...
</span><span style="color: black;">}
</code></pre>
<p>}</span></pre></p>
So the document becomes not readable:

(See https://jsfiddle.net/dixin/6j7yLx85/)
Partial solution
I found #348, and used its code:
builder.BlockParsers.TryRemove<IndentedCodeBlockParser>();
Now the HTML result gets better:
<h2 id="md-heading">MD Heading</h2>
<p>MD Paragraph</p>
<p>HTML Paragraph</p>
<pre class="code"><span style="color: blue;">public partial class </span><span style="color: rgb(43, 145, 175);">AdventureWorks </span><span style="color: black;">: </span><span style="color: rgb(43, 145, 175);">DbContext
</span><span style="color: black;">{
</span><span style="color: blue;">protected override void </span><span style="color: black;">OnModelCreating(</span><span style="color: rgb(43, 145, 175);">DbModelBuilder </span><span style="color: black;">modelBuilder)
{
</span><span style="color: blue;">base</span><span style="color: black;">.OnModelCreating(modelBuilder);
<p></span><span style="color: green;">// Add functions on AdventureWorks to entity model.
</span><span style="color: black;">modelBuilder.Conventions.Add(</span><span style="color: blue;">new </span><span style="color: rgb(43, 145, 175);">FunctionConvention</span><span style="color: black;"><</span><span style="color: rgb(43, 145, 175);">AdventureWorks</span><span style="color: black;">>());</p>
<p></span><span style="color: green;">// Add all complex types used by functions.
</span><span style="color: black;">modelBuilder.ComplexType<</span><span style="color: rgb(43, 145, 175);">ContactInformation</span><span style="color: black;">>();
modelBuilder.ComplexType<</span><span style="color: rgb(43, 145, 175);">ManagerEmployee</span><span style="color: black;">>();
</span><span style="color: green;">// ...
</span><span style="color: black;">}
}</span></pre></p>
The document becomes a little more readable:

(See: https://jsfiddle.net/dixin/tzkphu2m/1/)
It still has problems:
- Inside
<pre>block, it removes the intentional blank line, - It adds
<p>inside<pre>and outside</pre> - It removes some of the indentation inside
<pre>block.
Questions
What should I do to render the above example document correctly?
Regarding the result HTML has wrong indent and even wrong format (<pre>...<p>...</pre></p>), is this a bug?
Thank you for help.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided MarkdownPipelineBuilder example, including UseAdvancedExtensions and the BlockParsers configuration, and reproduce the mixed Markdown/HTML case before and after removing IndentedCodeBlockParser. Trace how the inline HTML pre block and its nested spans are parsed, then verify that the output preserves blank lines and indentation without inserting invalid p elements.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100