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
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.
Reacted by Roberto Prevato, Marcelo Salloum dos Santos, Spencer Hann, Glenn Jocher, Leonard Sheng Sheng Lee, Dmitry Tokarev and locrianz
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.
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.
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.
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:
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.
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/).
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.
@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.
@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.
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
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.
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)).
Reacted by Dougal Matthews, bjan-sag, Santatic, Leonard Sheng Sheng Lee, Jake and Brian Vanderwende
Activity
d0ugal commentedon Nov 6, 2014
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.
lovelydinosaur commentedon Nov 6, 2014
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.
d0ugal commentedon Nov 6, 2014
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"intutorial/hello-world.mdwouldn't make sense without the context of knowing the root. So, for example, this wont work on github's markdown rendering AFAICT.lovelydinosaur commentedon Nov 6, 2014
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
d0ugal commentedon Nov 7, 2014
Seems reasonable, it is a bit of a tradeoff but worthwhile. So I'd be happy to see this change happen.
waylan commentedon Feb 20, 2015
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 (usingfile://) 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:
/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/buthttpor similar and also not be altered.http://localhost/) and production (eg:http://pythonhosted.org/packagename/).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.
jodeleeuw commentedon Feb 20, 2015
I think (1) from your list is exactly how it would (and should) work.
waylan commentedon Feb 21, 2015
@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.
3 remaining items
zoubin commentedon Dec 18, 2017
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?
waylan commentedon Dec 18, 2017
@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.zoubin commentedon Dec 18, 2017
Thanks for your response.
../proj_2will be OK. 👍A few tweaks to relative path handling
A few tweaks to relative path handling
Cleanup and refactoring of new structure
csandanov commentedon Sep 7, 2018
One thing I've noticed with relative URLs is that google doesn't get it, if you link
../articleonhttps://example.com/docs/faq(withsite_url: https://example.com/docs), googlebot will try to indexhttps://example.com/docs/faq/articleRevert "manifest: fixed images"
majkinetor commentedon Feb 2, 2021
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.
foosel commentedon Jul 29, 2023
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_urlinto account and generates site relative URLs from something likesite:images/foo.png. Also works for site relative URLs in links ([foo](site:path/to/foo)).Internal Refactor of Pages, Files, and Navigation (mkdocs#1504)