Code block not adding new lines or pretty formatting #241

Closed
opened 2026-01-29 14:31:25 +00:00 by claunia · 7 comments
Owner

Originally created by @martinkearn on GitHub (Nov 13, 2018).

I have this markdown representing a C# code sample:

if (env.IsDevelopment())
{
    builder.AddUserSecrets<Startup>(false);
}

I'm using this code to convert it to HTML

var pipeline = new MarkdownPipelineBuilder()
    .UseYamlFrontMatter()
    .Build();
var html = Markdown.ToHtml(response, pipeline);

It is producing HTML with no new lines in it as follows

<pre><code class="language-c#"> if (env.IsDevelopment()) { builder.AddUserSecrets&lt;Startup&gt;(false); }</code></pre>

Is there an extension or some other solution to produce HTML code blocks that are nicely formatted?

Originally created by @martinkearn on GitHub (Nov 13, 2018). I have this markdown representing a C# code sample: ```c# if (env.IsDevelopment()) { builder.AddUserSecrets<Startup>(false); } ``` I'm using this code to convert it to HTML ``` var pipeline = new MarkdownPipelineBuilder() .UseYamlFrontMatter() .Build(); var html = Markdown.ToHtml(response, pipeline); ``` It is producing HTML with no new lines in it as follows `<pre><code class="language-c#"> if (env.IsDevelopment()) { builder.AddUserSecrets&lt;Startup&gt;(false); }</code></pre>` Is there an extension or some other solution to produce HTML code blocks that are nicely formatted?
claunia added the invalid label 2026-01-29 14:31:25 +00:00
Author
Owner

@MihaZupan commented on GitHub (Nov 13, 2018):

Copy-pasting your sample generates html with correct new lines for me

@MihaZupan commented on GitHub (Nov 13, 2018): Copy-pasting your sample generates html with correct new lines for me
Author
Owner

@Kryptos-FR commented on GitHub (Nov 14, 2018):

How are your line endings in your C# code? Have a look at the ConfigureNewLine extension.

@Kryptos-FR commented on GitHub (Nov 14, 2018): How are your line endings in your C# code? Have a look at the `ConfigureNewLine` extension.
Author
Owner

@martinkearn commented on GitHub (Nov 14, 2018):

@MihaZupan please try the contents of this file: https://raw.githubusercontent.com/martinkearn/Content/master/Blogs/Test.md which contains some simple Markdown with a code block to see if you can repro the problem there. I get this HTML from this file:

<h1 id="h1">h1</h1><p>some markdown content</p><p>And now for some code ...</p><pre><code class="language-c#">foreach (var thing in things){  var thisThing = thing;}</code></pre><h2 id="h2">h2</h2><p>Some markdown text after the code</p>

@Kryptos-FR My code now looks like this but makes no difference

var pipeline = new MarkdownPipelineBuilder()
    .UseYamlFrontMatter()
    .UseAdvancedExtensions()
    .ConfigureNewLine("\r\n")
    .Build();
var html = Markdown.ToHtml(response, pipeline);

You can see my full Azure Function code here: https://github.com/martinkearn/MartinK-Me/blob/GitHubCMS/Functions/Functions/ConvertToHtmlWithMetadata.cs

Thanks all

@martinkearn commented on GitHub (Nov 14, 2018): @MihaZupan please try the contents of this file: <https://raw.githubusercontent.com/martinkearn/Content/master/Blogs/Test.md> which contains some simple Markdown with a code block to see if you can repro the problem there. I get this HTML from this file: ```html <h1 id="h1">h1</h1><p>some markdown content</p><p>And now for some code ...</p><pre><code class="language-c#">foreach (var thing in things){ var thisThing = thing;}</code></pre><h2 id="h2">h2</h2><p>Some markdown text after the code</p> ``` @Kryptos-FR My code now looks like this but makes no difference ``` var pipeline = new MarkdownPipelineBuilder() .UseYamlFrontMatter() .UseAdvancedExtensions() .ConfigureNewLine("\r\n") .Build(); var html = Markdown.ToHtml(response, pipeline); ``` You can see my full Azure Function code here: <https://github.com/martinkearn/MartinK-Me/blob/GitHubCMS/Functions/Functions/ConvertToHtmlWithMetadata.cs> Thanks all
Author
Owner

@Kryptos-FR commented on GitHub (Nov 14, 2018):

Looks like something is wrong on your end: all the line endings are removed. Are you sure you don't have some other post-processing?

Looking at https://babelmark.github.io/?text=%60%60%60c%23%0Aforeach+(var+thing+in+things)%0A%7B%0A++var+thisThing+%3D+thing%3B%0A%7D%0A%60%60%60 the result with markdig seems correct.

@Kryptos-FR commented on GitHub (Nov 14, 2018): Looks like something is wrong on your end: all the line endings are removed. Are you sure you don't have some other post-processing? Looking at https://babelmark.github.io/?text=%60%60%60c%23%0Aforeach+(var+thing+in+things)%0A%7B%0A++var+thisThing+%3D+thing%3B%0A%7D%0A%60%60%60 the result with markdig seems correct.
Author
Owner

@martinkearn commented on GitHub (Nov 14, 2018):

OK, thanks, it could be how I'm getting the raw Markdown. I'll investigate and post back here when I've figured it out

@martinkearn commented on GitHub (Nov 14, 2018): OK, thanks, it could be how I'm getting the raw Markdown. I'll investigate and post back here when I've figured it out
Author
Owner

@martinkearn commented on GitHub (Nov 14, 2018):

OK, I've resolved this now … it was nothing to do with MarkDig but the way I was getting my Markdown string (thanks @Kryptos-FR for the hint). The Markdown is on GitHub and I was getting the raw file using a GET request to https://raw.githubusercontent.com/martinkearn/Content/master/Blogs/Test.md. I'm now using the GitHub API to get the File object (GET request to https://api.github.com/repos/martinkearn/Content/contents/Blogs/Test.md) which includes a Base64 encoded version of its content. Once I've Base64Decoded that string and passed to MarkDig it all works fine. Thanks for the help. I hope this thread helps someone from the future.

@martinkearn commented on GitHub (Nov 14, 2018): OK, I've resolved this now … it was nothing to do with MarkDig but the way I was getting my Markdown string (thanks @Kryptos-FR for the hint). The Markdown is on GitHub and I was getting the raw file using a GET request to <https://raw.githubusercontent.com/martinkearn/Content/master/Blogs/Test.md>. I'm now using the GitHub API to get the File object (GET request to <https://api.github.com/repos/martinkearn/Content/contents/Blogs/Test.md>) which includes a Base64 encoded version of its content. Once I've Base64Decoded that string and passed to MarkDig it all works fine. Thanks for the help. I hope this thread helps someone from the future.
Author
Owner

@Kryptos-FR commented on GitHub (Nov 14, 2018):

@martinkearn welcome. Thanks for providing the explanation as other people might stumble upon the same issue.

@Kryptos-FR commented on GitHub (Nov 14, 2018): @martinkearn welcome. Thanks for providing the explanation as other people might stumble upon the same issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#241