@@ -9,7 +9,7 @@ use crate::html::{ChapterTree, Element, serialize};
99use crate :: utils:: { ToUrlPath , id_from_content, normalize_path, unique_id} ;
1010use mdbook_core:: static_regex;
1111use std:: collections:: { HashMap , HashSet } ;
12- use std:: path:: { Component , PathBuf } ;
12+ use std:: path:: PathBuf ;
1313
1414/// Takes all the chapter trees, modifies them to be suitable to render for
1515/// the print page, and returns an string of all the chapters rendered to a
@@ -166,13 +166,9 @@ fn rewrite_links(
166166 {
167167 lookup_key. pop ( ) ;
168168 lookup_key. push ( href_path) ;
169- let normalized = normalize_path ( & lookup_key) ;
170- // If this points outside of the book, don't modify it.
171- let is_outside = matches ! (
172- normalized. components( ) . next( ) ,
173- Some ( Component :: ParentDir | Component :: RootDir )
174- ) ;
175- if is_outside || !href_path. ends_with ( ".html" ) {
169+ lookup_key = normalize_path ( & lookup_key) ;
170+ let is_a_chapter = path_to_root_id. contains_key ( & lookup_key) ;
171+ if !is_a_chapter {
176172 // Make the link relative to the print page location.
177173 let mut rel_path = normalize_path ( & base. join ( href_path) ) . to_url_path ( ) ;
178174 if let Some ( anchor) = caps. name ( "anchor" ) {
@@ -184,10 +180,7 @@ fn rewrite_links(
184180 }
185181 }
186182
187- let lookup_key = normalize_path ( & lookup_key) ;
188-
189- let anchor = caps. name ( "anchor" ) ;
190- let id = match anchor {
183+ let id = match caps. name ( "anchor" ) {
191184 Some ( anchor_id) => {
192185 let anchor_id = anchor_id. as_str ( ) . to_string ( ) ;
193186 match id_remap. get ( & lookup_key) {
@@ -204,7 +197,15 @@ fn rewrite_links(
204197 }
205198 None => match path_to_root_id. get ( & lookup_key) {
206199 Some ( id) => id. to_string ( ) ,
207- None => continue ,
200+ None => {
201+ // This should be guaranteed that either the
202+ // chapter itself is in the map (for anchor-only
203+ // links), or the is_a_chapter check above.
204+ panic ! (
205+ "internal error: expected `{lookup_key:?}` to be in \
206+ root map (chapter path is `{html_path:?}`)"
207+ ) ;
208+ }
208209 } ,
209210 } ;
210211 el. insert_attr ( attr, format ! ( "#{id}" ) . into ( ) ) ;
0 commit comments