Skip to content

Commit 2ddbb37

Browse files
committed
Fix the bug
1 parent a481735 commit 2ddbb37

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ impl HtmlHandlebars {
3333
if let BookItem::Chapter(ref ch) = *item {
3434
let content = ch.content.clone();
3535
let content = utils::render_markdown(&content, ctx.html_config.curly_quotes);
36-
print_content.push_str(&content);
36+
37+
let string_path = ch.path.parent().unwrap().display().to_string();
38+
39+
let fixed_content = utils::render_markdown_with_base(&ch.content, ctx.html_config.curly_quotes, &string_path);
40+
print_content.push_str(&fixed_content);
3741

3842
// Update the context with data for this file
3943
let path = ch

src/utils/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub fn id_from_content(content: &str) -> String {
6666
normalize_id(trimmed)
6767
}
6868

69-
fn adjust_links(event: Event) -> Event {
69+
fn adjust_links<'a>(event: Event<'a>, with_base: &str) -> Event<'a> {
7070
lazy_static! {
7171
static ref HTTP_LINK: Regex = Regex::new("^https?://").unwrap();
7272
static ref MD_LINK: Regex = Regex::new("(?P<link>.*).md(?P<anchor>#.*)?").unwrap();
@@ -75,6 +75,12 @@ fn adjust_links(event: Event) -> Event {
7575
match event {
7676
Event::Start(Tag::Link(dest, title)) => {
7777
if !HTTP_LINK.is_match(&dest) {
78+
let dest = if !with_base.is_empty() {
79+
format!("{}/{}", with_base, dest)
80+
} else {
81+
dest.clone().into_owned()
82+
};
83+
7884
if let Some(caps) = MD_LINK.captures(&dest) {
7985
let mut html_link = [&caps["link"], ".html"].concat();
8086

@@ -94,6 +100,10 @@ fn adjust_links(event: Event) -> Event {
94100

95101
/// Wrapper around the pulldown-cmark parser for rendering markdown to HTML.
96102
pub fn render_markdown(text: &str, curly_quotes: bool) -> String {
103+
render_markdown_with_base(text, curly_quotes, "")
104+
}
105+
106+
pub fn render_markdown_with_base(text: &str, curly_quotes: bool, base: &str) -> String {
97107
let mut s = String::with_capacity(text.len() * 3 / 2);
98108

99109
let mut opts = Options::empty();
@@ -104,7 +114,7 @@ pub fn render_markdown(text: &str, curly_quotes: bool) -> String {
104114
let mut converter = EventQuoteConverter::new(curly_quotes);
105115
let events = p
106116
.map(clean_codeblock_headers)
107-
.map(adjust_links)
117+
.map(|event| adjust_links(event, base))
108118
.map(|event| converter.convert(event));
109119

110120
html::push_html(&mut s, events);

0 commit comments

Comments
 (0)