RoundTrip renderer not working?, breaking changes in output. #642

Closed
opened 2026-01-29 14:41:49 +00:00 by claunia · 3 comments
Owner

Originally created by @NetTecture on GitHub (Nov 26, 2023).

Tests done with 0.33.0

I have complex markdown that Itry to process and I find the simpple issue that at least links are not properly processed at all.

The following is a simple code snippet that I use to debug this:

void MarkdownTreeRework(string original)
{
    var document = Markdig.Markdown.Parse(original)!;
    var writer = new StringWriter();
    var renderer = new Markdig.Renderers.Roundtrip.RoundtripRenderer(writer);
    renderer.Render(document);
    var updatedMarkdown = writer.ToString();
    Console.WriteLine(updatedMarkdown);

}

Doing really nothing, it takes a text as input, generates the parsed document, turns it out through the RoundtripRenderer. This is as simple as it gets, essentially.

Here are some (not so nice) findings I find:

  • There is a loss of empty lines:

This:

# Universal Basic Income: Pros, Cons, and its Feasibility in a World Dominated by AI

## Overview

### What is Universal Basic Income?

Universal Basic Income (UBI) is a policy that guarantees every individual a regular, 

turns into:

#Universal Basic Income: Pros, Cons, and its Feasibility in a World Dominated by AI
##Overview
###What is Universal Basic Income?
Universal Basic Income (UBI) is a policy that guarantees every individual a regular,

In some parts, it is breaking:

while ensuring a fair and equitable distribution of resources.

## Impact on Employment

