Skip to content

Commit

Permalink
Change Request 1
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
HollowMan6 committed Oct 17, 2022
1 parent 00d56ab commit a35b643
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ impl HtmlHandlebars {
let content = ch.content.clone();
let content = utils::render_markdown(&content, ctx.html_config.curly_quotes);

let fixed_content = utils::render_markdown_with_path(
let fixed_content = utils::render_markdown_with_path_and_redirects(
&ch.content,
ctx.html_config.curly_quotes,
Some(path),
ctx.html_config.redirect,
&ctx.html_config.redirect,
);
if !ctx.is_index && ctx.html_config.print.page_break {
// Add page break between chapters
Expand All @@ -73,7 +73,7 @@ impl HtmlHandlebars {
let path_id = {
let mut base = path.display().to_string();
if base.ends_with(".md") {
base.replace_range(base.len() - 3.., "");
base.truncate(base.len() - 3);
}
&base
.replace("/", "-")
Expand Down
25 changes: 15 additions & 10 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub fn normalize_path_id(mut path: String) -> String {
fn adjust_links<'a>(
event: Event<'a>,
path: Option<&Path>,
redirects: HashMap<String, String>,
redirects: &HashMap<String, String>,
) -> Event<'a> {
static SCHEME_LINK: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[a-z][a-z0-9+.-]*:").unwrap());
static HTML_MD_LINK: Lazy<Regex> =
Expand Down Expand Up @@ -176,14 +176,14 @@ fn adjust_links<'a>(
fn fix_a_links<'a>(
dest: CowStr<'a>,
path: Option<&Path>,
redirects: HashMap<String, String>,
redirects: &HashMap<String, String>,
) -> CowStr<'a> {
if dest.starts_with('#') {
// Fragment-only link.
if let Some(path) = path {
let mut base = path.display().to_string();
if base.ends_with(".md") {
base.replace_range(base.len() - 3.., "");
base.truncate(base.len() - 3);
}
return format!(
"#{}{}",
Expand Down Expand Up @@ -228,7 +228,7 @@ fn adjust_links<'a>(
if let Some(_) = path {
// Fix redirect links
let normalized_path_split: Vec<&str> = normalized_path.split('#').collect();
for (original, redirect) in &redirects {
for (original, redirect) in redirects {
if normalize_path(original.trim_start_matches('/'))
.eq_ignore_ascii_case(&normalized_path)
|| normalize_path(original.trim_start_matches('/'))
Expand Down Expand Up @@ -295,7 +295,7 @@ fn adjust_links<'a>(
fn fix_html<'a>(
html: CowStr<'a>,
path: Option<&Path>,
redirects: HashMap<String, String>,
redirects: &HashMap<String, String>,
) -> CowStr<'a> {
// This is a terrible hack, but should be reasonably reliable. Nobody
// should ever parse a tag with a regex. However, there isn't anything
Expand All @@ -319,7 +319,7 @@ fn adjust_links<'a>(

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

/// Wrapper around the pulldown-cmark parser for rendering markdown to HTML.
pub fn render_markdown(text: &str, curly_quotes: bool) -> String {
render_markdown_with_path(text, curly_quotes, None, HashMap::new())
render_markdown_with_path(text, curly_quotes, None)
}

/// Wrapper around for API compatibility.
pub fn render_markdown_with_path(text: &str, curly_quotes: bool, path: Option<&Path>) -> String {
render_markdown_with_path_and_redirects(text, curly_quotes, path, &HashMap::new())
}

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

pub fn render_markdown_with_path(
pub fn render_markdown_with_path_and_redirects(
text: &str,
curly_quotes: bool,
path: Option<&Path>,
redirects: HashMap<String, String>,
redirects: &HashMap<String, String>,
) -> String {
let mut s = String::with_capacity(text.len() * 3 / 2);
let p = new_cmark_parser(text, curly_quotes);
let events = p
.map(clean_codeblock_headers)
.map(|event| adjust_links(event, path, redirects.clone()))
.map(|event| adjust_links(event, path, &redirects))
.flat_map(|event| {
let (a, b) = wrap_tables(event);
a.into_iter().chain(b)
Expand Down

0 comments on commit a35b643

Please sign in to comment.