Skip to content
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

Change absolute link to relative link during doc generation #8641

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion crates/doc/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,15 @@ impl DocBuilder {
}
} else {
let name = path.iter().last().unwrap().to_string_lossy();
let readme_path = Path::new("/").join(&path).display().to_string();
let readme_path = Path::new("/")
.join(&path)
.display()
.to_string()
.trim_start_matches('/')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this step seems redundant because we first join then remove the leading /

.to_owned();
let slash_count = readme_path.chars().filter(|&c| c == '/').count();
let prefix = "../".repeat(slash_count);
let readme_path = format!("{prefix}{readme_path}/index.html");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh, unclear what this does.

Copy link
Author

@glazec glazec Sep 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattsse Thanks for the comment. I optimized the code in the new commit. The code here transform the path into a relative path which can be used in a web server.

                // Generate a relative path to "index.html" based on the current directory depth and
                let name = path.iter().last().unwrap().to_string_lossy();
                let depth = path.components().count();
                let mut prefix = PathBuf::new();
                for _ in 0..depth {
                    prefix.push("..");
                }
                let readme_path = prefix.join(&path).join("index.html");
                let readme_path_str = readme_path.to_string_lossy();
                readme.write_link_list_item(&name, &readme_path_str, 0)?;
                self.write_summary_section(summary, &files, Some(&path), depth + 1)?;

readme.write_link_list_item(&name, &readme_path, 0)?;
self.write_summary_section(summary, &files, Some(&path), depth + 1)?;
}
Expand Down
12 changes: 10 additions & 2 deletions crates/doc/src/writer/as_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,16 @@ impl AsDoc for Document {
.ok()
.unwrap_or(path),
);
Markdown::Link(&base_doc, &path.display().to_string())
.as_doc()
Markdown::Link(
&base_doc,
&format!(
"../../../{}",
path.display()
.to_string()
.trim_start_matches('/')
),
)
.as_doc()
})
})
.transpose()?
Expand Down
Loading