mdBook icon indicating copy to clipboard operation
mdBook copied to clipboard

site-url not respected in links

Open mimoo opened this issue 2 years ago • 17 comments

Since I set the site-url (or base url in other frameworks):

[output.html]
site-url = "/proof-systems/"

I would expect all markdown links to be rewritten to respect that in my book. They aren't :o

mimoo avatar Mar 09 '22 19:03 mimoo

All links in your book should be relative, so it shouldn't matter where it is located. The site-url is only needed for generating the 404 page which has to be able to work when loaded from any location.

If you are using absolute links, like /foo.md, then I recommend changing them to be relative (such as foo.md or ../foo.md). I'm not sure mdbook should rewrite absolute links, since they may legitimately be trying to link to a location outside of the book.

ehuss avatar Mar 09 '22 19:03 ehuss

in my case, I am moving the generated rust documentation into the /rustdoc folder in the book. So that on github pages, I can access both the book and the rustdoc.

I'm not sure mdbook should rewrite absolute links, since they may legitimately be trying to link to a location outside of the book.

I'm not sure I understand, all frameworks that expose a base-url do this: they rewrite all absolute links using the base url. What is a use case where you wouldn't want that?

mimoo avatar Mar 09 '22 21:03 mimoo

I'm not sure I understand, all frameworks that expose a base-url do this: they rewrite all absolute links using the base url. What is a use case where you wouldn't want that?

I'd second this.

Writing a large book with mdbook means that I'm constantly shifting chapters from one place to another, sometimes across folders or up/down levels. It would be a huge mess to manually fix all relative url links whenever I move a file.

Always using absolute url links resolve this issue nicely, resulting in a huge savings of time. I believe this feature is sorely needed.

Right now, I use a templating engine mdbook-tera and put {{rootUrl}}/ in front of all my url links, but it is not as good as built-in support.

schungx avatar Mar 10 '22 00:03 schungx

What is a use case where you wouldn't want that?

This probably isn't used in practice very often, but I can imagine a book that wants to link to other pages on the same website (that aren't part of a book). We don't use it for rust-lang, so I would be fine changing it, but it could possibly be a breaking change for other users, so it would need to wait for the next incompatible release.

ehuss avatar Mar 14 '22 17:03 ehuss

This probably isn't used in practice very often, but I can imagine a book that wants to link to other pages on the same website (that aren't part of a book)

yeah I'm not personally convinced, from a site-url standpoint everything else is "external" and should be linked with a fully-qualified URL

but it could possibly be a breaking change for other users, so it would need to wait for the next incompatible release.

no worries : )

mimoo avatar Mar 14 '22 18:03 mimoo

This probably isn't used in practice very often

Actually it is used very often for me.

It is not absolutely needed as I can always use ../../../some/path/doc.md instead of the much better /some/path/doc.md, but that means that I can never move files around as well as up/down levels without doing a global scan for broken links!

It happens a lot during periods of heavy revision.

If I move a folder up/down, then God forbid!

schungx avatar Mar 15 '22 00:03 schungx

Hey you all! Not sure if what I did solves your issue. But could you check #1802 and test it if you have some time? Thank you!

joaofreires avatar May 07 '22 19:05 joaofreires

What should I do to test an open unmerged PR? Never done this before...

or is it in the add_absolute_links_support branch?

schungx avatar May 08 '22 03:05 schungx

or is it in the add_absolute_links_support branch?

Yes, you can build the add_absolute_links_support branch and test from there.

Here's the repo/branch: https://github.com/joaofreires/mdBook/tree/add_absolute_links_support

Note: as described in the PR, you need to set the feature as true. So, inside the book.toml you will need to have something like:

[output.html]
site-url = "/some/path/"
use-site-url-as-root = true

joaofreires avatar May 08 '22 03:05 joaofreires

Seems to work just fine!

I particularly like the fact that it doesn't add the site url if going through mdbook serve, that makes things Just Work everywhere.

schungx avatar May 08 '22 04:05 schungx

@schungx , mdBook already set the site-url to / in the "serve mode". 😃

https://github.com/rust-lang/mdBook/blob/7c37dd5e8570ffc93701c1d6b6ffdd97b430467e/src/cmd/serve.rs#L79

joaofreires avatar May 08 '22 05:05 joaofreires

@schungx , mdBook already set the site-url to / in the "serve mode". 😃

With this change, I can actually remove mdbook-tera because I only used the templating engine in order to put in the correct url prefix. It makes the entire project much simpler to build and deploy!

@ehuss I wonder if this PR can be merged? Since it introduces a new option flag, it should not break any existing build.

schungx avatar May 08 '22 14:05 schungx

Now if I would ask for one more thing: make the #include command take paths that are based off src.

Right now, it is either relative path based off the current file, or an absolute path based off the file system root (which is extremely unusual). It is much more common to have include files offset from src.

schungx avatar May 08 '22 14:05 schungx

@schungx, the #include is part of the link preprocessor and it doesn't have access to the HTML configuration. It's possible to build a different preprocessor that does the "same", but adding your src at the preprocessor configuration (or change a lot the current one and add the prefix path. I guess that the easiest way to do it will apply it to all "link tags" ({{ #playground }}, {{ #include }}, and {{ #rustdoc_include }})).

If I have some free time I can implement this preprocessor or try to extend/change the current link preprocessor.

joaofreires avatar May 10 '22 01:05 joaofreires

The #include tag is what I use to automatically include a file containing URL links in abstract form, so that I can use simple link tags everywhere.

For example, I have links.md:

[`Foo`]: /path/to/foo.md
[`Bar`]: /path/to/bar.md
[baz]: /path/to/baz.md

And then in each file:

{{ #include ../links.md }}

Now I can use [`Foo`] and [`Bar`] and [baz] anywhere I like!

It works very well, except for having to always put in the right number of ../ to reach that particular file.

schungx avatar May 10 '22 03:05 schungx

Is there any update on this?

The PR/Branch proposed seems like a decent fix, and would resolve the issues im having too, but this seems to have died in the water?

Thanks

watsom27 avatar Sep 04 '22 09:09 watsom27

Bump please @ehuss

Thanks

watsom27 avatar Sep 06 '22 20:09 watsom27