Skip to content

Link features #170

Open
Open

Description

@JoshClose

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?

Activity

  1. added
    ⚠️ Bug
    on Feb 16, 2021
  2. daveaglick commented on Feb 16, 2021

    @daveaglick
    Member

    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.

  3. daveaglick commented on Feb 16, 2021

    @daveaglick
    Member

    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.

  4. JoshClose commented on Feb 16, 2021

    @JoshClose
    ContributorAuthor

    I'll take a look. I'm sure I'll have a lot of questions.

  5. JoshClose commented on Feb 17, 2021

    @JoshClose
    ContributorAuthor

    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 ~.

  6. daveaglick commented on Feb 18, 2021

    @daveaglick
    Member

    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.

  7. JoshClose commented on Feb 18, 2021

    @JoshClose
    ContributorAuthor

    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.

  8. daveaglick commented on Feb 18, 2021

    @daveaglick
    Member

    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.

  9. JoshClose commented on Feb 18, 2021

    @JoshClose
    ContributorAuthor

    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.

  10. daveaglick commented on Feb 18, 2021

    @daveaglick
    Member

    It's my favorite XKCD - mostly because it constantly happens to me 😂

  11. JoshClose commented on Feb 18, 2021

    @JoshClose
    ContributorAuthor

    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.

  12. JoshClose commented on Feb 18, 2021

    @JoshClose
    ContributorAuthor

    Never mind. I can set the TextExecutionContext.Settings and it works.

  13. daveaglick commented on Feb 18, 2021

    @daveaglick
    Member

    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.

  14. JoshClose commented on Feb 18, 2021

    @JoshClose
    ContributorAuthor

    RenderMarkdown has this line in it

    if (prependLinkRoot && document.ContainsKey(Keys.LinkRoot))

    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?

  15. JoshClose commented on Feb 18, 2021

    @JoshClose
    ContributorAuthor

    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. ;)

  16. 10 remaining items

  17. JoshClose commented on Feb 20, 2021

    @JoshClose
    ContributorAuthor

    A relative link wouldn't need any rewriting

    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.

  18. daveaglick commented on Feb 20, 2021

    @daveaglick
    Member

    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).

  19. JoshClose commented on Feb 20, 2021

    @JoshClose
    ContributorAuthor

    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.

  20. daveaglick commented on Feb 20, 2021

    @daveaglick
    Member

    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.

  21. JoshClose commented on Feb 20, 2021

    @JoshClose
    ContributorAuthor

    Ok. I'll make a pull request for my relative url stuff that I think could help you.

  22. daveaglick commented on Feb 20, 2021

    @daveaglick
    Member

    Awesome! I can always ship that out sooner if I get held up.

  23. daveaglick commented on Feb 20, 2021

    @daveaglick
    Member

    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?

  24. JoshClose commented on Feb 20, 2021

    @JoshClose
    ContributorAuthor

    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.

  25. JoshClose commented on Feb 20, 2021

    @JoshClose
    ContributorAuthor

    I just created a pull request. #171

    This allow for ~/ links in markdown. This is temporary until your feature is complete.

    I believe the RelativeUrl class may be of use in the stuff you're working on.

  26. daveaglick commented on Feb 21, 2021

    @daveaglick
    Member

    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.

  27. JoshClose commented on Feb 21, 2021

    @JoshClose
    ContributorAuthor

    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.

  28. JoshClose commented on Feb 22, 2021

    @JoshClose
    ContributorAuthor

    I created another pull request that fixes an issue with RelativeUrl.

    #175

  29. JoshClose commented on Feb 22, 2021

    @JoshClose
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ⚠️ Bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions