Skip to content

Commit dfec74c

Browse files
committed
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.
1 parent 9c5904d commit dfec74c

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/cmd/shelf.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const REPOS_DIR: &str = "repositories";
1616
const INDEX_MD_FILE: &str = "index.md";
1717
const INDEX_HTML_FILE: &str = "index.html";
1818
const BOOKS_DIR: &str = "books";
19+
const BOOKSHELF_DIR: &str = "bookshelf";
1920

2021
pub fn make_subcommand() -> Command {
2122
Command::new("shelf").about("Build a bookshelf from shelf.toml file")
@@ -35,6 +36,7 @@ fn process_book(
3536
// Build book
3637
let title = book.config.book.title.clone().unwrap();
3738
let mut path = current_dir()?;
39+
path.push(BOOKSHELF_DIR);
3840
path.push(BOOKS_DIR);
3941
path.push(title);
4042
book.config.build.build_dir = path;
@@ -76,15 +78,25 @@ pub fn execute(_args: &ArgMatches) -> Result<()> {
7678
let mut file = File::open("shelf.toml")?;
7779
let mut contents = String::new();
7880
file.read_to_string(&mut contents)?;
79-
let shelf: Shelf = toml::from_str(&contents)?;
81+
let shelf_config: Shelf = toml::from_str(&contents)?;
8082

81-
let _ = std::fs::remove_dir_all(SHELF_DIR);
83+
let _ = std::fs::remove_dir_all(BOOKSHELF_DIR);
8284
let _ = std::fs::remove_dir_all(REPOS_DIR);
83-
let shelf_book = MDBook::init(SHELF_DIR).create_gitignore(false).build()?;
85+
let shelf_book_dir = format!("{BOOKSHELF_DIR}/{SHELF_DIR}");
86+
let shelf_book = MDBook::init(&shelf_book_dir)
87+
.create_gitignore(false)
88+
.build()?;
8489
let shelf_source = shelf_book.source_dir();
8590
let shelf_build_dir = shelf_book.config.build.build_dir.to_str().unwrap_or("book");
91+
let shelf_url_prefix = if let Some(prefix) = shelf_config.root_url_prefix {
92+
let mut full_prefix = "/".to_owned();
93+
full_prefix.push_str(&prefix);
94+
full_prefix
95+
} else {
96+
"".to_owned()
97+
};
8698
let shelf_url = PathBuf::from(format!(
87-
"../../{SHELF_DIR}/{shelf_build_dir}/{INDEX_HTML_FILE}"
99+
"{shelf_url_prefix}/{shelf_book_dir}/{shelf_build_dir}/{INDEX_HTML_FILE}"
88100
));
89101

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

102-
for sb in &shelf.book {
114+
for sb in &shelf_config.book {
103115
if let Some(url) = &sb.git_url {
104116
println!("{:?}", sb);
105117
let path = sb.path.clone().unwrap_or("root".to_owned());
@@ -169,7 +181,7 @@ pub fn execute(_args: &ArgMatches) -> Result<()> {
169181
}
170182
}
171183

172-
let shelf = MDBook::load("shelf")?;
184+
let shelf = MDBook::load(&shelf_book_dir)?;
173185
shelf.build()?;
174186

175187
Ok(())

src/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,11 @@ pub struct ShelfBook {
656656
pub struct Shelf {
657657
/// The books in the shelf
658658
pub book: Vec<ShelfBook>,
659+
/// this will be prepeneded to the backreference url
660+
/// Say you want to publish to www.example.com/mydocs
661+
/// you would set this to "mydocs" and then find your bookshelf at
662+
/// www.example.com/mydocs/bookshelf/shelf/book/index.html
663+
pub root_url_prefix: Option<String>,
659664
}
660665

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

0 commit comments

Comments
 (0)