| 1 |
use crate::utils::ToUrlPath; |
| 2 |
use handlebars::{ |
| 3 |
Context, Handlebars, Helper, HelperDef, Output, RenderContext, RenderError, RenderErrorReason, |
| 4 |
}; |
| 5 |
use mdbook_core::utils::escape_html_attribute; |
| 6 |
use std::path::Path; |
| 7 |
use std::{cmp::Ordering, collections::BTreeMap}; |
| 8 |
|
| 9 |
// Handlebars helper to construct TOC |
| 10 |
#[derive(Clone, Copy)] |
| 11 |
pub(crate) struct RenderToc { |
| 12 |
pub no_section_label: bool, |
| 13 |
} |
| 14 |
|
| 15 |
impl HelperDef for RenderToc { |
| 16 |
fn call<'reg: 'rc, 'rc>( |
| 17 |
&self, |
| 18 |
_h: &Helper<'rc>, |
| 19 |
_r: &'reg Handlebars<'_>, |
| 20 |
ctx: &'rc Context, |
| 21 |
rc: &mut RenderContext<'reg, 'rc>, |
| 22 |
out: &mut dyn Output, |
| 23 |
) -> Result<(), RenderError> { |
| 24 |
// get value from context data |
| 25 |
// rc.get_path() is current json parent path, you should always use it like this |
| 26 |
// param is the key of value you want to display |
| 27 |
let chapters = rc.evaluate(ctx, "@root/chapters").and_then(|c| { |
| 28 |
serde_json::value::from_value::<Vec<BTreeMap<String, String>>>(c.as_json().clone()) |
| 29 |
.map_err(|_| { |
| 30 |
RenderErrorReason::Other("Could not decode the JSON data".to_owned()).into() |
| 31 |
}) |
| 32 |
})?; |
| 33 |
|
| 34 |
let fold_enable = rc |
| 35 |
.evaluate(ctx, "@root/fold_enable")? |
| 36 |
.as_json() |
| 37 |
.as_bool() |
| 38 |
.ok_or_else(|| { |
| 39 |
RenderErrorReason::Other("Type error for `fold_enable`, bool expected".to_owned()) |
| 40 |
})?; |
| 41 |
|
| 42 |
let fold_level = rc |
| 43 |
.evaluate(ctx, "@root/fold_level")? |
| 44 |
.as_json() |
| 45 |
.as_u64() |
| 46 |
.ok_or_else(|| { |
| 47 |
RenderErrorReason::Other("Type error for `fold_level`, u64 expected".to_owned()) |
| 48 |
})?; |
| 49 |
|
| 50 |
// If true, then this is the iframe and we need target="_parent" |
| 51 |
let is_toc_html = rc |
| 52 |
.evaluate(ctx, "@root/is_toc_html")? |
| 53 |
.as_json() |
| 54 |
.as_bool() |
| 55 |
.unwrap_or(false); |
| 56 |
|
| 57 |
out.write("<ol class=\"chapter\">")?; |
| 58 |
|
| 59 |
let mut current_level = 1; |
| 60 |
let mut first = true; |
| 61 |
|
| 62 |
for item in chapters { |
| 63 |
let level = item |
| 64 |
.get("section") |
| 65 |
.map(|s| s.matches('.').count()) |
| 66 |
.unwrap_or(1); |
| 67 |
|
| 68 |
// Expand if folding is disabled, or if levels that are larger than this would not |
| 69 |
// be folded. |
| 70 |
let is_expanded = !fold_enable || level - 1 < (fold_level as usize); |
| 71 |
|
| 72 |
match level.cmp(¤t_level) { |
| 73 |
Ordering::Greater => { |
| 74 |
// There is an assumption that when descending, it can |
| 75 |
// only go one level down at a time. This should be |
| 76 |
// enforced by the nature of markdown lists and the |
| 77 |
// summary parser. |
| 78 |
assert_eq!(level, current_level + 1); |
| 79 |
current_level += 1; |
| 80 |
out.write("<ol class=\"section\">")?; |
| 81 |
write_li_open_tag(out, is_expanded)?; |
| 82 |
} |
| 83 |
Ordering::Less => { |
| 84 |
while level < current_level { |
| 85 |
out.write("</li>")?; |
| 86 |
out.write("</ol>")?; |
| 87 |
current_level -= 1; |
| 88 |
} |
| 89 |
write_li_open_tag(out, is_expanded)?; |
| 90 |
} |
| 91 |
Ordering::Equal => { |
| 92 |
if !first { |
| 93 |
out.write("</li>")?; |
| 94 |
} |
| 95 |
write_li_open_tag(out, is_expanded)?; |
| 96 |
} |
| 97 |
} |
| 98 |
first = false; |
| 99 |
|
| 100 |
// Spacer |
| 101 |
if item.contains_key("spacer") { |
| 102 |
out.write("<li class=\"spacer\"></li>")?; |
| 103 |
continue; |
| 104 |
} |
| 105 |
|
| 106 |
// Part title |
| 107 |
if let Some(title) = item.get("part") { |
| 108 |
out.write("<li class=\"part-title\">")?; |
| 109 |
out.write(&escape_html_attribute(title))?; |
| 110 |
out.write("</li>")?; |
| 111 |
continue; |
| 112 |
} |
| 113 |
|
| 114 |
out.write("<span class=\"chapter-link-wrapper\">")?; |
| 115 |
|
| 116 |
// Link |
| 117 |
let path_exists = match item.get("path") { |
| 118 |
Some(path) if !path.is_empty() => { |
| 119 |
out.write("<a href=\"")?; |
| 120 |
let tmp = Path::new(path).with_extension("html").to_url_path(); |
| 121 |
|
| 122 |
// Add link |
| 123 |
out.write(&tmp)?; |
| 124 |
out.write(if is_toc_html { |
| 125 |
"\" target=\"_parent\">" |
| 126 |
} else { |
| 127 |
"\">" |
| 128 |
})?; |
| 129 |
true |
| 130 |
} |
| 131 |
_ => { |
| 132 |
out.write("<span>")?; |
| 133 |
false |
| 134 |
} |
| 135 |
}; |
| 136 |
|
| 137 |
if !self.no_section_label { |
| 138 |
// Section does not necessarily exist |
| 139 |
if let Some(section) = item.get("section") { |
| 140 |
out.write("<strong aria-hidden=\"true\">")?; |
| 141 |
out.write(section)?; |
| 142 |
out.write("</strong> ")?; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
if let Some(name) = item.get("name") { |
| 147 |
out.write(&escape_html_attribute(name))?; |
| 148 |
} |
| 149 |
|
| 150 |
if path_exists { |
| 151 |
out.write("</a>")?; |
| 152 |
} else { |
| 153 |
out.write("</span>")?; |
| 154 |
} |
| 155 |
|
| 156 |
// Render expand/collapse toggle |
| 157 |
if let Some(flag) = item.get("has_sub_items") { |
| 158 |
let has_sub_items = flag.parse::<bool>().unwrap_or_default(); |
| 159 |
if fold_enable && has_sub_items { |
| 160 |
// The <div> here is to manage rotating the element when |
| 161 |
// the chapter title is long and word-wraps. |
| 162 |
out.write("<a class=\"chapter-fold-toggle\"><div>❱</div></a>")?; |
| 163 |
} |
| 164 |
} |
| 165 |
out.write("</span>")?; |
| 166 |
} |
| 167 |
while current_level > 0 { |
| 168 |
out.write("</li>")?; |
| 169 |
out.write("</ol>")?; |
| 170 |
current_level -= 1; |
| 171 |
} |
| 172 |
|
| 173 |
Ok(()) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
fn write_li_open_tag(out: &mut dyn Output, is_expanded: bool) -> Result<(), std::io::Error> { |
| 178 |
let mut li = String::from("<li class=\"chapter-item "); |
| 179 |
if is_expanded { |
| 180 |
li.push_str("expanded "); |
| 181 |
} |
| 182 |
li.push_str("\">"); |
| 183 |
out.write(&li) |
| 184 |
} |