Matrix: Parse hard breaks, block quotes, and spoilers
This commit is contained in:
parent
5e2b6bee64
commit
3b47de1fde
1 changed files with 54 additions and 3 deletions
|
@ -23,10 +23,21 @@ pub fn convert_matrix(message: &str) -> MessageContent {
|
|||
| local_name!("b")
|
||||
| local_name!("em")
|
||||
| local_name!("i")
|
||||
| local_name!("a") => {
|
||||
| local_name!("s")
|
||||
| local_name!("u")
|
||||
| local_name!("a")
|
||||
| local_name!("blockquote") => {
|
||||
parents.push(components);
|
||||
components = vec![];
|
||||
}
|
||||
|
||||
local_name!("span") => {
|
||||
let attrs = element.attributes.borrow();
|
||||
if attrs.get("data-mx-spoiler").is_some() {
|
||||
parents.push(components);
|
||||
components = vec![];
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
@ -58,6 +69,12 @@ pub fn convert_matrix(message: &str) -> MessageContent {
|
|||
local_name!("em") | local_name!("i") => {
|
||||
construct_component!(MessageComponent::Italic)
|
||||
}
|
||||
local_name!("s") => {
|
||||
construct_component!(MessageComponent::Strikethrough)
|
||||
}
|
||||
local_name!("u") => {
|
||||
construct_component!(MessageComponent::Underline)
|
||||
}
|
||||
local_name!("a") => {
|
||||
if let Some(mut parent_components) = parents.pop() {
|
||||
let attrs = element.attributes.borrow();
|
||||
|
@ -73,6 +90,22 @@ pub fn convert_matrix(message: &str) -> MessageContent {
|
|||
components = parent_components;
|
||||
}
|
||||
}
|
||||
local_name!("br") => {
|
||||
components.push(MessageComponent::HardBreak);
|
||||
}
|
||||
local_name!("blockquote") => {
|
||||
construct_component!(MessageComponent::BlockQuote)
|
||||
}
|
||||
local_name!("span") => {
|
||||
let attrs = element.attributes.borrow();
|
||||
if let Some(spoiler_reason) = attrs.get("data-mx-spoiler") {
|
||||
construct_component!(|inner| MessageComponent::Spoiler {
|
||||
reason: (!spoiler_reason.is_empty())
|
||||
.then(|| spoiler_reason.to_string()),
|
||||
content: inner,
|
||||
})
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
@ -142,10 +175,9 @@ fn simple_matrix_parsing() {
|
|||
|
||||
let html =
|
||||
r#"<strong>hello! <i><></i></strong> <a href="https://example.com/">example</a>"#;
|
||||
let ast = convert_matrix(html);
|
||||
|
||||
assert_eq!(
|
||||
ast,
|
||||
convert_matrix(html),
|
||||
vec![
|
||||
Bold(vec![
|
||||
Plain("hello! ".to_string(),),
|
||||
|
@ -159,3 +191,22 @@ fn simple_matrix_parsing() {
|
|||
]
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spoiler_parsing() {
|
||||
use MessageComponent::*;
|
||||
|
||||
let html = r#"<span data-mx-spoiler>the <em>whole</em> island is populated by lesbians</span>"#;
|
||||
|
||||
assert_eq!(
|
||||
convert_matrix(html),
|
||||
vec![Spoiler {
|
||||
reason: None,
|
||||
content: vec![
|
||||
Plain("the ".to_string()),
|
||||
Italic(vec![Plain("whole".to_string())]),
|
||||
Plain(" island is populated by lesbians".to_string())
|
||||
]
|
||||
}]
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue