Skip to content

Commit

Permalink
Use absolute links for bookshelf back reference
Browse files Browse the repository at this point in the history
Books with deeper hierachies than just one level would not get the
correct backreference to the bookshelf index.
Use an absolute URL link to make sure all pages in a book will link back
correctly.

This also means that we need to provide an option to prepend the
absolute URL link with so that you can publish to a non root url.
  • Loading branch information
Coi-l committed Mar 29, 2024
1 parent 9c5904d commit 544aa79
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/cmd/shelf.rs
Expand Up @@ -16,6 +16,7 @@ const REPOS_DIR: &str = "repositories";
const INDEX_MD_FILE: &str = "index.md";
const INDEX_HTML_FILE: &str = "index.html";
const BOOKS_DIR: &str = "books";
const BOOKSHELF_DIR: &str = "bookshelf";

pub fn make_subcommand() -> Command {
Command::new("shelf").about("Build a bookshelf from shelf.toml file")
Expand All @@ -35,6 +36,7 @@ fn process_book(
// Build book
let title = book.config.book.title.clone().unwrap();
let mut path = current_dir()?;
path.push(BOOKSHELF_DIR);
path.push(BOOKS_DIR);
path.push(title);
book.config.build.build_dir = path;
Expand Down Expand Up @@ -76,15 +78,26 @@ pub fn execute(_args: &ArgMatches) -> Result<()> {
let mut file = File::open("shelf.toml")?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let shelf: Shelf = toml::from_str(&contents)?;
let shelf_config: Shelf = toml::from_str(&contents)?;

let _ = std::fs::remove_dir_all(SHELF_DIR);
let _ = std::fs::remove_dir_all(BOOKSHELF_DIR);
let _ = std::fs::remove_dir_all(REPOS_DIR);
let shelf_book = MDBook::init(SHELF_DIR).create_gitignore(false).build()?;
let shelf_book_dir = format!("{BOOKSHELF_DIR}/{SHELF_DIR}");
let shelf_book = MDBook::init(&shelf_book_dir)
.create_gitignore(false)
.build()?;
let shelf_source = shelf_book.source_dir();
let shelf_build_dir = shelf_book.config.build.build_dir.to_str().unwrap_or("book");
let shelf_url_prefix = if let Some(prefix) = shelf_config.root_url_prefix {
let mut full_prefix = "/".to_owned();
full_prefix.push_str(&prefix);
full_prefix
} else {
"".to_owned()
};
error!("full prefix {shelf_url_prefix}");
let shelf_url = PathBuf::from(format!(
"../../{SHELF_DIR}/{shelf_build_dir}/{INDEX_HTML_FILE}"
"{shelf_url_prefix}/{shelf_book_dir}/{shelf_build_dir}/{INDEX_HTML_FILE}"
));

let mut index_file_name = shelf_book.source_dir();
Expand All @@ -99,7 +112,7 @@ pub fn execute(_args: &ArgMatches) -> Result<()> {
writeln!(summary, "# Summary")?;
writeln!(summary, "- [Index](./{INDEX_MD_FILE})")?;

for sb in &shelf.book {
for sb in &shelf_config.book {
if let Some(url) = &sb.git_url {
println!("{:?}", sb);
let path = sb.path.clone().unwrap_or("root".to_owned());
Expand Down Expand Up @@ -169,7 +182,7 @@ pub fn execute(_args: &ArgMatches) -> Result<()> {
}
}

let shelf = MDBook::load("shelf")?;
let shelf = MDBook::load(&shelf_book_dir)?;
shelf.build()?;

Ok(())
Expand Down
5 changes: 5 additions & 0 deletions src/config.rs
Expand Up @@ -656,6 +656,11 @@ pub struct ShelfBook {
pub struct Shelf {
/// The books in the shelf
pub book: Vec<ShelfBook>,
/// this will be prepeneded to the backreference url
/// Say you want to publish to www.example.com/mydocs
/// you would set this to "mydocs" and then find your bookshelf at
/// www.example.com/mydocs/bookshelf/shelf/book/index.html
pub root_url_prefix: Option<String>,
}

/// Configuration for how to render the print icon, print.html, and print.css.
Expand Down

0 comments on commit 544aa79

Please sign in to comment.