Skip to content

Commit 2268e9c

Browse files
committed
Change Request 1
- make render_markdown_with_path parameters unchanged - pass the map using a reference - use truncate instead of replace_range Signed-off-by: Hollow Man <[email protected]>
1 parent 00d56ab commit 2268e9c

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ impl HtmlHandlebars {
5757
let content = ch.content.clone();
5858
let content = utils::render_markdown(&content, ctx.html_config.curly_quotes);
5959

60-
let fixed_content = utils::render_markdown_with_path(
60+
let fixed_content = utils::render_markdown_with_path_and_redirects(
6161
&ch.content,
6262
ctx.html_config.curly_quotes,
6363
Some(path),
64-
ctx.html_config.redirect,
64+
&ctx.html_config.redirect,
6565
);
6666
if !ctx.is_index && ctx.html_config.print.page_break {
6767
// Add page break between chapters
@@ -73,7 +73,7 @@ impl HtmlHandlebars {
7373
let path_id = {
7474
let mut base = path.display().to_string();
7575
if base.ends_with(".md") {
76-
base.replace_range(base.len() - 3.., "");
76+
base.truncate(base.len() - 3);
7777
}
7878
&base
7979
.replace("/", "-")

src/utils/mod.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ pub fn normalize_path_id(mut path: String) -> String {
146146
fn adjust_links<'a>(
147147
event: Event<'a>,
148148
path: Option<&Path>,
149-
redirects: HashMap<String, String>,
149+
redirects: &HashMap<String, String>,
150150
) -> Event<'a> {
151151
static SCHEME_LINK: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[a-z][a-z0-9+.-]*:").unwrap());
152152
static HTML_MD_LINK: Lazy<Regex> =
@@ -176,14 +176,14 @@ fn adjust_links<'a>(
176176
fn fix_a_links<'a>(
177177
dest: CowStr<'a>,
178178
path: Option<&Path>,
179-
redirects: HashMap<String, String>,
179+
redirects: &HashMap<String, String>,
180180
) -> CowStr<'a> {
181181
if dest.starts_with('#') {
182182
// Fragment-only link.
183183
if let Some(path) = path {
184184
let mut base = path.display().to_string();
185185
if base.ends_with(".md") {
186-
base.replace_range(base.len() - 3.., "");
186+
base.truncate(base.len() - 3);
187187
}
188188
return format!(
189189
"#{}{}",
@@ -228,7 +228,7 @@ fn adjust_links<'a>(
228228
if let Some(_) = path {
229229
// Fix redirect links
230230
let normalized_path_split: Vec<&str> = normalized_path.split('#').collect();
231-
for (original, redirect) in &redirects {
231+
for (original, redirect) in redirects {
232232
if normalize_path(original.trim_start_matches('/'))
233233
.eq_ignore_ascii_case(&normalized_path)
234234
|| normalize_path(original.trim_start_matches('/'))
@@ -295,7 +295,7 @@ fn adjust_links<'a>(
295295
fn fix_html<'a>(
296296
html: CowStr<'a>,
297297
path: Option<&Path>,
298-
redirects: HashMap<String, String>,
298+
redirects: &HashMap<String, String>,
299299
) -> CowStr<'a> {
300300
// This is a terrible hack, but should be reasonably reliable. Nobody
301301
// should ever parse a tag with a regex. However, there isn't anything
@@ -319,7 +319,7 @@ fn adjust_links<'a>(
319319

320320
A_LINK
321321
.replace_all(&temp_html, |caps: &regex::Captures<'_>| {
322-
let fixed = fix_a_links(caps[2].into(), path, redirects.clone());
322+
let fixed = fix_a_links(caps[2].into(), path, &redirects);
323323
format!("{}{}\"", &caps[1], fixed)
324324
})
325325
.into_owned()
@@ -342,7 +342,12 @@ fn adjust_links<'a>(
342342

343343
/// Wrapper around the pulldown-cmark parser for rendering markdown to HTML.
344344
pub fn render_markdown(text: &str, curly_quotes: bool) -> String {
345-
render_markdown_with_path(text, curly_quotes, None, HashMap::new())
345+
render_markdown_with_path(text, curly_quotes, None)
346+
}
347+
348+
/// Wrapper around for API compatibility.
349+
pub fn render_markdown_with_path(text: &str, curly_quotes: bool, path: Option<&Path>) -> String {
350+
render_markdown_with_path_and_redirects(text, curly_quotes, path, &HashMap::new())
346351
}
347352

348353
pub fn new_cmark_parser(text: &str, curly_quotes: bool) -> Parser<'_, '_> {
@@ -357,17 +362,17 @@ pub fn new_cmark_parser(text: &str, curly_quotes: bool) -> Parser<'_, '_> {
357362
Parser::new_ext(text, opts)
358363
}
359364

360-
pub fn render_markdown_with_path(
365+
pub fn render_markdown_with_path_and_redirects(
361366
text: &str,
362367
curly_quotes: bool,
363368
path: Option<&Path>,
364-
redirects: HashMap<String, String>,
369+
redirects: &HashMap<String, String>,
365370
) -> String {
366371
let mut s = String::with_capacity(text.len() * 3 / 2);
367372
let p = new_cmark_parser(text, curly_quotes);
368373
let events = p
369374
.map(clean_codeblock_headers)
370-
.map(|event| adjust_links(event, path, redirects.clone()))
375+
.map(|event| adjust_links(event, path, &redirects))
371376
.flat_map(|event| {
372377
let (a, b) = wrap_tables(event);
373378
a.into_iter().chain(b)

tests/rendered_output.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ fn redirects_are_emitted_correctly() {
568568

569569
md.build().unwrap();
570570

571-
for (original, redirect) in &redirects {
571+
for (original, redirect) in redirects {
572572
let mut redirect_file = md.build_dir_for("html");
573573
// append everything except the bits that make it absolute
574574
// (e.g. "/" or "C:\")

0 commit comments

Comments
 (0)