events: Enable more markdown syntax
Enable markdown extensions that produces spec-compliant HTML.
This commit is contained in:
parent
a710229d9d
commit
21bda6678d
@ -634,11 +634,13 @@ pub struct CustomEventContent {
|
|||||||
|
|
||||||
#[cfg(feature = "markdown")]
|
#[cfg(feature = "markdown")]
|
||||||
pub(crate) fn parse_markdown(text: &str) -> Option<String> {
|
pub(crate) fn parse_markdown(text: &str) -> Option<String> {
|
||||||
use pulldown_cmark::{Event, Parser, Tag};
|
use pulldown_cmark::{Event, Options, Parser, Tag};
|
||||||
|
|
||||||
|
const OPTIONS: Options = Options::ENABLE_TABLES.union(Options::ENABLE_STRIKETHROUGH);
|
||||||
|
|
||||||
let mut found_first_paragraph = false;
|
let mut found_first_paragraph = false;
|
||||||
|
|
||||||
let has_markdown = Parser::new(text).any(|ref event| {
|
let has_markdown = Parser::new_ext(text, OPTIONS).any(|ref event| {
|
||||||
let is_text = matches!(event, Event::Text(_));
|
let is_text = matches!(event, Event::Text(_));
|
||||||
let is_break = matches!(event, Event::SoftBreak | Event::HardBreak);
|
let is_break = matches!(event, Event::SoftBreak | Event::HardBreak);
|
||||||
let is_first_paragraph_start = if matches!(event,
|
let is_first_paragraph_start = if matches!(event,
|
||||||
@ -667,7 +669,7 @@ pub(crate) fn parse_markdown(text: &str) -> Option<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut html_body = String::new();
|
let mut html_body = String::new();
|
||||||
pulldown_cmark::html::push_html(&mut html_body, Parser::new(text));
|
pulldown_cmark::html::push_html(&mut html_body, Parser::new_ext(text, OPTIONS));
|
||||||
|
|
||||||
Some(html_body)
|
Some(html_body)
|
||||||
}
|
}
|
||||||
|
@ -324,6 +324,31 @@ fn markdown_detection() {
|
|||||||
formatted_body.unwrap();
|
formatted_body.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(feature = "markdown")]
|
||||||
|
fn markdown_options() {
|
||||||
|
use ruma_common::events::room::message::FormattedBody;
|
||||||
|
|
||||||
|
// Tables
|
||||||
|
let formatted_body = FormattedBody::markdown(
|
||||||
|
"|head1|head2|\n\
|
||||||
|
|---|---|\n\
|
||||||
|
|body1|body2|\
|
||||||
|
",
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
formatted_body.unwrap().body,
|
||||||
|
"<table>\
|
||||||
|
<thead><tr><th>head1</th><th>head2</th></tr></thead>\
|
||||||
|
<tbody>\n<tr><td>body1</td><td>body2</td></tr>\n</tbody>\
|
||||||
|
</table>\n"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Strikethrough
|
||||||
|
let formatted_body = FormattedBody::markdown("A message with a ~~strike~~");
|
||||||
|
assert_eq!(formatted_body.unwrap().body, "<p>A message with a <del>strike</del></p>\n");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn verification_request_deserialization() {
|
fn verification_request_deserialization() {
|
||||||
let user_id = user_id!("@example2:localhost");
|
let user_id = user_id!("@example2:localhost");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user