Skip to content

Use md file's header to link name while summary file's title is blank #1980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/book/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use memchr::{self, Memchr};
use pulldown_cmark::{self, Event, HeadingLevel, Tag};
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display, Formatter};
use std::fs::File;
use std::io::Read;
use std::iter::FromIterator;
use std::ops::{Deref, DerefMut};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -502,6 +504,29 @@ impl<'a> SummaryParser<'a> {
);

link.number = Some(number);
if link.name == "" {
let mut chapter_content = String::new();
let fname = format!("./src/{}", href.to_string());
File::open(&fname)
.with_context(|| {
format!("Couldn't open {:?} file while title is blank", fname)
})?
.read_to_string(&mut chapter_content)?;
let parser = pulldown_cmark::Parser::new(&chapter_content);
let mut found_h1 = false;
for event in parser {
match event {
Event::Start(Tag::Heading(HeadingLevel::H1, ..)) => found_h1 = true,
Event::Text(text) => {
if found_h1 {
link.name = text.into_string();
break;
}
}
_ => (),
}
}
}

return Ok(SummaryItem::Link(link));
}
Expand Down
2 changes: 1 addition & 1 deletion test_book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
- [Tables](individual/table.md)
- [Tasks](individual/task.md)
- [Strikethrough](individual/strikethrough.md)
- [Mixed](individual/mixed.md)
- [](individual/mixed.md)
- [Languages](languages/README.md)
- [Syntax Highlight](languages/highlight.md)
- [Rust Specific](rust/README.md)
Expand Down