You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Links with query params or fragments are getting encoded. I would like to be able to use links with query params and fragments.
There is a workaround listed here statiqdev/Statiq#744 but that won't work for markdown.
Links that contain a query param or fragment get generated properly.
Links that start with ~ should be converted correctly as if using Context.GetLink().
I can look into adding these features as I need them right now. I'll need a little guidance on how it should be done though.
It looks like Statiq.Common.LinkGenerator.GetLink needs query and fragment parameters added. Those could be classes QueryString and FragmentIdentifier to add functionality for creating, parsing, etc.
Where in the system do links get converted from code to their generated value? Looking at the markdown module, it adds the LinkRoot there. Does that mean each module (markdown, html, razor, etc) needs to handle links on their own?
Thanks for taking a look at this! If you run into problems or need an assist, I'm happy to continue working this with you.
Where in the system do links get converted from code to their generated value?
Generally that's exactly what LinkGenerator is supposed to do (along with the various extension methods like context.GetLink() and document.GetLink() that call it). It's supposed to be a single place to get a link given host name, link root, etc.
Of course it looks like the RenderMarkdown module (and more specifically the MarkdownHelper that it calls) don't actually use the LinkGenerator. Instead, the LinkRoot appears to just be prefixed to links that start with /. I'm not sure if that's due to that part of the code being written before LinkGenerator or what, but it's probably not the "right" way to do it. Ideally the MarkdownHelper would call LinkGenerator like everywhere else.
Does that mean each module (markdown, html, razor, etc) needs to handle links on their own?
Ideally each rendering/template module would have some sort of support for it's own conventions regarding links that eventually call the LinkGenerator. That said, we do need to be a little careful because "rewriting" links should always be opt-in. Something like a ~ prefix is probably fair game since that doesn't have a close analogy to actual paths, but a / prefix should be left as-is by default.
To that end, I could see a universal capability to rewrite ~ links - maybe even a RewriteTildeLinks module or something. That might remove the need for more module-specific functionality like RenderMarkdown.PrependLinkRoot(). In other words, in any format (Markdown, Razor, Handlebars, HTML, etc.) go ahead and render links normally without rewriting or editing them. Then as a final stage, run all the finished and rendered HTML through the RewriteTildeLinks module, looking for any link in <a>, <script>, <link>, etc. elements that is prefixed with ~ and run those links through the LinkGenerator to prepend the LinkRoot and do any other manipulation. Does that make sense? The more I think about it, the more I like it.
BTW - happy for you to make an attempt at this, more folks who spelunk through the framework code and get familiar with it is always welcome. But that said, if you find it's getting confusing or will take longer than you have let me know and I'll dive in to work this.
Is there a way to have Razor not transform the ~/ URLs? If not, Razor will have to Context.GetLink. I don't know if that will be confusing or not if other content like markdown will process the ~.
That's a really good point - if individual modules handle the ~ links, then behavior when those modules "fall through" (like how Markdown is rendered and then that's passed to the Razor engine) might start having strange side effects.
Let me see if I can get the Razor engine to totally ignore tilde links. They're generally used in a server-side context for resolving stuff like partial views to the root of the server directory, but that's not really a thing for us. There's no reason we need special resolution while rendering, only afterwards in the resulting HTML to account for virtual directories, etc.
If you can't disable it, maybe you just have to use GetLink in Razor to resolve properly. I think in that case the other languages could still parse automatically. Any ~ gets the LinkRoot substituted. Any query or fragment gets processed properly. I think the lower levels of Context.GetLink could parse a URL and handle that stuff to pass to LinkGenerator.
I may submit a pull request with a few things to fix my current issue so I can deploy my site. If the RewriteTilde thing happens, it'll be a much larger change that would take me quite a while to figure out how to drop into this system.
You've successfully nerd sniped me, so if you get in a PR that unblocks you go for it and I'll get it released. In the meantime, I'm going to start taking a look at the RewriteTilde idea to see how hard that'll be to get working universally.
I had to look that up. That's funny. I remember seeing that on XKCD a long time ago.
I get the same way about stuff. This post caused me to work for weeks on making things as fast as possible without losing any functionality. It got it (CsvHelper) to be the fastest until I started adding in all the features to make people lives easier and parse non-standard files. In the end, features supersede speed.
How can I call Context.GetLink from MarkdownHelper.RenderMarkdown? I'm doing IExecutionContext.Current.GetLink(link) but the settings from RenderMarkdownFixture.UsePrependLinkRootSetting don't exist. Maybe this is just an issue with the test setup, but I don't know.
BTW - I'll definitely be able to prevent Razor from handling ~/ links, though it's way harder than it should be (there's a default tag helper that gets registered for every page behind the scenes that processes all a tags and others to deal with tilde links). Hoping to get code up for that tonight at some point.
Is there any reason it needs to get it from the document? I'm using it from the context. I don't believe the document has anything to do with it, but I don't know how the whole system goes together. Is it safe to ignore LinkRoot on the document and only use it from the `Context?
I created a RelativeUrl that parses a URL and handles ~, query, and fragment. It's used inside IExecutionStateGetLinkExtensions.GetLink(string path). I'm just calling this from the markdown renderer. Maybe this will be useful elsewhere in the system. For now, it solves my markdown issue. ;)
I was just thinking all links should go through a link processor so things like ~ can be handled. Otherwise each document type that has a link would need to include logic on if it's absolute or relative, if it's root relative or folder relative, if there's a ~, etc. It would be nice to just call GetLink(string link) from at least markdown (probably others) since in markdown all you have is a string.
Yep, agreed. My thought process here is that an additional module would handle this, and you'd add the module after running whichever rendering engine(s) you want (Statiq Web would include it by default). The advantage of that is you won't need to embed link processing into each rendering engine - the engine should "pass-through" links as natively as possible without any processing. That way folks who don't want or need to rewrite their links can not use the new module and it'll all work exactly as they wrote it. Otherwise, the module will provide universal link rewriting and processing after any rendering engines are done - the net effect should be the same whether links are processed inside the engine or in a second pass by a module (I think).
That sounds good to me. When do you think this feature could be released?
I have my immediate needs of getting markdown fixed, so I could give you a pull request that just fixes markdown for the time being, unless you think the module feature will be out soon.
I've got some time this weekend and am working on it now, so hopefully in the next few days 🤞. This link stuff has the potential to break folks though so I've got to be really careful with the testing which always takes a bit longer.
Now that I'm thinking through the relative link problem, I think what we really need isn't a fromRoot toggle, it's a NormalizedPath origin param. In other words, generating relative links doesn't make much sense unless the link is relative to something. If origin is NormalizedPath.Null then we'll generate an absolute link like we currently do. If it's . then we'll generate a relative link basically by leaving it alone and not prefixing a / (which will also have to ignore the host and scheme params since you can't add a host name to a relative link). But if origin is anything else, we can try to figure out what the absolute path to origin is and then generate a relative link from there.
This way if LinkRoot is set to something like /a/b, the path argument (the link we want to get) is ../../x, and the origin is set to something like c/d.md, we'll return a link like /a/x taking into account the LinkRoot and crawling up from the origin. So it'll return an absolute link, but one that was generated from the origin and relative path.
Give me a sanity check here, I might be overthinking - does that all make sense?
Give me a sanity check here, I might be overthinking - does that all make sense?
lol, I do think you're over complicating it. Browsers will handle foo, ./foo, and .../foo for you. I think if a relative URL doesn't start with / or our special case ~/, then you leave it alone. Maybe I'm missing something though.
Thanks for the PR! I've merged it and new releases of Statiq Framework and Statiq Web are rolling out now. The relative path helper you wrote is going to come in very handy for the new module too, so I'm hoping to get that completed today or tomorrow.
Awesome! I believe that's the only thing that was stopping me from releasing my site on Statiq. Statiq will make updating my documentation site so much easier to update.
I was going to add the query and fragment in LinkGenerator.GetLink, but decided it would be better if you do that. My site uses RelativeUrl instead of GetLink now, which is where I came across that bug.
Activity
daveaglick commentedon Feb 16, 2021
Thanks for taking a look at this! If you run into problems or need an assist, I'm happy to continue working this with you.
Generally that's exactly what
LinkGeneratoris supposed to do (along with the various extension methods likecontext.GetLink()anddocument.GetLink()that call it). It's supposed to be a single place to get a link given host name, link root, etc.Of course it looks like the
RenderMarkdownmodule (and more specifically theMarkdownHelperthat it calls) don't actually use theLinkGenerator. Instead, theLinkRootappears to just be prefixed to links that start with/. I'm not sure if that's due to that part of the code being written beforeLinkGeneratoror what, but it's probably not the "right" way to do it. Ideally theMarkdownHelperwould callLinkGeneratorlike everywhere else.Ideally each rendering/template module would have some sort of support for it's own conventions regarding links that eventually call the
LinkGenerator. That said, we do need to be a little careful because "rewriting" links should always be opt-in. Something like a~prefix is probably fair game since that doesn't have a close analogy to actual paths, but a/prefix should be left as-is by default.To that end, I could see a universal capability to rewrite
~links - maybe even aRewriteTildeLinksmodule or something. That might remove the need for more module-specific functionality likeRenderMarkdown.PrependLinkRoot(). In other words, in any format (Markdown, Razor, Handlebars, HTML, etc.) go ahead and render links normally without rewriting or editing them. Then as a final stage, run all the finished and rendered HTML through theRewriteTildeLinksmodule, looking for any link in<a>,<script>,<link>, etc. elements that is prefixed with~and run those links through theLinkGeneratorto prepend theLinkRootand do any other manipulation. Does that make sense? The more I think about it, the more I like it.daveaglick commentedon Feb 16, 2021
BTW - happy for you to make an attempt at this, more folks who spelunk through the framework code and get familiar with it is always welcome. But that said, if you find it's getting confusing or will take longer than you have let me know and I'll dive in to work this.
JoshClose commentedon Feb 16, 2021
I'll take a look. I'm sure I'll have a lot of questions.
JoshClose commentedon Feb 17, 2021
Is there a way to have Razor not transform the
~/URLs? If not, Razor will have toContext.GetLink. I don't know if that will be confusing or not if other content like markdown will process the~.daveaglick commentedon Feb 18, 2021
That's a really good point - if individual modules handle the
~links, then behavior when those modules "fall through" (like how Markdown is rendered and then that's passed to the Razor engine) might start having strange side effects.Let me see if I can get the Razor engine to totally ignore tilde links. They're generally used in a server-side context for resolving stuff like partial views to the root of the server directory, but that's not really a thing for us. There's no reason we need special resolution while rendering, only afterwards in the resulting HTML to account for virtual directories, etc.
JoshClose commentedon Feb 18, 2021
If you can't disable it, maybe you just have to use
GetLinkin Razor to resolve properly. I think in that case the other languages could still parse automatically. Any~gets theLinkRootsubstituted. Any query or fragment gets processed properly. I think the lower levels ofContext.GetLinkcould parse a URL and handle that stuff to pass toLinkGenerator.I may submit a pull request with a few things to fix my current issue so I can deploy my site. If the
RewriteTildething happens, it'll be a much larger change that would take me quite a while to figure out how to drop into this system.daveaglick commentedon Feb 18, 2021
You've successfully nerd sniped me, so if you get in a PR that unblocks you go for it and I'll get it released. In the meantime, I'm going to start taking a look at the
RewriteTildeidea to see how hard that'll be to get working universally.JoshClose commentedon Feb 18, 2021
I had to look that up. That's funny. I remember seeing that on XKCD a long time ago.
I get the same way about stuff. This post caused me to work for weeks on making things as fast as possible without losing any functionality. It got it (CsvHelper) to be the fastest until I started adding in all the features to make people lives easier and parse non-standard files. In the end, features supersede speed.
daveaglick commentedon Feb 18, 2021
It's my favorite XKCD - mostly because it constantly happens to me 😂
JoshClose commentedon Feb 18, 2021
How can I call
Context.GetLinkfromMarkdownHelper.RenderMarkdown? I'm doingIExecutionContext.Current.GetLink(link)but the settings fromRenderMarkdownFixture.UsePrependLinkRootSettingdon't exist. Maybe this is just an issue with the test setup, but I don't know.JoshClose commentedon Feb 18, 2021
Never mind. I can set the
TextExecutionContext.Settingsand it works.daveaglick commentedon Feb 18, 2021
BTW - I'll definitely be able to prevent Razor from handling
~/links, though it's way harder than it should be (there's a default tag helper that gets registered for every page behind the scenes that processes allatags and others to deal with tilde links). Hoping to get code up for that tonight at some point.JoshClose commentedon Feb 18, 2021
RenderMarkdownhas this line in itIs there any reason it needs to get it from the document? I'm using it from the context. I don't believe the document has anything to do with it, but I don't know how the whole system goes together. Is it safe to ignore
LinkRooton the document and only use it from the `Context?JoshClose commentedon Feb 18, 2021
I created a
RelativeUrlthat parses a URL and handles~, query, and fragment. It's used insideIExecutionStateGetLinkExtensions.GetLink(string path). I'm just calling this from the markdown renderer. Maybe this will be useful elsewhere in the system. For now, it solves my markdown issue. ;)10 remaining items
JoshClose commentedon Feb 20, 2021
I was just thinking all links should go through a link processor so things like
~can be handled. Otherwise each document type that has a link would need to include logic on if it's absolute or relative, if it's root relative or folder relative, if there's a~, etc. It would be nice to just callGetLink(string link)from at least markdown (probably others) since in markdown all you have is a string.daveaglick commentedon Feb 20, 2021
Yep, agreed. My thought process here is that an additional module would handle this, and you'd add the module after running whichever rendering engine(s) you want (Statiq Web would include it by default). The advantage of that is you won't need to embed link processing into each rendering engine - the engine should "pass-through" links as natively as possible without any processing. That way folks who don't want or need to rewrite their links can not use the new module and it'll all work exactly as they wrote it. Otherwise, the module will provide universal link rewriting and processing after any rendering engines are done - the net effect should be the same whether links are processed inside the engine or in a second pass by a module (I think).
JoshClose commentedon Feb 20, 2021
That sounds good to me. When do you think this feature could be released?
I have my immediate needs of getting markdown fixed, so I could give you a pull request that just fixes markdown for the time being, unless you think the module feature will be out soon.
daveaglick commentedon Feb 20, 2021
I've got some time this weekend and am working on it now, so hopefully in the next few days 🤞. This link stuff has the potential to break folks though so I've got to be really careful with the testing which always takes a bit longer.
JoshClose commentedon Feb 20, 2021
Ok. I'll make a pull request for my relative url stuff that I think could help you.
daveaglick commentedon Feb 20, 2021
Awesome! I can always ship that out sooner if I get held up.
daveaglick commentedon Feb 20, 2021
Now that I'm thinking through the relative link problem, I think what we really need isn't a
fromRoottoggle, it's aNormalizedPath originparam. In other words, generating relative links doesn't make much sense unless the link is relative to something. IforiginisNormalizedPath.Nullthen we'll generate an absolute link like we currently do. If it's.then we'll generate a relative link basically by leaving it alone and not prefixing a/(which will also have to ignore thehostandschemeparams since you can't add a host name to a relative link). But iforiginis anything else, we can try to figure out what the absolute path tooriginis and then generate a relative link from there.This way if
LinkRootis set to something like/a/b, thepathargument (the link we want to get) is../../x, and theoriginis set to something likec/d.md, we'll return a link like/a/xtaking into account theLinkRootand crawling up from the origin. So it'll return an absolute link, but one that was generated from the origin and relative path.Give me a sanity check here, I might be overthinking - does that all make sense?
JoshClose commentedon Feb 20, 2021
lol, I do think you're over complicating it. Browsers will handle
foo,./foo, and.../foofor you. I think if a relative URL doesn't start with/or our special case~/, then you leave it alone. Maybe I'm missing something though.JoshClose commentedon Feb 20, 2021
I just created a pull request. #171
This allow for
~/links in markdown. This is temporary until your feature is complete.I believe the
RelativeUrlclass may be of use in the stuff you're working on.daveaglick commentedon Feb 21, 2021
Thanks for the PR! I've merged it and new releases of Statiq Framework and Statiq Web are rolling out now. The relative path helper you wrote is going to come in very handy for the new module too, so I'm hoping to get that completed today or tomorrow.
JoshClose commentedon Feb 21, 2021
Awesome! I believe that's the only thing that was stopping me from releasing my site on Statiq. Statiq will make updating my documentation site so much easier to update.
JoshClose commentedon Feb 22, 2021
I created another pull request that fixes an issue with
RelativeUrl.#175
JoshClose commentedon Feb 22, 2021
I was going to add the query and fragment in
LinkGenerator.GetLink, but decided it would be better if you do that. My site usesRelativeUrlinstead ofGetLinknow, which is where I came across that bug.Fixed LinkGenerator behavior when using query and/or fragment compone…
Added `makeAbsolute` parameters to `IExecutionState.GetLink()` extens…