Unable to parse relative URL to parent directories #701

Open
opened 2026-01-29 14:43:21 +00:00 by claunia · 3 comments
Owner

Originally created by @AmadeusW on GitHub (Sep 30, 2024).

I'm writing a Markdown renderer that formats user .md files into navigable .html pages.
Some user documents contain relative paths to a parent directory, like these:

[example link](..\example.md)
[example link](\..\example.md)

Currently, LinkHelper.TryParseInlineLink calls LinkHelper.TryParseInlineLink, which calls LinkHelper.TryParseUrl leaving isAutoLink unset as false. This means that TryParseUrl treats the slash as escape character and effectively removes it.
The character is not recorded as a part of trivia, and there's no way to infer its presence by examining the AST.

Setting isAutoLink to true seems to address this issue, but I don't know what auto link is, and whether this is an appropriate wokaround.
I was thinking about a solution where we'd set a property on InlineProcessor.MarkdownPaserContext to influence LinkInlineParser.TryProcessLinkOrImage, but would like to get your opinion before making code changes.

Originally created by @AmadeusW on GitHub (Sep 30, 2024). I'm writing a Markdown renderer that formats user .md files into navigable .html pages. Some user documents contain relative paths to a parent directory, like these: ``` [example link](..\example.md) [example link](\..\example.md) ``` Currently, `LinkHelper.TryParseInlineLink` calls `LinkHelper.TryParseInlineLink`, which calls `LinkHelper.TryParseUrl` leaving `isAutoLink` unset as `false`. This means that `TryParseUrl` treats the slash as escape character and effectively removes it. The character is not recorded as a part of trivia, and there's no way to infer its presence by examining the AST. Setting `isAutoLink` to true seems to address this issue, but I don't know what _auto link_ is, and whether this is an appropriate wokaround. I was thinking about a solution where we'd set a property on `InlineProcessor`.`MarkdownPaserContext` to influence `LinkInlineParser.TryProcessLinkOrImage`, but would like to get your opinion before making code changes.
claunia added the question label 2026-01-29 14:43:21 +00:00
Author
Owner

@AmadeusW commented on GitHub (Oct 15, 2024):

@xoofx I'd appreciate your opinion how to best fix this, or how to override the behavior of LinkInlineParser

@AmadeusW commented on GitHub (Oct 15, 2024): @xoofx I'd appreciate your opinion how to best fix this, or how to override the behavior of `LinkInlineParser`
Author
Owner

@xoofx commented on GitHub (Oct 15, 2024):

I would use / instead of \ and you wouldn't have to change the parser:

[example link](../example.md)
[example link](/../example.md)

None of the CommonMark parser are supporting \ in that case and they all trim it in the same way here

@xoofx commented on GitHub (Oct 15, 2024): I would use `/` instead of \\ and you wouldn't have to change the parser: ```md [example link](../example.md) [example link](/../example.md) ``` None of the CommonMark parser are supporting \\ in that case and they all trim it in the same way [here](https://babelmark.github.io/?text=%5Bexample+link%5D(..%5Cexample.md)%0A%5Bexample+link%5D(%5C..%5Cexample.md))
Author
Owner

@jefflomax commented on GitHub (Nov 24, 2024):

While I do not recommend it - I pre-parsed markdown to rewrite the links to the standard, then passed that to this wonderful library. It's a pain, if you can get escaped unix style paths that's better.

@jefflomax commented on GitHub (Nov 24, 2024): While I do not recommend it - I pre-parsed markdown to rewrite the links to the standard, then passed that to this wonderful library. It's a pain, if you can get escaped unix style paths that's better.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#701