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

This commit is contained in:
wkevin 2023-01-09 16:11:18 +08:00
parent 41a6f0d43e
commit 4288b52555
2 changed files with 26 additions and 1 deletions

View File

@ -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};
@ -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));
}

View File

@ -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)