Skip to content

Allow absolute URLs #192

Closed
Closed

Description

@jodeleeuw

One thing I'm finding frustrating in crafting my documentation is the lack of support for absolute URLs. Say I've got a directory structure like this:

docs
|-- tutorial/
|---- hello-world.md
|-- api /
|---- foo.md

If I want to link to foo.md from hello-world.md then my link needs to be [foo function](../api/foo.md) but it would be easier if I could just do [foo function](/api/foo.md) and not worry about where I am in the directory tree as I'm writing the docs.

Activity

  1. d0ugal commented on Nov 6, 2014

    @d0ugal
    Contributor

    Yeah, so this is annoying. I agree. My main concern here is that I'm not sure what other Markdown parsers would make of this. How do they handle links starting with /? Ideally we don't want to end up with Markdown files that are incompatible with other systems.

  2. lovelydinosaur commented on Nov 6, 2014

    @lovelydinosaur
    Collaborator

    The absolute links are surely going to be valid for any markdown parsers - the only difference I'd expect to see is that with the relative style you'll get links you can follow in the editor, but with the absolute links they wont be.

  3. d0ugal commented on Nov 6, 2014

    @d0ugal
    Contributor

    well, not necessarily. We know that / means the location of the docs directory. Another markdown parser doesn't know that, so a "/api/foo.md" in tutorial/hello-world.md wouldn't make sense without the context of knowing the root. So, for example, this wont work on github's markdown rendering AFAICT.

  4. lovelydinosaur commented on Nov 6, 2014

    @lovelydinosaur
    Collaborator

    So, for example, this wont work on github's markdown rendering AFAICT.

    Indeed, they won't hyperlink to a sensible location, but they should still work and be rendered as links.

    Single data point: https://gist.github.com/tomchristie/746af50f6ccb3930f430

  5. d0ugal commented on Nov 7, 2014

    @d0ugal
    Contributor

    Seems reasonable, it is a bit of a tradeoff but worthwhile. So I'd be happy to see this change happen.

  6. modified the milestone: 1.0.0 on Nov 7, 2014
  7. waylan commented on Feb 20, 2015

    @waylan
    Contributor

    Personally, I prefer relative URLs only. For example, when documenting a Python project, I may want to upload a set of static HTML files to http://pythonhosted.org (as provided by PyPI). I generate the static HTML, zip it up and upload it. However, the document root on pythonhosted.org is http://pythonhosted.org/packagename/ (as opposed to readthedocs which uses a subdomain: http://packagename.readthedocs.org). That means that when serving the files locally (http://localhost/) the root is different than on pythonhosted.org. And of course, if I'm browsing the local static files (using file://) the root is different there and could be different for the same project on different systems (depending on where my project is locally stored on each system). Additionally, as @tomchristie demonstrated in a previous comment, the root is also different on GitHub (as well as other source code hosting sites) The only way that I have found to get consistent results is to use relative links. Yes, it can be a little annoying to always know exactly where you are in relation to the entire document structure, but it works everywhere.

    A few possible workarounds:

    1. Expect all URLs that start with / to be internal absolute URLs from the doc root and convert them to relative URLs saving the author the hassle. The author can still use relative URLs (which do not get altered) for total control if desired. Of course, external links would not start with / but http or similar and also not be altered.
    2. Same as (1) except that a preface is prepended to absolute URLs from the configuration settings keeping the URLs absolute. But that preface could be different in different contexts, which means the setting would need to change for development (eg: http://localhost/) and production (eg: http://pythonhosted.org/packagename/).
    3. Add some special syntax to Markdown (through a Markdown extension) which is used for internal links only. As it knows about internal links it always does the "right thing" (which may be absolute or relative URLs). See the wikilinks extension for an example (it even accepts a callable to custom format the URLs which could be used here).

    Both (2) and (3) need to know if they are in development or production, so I would say (1) is the simplest. Although (3) could be used to generate relative URLs obviating the need for the document author to have to deal with that. The one advantage that (3) (with relative URLs) has over (1) is the URLs are only altered when specifically identified as internal links (using the special syntax) and regular Markdown style links are left alone. Of course, that also makes the source documents less portable to other Markdown systems and/or parsers.

  8. jodeleeuw commented on Feb 20, 2015

    @jodeleeuw
    Author

    I think (1) from your list is exactly how it would (and should) work.

  9. waylan commented on Feb 21, 2015

    @waylan
    Contributor

    @jodeleeuw I tend to agree, but it just feels a little to much like magic. Some users might be surprised that their carefully crafted URLs have been altered. Perhaps if it was a config setting which would be off by default (the default should be less surprising) I wouldn't mind so much. But I wouldn't want to write the documentation to explain that setting.

    In any event, I think the possible solutions distracted me from the point of my comment. Which is, simply put: the doc root is not always the same as the server root. Whatever solution (including doing nothing -- requiring users to craft their own relative URLs) is chosen must take that into consideration.

  10. modified the milestones: 0.15.0, 1.0.0 on Jun 30, 2015
  11. modified the milestone: 0.15.0 on Jan 14, 2016
  12. 3 remaining items

  13. zoubin commented on Dec 18, 2017

    @zoubin

    I run into some case where absolute URLs are necessary.
    We host multiple docs projects on the same host, say https://docs.awesome.com/.
    Projects are assigned different paths. For example, https://docs.awesome.com/proj_1/.
    So, with relative URLs, it seems impossible to write a link to other projects, i.e., documents in proj_1 can't link to proj_2, unless the hostname is included (which should be avoided).

    Are there workarounds?

  14. waylan commented on Dec 18, 2017

    @waylan
    Contributor

    @zoubin, as a reminder, MkDocs only alters URLs which point to internal Markdown documents. Specifically, relative URLs that end with the .mdextension. If you are linking to anything else (anything outside of the current project), then you should be using non-internal URLs (in fact a future version may raise an error if you were to use internal URLs). Therefore, use a relative URL pointing to the already converted HTML document. So, for your example, to link from the homepage of proj_1 to proj_2, use ../proj_2/. Because the URL doesn't end in .md, MkDocs will leave it as-is.

  15. zoubin commented on Dec 18, 2017

    @zoubin

    Thanks for your response. ../proj_2 will be OK. 👍

  16. added 2 commits that reference this issue on May 8, 2018
    73558ff
    8e205e5
  17. added a commit that references this issue on Jun 8, 2018
    ac6581b
  18. csandanov commented on Sep 7, 2018

    @csandanov

    One thing I've noticed with relative URLs is that google doesn't get it, if you link ../article on https://example.com/docs/faq (with site_url: https://example.com/docs), googlebot will try to index https://example.com/docs/faq/article

  19. added a commit that references this issue on Nov 22, 2018
  20. majkinetor commented on Feb 2, 2021

    @majkinetor

    I handle absolute URLs with ease by introducing url_prefix variable that I can use with macros plugin such as [[some page]({{url_prefix}}/some/page).

    I inject this during the build time via environment var ( example for GitHub pages.)

    Using non-absolute URLs is horror when moving docs around. Using url_prefix you can have a single location for html cross-references that you can use in entire mkdocs site if added to each page via some automatic footer mechanism (again, macros can do that via {{ include footer.md }}). This allows you to move documents around and reorganize them with single page containing all relevant links that can be easily fixed. I combine this with link checker to make sure everything is in place.

    This scheme allows you to have multiple environments each having its own prefix and all links working correctly. See build for details.

  21. foosel commented on Jul 29, 2023

    @foosel

    Just as an FYI, I solved this issue for my own workflow with a custom plugin. No link validation, but at least a solution that takes site_url into account and generates site relative URLs from something like site:images/foo.png. Also works for site relative URLs in links ([foo](site:path/to/foo)).

  22. added a commit that references this issue on Jan 18, 2025
    34ef3ca
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions