mkdocs icon indicating copy to clipboard operation
mkdocs copied to clipboard

relative url start with "../" in nav no longer works

Open akinmr opened this issue 4 years ago • 6 comments

When I tried to make a link to an external resource in nav section of mkdos.yml, using the following example, the actual output HTML omits "../" in the relative URL and I couldn't point to external resources out of mkdocs with relative URL. https://www.mkdocs.org/user-guide/configuration/#nav

The minimal mkdocs.yml to reproduce the problem is as follows:

site_name: Example
site_url: https://example.com/foo/

nav:
    - Parent: '../'
    - Sibling: '../some/dir/'
    - Home: './'
    - Index: 'index.md'

When I compile it with empty docs/index.md file, the navigation section in site/index.html became as follows:

<!-- Main navigation -->
<ul class="nav navbar-nav">
    <li class="navitem">
        <a href="./" class="nav-link">Parent</a>
    </li>
    <li class="navitem">
        <a href="some/dir/" class="nav-link">Sibling</a>
    </li>
    <li class="navitem">
        <a href="./" class="nav-link">Home</a>
    </li>
    <li class="navitem active">
        <a href="." class="nav-link">Index</a>
    </li>
</ul>

What I expect was that the link of "Parent" should point to "../"(= https://example.com/) and the link of Sibling should point to "../some/dir"(=https://example.com/some/dir/), which are outside of the generated doc.

I remember this way (Sibling case) worked in past, but when I recompiled the document with the latest mkdocs, it started emitting unexpected URL.

akinmr avatar Jan 25 '22 09:01 akinmr

I just encountered the same issue. Surprised such a killer bug has not received any response

bloer avatar Mar 24 '22 18:03 bloer

I confirm that https://github.com/mkdocs/mkdocs/pull/2296 (released in 1.2) changed this behavior.

The thing is, this usage was never supported and always showed warnings anyway:

WARNING -  A relative path to '../' is included in the 'nav' configuration, which is not found in the documentation files 

In MkDocs nav, relative links are always assumed to be links to Markdown documents within the site.

You can use this appoach instead:

site_url: https://example.com/known/absolute/path/

nav:
    - Sibling: '/known/absolute/some/dir/'

I am not sure if this will be revisited.

oprypin avatar Mar 25 '22 13:03 oprypin

The reason why I'm using this relative URL approach is that I'm deploying the same document to different path of the server, like /docs/docA/ and /docs_testing/docA/, in order to check the appearance before deploying it to the public, and want to make links to /docs/docB/ and /docs_testing/docB/ with single '../docB/'. I made this config because I found such '../' way in the example in the official user guide, and it worked before, even though it emits some warnings. I would be happy if this restriction can be switched off with some configuration in the mkdocs.yml. At least, the documentation should be updated if '../' is no longer supported.

akinmr avatar Mar 25 '22 14:03 akinmr

Oh wow, you're right! :disappointed:

https://github.com/mkdocs/mkdocs/blob/f725e225adf9b6cfab45042e44637f137f56d1bd/docs/user-guide/configuration.md?plain=1#L195-L206

oprypin avatar Mar 25 '22 14:03 oprypin

Is there any fix or workaround planned for this?

akinmr avatar Jun 23 '22 02:06 akinmr

I also would like this behaviour restored. My use case is wanting to deploy effectively two linked sites under the same base URL.

I want to do this so that each sub-site can have it's own navigation tabs, and to make a clear distinction between the two sub-sites by styling them with different colours and titles.

I can achieve the sub-sites easily, by having site1_mkdocs.yml and site2_mkdocs.yml, each of which build to a subdirectory (main/site1 and main/site2). The problem comes that I want each sub-site to link back to the other with a relative link, ../site1 and ../site2, and then I get the error and the URL stripped of the relative path.

aidanheerdegen avatar Sep 12 '22 05:09 aidanheerdegen

Also being bit by the same bug. In my case I want to link in my nav to some sphinx docs that are built and available via a relative URL locally. Absolute paths are not an option and neither are URLs.

psifertex avatar Oct 17 '22 18:10 psifertex

OK. I intend to fix this - I have linked a pull request, it just doesn't have tests yet.

oprypin avatar Oct 17 '22 20:10 oprypin