Fix rendering of mentions in bio

This commit is contained in:
silverpill 2022-12-18 23:14:27 +00:00
parent ee7a61833d
commit a2f7140164

View file

@ -59,6 +59,46 @@ fn node_to_markdown<'a>(
Ok(markdown) Ok(markdown)
} }
fn replace_with_markdown<'a>(
node: &'a AstNode<'a>,
options: &ComrakOptions,
) -> Result<(), MarkdownError> {
// Replace node with text node containing markdown
let markdown = node_to_markdown(node, options)?;
for child in node.children() {
child.detach();
};
let text = NodeValue::Text(markdown.as_bytes().to_vec());
let mut borrowed_node = node.data.borrow_mut();
*borrowed_node = Ast::new(text);
Ok(())
}
fn fix_microsyntaxes<'a>(
node: &'a AstNode<'a>,
) -> Result<(), MarkdownError> {
if let Some(prev) = node.previous_sibling() {
if let NodeValue::Text(ref prev_text) = prev.data.borrow().value {
let prev_text = String::from_utf8(prev_text.to_vec())?;
// Remove autolink if mention or object link syntax is found
if prev_text.ends_with('@') || prev_text.ends_with("[[") {
let mut link_text = vec![];
for child in node.children() {
child.detach();
let child_value = &child.data.borrow().value;
if let NodeValue::Text(child_text) = child_value {
link_text.extend(child_text);
};
};
let text = NodeValue::Text(link_text);
let mut borrowed_node = node.data.borrow_mut();
*borrowed_node = Ast::new(text);
};
};
};
Ok(())
}
fn document_to_html<'a>( fn document_to_html<'a>(
document: &'a AstNode<'a>, document: &'a AstNode<'a>,
options: &ComrakOptions, options: &ComrakOptions,
@ -117,17 +157,7 @@ pub fn markdown_lite_to_html(text: &str) -> Result<String, MarkdownError> {
let mut borrowed_node = node.data.borrow_mut(); let mut borrowed_node = node.data.borrow_mut();
*borrowed_node = Ast::new(NodeValue::Paragraph); *borrowed_node = Ast::new(NodeValue::Paragraph);
}, },
// Inlines NodeValue::Image(_) => replace_with_markdown(node, &options)?,
NodeValue::Image(_) => {
// Replace node with text node containing markdown
let markdown = node_to_markdown(node, &options)?;
for child in node.children() {
child.detach();
};
let text = NodeValue::Text(markdown.as_bytes().to_vec());
let mut borrowed_node = node.data.borrow_mut();
*borrowed_node = Ast::new(text);
},
NodeValue::List(_) => { NodeValue::List(_) => {
// Replace list and list item nodes // Replace list and list item nodes
// while preserving their contents // while preserving their contents
@ -169,27 +199,7 @@ pub fn markdown_lite_to_html(text: &str) -> Result<String, MarkdownError> {
let mut borrowed_node = node.data.borrow_mut(); let mut borrowed_node = node.data.borrow_mut();
*borrowed_node = Ast::new(NodeValue::Paragraph); *borrowed_node = Ast::new(NodeValue::Paragraph);
}, },
NodeValue::Link(_) => { NodeValue::Link(_) => fix_microsyntaxes(node)?,
if let Some(prev) = node.previous_sibling() {
if let NodeValue::Text(ref prev_text) = prev.data.borrow().value {
let prev_text = String::from_utf8(prev_text.to_vec())?;
// Remove autolink if mention or object link syntax is found
if prev_text.ends_with('@') || prev_text.ends_with("[[") {
let mut link_text = vec![];
for child in node.children() {
child.detach();
let child_value = &child.data.borrow().value;
if let NodeValue::Text(child_text) = child_value {
link_text.extend(child_text);
};
};
let text = NodeValue::Text(link_text);
let mut borrowed_node = node.data.borrow_mut();
*borrowed_node = Ast::new(text);
};
};
};
},
_ => (), _ => (),
}; };
Ok(()) Ok(())
@ -216,10 +226,10 @@ pub fn markdown_basic_to_html(text: &str) -> Result<String, MarkdownError> {
match node_value { match node_value {
NodeValue::Document | NodeValue::Document |
NodeValue::Text(_) | NodeValue::Text(_) |
NodeValue::Link(_) |
NodeValue::SoftBreak | NodeValue::SoftBreak |
NodeValue::LineBreak NodeValue::LineBreak
=> (), => (),
NodeValue::Link(_) => fix_microsyntaxes(node)?,
NodeValue::Paragraph => { NodeValue::Paragraph => {
if node.next_sibling().is_some() { if node.next_sibling().is_some() {
// If this is not the last paragraph, // If this is not the last paragraph,
@ -234,16 +244,7 @@ pub fn markdown_basic_to_html(text: &str) -> Result<String, MarkdownError> {
}; };
}; };
}, },
_ => { _ => replace_with_markdown(node, &options)?,
// Replace node with text node containing markdown
let markdown = node_to_markdown(node, &options)?;
for child in node.children() {
child.detach();
};
let text = NodeValue::Text(markdown.as_bytes().to_vec());
let mut borrowed_node = node.data.borrow_mut();
*borrowed_node = Ast::new(text);
},
}; };
Ok(()) Ok(())
})?; })?;
@ -317,6 +318,13 @@ mod tests {
assert_eq!(html, expected_html); assert_eq!(html, expected_html);
} }
#[test]
fn test_markdown_basic_to_html_mention() {
let text = "@user@example.org test";
let html = markdown_basic_to_html(text).unwrap();
assert_eq!(html, format!("<p>{}</p>", text));
}
#[test] #[test]
fn test_markdown_to_html() { fn test_markdown_to_html() {
let text = "# heading\n\ntest"; let text = "# heading\n\ntest";