diff --git a/Cargo.toml b/Cargo.toml index 83ecf3e..2d9e329 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ dashmap = { version = "6.1", features = ["serde"] } dyn-clone = "1.0" fastwebsockets = { version = "0.8", features = ["upgrade"] } futures = { version = "0.3", default-features = false } -html5ever = "0.26" +html5ever = "0.27" http-body = "1.0.1" http-body-util = "0.1.2" hyper = { version = "1.4", features = ["client", "server", "http1"] } @@ -35,14 +35,14 @@ hyper-util = { version = "0.1.8", features = [ "client-legacy", "http1", ] } -markup5ever_rcdom = "0.2" +markup5ever_rcdom = "0.3" minijinja = { version = "2.3" } notify-debouncer-mini = { version = "0.4", default-features = false } once_cell = "1" opener = "0.7" parking_lot = "0.12" promptly = "0.3" -pulldown-cmark = "0.9" +pulldown-cmark = "0.12" rayon = "1.6" serde = { version = "1", features = ["derive", "rc"] } serde_json = "1" diff --git a/src/markdown/mod.rs b/src/markdown/mod.rs index f8839a6..84dee5f 100644 --- a/src/markdown/mod.rs +++ b/src/markdown/mod.rs @@ -1,5 +1,5 @@ use pulldown_cmark::Event::{Code, End, HardBreak, Rule, SoftBreak, Start, Text}; -use pulldown_cmark::{Options, Parser, Tag}; +use pulldown_cmark::{Options, Parser, Tag, TagEnd}; mod render; mod visitor; @@ -121,7 +121,7 @@ pub fn count_words(markdown: &str) -> usize { fn start_tag(tag: &Tag, buffer: &mut String) { match tag { Tag::CodeBlock(_) | Tag::List(_) => fresh_line(buffer), - Tag::Link(_, _, title) => { + Tag::Link { title, .. } => { if !title.is_empty() { buffer.push_str(title); } @@ -130,15 +130,15 @@ fn start_tag(tag: &Tag, buffer: &mut String) { } } -fn end_tag(tag: &Tag, buffer: &mut String) { +fn end_tag(tag: &TagEnd, buffer: &mut String) { match tag { - Tag::Table(_) - | Tag::TableHead - | Tag::TableRow - | Tag::Heading(..) - | Tag::BlockQuote - | Tag::CodeBlock(_) - | Tag::Item => fresh_line(buffer), + TagEnd::Table + | TagEnd::TableHead + | TagEnd::TableRow + | TagEnd::Heading { .. } + | TagEnd::BlockQuote(..) + | TagEnd::CodeBlock + | TagEnd::Item => fresh_line(buffer), _ => (), } } diff --git a/src/markdown/render.rs b/src/markdown/render.rs index 6f48d82..d06e29f 100644 --- a/src/markdown/render.rs +++ b/src/markdown/render.rs @@ -8,6 +8,7 @@ use crate::{ use minijinja::{context, Environment}; use once_cell::sync::Lazy; +use pulldown_cmark::TagEnd; use pulldown_cmark::*; use serde::Serialize; use syntect::{ @@ -78,12 +79,12 @@ pub struct Heading<'a> { } impl<'a> Heading<'a> { - fn new(level: usize, id: Option<&'a str>) -> Self { + fn new(level: usize, id: Option) -> Self { Heading { toc: Toc { depth: level, level, - id: id.map(|i| i.to_owned()), + id, title: String::new(), }, events: Vec::new(), @@ -264,12 +265,26 @@ impl<'a> MarkdownRender<'a> { self.code_block_fenced = Some(name.clone()); Visiting::Ignore } - Tag::Image(..) => { + Tag::Image { + dest_url, title, .. + } => { + let alt = self.image_alt.take().unwrap_or_else(|| CowStr::from("")); + self.processing_image = false; + self.processing_image = true; - Visiting::Ignore + // Add loading="lazy" attribute for markdown image. + Visiting::Event(Event::Html( + format!( + "\"{alt}\"" + ) + .into(), + )) } - Tag::Heading(level, id, _) => { - self.curr_heading = Some(Heading::new(*level as usize, *id)); + Tag::Heading { level, id, .. } => { + self.curr_heading = Some(Heading::new( + *level as usize, + id.as_ref().map(|i| i.to_string()), + )); Visiting::Ignore } _ => { @@ -283,23 +298,17 @@ impl<'a> MarkdownRender<'a> { } } - fn visit_end_tag(&mut self, tag: &Tag<'a>) -> Visiting { + fn visit_end_tag(&mut self, tag: &TagEnd) -> Visiting { match tag { - Tag::Image(_, src, title) => { - let alt = self.image_alt.take().unwrap_or_else(|| CowStr::from("")); + TagEnd::Image => { self.processing_image = false; - - // Add loading="lazy" attribute for markdown image. - Visiting::Event(Event::Html( - format!("\"{alt}\"") - .into(), - )) + Visiting::Ignore } - Tag::CodeBlock(_) => { + TagEnd::CodeBlock => { self.code_block_fenced = None; Visiting::Ignore } - Tag::Heading(..) => { + TagEnd::Heading(..) => { if let Some(mut heading) = self.curr_heading.take() { self.levels.insert(heading.toc.level); // Render heading event.