Add absolute links support#1802
Conversation
9374e52 to
addf00d
Compare
|
Hey, Is there any update on this one? This fixes the exact issue im having. Would be awesome if we could get this merged/looked at again. Thanks |
|
I can fix the merge conflict. Though, I still need someone to review/merge it. |
|
You'd be an actual hero if you did, thank you. I replied on the issue thread too so hopefully someone notices. |
Yes, this is long overdue. I currently have to resort to a templating engine to handle absolute paths. I use absolute paths almost exclusively as they don't require massive editing when I move a chapter up/down folder levels. |
addf00d to
d9c5f10
Compare
|
Bump @ehuss |
ehuss
left a comment
There was a problem hiding this comment.
Thanks for the PR!
I think it is a little unfortunate to need to add a new config setting for this, but I don't see much of a way around it.
This will need some tests to be added.
It looks like links on the print page aren't using the adjusted links.
It also looks like there are some merge conflicts.
| pub fn render_markdown_with_path( | ||
| text: &str, | ||
| curly_quotes: bool, | ||
| path: Option<&Path>, | ||
| abs_url: Option<&String>, | ||
| ) -> String { |
There was a problem hiding this comment.
Since this is a public API, it can't be changed without a semver break. I think as a workaround, there will need to be another function added which takes the new parameter, and render_markdown_with_path can call it with None. We can then drop the new (or old) function in the future.
There was a problem hiding this comment.
Done, I also left a comment to merge those functions in the future.
| } | ||
| .into(); |
There was a problem hiding this comment.
I'm not sure what the .into() is here for, but I don't think it is necessary?
| } | |
| .into(); | |
| }; |
| fixed_link = match abs_url { | ||
| Some(abs_url) => format!("{}{}", abs_url.trim_end_matches('/'), &fixed_link), | ||
| None => fixed_link, | ||
| } |
There was a problem hiding this comment.
Just a minor opinion on style, I think it might be a little clearer to use an if let instead of a match. It is 1 less line and makes it a little clearer that there is "no change" when the value is None.
| fixed_link = match abs_url { | |
| Some(abs_url) => format!("{}{}", abs_url.trim_end_matches('/'), &fixed_link), | |
| None => fixed_link, | |
| } | |
| if let Some(abs_url) = abs_url { | |
| fixed_link = format!("{}{}", abs_url.trim_end_matches('/'), &fixed_link); | |
| } |
| /// Prepend the `site_url` in links with absolute path. | ||
| pub use_site_url_as_root: bool, |
There was a problem hiding this comment.
Hm, so technically adding this is a breaking change since HtmlConfig is in the public API. However, we have been adding new fields to this struct for a while now, and nobody has complained. That is something we should definitely fix in the future, but for now I guess we can let it slide. 😦
| navigation links and script/css imports in the 404 file work correctly, even when accessing | ||
| urls in subdirectories. Defaults to `/`. If `site-url` is set, | ||
| make sure to use document relative links for your assets, meaning they should not start with `/`. | ||
| - **use-site-url-as-root:** Prepend the `site_url` in links with absolute path. |
There was a problem hiding this comment.
Can you also add this to the TOML summary up above?
There was a problem hiding this comment.
links with absolute path
As a user, I would wonder "what links are those"? It could be
- links generated by templates, such as those referencing style sheets and JavaScript code
- links generated between pages of the book
- links I insert using Markdown syntax
| fixed_link.push_str(&caps["link"].trim_start_matches('/')); | ||
| fixed_link.push_str(".html"); | ||
| if let Some(anchor) = caps.name("anchor") { | ||
| fixed_link.push_str(anchor.as_str()); | ||
| } | ||
| } else if !fixed_link.is_empty() { | ||
| // prevent links with double slashes | ||
| fixed_link.push_str(&dest.trim_start_matches('/')); | ||
| } else { | ||
| fixed_link.push_str(&dest); | ||
| }; | ||
| return CowStr::from(fixed_link); | ||
| if dest.starts_with('/') || path.is_some() { | ||
| if let Some(abs_url) = abs_url { | ||
| fixed_link = format!( | ||
| "{}/{}", | ||
| abs_url.trim_end_matches('/'), | ||
| &fixed_link.trim_start_matches('/') | ||
| ); | ||
| } | ||
| } | ||
| return CowStr::from(format!("{}", fixed_link)); |
There was a problem hiding this comment.
I needed to add changes to this function because of leading double slashes when the path and abs_url var are defined. I've added some tests to make sure it only affects those cases.
| text: &str, | ||
| curly_quotes: bool, | ||
| path: Option<&Path>, | ||
| abs_url: Option<&String>, |
There was a problem hiding this comment.
The &String type should normally not be used — &str is more flexible for the callers since they can pass in both string literals (that would simplify the tests below) and borrowed owned strings.
Would it not be better to add a syntax to let links use the I'm thinking of something like To back to [the root]($path_to_root/index.md)The |
|
site-url value in book.toml did not work for me as far as I tried with mdbook v0.4.29
I can not rely on base href to solve all links and references use cases, dots in paths does not always help. To solve this I made some changes in: Changes can be seen in this fork (updated to v0.4.31) : master...JesusPerez:mdBook:master Obviously to complete the absolute paths in the pages I use a variable {{baseurl}} as a prefix of the href and src links. [Introduction]({{urlbase}}introduction.md)
 |
