Skip to content

Commit

Permalink
Upgrade pulldown-cmark
Browse files Browse the repository at this point in the history
  • Loading branch information
Folyd committed Sep 20, 2024
1 parent 056de37 commit d868d3c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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"
Expand Down
20 changes: 10 additions & 10 deletions src/markdown/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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),
_ => (),
}
}
Expand Down
43 changes: 26 additions & 17 deletions src/markdown/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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<String>) -> Self {
Heading {
toc: Toc {
depth: level,
level,
id: id.map(|i| i.to_owned()),
id,
title: String::new(),
},
events: Vec::new(),
Expand Down Expand Up @@ -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!(
"<img src=\"{dest_url}\" alt=\"{alt}\" title=\"{title}\" loading=\"lazy\">"
)
.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
}
_ => {
Expand All @@ -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!("<img src=\"{src}\" alt=\"{alt}\" title=\"{title}\" loading=\"lazy\">")
.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.
Expand Down

0 comments on commit d868d3c

Please sign in to comment.