@@ -126,20 +126,25 @@ fn adjust_links<'a>(event: Event<'a>, path: Option<&Path>, abs_url: Option<&Stri
126
126
}
127
127
128
128
if let Some ( caps) = MD_LINK . captures ( & dest) {
129
- fixed_link. push_str ( & caps[ "link" ] ) ;
129
+ fixed_link. push_str ( & caps[ "link" ] . trim_start_matches ( '/' ) ) ;
130
130
fixed_link. push_str ( ".html" ) ;
131
131
if let Some ( anchor) = caps. name ( "anchor" ) {
132
132
fixed_link. push_str ( anchor. as_str ( ) ) ;
133
133
}
134
+ } else if !fixed_link. is_empty ( ) {
135
+ // prevent links with double slashes
136
+ fixed_link. push_str ( & dest. trim_start_matches ( '/' ) ) ;
134
137
} else {
135
138
fixed_link. push_str ( & dest) ;
136
139
} ;
137
- if fixed_link. starts_with ( '/' ) {
138
- fixed_link = match abs_url {
139
- Some ( abs_url) => format ! ( "{}{}" , abs_url. trim_end_matches( '/' ) , & fixed_link) ,
140
- None => fixed_link,
140
+ if dest. starts_with ( '/' ) || path. is_some ( ) {
141
+ if let Some ( abs_url) = abs_url {
142
+ fixed_link = format ! (
143
+ "{}/{}" ,
144
+ abs_url. trim_end_matches( '/' ) ,
145
+ & fixed_link. trim_start_matches( '/' )
146
+ ) ;
141
147
}
142
- . into ( ) ;
143
148
}
144
149
return CowStr :: from ( format ! ( "{}" , fixed_link) ) ;
145
150
}
@@ -181,7 +186,7 @@ fn adjust_links<'a>(event: Event<'a>, path: Option<&Path>, abs_url: Option<&Stri
181
186
182
187
/// Wrapper around the pulldown-cmark parser for rendering markdown to HTML.
183
188
pub fn render_markdown ( text : & str , curly_quotes : bool ) -> String {
184
- render_markdown_with_path ( text, curly_quotes, None , None )
189
+ render_markdown_with_path ( text, curly_quotes, None )
185
190
}
186
191
187
192
pub fn new_cmark_parser ( text : & str , curly_quotes : bool ) -> Parser < ' _ , ' _ > {
@@ -196,12 +201,18 @@ pub fn new_cmark_parser(text: &str, curly_quotes: bool) -> Parser<'_, '_> {
196
201
Parser :: new_ext ( text, opts)
197
202
}
198
203
199
- pub fn render_markdown_with_path (
204
+ pub fn render_markdown_with_path ( text : & str , curly_quotes : bool , path : Option < & Path > ) -> String {
205
+ render_markdown_with_abs_path ( text, curly_quotes, path, None )
206
+ }
207
+
208
+ pub fn render_markdown_with_abs_path (
200
209
text : & str ,
201
210
curly_quotes : bool ,
202
211
path : Option < & Path > ,
203
212
abs_url : Option < & String > ,
204
213
) -> String {
214
+ // This function should be merged with `render_markdown_with_path`
215
+ // in the future. Currently, it is used not to break compatibility.
205
216
let mut s = String :: with_capacity ( text. len ( ) * 3 / 2 ) ;
206
217
let p = new_cmark_parser ( text, curly_quotes) ;
207
218
let events = p
0 commit comments