From 544aa79d74af94c2607ed3a870a8cb02da3f08cf Mon Sep 17 00:00:00 2001 From: Coil Date: Fri, 29 Mar 2024 20:21:55 +0100 Subject: [PATCH] Use absolute links for bookshelf back reference 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. --- src/cmd/shelf.rs | 25 +++++++++++++++++++------ src/config.rs | 5 +++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/cmd/shelf.rs b/src/cmd/shelf.rs index adf788a955..e8f548cdf0 100644 --- a/src/cmd/shelf.rs +++ b/src/cmd/shelf.rs @@ -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") @@ -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; @@ -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(); @@ -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()); @@ -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(()) diff --git a/src/config.rs b/src/config.rs index f39b28b074..40751c5051 100644 --- a/src/config.rs +++ b/src/config.rs @@ -656,6 +656,11 @@ pub struct ShelfBook { pub struct Shelf { /// The books in the shelf pub book: Vec, + /// 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, } /// Configuration for how to render the print icon, print.html, and print.css.