-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix relative links to README.md files #1921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Fixes #984 |
@ehuss Is this change easy to review and accept? |
I would prefer to not use regular expressions to translate the links. Can the translation be done in |
Would love to see this merged. |
@CaydenPierce would you be able to make the requested improvement to the original PR, using a more robust/performant matching algorithm than regular expression matching? |
Hi, I've been reading the discussions on this topic and I'm also interested in seeing mdBook incorporate a solution for this issue.
I had a look and came up with something that extends the existing regular expression in diff --git a/src/utils/mod.rs b/src/utils/mod.rs
index 9156916..d0cda17 100644
--- a/src/utils/mod.rs
+++ b/src/utils/mod.rs
@@ -95,7 +95,7 @@ pub fn unique_id_from_content(content: &str, id_counter: &mut HashMap<String, us
fn adjust_links<'a>(event: Event<'a>, path: Option<&Path>) -> Event<'a> {
static SCHEME_LINK: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[a-z][a-z0-9+.-]*:").unwrap());
static MD_LINK: Lazy<Regex> =
- Lazy::new(|| Regex::new(r"(?P<link>.*)\.md(?P<anchor>#.*)?").unwrap());
+ Lazy::new(|| Regex::new(r"(?P<link>.*?)(?P<readme>README)?\.md(?P<anchor>#.*)?").unwrap());
fn fix<'a>(dest: CowStr<'a>, path: Option<&Path>) -> CowStr<'a> {
if dest.starts_with('#') {
@@ -126,7 +126,16 @@ fn adjust_links<'a>(event: Event<'a>, path: Option<&Path>) -> Event<'a> {
}
if let Some(caps) = MD_LINK.captures(&dest) {
- fixed_link.push_str(&caps["link"]);
+ let link = &caps["link"];
+ fixed_link.push_str(link);
+ // "Links to README.md will be converted to index.html"
+ if let Some(readme) = caps.name("readme") {
+ if link.is_empty() || link.ends_with('/') {
+ fixed_link.push_str("index");
+ } else {
+ fixed_link.push_str(readme.as_str());
+ }
+ }
fixed_link.push_str(".html");
if let Some(anchor) = caps.name("anchor") {
fixed_link.push_str(anchor.as_str()); Several questions I can think of:
|
Maybe |
That would work great, but the canonical address should be recorded in the HTML to prevent duplicate content for indexers like online search engines. |
Looking forward to seeing this fixed. Thanks! |
Hey all, what's the status on getting this fix in? |
This commits explicitly sets many configuration options for the mdbook generation. In particular, for generic options (see https://rust-lang.github.io/mdBook/format/configuration/general.html#build-options): - 'create-missing' is set to false. This will make the build process exit with an error if any files do not exist in 'SUMMARY.md'. - 'use-default-preprocessors' is set to false. This disables the default preprocessors ('links' & 'index'). The 'index' preprocessor converts all chapter files named 'README.md' into 'index.md', but there is a catch: relative links are not dynamically updated (see rust-lang/mdBook#1921), leading to errors at build generation. We can live just fine without that plugin. The 'links' preprocessor expands the {{ #playground }}, {{ #include }}, and {{ #rustdoc_include }} handlebars helpers in a chapter to include the contents of a file. We can probably live without too (although it could be re-enabled). And for specific "linkcheck" options (see https://github.com/Michael-F-Bryan/mdbook-linkcheck/blob/v0.7.7/README.md?plain=1#L64-L125): - 'optional' is set to true. This will output a warning if the plugin is not installed, rather than failing to build entirely. - 'warning-policy is set to 'warn'. This changes the level of messages to warnings, rather than errors that interrupt the build process. - 'traverse-parent-directories' is set to false. This disallows linking to files outside of the book's root directory. - 'exclude' is set to some links. This excludes the linkchecking from running on those. They were chosen after trial and error.
This commits explicitly sets many configuration options for the mdbook generation. In particular, for generic options (see https://rust-lang.github.io/mdBook/format/configuration/general.html#build-options): - 'create-missing' is set to false. This will make the build process exit with an error if any files do not exist in 'SUMMARY.md'. - 'use-default-preprocessors' is set to false. This disables the default preprocessors ('links' & 'index'). The 'index' preprocessor converts all chapter files named 'README.md' into 'index.md', but there is a catch: relative links are not dynamically updated (see rust-lang/mdBook#1921), leading to errors at build generation. We can live just fine without that plugin. The 'links' preprocessor expands the {{ #playground }}, {{ #include }}, and {{ #rustdoc_include }} handlebars helpers in a chapter to include the contents of a file. We can probably live without too (although it could be re-enabled). And for specific "linkcheck" options (see https://github.com/Michael-F-Bryan/mdbook-linkcheck/blob/v0.7.7/README.md?plain=1#L64-L125): - 'optional' is set to true. This will output a warning if the plugin is not installed, rather than failing to build entirely. - 'warning-policy' is set to 'warn'. This changes the level of messages to warnings, rather than errors that interrupt the build process. (No effective change: this was already the default value) - 'traverse-parent-directories' is set to false. This disallows linking to files outside of the book's root directory. (No effective change: this was already the default value) - 'exclude' is set to some links. This excludes the linkchecking from running on those. They were chosen after trial and error.
Fixes #1920
I suggest adding to the 'index' preprocessor a call to swap the filenames in links in the chapters' contents.