@@ -92,13 +92,13 @@ pub fn unique_id_from_content(content: &str, id_counter: &mut HashMap<String, us
9292/// page go to the original location. Normal page rendering sets `path` to
9393/// None. Ideally, print page links would link to anchors on the print page,
9494/// but that is very difficult.
95- fn adjust_links < ' a > ( event : Event < ' a > , path : Option < & Path > , abs_url : Option < & String > ) -> Event < ' a > {
95+ fn adjust_links < ' a > ( event : Event < ' a > , path : Option < & Path > , abs_url : Option < & str > ) -> Event < ' a > {
9696 static SCHEME_LINK : LazyLock < Regex > =
9797 LazyLock :: new ( || Regex :: new ( r"^[a-z][a-z0-9+.-]*:" ) . unwrap ( ) ) ;
9898 static MD_LINK : LazyLock < Regex > =
9999 LazyLock :: new ( || Regex :: new ( r"(?P<link>.*)\.md(?P<anchor>#.*)?" ) . unwrap ( ) ) ;
100100
101- fn fix < ' a > ( dest : CowStr < ' a > , path : Option < & Path > , abs_url : Option < & String > ) -> CowStr < ' a > {
101+ fn fix < ' a > ( dest : CowStr < ' a > , path : Option < & Path > , abs_url : Option < & str > ) -> CowStr < ' a > {
102102 if dest. starts_with ( '#' ) {
103103 // Fragment-only link.
104104 if let Some ( path) = path {
@@ -152,7 +152,7 @@ fn adjust_links<'a>(event: Event<'a>, path: Option<&Path>, abs_url: Option<&Stri
152152 dest
153153 }
154154
155- fn fix_html < ' a > ( html : CowStr < ' a > , path : Option < & Path > , abs_url : Option < & String > ) -> CowStr < ' a > {
155+ fn fix_html < ' a > ( html : CowStr < ' a > , path : Option < & Path > , abs_url : Option < & str > ) -> CowStr < ' a > {
156156 // This is a terrible hack, but should be reasonably reliable. Nobody
157157 // should ever parse a tag with a regex. However, there isn't anything
158158 // in Rust that I know of that is suitable for handling partial html
@@ -204,7 +204,7 @@ fn adjust_links<'a>(event: Event<'a>, path: Option<&Path>, abs_url: Option<&Stri
204204
205205/// Wrapper around the pulldown-cmark parser for rendering markdown to HTML.
206206pub fn render_markdown ( text : & str , smart_punctuation : bool ) -> String {
207- render_markdown_with_path ( text, smart_punctuation, None , None )
207+ render_markdown_with_path ( text, smart_punctuation, None )
208208}
209209
210210/// Creates a new pulldown-cmark parser of the given text.
@@ -226,11 +226,29 @@ pub fn new_cmark_parser(text: &str, smart_punctuation: bool) -> Parser<'_> {
226226/// `path` should only be set if this is being generated for the consolidated
227227/// print page. It should point to the page being rendered relative to the
228228/// root of the book.
229- pub fn render_markdown_with_path (
229+ pub fn render_markdown_with_path ( text : & str , curly_quotes : bool , path : Option < & Path > ) -> String {
230+ render_markdown_with_abs_path ( text, curly_quotes, path, None )
231+ }
232+
233+
234+ /// Renders markdown to HTML.
235+ ///
236+ /// `path` should only be set if this is being generated for the consolidated
237+ /// print page. It should point to the page being rendered relative to the
238+ /// root of the book.
239+ /// `abs_url` is the absolute URL to use for links that start with `/`.
240+ /// If `abs_url` is `None`, then links that start with `/` will be
241+ /// rendered relative to the current path.
242+ /// If `abs_url` is `Some`, then links that start with `/` will be
243+ /// rendered as absolute links using the provided URL.
244+ ////// This is useful for generating links in the print page, where the
245+ /// links should point to the original location of the page, not the
246+ /// print page itself.
247+ pub fn render_markdown_with_abs_path (
230248 text : & str ,
231249 smart_punctuation : bool ,
232250 path : Option < & Path > ,
233- abs_url : Option < & String > ,
251+ abs_url : Option < & str > ,
234252) -> String {
235253 let mut body = String :: with_capacity ( text. len ( ) * 3 / 2 ) ;
236254
@@ -624,7 +642,7 @@ more text with spaces
624642 "[example](https://www.rust-lang.org/)" ,
625643 false ,
626644 None ,
627- Some ( & "ABS_PATH" . to_string ( ) )
645+ Some ( "ABS_PATH" )
628646 ) ,
629647 "<p><a href=\" https://www.rust-lang.org/\" >example</a></p>\n "
630648 ) ;
@@ -637,7 +655,7 @@ more text with spaces
637655 "[example](/testing)" ,
638656 false ,
639657 None ,
640- Some ( & "ABS_PATH" . to_string ( ) )
658+ Some ( "ABS_PATH" )
641659 ) ,
642660 "<p><a href=\" ABS_PATH/testing\" >example</a></p>\n "
643661 ) ;
@@ -650,7 +668,7 @@ more text with spaces
650668 "[example](bar.md)" ,
651669 false ,
652670 Some ( Path :: new( "foo/chapter.md" ) ) ,
653- Some ( & "ABS_PATH" . to_string ( ) )
671+ Some ( "ABS_PATH" )
654672 ) ,
655673 "<p><a href=\" ABS_PATH/foo/bar.html\" >example</a></p>\n "
656674 ) ;
@@ -659,7 +677,7 @@ more text with spaces
659677 "[example](/bar.md)" ,
660678 false ,
661679 Some ( Path :: new( "foo/chapter.md" ) ) ,
662- Some ( & "ABS_PATH" . to_string ( ) )
680+ Some ( "ABS_PATH" )
663681 ) ,
664682 "<p><a href=\" ABS_PATH/foo/bar.html\" >example</a></p>\n "
665683 ) ;
@@ -681,7 +699,7 @@ more text with spaces
681699 "[example](../testing)" ,
682700 false ,
683701 None ,
684- Some ( & "ABS_PATH" . to_string ( ) )
702+ Some ( "ABS_PATH" )
685703 ) ,
686704 "<p><a href=\" ../testing\" >example</a></p>\n "
687705 ) ;
0 commit comments