Skip to content

Add support for anchor tags #185

Closed
Closed

Description

@lenisha

Links that point to non existent anchors https://github.com/lenisha/linkchecker-test/blob/main/about.html#L92 are not detected

<a href="https://github.com/Microsoft/onnxruntime#api-documentation" target="_blank" class="link">

Activity

  1. pawroman commented on Mar 16, 2021

    @pawroman
    Member

    At this point, they're not supported.

    This is a bit of a tricky one. Anchors are really the properties of the document contents, not really being integral parts of the URLs.

    What I really mean by that is this:

    • You can't tell if an anchor is valid by merely making a request (you can only check the URL path without the anchor)
    • To verify anchors, you'd have to parse the retrieved HTML and check they're there (but we already do it for link extraction using html5ever, a browser grade parser)

    So this might not be very straightforward, but should be perfectly doable. I'd welcome such a feature 😃

  2. added
    question
    enhancement
    help wanted
    and removed
    question
    on Mar 29, 2021
  3. changed the title [-]Are anchors supported?[/-] [+]Add support for anchor tags[/+] on Mar 29, 2021
  4. MichaIng commented on Jun 11, 2021

    @MichaIng
    Member

    To verify anchors, you'd have to parse the retrieved HTML and check they're there (but we already do it for link extraction using html5ever, a browser grade parser)

    HTMLs are parsed for link extraction on the input HTML documents, but not for the target HTML documents. So it would required to first check whether the target is actually an HTML document, then download it fully (currently it is only partially downloaded or not at all when using -X head) and then parse the target HTML for a matching id attribute.

    When support for internal links via file existence check is added (#21), then it could be at least cheaply added for those:

    • If the link/URL is an internal one, check only if the target file exists, relative to the input HTML file location or relative to the base path, when given and it is an absolute internal link.
    • Then, if the target is an HTML file, or a directory that contains an index.htm/index.html, check for a contained matching id attribute.

    In other cases, I think it's difficult if one does not want to imply much overhead for checking the URLs document type and downloading it completely. But nevertheless, the idea/feature itself would be awesome of course 🙂.

  5. MichaIng commented on Jun 12, 2021

    @MichaIng
    Member

    For reference the hint by @polarathene: #21 (comment)

    Another case where the difficulties with target downloads etc do not apply, and probably this is what you had in mind anyway, is to check for scroll links within the input HTMLs. so if there is an <a href="#heading">, to check whether there is an id="heading" as well. If such is wanted, it's good to create a generic method/function for it, so that URL fragments can be generally detected and HTMLs generally scanned for matching IDs, but at first implemented for such same-page scroll links, then depending on #21 for locally stored target HTML files, and gradually for online HTML documents, when this is found to be important enough to fully download the target, based on content-type: text/html or so. But I'm not sure if the content type header is reliable enough of if there is a risk that a large archive or video file is unintentionally downloaded 😄.

  6. polarathene commented on Jun 12, 2021

    @polarathene
    Contributor

    But I'm not sure if the content type header is reliable enough of if there is a risk that a large archive or video file is unintentionally downloaded

    Response headers should provide info about content size, completing a request in full (eg downloading a large resource) shouldn't be necessary and could be terminated early AFAIK?

  7. MichaIng commented on Jun 12, 2021

    @MichaIng
    Member

    completing a request in full (eg downloading a large resource) shouldn't be necessary and could be terminated early AFAIK?

    If you want to check whether an URL fragment is valid or not, you need to download the whole document of course, which is the whole issue I see here, as long as it's not about same-page links or local files. Currently the download is terminated early, reasonably, so currently it is impossible to apply this feature for remote HTML documents without changing the download behaviour based on such things like HTTP headers, which is why I think one needs to evaluate carefully if the effort is worth it or not 😉.

  8. polarathene commented on Jun 12, 2021

    @polarathene
    Contributor

    you need to download the whole document of course, which is the whole issue I see here

    Downloading a complete HTML document to operate on isn't much of a concern vs larger files which I thought was being raised as the concern but could be avoided.

    You make a good point though, especially with some HTML documents that are dependent on JS to populate the real content, such as React pages without any pre-rendering process involved. That'd require actually loading and running the JS..

    I haven't used lychee myself yet, so I'm not sure if it supports that well (anchor tag feature aside).

  9. lebensterben commented on Jun 12, 2021

    @lebensterben
    Member

    If other attributes of 'a' tag is provided, you can use that to help you determine whether to download that webpage, or whether it links to webpage, etc. But it gets complicated.

    https://html.spec.whatwg.org/multipage/links.html#links

  10. mre commented on Mar 28, 2022

    @mre
    Member

    Anchor tags are supported by https://github.com/killercup/link-checker and the implementation is done with a HashSet of anchor links per page. https://github.com/killercup/link-checker/blob/master/src/links.rs. Something to look into if we get around to adding it to lychee.

  11. lara-rium commented on Sep 26, 2022

    @lara-rium

    Sometimes anchors aren’t even in the HTML itself, like for static websites! Another approach could be to make the request with and without the anchor and compare the scroll location if that’s possible, it means making two requests though

  12. lebensterben commented on Sep 26, 2022

    @lebensterben
    Member

    make the request with and without the anchor and compare the scroll location if that’s possible

    that requires emulation of webpage rendering and is outside the scope of this project.

  13. 15 remaining items

  14. lara-rium commented on Jul 28, 2023

    @lara-rium

    thats fair enough but another major use case would be linking to documentation, for example swagger

    i think we should design the header format used in an independent way so that in the future we can let others set it easier

  15. mre commented on Jul 28, 2023

    @mre
    Member

    Sorry, I don't quite get it. If we find a fragment link in a document, we have to check if that fragment exists. Say we find "#helloworld" and there is a headline named "Hello World", for which a fragment was generated automatically by the hoster (GitHub, GitLab, Swagger (?)). Now the question is if that headline gets converted into "#helloworld" or "#hello-world" or something else. How can we design the header format in such a case? Don't we have to generate the same fragment link as the hoster?

    It's likely I might be missing something here.

  16. lara-rium commented on Jul 28, 2023

    @lara-rium

    Don't we have to generate the same fragment link as the hoster

    yes thats what i mean, the exact implementation would need a balance between verbosity and conciseness but for example something like

    [ { " ": "-" } ]

    this would replace in the content with - in the header, transforming hello world to hello-world, we'd likely need more configuration like escaping, casing, handling duplicates and so on

  17. mre commented on Jul 29, 2023

    @mre
    Member

    Ah, I understand now. Not sure how to find the right balance between simplicity and flexibility here. Open for suggestions for the configuration format.

  18. lara-rium commented on Jul 29, 2023

    @lara-rium

    we could use toml, i believe it supports arbitrary keys as string ("abc" = "xyz") which could be used for replacing characters

    we could take an "enum" (not sure toml supports them) for casing, options i can think of are camel case, snake case, kebab case, pascal case and macro case

    for escaping i think we already do percent encoding so not sure if theres additional configuration necessary

    lastly duplicates are a little tricky, we could have them provide a format template such as {anchor}-{n} where n is the index of the occurrence, we'd have to also let them set if its 0 indexed or 1 indexed and whether the first occurrence still needs this formatting (whether to use {anchor} or {anchor}-{0} for the first occurrence)

    or we could take an entirely different approach and direct users to use the crate for configuration, maybe having them pass a closure like |element| -> anchor, which is infinitely extensible but may be a dealbreaker for non-rust users or people that dont want to spend the time on that

  19. HU90m commented on Jul 31, 2023

    @HU90m
    Contributor

    should we provide a way for the user to configure this (probably the same way we pre-configure these websites)?

    I'm personally not keen on this idea.

    In an ideal world, fragment generation would be specified in the common mark spec, but it isn't and there are a few different common methods. I don't want to encourage xkcd № 927, especially if it involves adding significant complexity to the tool and configuration.

    I think hard coding the common generation methods in lychee (or in a separate crate) would help clearly document existing generation methods and hopefully help reduce, stop or even reverse their proliferation.

  20. HU90m commented on Aug 23, 2023

    @HU90m
    Contributor

    #1198 has been merged, so of the link types laid out in #185 (comment) Lychee now supports types 1 and 2.

  21. mre commented on Aug 27, 2023

    @mre
    Member

    Awesome progress! 🚀

    Here's our current architecture after the changes:
    image

    Supporting fragments in URLs (e.g. https://foo/bar.html#frag as in option 3 in the link types) won't be a small change, I guess.

    We'd probably have to make few changes to the architecture, because right now there's no way for the check step to "ask" for all links of a given input or even just ask if a link occurs in a given input (e.g. "is https://foo/bar.html#frag valid?").

    One way to go about it might be to fully decouple input handling from link checking (where inputs can be files or websites).

    The fragment cache is a basic version of that, but it's limited to fragments. I think we need a bigger cache for all inputs we encounter and a central entity, which manages this cache. We can think of it as an abstraction on top of the network and the file system, purpose-built for our use-case.

    It could lazy-load resources on demand and store the parsed information from inputs, which would be used by the rest of the system; so our parsed representation would be the ground-truth for the rest of the link checking. For each input, it would contain a big map of the URI of the input (i.e. the path or URL) and its parsed links/fragments.
    It should be fully async, and we will need read/write access throughout the program's runtime.

    Maybe this is even a graph problem, but I don't feel comfortable going down that route.
    In any case, we will need a lot of discussions to come up with a solid design.

    However we model it, a check to see if an input contains a link or fragment should be trivial from other parts of the program. We should not deal with ad-hoc resource-fetching within the checking code.

    @HU90m, any thoughts on this? Maybe I'm overcomplicating things.

  22. HU90m commented on Sep 3, 2023

    @HU90m
    Contributor

    That sounds like a splendid plan.

    A simplified view of the current flow could be illustrated as:

    current_arch

    I think an elegant implementation of this idea would be to remove the collector and move the extractors into the Client. The inputs are converted directly to Requests, with the only transformation being the de-globbing of globs. These initial requests will be given a depth of 0, depth being a new member of Request:

    pub struct Request {
        /// A valid Uniform Resource Identifier of a given endpoint, which can be
        /// checked with lychee
        pub uri: Uri,
    
        /// The resource which contained the given URI
        pub source: InputSource,
    
        // ...
    
        /// Depth of the link,
        /// where a depth of 0 is a link provided in the user input.
        pub depth: u32,
    }

    When the client handles a request it will fetch the file (form the file system or network). If successfully fetched, It will then parse the file extracting links and optionally fragments, if the file isn't in it's cache. After which, the file will be added to a cache in the client along with it's links and fragments. The extracted links will be converted to Requests with a depth of their origin file plus one, and be put back into the request channel to be handled later.

    There will be max_depth option that defaults to 1. If a request's depth is greater or equal to max_depth, it's extracted links won't be put back into the request channel.

    This gives us recursive checks for free, when max_depth >= 1! Another advantage is the possibility of combining the fragment and link extractors so that files are only parsed once.

    A simplistic illustration of the above:

    new_arch

    As it is possible for Client to produce more requests than it consumes, we should probably use mpsc::unbounded_channel.

  23. HU90m commented on Sep 3, 2023

    @HU90m
    Contributor

    Also I enjoyed the lychee talk, props!

  24. mre commented on Sep 4, 2023

    @mre
    Member

    Also I enjoyed the lychee talk, props!

    Thanks for watching and glad you liked it.

    Should we move the architecture discussion into a separate issue?

  25. HU90m commented on Sep 4, 2023

    @HU90m
    Contributor

    Should we move the architecture discussion into a separate issue?

    Let's!

  26. mre commented on Sep 7, 2023

    @mre
    Member

    Done. ✌️

  27. mre commented on Jan 28, 2024

    @mre
    Member

    Anchor support has landed now.
    Future improvements can be discussed in new issues and the architecture discussion at #1252.
    Big thanks to @HU90m, who single-handedly pulled this off.

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

    enhancementhelp wanted

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions