mdBook icon indicating copy to clipboard operation
mdBook copied to clipboard

How to embed absolute links when serving from a subdirectory in a domain?

Open schungx opened this issue 5 years ago • 3 comments

Say, I have a link going to another page and I need it starting from the root:

[some url link](/path/to/page.md)

This gets compiled to a link to the correct page:

<a href="/path/to/page.html">some url link</a>

However, this doesn't work if I'm serving from a subdirectory:

http://example.com/my_book/path/to/page.html

The absolute url will skip over the subdirectory my_book and point to http://example.com/path/to/page.html which doesn't exist.

How can we specify that links should go to an absolute path based upon the root location of the book?

schungx avatar Jun 20 '20 05:06 schungx

Right now, I solved it by using https://github.com/avitex/mdbook-tera which is a pre-processor.

I added a variable in front of every URL link. The variable maps to the subdirectory name.

I am hoping for a simpler, not so hacky solution though.

schungx avatar Jun 20 '20 07:06 schungx

see https://github.com/rust-lang/mdBook/issues/1764

mimoo avatar Jul 26 '22 14:07 mimoo

Right now, I solved it by using https://github.com/avitex/mdbook-tera which is a pre-processor.

I added a variable in front of every URL link. The variable maps to the subdirectory name.

I am hoping for a simpler, not so hacky solution though.

@schungx Would you mind sharing exactly how you solved it in this way?

engeir avatar Aug 02 '24 11:08 engeir

@schungx Would you mind sharing exactly how you solved it in this way?

Oops, sorry, just realize that I have not responded to this. So, this is how I did it (one year too late):

  • Install mdbook-tera

  • Define a Tera variable in a context file, e.g. root_url = /my/book

  • In book pages, for links, use {{ root_url }}/path/to/doc.md

  • Bob's your uncle

You need this unless your root is really the book's root. In many sites it isn't.

schungx avatar Aug 18 '25 07:08 schungx

Seeing that version 0.5 is coming out... I wonder if there is now a solution to this issue?

schungx avatar Oct 02 '25 02:10 schungx