KFearsoff
left a comment
There was a problem hiding this comment.
The code looks good to me, and the tests run, but unfortunately I wasn't able to actually confirm on the test_book the repo has that this PR fixes the issue. Is there some issue introduced in the latest commits that breaks this?
Confirmed here as well. Not working here: https://github.com/calvinbrewer/mdBook/tree/add_absolute_links_support with the configuration added as reference |
Sorry If I'm saying something wrong, but I opened this PR more than a year ago. |
I've added an example in the test_book In addition, from a usability perspective, I'd assume the left nav items to also include the |
I have successfully built your branch, and the HTML output from your example seems to be producing the expected results. In ....
<a href="/docs/prefix.html">This</a> should link to prefix.md.
....IMO, this outcome aligns with our assumptions. Could you please share your build results? Maybe I missed something.
My intention with this particular changes was not to address all those cases. Regarding the navigation links, they are generated based on |
Hmm. I've tried doing this by myself, and after you wrote a response I checked again with the exact inputs you've provided. Here's a patch that I'm got for testing: diff --git a/test_book/book.toml b/test_book/book.toml
index a305007..91e8c9d 100644
--- a/test_book/book.toml
+++ b/test_book/book.toml
@@ -9,6 +9,8 @@ edition = "2018"
[output.html]
mathjax-support = true
+site-url = "/docs/"
+use-site-url-as-root = true
[output.html.playground]
editable = true
diff --git a/test_book/src/individual/README.md b/test_book/src/individual/README.md
index 67a1e9d..fceb065 100644
--- a/test_book/src/individual/README.md
+++ b/test_book/src/individual/README.md
@@ -2,6 +2,8 @@
This contains following tags:
+[Prefix](/prefix.md)
+
- Headings
- Paragraphs
- Line breaksI then run |
|
@KFearsoff to view the results, it is necessary to execute the command You can check it here: Line 67 in c1d622e |
Oh. Ohhh. Yeah, I can confirm this works. This is... pretty unfortunate, though. I think |
|
@KFearsoff, I believe this constitutes a reasonable usage. The serve mode is designed exclusively for development purposes and is tasked solely with hosting the book files. I agree that enhanced documentation would be beneficial in this context though. Another aspect to consider carefully is the content of It would be good to have more feedbacks on it before any change. |
|
Just in case if it helps I updated my mdBook with absolute links via site-url based in current mdBook master branch (v0.4.40 f480534) I only fixed a couple of files, maybe it is not enough but it seems to work for me. You can clone it, build and try following how-to-use-it notes with sitefix-book as example. For instance I have tested absolute URL with hostnames and schemas and it works ! I have to deal with multi-books services with auth, for me it is critical to support absolute links. Let me know if I can help on this. |
|
What needs to be done for this feature to be merged? |
|
☔ The latest upstream changes (possibly #2681) made this pull request unmergeable. Please resolve the merge conflicts. |
Refactors multiple functions to replace `Option<&String>` with `Option<&str>` for improved readability and adherence to Rust best practices. Simplifies `render_markdown_with_path` to remove unused `abs_url` parameter and introduces `render_markdown_with_abs_path` for enhanced link rendering flexibility. Fixes test cases to align with the updated parameter types.
2336e6a to
7c42e4a
Compare
|
Three years and I'm still relying on a templating engine to put in the correct root for links. Will this feature ever get into production? |
Seconded |
Port of the 0.4.x site-url absolute-links patch to the 0.5 crates/ layout, toward upstreaming as PR rust-lang#1802. When output.html.site-url is set, internal links and assets are emitted as absolute URLs anchored at site-url, so the book works under a sub-path (e.g. /cdcidao/) regardless of page depth. - html/tree.rs: fix_link/fix_html_link rewrite ./ content, image and raw-HTML links to {site_url}...; schemes and fragments untouched - html_handlebars/hbs_renderer.rs: path_to_root = site_url for normal and index pages; base_url = site_url only for the toc.html iframe (removed before the per-chapter clone so it cannot leak) - html_handlebars/helpers/resources.rs: {{resource}} honors an explicit path_to_root from data (absolute assets) with stock fallback - html/print.rs: print page honors site-url; internal cross-refs still fold to #anchors, non-chapter links keep absolute form - cmd/serve.rs: --preserve-site-url flag; serve still forces site-url to / for local preview but logs the override - tests/testsuite/rendering*: site_url fixture + tests (content, assets, print, no <base> leak, no-regression without site-url) - guide: document the serve flag and the renderer behavior
|
This problem is still live — Since this PR has been waiting on the author for a while and the codebase has since moved to the It covers each point from your review on this PR:
The original approach here is yours — the new PR is marked as superseding this one and credits it. Happy to fold in any feedback there. cc @ehuss |
Prepend the
site-urlin links with absolute path.Problem:
If we host in the book in a sub page we lost the references for the relative root. We already have the
site-urlin configs, but we do not use it. If we want to link a page in the root of the book, we need to prepend a lot of../to reach the actual root, and if we move pages these relative refs could lead us to dead links.Proposal
Add a new flag
use-site-url-as-rootin theHtmlConfig.The changes should have backward compatibility, we'll need to enable the
use-site-url-as-rootflag in[output.html]section inside thebook.tomlfile to use the new feature.TODO:
Closes: #1764