Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
Update comrak crate
Browse files Browse the repository at this point in the history
  • Loading branch information
silverpill committed Apr 2, 2023
1 parent 300d2ef commit ebbde53
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mitra-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
41 changes: 27 additions & 14 deletions mitra-utils/src/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::cell::RefCell;

use comrak::{
arena_tree::Node,
format_commonmark,
format_html,
nodes::{Ast, AstNode, ListType, NodeValue},
Expand Down Expand Up @@ -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,
Expand All @@ -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(())
}

Expand All @@ -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);
};
};
};
Expand Down Expand Up @@ -151,10 +162,9 @@ pub fn markdown_lite_to_html(text: &str) -> Result<String, MarkdownError> {
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(_) => {
Expand All @@ -179,13 +189,15 @@ pub fn markdown_lite_to_html(text: &str) -> Result<String, MarkdownError> {
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);
};
Expand All @@ -194,8 +206,7 @@ pub fn markdown_lite_to_html(text: &str) -> Result<String, MarkdownError> {
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)?,
_ => (),
Expand Down Expand Up @@ -236,8 +247,10 @@ pub fn markdown_basic_to_html(text: &str) -> Result<String, MarkdownError> {
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);
};
};
};
Expand Down

0 comments on commit ebbde53

Please sign in to comment.