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

Fix wrong absolute link in doc generated markdown #8600

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
11 changes: 8 additions & 3 deletions crates/doc/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl DocBuilder {
path.clone(),
target_path,
from_library,
self.config.out.clone(),
self.root.join(&self.config.out),
)
.with_content(DocumentContent::Single(item), ident))
})
Expand Down Expand Up @@ -442,11 +442,16 @@ impl DocBuilder {
.transpose()?
.unwrap_or(summary_path);
readme.write_link_list_item(ident, &readme_path.display().to_string(), 0)?;
println!("if readme_path: {}", readme_path.display());
println!("if summary_path: {}", summary_path.display());
}
} else {
let name = path.iter().last().unwrap().to_string_lossy();
let readme_path = Path::new("/").join(&path).display().to_string();
readme.write_link_list_item(&name, &readme_path, 0)?;
let doc_root = self.out_dir();
let relative_path = path.strip_prefix(&self.root).unwrap_or(&path);
let full_path = doc_root.join("book/").join(relative_path);
let full_absolute_link = format!("file://{}/index.html", full_path.display());
readme.write_link_list_item(&name, &full_absolute_link, 0)?;
self.write_summary_section(summary, &files, Some(&path), depth + 1)?;
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/doc/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ impl Document {
context.insert(id, output);
}

pub fn full_output_path(&self) -> PathBuf {
self.out_target_dir.join(self.relative_output_path())
}

/// Read preprocessor result from context
pub fn get_from_context(&self, id: PreprocessorId) -> Option<PreprocessorOutput> {
let context = self.context.lock().expect("failed to lock context");
Expand Down
3 changes: 2 additions & 1 deletion crates/doc/src/preprocessor/contract_inheritance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ impl ContractInheritance {
if let DocumentContent::Single(ref item) = candidate.content {
if let ParseSource::Contract(ref contract) = item.source {
if base == contract.name.safe_unwrap().name {
return Some(candidate.target_path.clone())
let full_path = candidate.full_output_path();
return Some(full_path);
}
}
}
Expand Down
33 changes: 14 additions & 19 deletions crates/doc/src/writer/as_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::{
use forge_fmt::solang_ext::SafeUnwrap;
use itertools::Itertools;
use solang_parser::pt::{Base, FunctionDefinition};
use std::path::{Path, PathBuf};

/// The result of [`AsDoc::as_doc`].
pub type AsDocResult = Result<String, std::fmt::Error>;
Expand Down Expand Up @@ -126,34 +125,35 @@ impl AsDoc for Document {
ParseSource::Contract(contract) => {
if !contract.base.is_empty() {
writer.write_bold("Inherits:")?;

// we need this to find the _relative_ paths
let src_target_dir = self.target_src_dir();

let mut bases = vec![];
let linked =
read_context!(self, CONTRACT_INHERITANCE_ID, ContractInheritance);
for base in contract.base.iter() {
let base_doc = base.as_doc()?;
let base_ident = &base.name.identifiers.last().unwrap().name;

let link = linked
.as_ref()
.and_then(|link| {
link.get(base_ident).map(|path| {
let path = Path::new("/").join(
path.strip_prefix(&src_target_dir)
.ok()
.unwrap_or(path),
);
Markdown::Link(&base_doc, &path.display().to_string())
.as_doc()
let path_str = path.to_str().unwrap_or_default();

// Remove duplicate 'docs/' after concatanate path
let path_str = path_str.replace("docs/docs/", "docs/");

// Change 'src' to 'book'
let path_str = path_str.replace("/src/", "/book/");

// Change file extension from '.md' to '.html'
let path_str = path_str.replace(".md", ".html");

let full_path = format!("file://{path_str}");
Markdown::Link(&base_doc, &full_path).as_doc()
})
})
.transpose()?
.unwrap_or(base_doc);

bases.push(link);
bases.push(link.clone());
}

writer.writeln_raw(bases.join(", "))?;
Expand Down Expand Up @@ -272,11 +272,6 @@ impl AsDoc for Document {
}

impl Document {
/// Where all the source files are written to
fn target_src_dir(&self) -> PathBuf {
self.out_target_dir.join("src")
}

/// Writes a function to the buffer.
fn write_function(
&self,
Expand Down