Add a test
This commit is contained in:
parent
c6a679d1a3
commit
c87fdf7e3e
63
src/lib.rs
63
src/lib.rs
|
@ -46,15 +46,14 @@ fn build_toc<'a>(toc: &[(i32, Cow<'a, str>)]) -> String {
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Toc {
|
fn add_toc(content: &str) -> Result<String> {
|
||||||
fn add_toc(chapter: &mut Chapter) -> Result<String> {
|
let mut buf = String::with_capacity(content.len());
|
||||||
let mut buf = String::with_capacity(chapter.content.len());
|
|
||||||
let mut toc_found = false;
|
let mut toc_found = false;
|
||||||
|
|
||||||
let mut toc_content = vec![];
|
let mut toc_content = vec![];
|
||||||
let mut current_header_level : Option<i32> = None;
|
let mut current_header_level : Option<i32> = None;
|
||||||
|
|
||||||
for e in Parser::new(&chapter.content) {
|
for e in Parser::new(&content) {
|
||||||
if let Event::Html(html) = e {
|
if let Event::Html(html) = e {
|
||||||
if html == "<!-- toc -->\n" {
|
if html == "<!-- toc -->\n" {
|
||||||
toc_found = true;
|
toc_found = true;
|
||||||
|
@ -87,7 +86,7 @@ impl Toc {
|
||||||
let toc_events = build_toc(&toc_content);
|
let toc_events = build_toc(&toc_content);
|
||||||
let toc_events = Parser::new(&toc_events).collect::<Vec<_>>();
|
let toc_events = Parser::new(&toc_events).collect::<Vec<_>>();
|
||||||
|
|
||||||
let events = Parser::new(&chapter.content).map(|e| {
|
let events = Parser::new(&content).map(|e| {
|
||||||
if let Event::Html(html) = e.clone() {
|
if let Event::Html(html) = e.clone() {
|
||||||
if html == "<!-- toc -->\n" {
|
if html == "<!-- toc -->\n" {
|
||||||
return toc_events.clone();
|
return toc_events.clone();
|
||||||
|
@ -99,5 +98,59 @@ impl Toc {
|
||||||
cmark(events, &mut buf, None)
|
cmark(events, &mut buf, None)
|
||||||
.map(|_| buf)
|
.map(|_| buf)
|
||||||
.map_err(|err| Error::from(format!("Markdown serialization failed: {}", err)))
|
.map_err(|err| Error::from(format!("Markdown serialization failed: {}", err)))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Toc {
|
||||||
|
fn add_toc(chapter: &mut Chapter) -> Result<String> {
|
||||||
|
add_toc(&chapter.content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::add_toc;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn adds_toc() {
|
||||||
|
let content = r#"# Chapter
|
||||||
|
|
||||||
|
<!-- toc -->
|
||||||
|
|
||||||
|
# Header 1
|
||||||
|
|
||||||
|
## Header 1.1
|
||||||
|
|
||||||
|
# Header 2
|
||||||
|
|
||||||
|
## Header 2.1
|
||||||
|
|
||||||
|
## Header 2.2
|
||||||
|
|
||||||
|
### Header 2.2.1
|
||||||
|
|
||||||
|
"#;
|
||||||
|
|
||||||
|
let expected = r#"# Chapter
|
||||||
|
|
||||||
|
* [Header 1](#header-1)
|
||||||
|
* [Header 1.1](#header-11)
|
||||||
|
* [Header 2](#header-2)
|
||||||
|
* [Header 2.1](#header-21)
|
||||||
|
* [Header 2.2](#header-22)
|
||||||
|
|
||||||
|
# Header 1
|
||||||
|
|
||||||
|
## Header 1.1
|
||||||
|
|
||||||
|
# Header 2
|
||||||
|
|
||||||
|
## Header 2.1
|
||||||
|
|
||||||
|
## Header 2.2
|
||||||
|
|
||||||
|
### Header 2.2.1"#;
|
||||||
|
|
||||||
|
assert_eq!(expected, add_toc(content).unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue