标题粗体显示:h1-h6 标题在正文中标粗区分
This commit is contained in:
28
src/book.rs
28
src/book.rs
@@ -46,6 +46,7 @@ fn tag_name_from(tag_content: &str) -> &str {
|
||||
pub fn strip_html(input: &str) -> String {
|
||||
let mut out = String::with_capacity(input.len());
|
||||
let mut pos = 0;
|
||||
let mut heading_level: Option<u32> = None;
|
||||
|
||||
while pos < input.len() {
|
||||
// Find next '<'
|
||||
@@ -63,7 +64,14 @@ pub fn strip_html(input: &str) -> String {
|
||||
|
||||
// Emit text before the tag
|
||||
if tag_start > pos {
|
||||
out.push_str(&decode_entities(&input[pos..tag_start]));
|
||||
let text = decode_entities(&input[pos..tag_start]);
|
||||
if heading_level.is_some() {
|
||||
out.push_str("\x01");
|
||||
out.push_str(&text);
|
||||
out.push_str("\x02");
|
||||
} else {
|
||||
out.push_str(&text);
|
||||
}
|
||||
}
|
||||
|
||||
// Find '>' to close the tag
|
||||
@@ -115,10 +123,24 @@ pub fn strip_html(input: &str) -> String {
|
||||
"/li" | "/dd" | "/dt" | "/ol" | "/ul" => {
|
||||
pos = tag_end + 1;
|
||||
}
|
||||
"p" | "div" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "blockquote" => {
|
||||
"h1" => { heading_level = Some(1); pos = tag_end + 1; }
|
||||
"h2" => { heading_level = Some(2); pos = tag_end + 1; }
|
||||
"h3" => { heading_level = Some(3); pos = tag_end + 1; }
|
||||
"h4" => { heading_level = Some(4); pos = tag_end + 1; }
|
||||
"h5" => { heading_level = Some(5); pos = tag_end + 1; }
|
||||
"h6" => { heading_level = Some(6); pos = tag_end + 1; }
|
||||
"p" | "div" | "blockquote" => {
|
||||
pos = tag_end + 1;
|
||||
}
|
||||
"/p" | "/div" | "/blockquote" | "/h1" | "/h2" | "/h3" | "/h4" | "/h5" | "/h6" => {
|
||||
"/p" | "/div" | "/blockquote" => {
|
||||
if !out.is_empty() && !out.ends_with('\n') {
|
||||
out.push('\n');
|
||||
}
|
||||
out.push('\n');
|
||||
pos = tag_end + 1;
|
||||
}
|
||||
"/h1" | "/h2" | "/h3" | "/h4" | "/h5" | "/h6" => {
|
||||
heading_level = None;
|
||||
if !out.is_empty() && !out.ends_with('\n') {
|
||||
out.push('\n');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user