![](https://contenu.nyc3.digitaloceanspaces.com/journalist/d69a68ba-5f1a-4ddf-801e-f4fdb4b8be06/thumbnail.jpeg)

### Promoting Entrepreneurship and Innovation

To:

 while ensuring a fair and equitable distribution of resources.##Impact on Employment
![]()###Promoting Entrepreneurship and Innovation

2x the new line was skipped, putting the image link right into text and destroying the header information, as it must be on a new line. On top, this snippet shows another round trip bug:

  • Links are removed

As you can see in the last comparison up - the https link was removed and the output has no URL. It does keep the text associated:

`Artificial intelligence (AI) is [revolutionizing the financial markets](https://unescap.org/cgi-bin/koha/tracklinks.pl?uri=http://yfv8j11nou46.%D0%B0%D0%BB%D1%8C%D0%B4%D0%BE%D0%B3%D0%B0.%D1%80%D1%84) and the economy as a whole.`

To

`Artificial intelligence (AI) is [revolutionizing the financial markets]() and the economy as a whole. With its ability to analyse vast amounts of data and make predictions,`

but the links get removed.

it COULD bea configuration error on my side, but the code as shown is trivial enough I do not think so and follows the roundtrip documentation.

Not round trippy.

Originally created by @NetTecture on GitHub (Nov 26, 2023). Tests done with 0.33.0 I have complex markdown that Itry to process and I find the simpple issue that at least links are not properly processed at all. The following is a simple code snippet that I use to debug this: void MarkdownTreeRework(string original) { var document = Markdig.Markdown.Parse(original)!; var writer = new StringWriter(); var renderer = new Markdig.Renderers.Roundtrip.RoundtripRenderer(writer); renderer.Render(document); var updatedMarkdown = writer.ToString(); Console.WriteLine(updatedMarkdown); } Doing really nothing, it takes a text as input, generates the parsed document, turns it out through the RoundtripRenderer. This is as simple as it gets, essentially. Here are some (not so nice) findings I find: * There is a loss of empty lines: This: ``` # Universal Basic Income: Pros, Cons, and its Feasibility in a World Dominated by AI ## Overview ### What is Universal Basic Income? Universal Basic Income (UBI) is a policy that guarantees every individual a regular, ``` turns into: ``` #Universal Basic Income: Pros, Cons, and its Feasibility in a World Dominated by AI ##Overview ###What is Universal Basic Income? Universal Basic Income (UBI) is a policy that guarantees every individual a regular, ``` In some parts, it is breaking: ``` while ensuring a fair and equitable distribution of resources. ## Impact on Employment ![](https://contenu.nyc3.digitaloceanspaces.com/journalist/d69a68ba-5f1a-4ddf-801e-f4fdb4b8be06/thumbnail.jpeg) ### Promoting Entrepreneurship and Innovation ``` To: ``` while ensuring a fair and equitable distribution of resources.##Impact on Employment ![]()###Promoting Entrepreneurship and Innovation ``` 2x the new line was skipped, putting the image link right into text and destroying the header information, as it must be on a new line. On top, this snippet shows another round trip bug: * Links are removed As you can see in the last comparison up - the https link was removed and the output has no URL. It does keep the text associated: ``` `Artificial intelligence (AI) is [revolutionizing the financial markets](https://unescap.org/cgi-bin/koha/tracklinks.pl?uri=http://yfv8j11nou46.%D0%B0%D0%BB%D1%8C%D0%B4%D0%BE%D0%B3%D0%B0.%D1%80%D1%84) and the economy as a whole.` ``` To ``` `Artificial intelligence (AI) is [revolutionizing the financial markets]() and the economy as a whole. With its ability to analyse vast amounts of data and make predictions,` ``` but the links get removed. it COULD bea configuration error on my side, but the code as shown is trivial enough I do not think so and follows the roundtrip documentation. Not round trippy.
claunia added the question label 2026-01-29 14:41:49 +00:00
Author
Owner

@MihaZupan commented on GitHub (Nov 26, 2023):

You have to pass the trackTrivia flag to Markdown.Parse

-var document = Markdig.Markdown.Parse(original)!;
+var document = Markdig.Markdown.Parse(original, trackTrivia: true);
@MihaZupan commented on GitHub (Nov 26, 2023): You have to pass the `trackTrivia` flag to `Markdown.Parse` ```diff -var document = Markdig.Markdown.Parse(original)!; +var document = Markdig.Markdown.Parse(original, trackTrivia: true); ```
Author
Owner

@NetTecture commented on GitHub (Nov 28, 2023):

This smells like a bug still to me. Let me be clear here - I can understand that for things that re non breaking (like removing SURPLUS empty lines).... but if this parsing removes functional elements.... something is broken. trackTrivial should not leave to links becoming totlly useless or new lines that are essential to be removed.

@NetTecture commented on GitHub (Nov 28, 2023): This smells like a bug still to me. Let me be clear here - I can understand that for things that re non breaking (like removing SURPLUS empty lines).... but if this parsing removes functional elements.... something is broken. trackTrivial should not leave to links becoming totlly useless or new lines that are essential to be removed.
Author
Owner

@xoofx commented on GitHub (Nov 29, 2023):

This smells like a bug still to me. Let me be clear here - I can understand that for things that re non breaking (like removing SURPLUS empty lines).... but if this parsing removes functional elements.... something is broken. trackTrivial should not leave to links becoming totlly useless or new lines that are essential to be removed.

Markdig was initially developed for generating HTML. Not for doing roundtrip to Markdown which came later, came partially and was not ideal to bring because it is on its own, already quite complex to manage, but even more complicated to bring as an afterthought.

trackTrivia was introduced after, by someone outside of myself and @MihaZupan. Tracktrivia is not free, it makes the parsing slower, generates more GC and as I said, the primary usage of Markdig is for generating HTML. That's why it's not on by default.

So, it's not a bug, but a complexity/legacy that we need to deal with.

@xoofx commented on GitHub (Nov 29, 2023): > This smells like a bug still to me. Let me be clear here - I can understand that for things that re non breaking (like removing SURPLUS empty lines).... but if this parsing removes functional elements.... something is broken. trackTrivial should not leave to links becoming totlly useless or new lines that are essential to be removed. Markdig was initially developed for generating HTML. Not for doing roundtrip to Markdown which came later, came partially and was not ideal to bring because it is on its own, already quite complex to manage, but even more complicated to bring as an afterthought. `trackTrivia` was introduced after, by someone outside of myself and @MihaZupan. Tracktrivia is not free, it makes the parsing slower, generates more GC and as I said, the primary usage of Markdig is for generating HTML. That's why it's not on by default. So, it's not a bug, but a complexity/legacy that we need to deal with.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#642