From ebbde534af55424905522b7be3b7ac6016eb8e4d Mon Sep 17 00:00:00 2001 From: silverpill Date: Fri, 31 Mar 2023 18:39:15 +0000 Subject: [PATCH] Update comrak crate --- Cargo.lock | 5 +++-- mitra-utils/Cargo.toml | 2 +- mitra-utils/src/markdown.rs | 41 ++++++++++++++++++++++++------------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7683437..2444318 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -576,8 +576,9 @@ dependencies = [ [[package]] name = "comrak" -version = "0.17.1" -source = "git+https://github.com/kivikakk/comrak?rev=93a94858168536704c5772d5573cdfce0e4e7ed4#93a94858168536704c5772d5573cdfce0e4e7ed4" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "482aa5695bca086022be453c700a40c02893f1ba7098a2c88351de55341ae894" dependencies = [ "entities", "memchr", diff --git a/mitra-utils/Cargo.toml b/mitra-utils/Cargo.toml index 49e93b3..b0b1fe2 100644 --- a/mitra-utils/Cargo.toml +++ b/mitra-utils/Cargo.toml @@ -15,7 +15,7 @@ bs58 = "0.4.0" # Used for working with dates chrono = { version = "0.4.23", default-features = false, features = ["std"] } # Used for parsing markdown -comrak = { git = "https://github.com/kivikakk/comrak", rev = "93a94858168536704c5772d5573cdfce0e4e7ed4", default-features = false } +comrak = { version = "0.18.0", default-features = false } # Used to guess media type of a file mime_guess = "2.0.3" mime-sniffer = "0.1.2" diff --git a/mitra-utils/src/markdown.rs b/mitra-utils/src/markdown.rs index f8cfdc4..b8bc3a4 100644 --- a/mitra-utils/src/markdown.rs +++ b/mitra-utils/src/markdown.rs @@ -1,4 +1,7 @@ +use std::cell::RefCell; + use comrak::{ + arena_tree::Node, format_commonmark, format_html, nodes::{Ast, AstNode, ListType, NodeValue}, @@ -59,6 +62,16 @@ fn node_to_markdown<'a>( Ok(markdown) } +fn replace_node_value(node: &AstNode, value: NodeValue) -> () { + let mut borrowed_node = node.data.borrow_mut(); + *borrowed_node = Ast::new(value, borrowed_node.sourcepos.start); +} + +fn create_node<'a>(value: NodeValue) -> AstNode<'a> { + // Position doesn't matter + Node::new(RefCell::new(Ast::new(value, (0, 1).into()))) +} + fn replace_with_markdown<'a>( node: &'a AstNode<'a>, options: &ComrakOptions, @@ -69,8 +82,7 @@ fn replace_with_markdown<'a>( child.detach(); }; let text = NodeValue::Text(markdown); - let mut borrowed_node = node.data.borrow_mut(); - *borrowed_node = Ast::new(text); + replace_node_value(node, text); Ok(()) } @@ -90,8 +102,7 @@ fn fix_microsyntaxes<'a>( }; }; let text = NodeValue::Text(link_text); - let mut borrowed_node = node.data.borrow_mut(); - *borrowed_node = Ast::new(text); + replace_node_value(node, text); }; }; }; @@ -151,10 +162,9 @@ pub fn markdown_lite_to_html(text: &str) -> Result { child.detach(); }; let text = NodeValue::Text(markdown); - let text_node = arena.alloc(AstNode::from(text)); + let text_node = arena.alloc(create_node(text)); node.append(text_node); - let mut borrowed_node = node.data.borrow_mut(); - *borrowed_node = Ast::new(NodeValue::Paragraph); + replace_node_value(node, NodeValue::Paragraph); }, NodeValue::Image(_) => replace_with_markdown(node, &options)?, NodeValue::List(_) => { @@ -179,13 +189,15 @@ pub fn markdown_lite_to_html(text: &str) -> Result { list_prefix_markdown.replace('1', &item_index_str); }; }; - let list_prefix = NodeValue::Text(list_prefix_markdown); if !replacements.is_empty() { // Insert line break before next list item let linebreak = NodeValue::LineBreak; - replacements.push(arena.alloc(AstNode::from(linebreak))); + let linebreak_node = arena.alloc(create_node(linebreak)); + replacements.push(linebreak_node); }; - replacements.push(arena.alloc(AstNode::from(list_prefix))); + let list_prefix = NodeValue::Text(list_prefix_markdown); + let list_prefix_node = arena.alloc(create_node(list_prefix)); + replacements.push(list_prefix_node); for content_node in contents { replacements.push(content_node); }; @@ -194,8 +206,7 @@ pub fn markdown_lite_to_html(text: &str) -> Result { for child_node in replacements { node.append(child_node); }; - let mut borrowed_node = node.data.borrow_mut(); - *borrowed_node = Ast::new(NodeValue::Paragraph); + replace_node_value(node, NodeValue::Paragraph); }, NodeValue::Link(_) => fix_microsyntaxes(node)?, _ => (), @@ -236,8 +247,10 @@ pub fn markdown_basic_to_html(text: &str) -> Result { if let Some(last_child) = node.last_child() { let last_child_value = &last_child.data.borrow().value; if !matches!(last_child_value, NodeValue::LineBreak) { - let line_break = AstNode::from(NodeValue::LineBreak); - node.append(arena.alloc(line_break)); + let line_break = NodeValue::LineBreak; + let line_break_node = + arena.alloc(create_node(line_break)); + node.append(line_break_node); }; }; };