How to not encode query string parameters in link url #438

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

Originally created by @JoshClose on GitHub (Feb 17, 2021).

When a query string is in a URL, it is getting encoded. Is there a way to have it not encoded?

Markdown.ToHtml("[text](/foo/bar?a=b&c=d#fragment)")

outputs

<p><a href="/foo/bar?a=b&amp;c=d#fragment">text</a></p>

I would like

<p><a href="/foo/bar?a=b&c=d#fragment">text</a></p>
Originally created by @JoshClose on GitHub (Feb 17, 2021). When a query string is in a URL, it is getting encoded. Is there a way to have it not encoded? ```cs Markdown.ToHtml("[text](/foo/bar?a=b&c=d#fragment)") ``` outputs ```html <p><a href="/foo/bar?a=b&amp;c=d#fragment">text</a></p> ``` I would like ```html <p><a href="/foo/bar?a=b&c=d#fragment">text</a></p> ```
claunia added the questionenhancementPR Welcome! labels 2026-01-29 14:36:41 +00:00
Author
Owner

@MihaZupan commented on GitHub (Feb 17, 2021):

You should be able to use the LinkRewriter on HtmlRenderer to remove the query part

@MihaZupan commented on GitHub (Feb 17, 2021): You should be able to use the `LinkRewriter` on `HtmlRenderer` to remove the query part
Author
Owner

@JoshClose commented on GitHub (Feb 17, 2021):

I don't want the query removed. I just want to keep it intact.

@JoshClose commented on GitHub (Feb 17, 2021): I don't want the query removed. I just want to keep it intact.
Author
Owner

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

When a query string is in a URL, it is getting encoded. Is there a way to have it not encoded?

Nope. It requires a change to the code to add an option to HtmlRenderer to avoid the Url encoding. The CommonMark specs don't enforce the encoding, so I would believe that it would be ok to have an option. PR welcome.

@xoofx commented on GitHub (Feb 17, 2021): > When a query string is in a URL, it is getting encoded. Is there a way to have it not encoded? Nope. It requires a change to the code to add an option to HtmlRenderer to avoid the Url encoding. The CommonMark specs don't enforce the encoding, so I would believe that it would be ok to have an option. PR welcome.
Author
Owner

@MihaZupan commented on GitHub (Feb 17, 2021):

Ah, sorry. One way would be to use a custom LinkInlineRenderer and not call WriteEscapeUrl.

Why would you want to do this though?

@MihaZupan commented on GitHub (Feb 17, 2021): Ah, sorry. One way would be to use a custom `LinkInlineRenderer` and not call `WriteEscapeUrl`. Why would you want to do this though?
Author
Owner

@JoshClose commented on GitHub (Feb 17, 2021):

Why would you want to do this though?

So when someone clicks the link, it goes to the correct address. I feel like I'm missing something or am confused about this.

@JoshClose commented on GitHub (Feb 17, 2021): > Why would you want to do this though? So when someone clicks the link, it goes to the correct address. I feel like I'm missing something or am confused about this.
Author
Owner

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

So when someone clicks the link, it goes to the correct address. I feel like I'm missing something or am confused about this.

That's weird because the encoding has been here for years without being an issue... there is something else...

@xoofx commented on GitHub (Feb 17, 2021): > So when someone clicks the link, it goes to the correct address. I feel like I'm missing something or am confused about this. That's weird because the encoding has been here for years without being an issue... there is something else...
Author
Owner

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

To be fair, I don't recall why markdig escape & to &amp; in an URL...hm....

@xoofx commented on GitHub (Feb 17, 2021): To be fair, I don't recall why markdig escape `&` to `&amp;` in an URL...hm....
Author
Owner

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

it could be a bug actually, I really don't get this & to &amp;, it's HTML escape, not URL....

@xoofx commented on GitHub (Feb 17, 2021): it could be a bug actually, I really don't get this `&` to `&amp;`, it's HTML escape, not URL....
Author
Owner

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

babelmark is showing the same behavior for almost all Markdown implems... ok, now I'm confused! 😅

@xoofx commented on GitHub (Feb 17, 2021): babelmark is showing the same behavior for [almost all Markdown implems](https://babelmark.github.io/?text=%5Btext%5D(%2Ffoo%2Fbar%3Fa%3Db%26c%3Dd%23fragment))... ok, now I'm confused! 😅
Author
Owner

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

while the example 591 in the specs seems to escape to & to &amp;... so yeah, it seems the correct behavior, not sure what to thing about it, will have to think about it with a clearer mind tomorrow!

@xoofx commented on GitHub (Feb 17, 2021): while the [example 591](https://spec.commonmark.org/0.29/#example-591) in the specs seems to escape to `&` to `&amp;`... so yeah, it seems the correct behavior, not sure what to thing about it, will have to think about it with a clearer mind tomorrow!
Author
Owner

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

So when someone clicks the link, it goes to the correct address. I feel like I'm missing something or am confused about this.

By curiosity, with which browser are you actually testing this link?

@xoofx commented on GitHub (Feb 17, 2021): > So when someone clicks the link, it goes to the correct address. I feel like I'm missing something or am confused about this. By curiosity, with which browser are you actually testing this link?
Author
Owner

@JoshClose commented on GitHub (Feb 17, 2021):

Looks like it works as Chrome/Edge will change the raw url with &amp; to just &. Maybe this is a non-issue. It seemed like it shouldn't and the online markdown one I tested with showed it didn't, so here I am. :) https://dillinger.io/ Though now seeing that browsers will correct the html in the link, maybe this one is outputting the same as MarkDig.

@JoshClose commented on GitHub (Feb 17, 2021): Looks like it works as Chrome/Edge will change the raw url with `&amp;` to just `&`. Maybe this is a non-issue. It seemed like it shouldn't and the online markdown one I tested with showed it didn't, so here I am. :) https://dillinger.io/ Though now seeing that browsers will correct the html in the link, maybe this one is outputting the same as MarkDig.
Author
Owner

@JoshClose commented on GitHub (Feb 17, 2021):

If CommonMark shows it should be encoded then this is a non-issue. I don't understand why it would encode parts of the actual URL for an href, but I guess that's how it is.

@JoshClose commented on GitHub (Feb 17, 2021): If CommonMark shows it should be encoded then this is a non-issue. I don't understand why it would encode parts of the actual URL for an href, but I guess that's how it is.
Author
Owner

@JoshClose commented on GitHub (Feb 17, 2021):

Thanks for looking into this.

@JoshClose commented on GitHub (Feb 17, 2021): Thanks for looking into this.
Author
Owner

@jjxtra commented on GitHub (Mar 18, 2023):

What would it take to add an optional bool property somewhere to the pipeline to not write escaped urls?

@jjxtra commented on GitHub (Mar 18, 2023): What would it take to add an optional bool property somewhere to the pipeline to not write escaped urls?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#438