switch the markdown parser yet again

This commit is contained in:
Michal Vanko 2024-03-02 22:08:03 +01:00
parent 00e9473b0f
commit 872bf84a0e
2 changed files with 48 additions and 52 deletions

View File

@ -10,7 +10,7 @@ askama = { version = "0.12", features = ["with-axum", "mime", "mime_guess"] }
askama_axum = "0.4.0" askama_axum = "0.4.0"
axum = "0.7.3" axum = "0.7.3"
chrono = { version = "0.4.31", features = ["serde"] } chrono = { version = "0.4.31", features = ["serde"] }
comrak = { version = "0.21", features = ["shortcodes"] } pulldown-cmark = { version = "0.10" }
gray_matter = "0.2.6" gray_matter = "0.2.6"
rss = "2.0.7" rss = "2.0.7"
serde = "1.0.195" serde = "1.0.195"

View File

@ -2,8 +2,8 @@ use std::path::Path;
use axum::http::StatusCode; use axum::http::StatusCode;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use comrak::{format_html_with_plugins, parse_document, Arena, Options};
use gray_matter::{engine::YAML, Matter}; use gray_matter::{engine::YAML, Matter};
use pulldown_cmark::{Event, Options, Parser, Tag, TagEnd};
use serde::{de::DeserializeOwned, Deserialize, Deserializer}; use serde::{de::DeserializeOwned, Deserialize, Deserializer};
use tokio::fs; use tokio::fs;
@ -60,56 +60,52 @@ pub async fn parse_post<'de, Metadata: DeserializeOwned>(
} }
fn parse_html(markdown: &str) -> String { fn parse_html(markdown: &str) -> String {
let mut options = Options::default(); let mut options = Options::empty();
options.parse.smart = true; options.insert(Options::ENABLE_TABLES);
options.parse.relaxed_autolinks = true; options.insert(Options::ENABLE_FOOTNOTES);
options.extension.strikethrough = true; options.insert(Options::ENABLE_STRIKETHROUGH);
// options.extension.tagfilter = true; options.insert(Options::ENABLE_TASKLISTS);
options.extension.table = true; options.insert(Options::ENABLE_SMART_PUNCTUATION);
options.extension.autolink = true; options.insert(Options::ENABLE_HEADING_ATTRIBUTES);
options.extension.tasklist = true;
options.extension.superscript = true;
options.extension.header_ids = Some("".to_string());
options.extension.footnotes = false;
options.extension.description_lists = false;
options.extension.multiline_block_quotes = false;
options.extension.shortcodes = true;
options.render.hardbreaks = true;
options.render.github_pre_lang = true;
options.render.full_info_string = true;
options.render.unsafe_ = true;
// The returned nodes are created in the supplied Arena, and are bound by its lifetime. let parser = Parser::new_ext(&markdown, options).map(|event| match event {
let arena = Arena::new(); Event::Start(ref tag) => match tag {
// Parsing images considers `alt` attribute as inner `Text` event
let root = parse_document(&arena, markdown, &options); // Therefore the `[alt]` is rendered in html as subtitle
// and the `[](url "title")` `title` is rendered as `alt` attribute
// fn iter_nodes<'a, F>(node: &'a AstNode<'a>, f: &F) Tag::Image {
// where link_type,
// F: Fn(&'a AstNode<'a>), dest_url,
// { title,
// f(node); id,
// for c in node.children() { } => {
// iter_nodes(c, f); println!(
// } "Image link_type: {:?} url: {} title: {} id: {}",
// } link_type, dest_url, title, id
);
// iter_nodes(root, &mut |node| match &mut node.data.borrow_mut().value { // TODO src set
// &mut NodeValue::Text(ref mut _text) => { Event::Html(
// // let orig = std::mem::replace(text, String::new()); format!(
// // *text = orig.replace("my", "your"); r#"<figure>
// } <img
// &mut NodeValue::Image(ref mut img) => { alt="{alt}"
// let title = img.title; src="{src}"
// let mut fig_caption = Node::new(NodeValue::Text("Figure Caption".into())); />
// let mut fig = Node::new(NodeValue::HtmlInline("ahoj".to_string())); "#,
// *node = fig; alt = title,
// // NodeValue::HtmlInline("".to_string()); src = dest_url,
// } )
// _ => (), .into(),
// }); )
}
let mut html = vec![]; _ => event,
format_html_with_plugins(root, &options, &mut html, &plugins).unwrap(); },
return String::from_utf8(html).unwrap(); Event::End(TagEnd::Image) => Event::Html("</figure>".into()),
_ => event,
});
// Write to String buffer.
let mut html = String::new();
pulldown_cmark::html::push_html(&mut html, parser);
return html;
} }