switch the markdown parser yet again
This commit is contained in:
parent
00e9473b0f
commit
872bf84a0e
@ -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"
|
||||||
|
@ -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
|
||||||
|
// Therefore the `[alt]` is rendered in html as subtitle
|
||||||
|
// and the `[](url "title")` `title` is rendered as `alt` attribute
|
||||||
|
Tag::Image {
|
||||||
|
link_type,
|
||||||
|
dest_url,
|
||||||
|
title,
|
||||||
|
id,
|
||||||
|
} => {
|
||||||
|
println!(
|
||||||
|
"Image link_type: {:?} url: {} title: {} id: {}",
|
||||||
|
link_type, dest_url, title, id
|
||||||
|
);
|
||||||
|
// TODO src set
|
||||||
|
Event::Html(
|
||||||
|
format!(
|
||||||
|
r#"<figure>
|
||||||
|
<img
|
||||||
|
alt="{alt}"
|
||||||
|
src="{src}"
|
||||||
|
/>
|
||||||
|
"#,
|
||||||
|
alt = title,
|
||||||
|
src = dest_url,
|
||||||
|
)
|
||||||
|
.into(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
_ => event,
|
||||||
|
},
|
||||||
|
Event::End(TagEnd::Image) => Event::Html("</figure>".into()),
|
||||||
|
_ => event,
|
||||||
|
});
|
||||||
|
|
||||||
let root = parse_document(&arena, markdown, &options);
|
// Write to String buffer.
|
||||||
|
let mut html = String::new();
|
||||||
// fn iter_nodes<'a, F>(node: &'a AstNode<'a>, f: &F)
|
pulldown_cmark::html::push_html(&mut html, parser);
|
||||||
// where
|
return html;
|
||||||
// F: Fn(&'a AstNode<'a>),
|
|
||||||
// {
|
|
||||||
// f(node);
|
|
||||||
// for c in node.children() {
|
|
||||||
// iter_nodes(c, f);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// iter_nodes(root, &mut |node| match &mut node.data.borrow_mut().value {
|
|
||||||
// &mut NodeValue::Text(ref mut _text) => {
|
|
||||||
// // let orig = std::mem::replace(text, String::new());
|
|
||||||
// // *text = orig.replace("my", "your");
|
|
||||||
// }
|
|
||||||
// &mut NodeValue::Image(ref mut img) => {
|
|
||||||
// let title = img.title;
|
|
||||||
// let mut fig_caption = Node::new(NodeValue::Text("Figure Caption".into()));
|
|
||||||
// let mut fig = Node::new(NodeValue::HtmlInline("ahoj".to_string()));
|
|
||||||
// *node = fig;
|
|
||||||
// // NodeValue::HtmlInline("".to_string());
|
|
||||||
// }
|
|
||||||
// _ => (),
|
|
||||||
// });
|
|
||||||
|
|
||||||
let mut html = vec![];
|
|
||||||
format_html_with_plugins(root, &options, &mut html, &plugins).unwrap();
|
|
||||||
return String::from_utf8(html).unwrap();
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user