fix(cards): connector cards render HTML text instead of showing the tags
- MessageCard text and fact values with HTML tags are marked by the legacy converter and parsed as HTML - Tables in that HTML become table blocks instead of one long raw line - Issue #60 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -816,7 +816,7 @@ fn parse_text_block(value: &Value) -> Option<CardElement> {
816
816
string_field(value, "style").is_some_and(|style| style.eq_ignore_ascii_case("heading"));
817
817
let size = parse_text_size(string_field(value, "size"));
818
818
Some(CardElement::Text(CardText {
819
spans: html_to_spans(&card_markdown_to_html(&text)),
819
spans: card_text_spans(value, &text),
820
820
size: match size {
821
821
TextSize::Default if heading => TextSize::Large,
822
822
other => other,
@@ -1001,6 +1001,18 @@ fn parse_container(value: &Value) -> Option<CardElement> {
1001
1001
})
1002
1002
}
1003
1003
1004
fn card_text_spans(value: &Value, text: &str) -> Vec<Span> {
1005
if value
1006
.get(crate::legacy_card::HTML_TEXT)
1007
.and_then(Value::as_bool)
1008
== Some(true)
1009
{
1010
html_to_spans(text)
1011
} else {
1012
html_to_spans(&card_markdown_to_html(text))
1013
}
1014
}
1015
1004
1016
fn parse_fact_set(value: &Value) -> Option<CardElement> {
1005
1017
let facts: Vec<CardFact> = value
1006
1018
.get("facts")?
@@ -1011,7 +1023,7 @@ fn parse_fact_set(value: &Value) -> Option<CardElement> {
1011
1023
let text = string_field(fact, "value").unwrap_or_default();
1012
1024
Some(CardFact {
1013
1025
title: format_card_dates(title),
1014
value: html_to_spans(&card_markdown_to_html(&format_card_dates(text))),
1026
value: card_text_spans(fact, &format_card_dates(text)),
1015
1027
})
1016
1028
})
1017
1029
.collect();
@@ -816,7 +816,7 @@ fn parse_text_block(value: &Value) -> Option<CardElement> {816
string_field(value, "style").is_some_and(|style| style.eq_ignore_ascii_case("heading"));816
string_field(value, "style").is_some_and(|style| style.eq_ignore_ascii_case("heading"));817
let size = parse_text_size(string_field(value, "size"));817
let size = parse_text_size(string_field(value, "size"));818
Some(CardElement::Text(CardText {818
Some(CardElement::Text(CardText {819
spans: html_to_spans(&card_markdown_to_html(&text)),819
spans: card_text_spans(value, &text),820
size: match size {820
size: match size {821
TextSize::Default if heading => TextSize::Large,821
TextSize::Default if heading => TextSize::Large,822
other => other,822
other => other,@@ -1001,6 +1001,18 @@ fn parse_container(value: &Value) -> Option<CardElement> {1001
})1001
})1002
}1002
}1003
1003
1004
fn card_text_spans(value: &Value, text: &str) -> Vec<Span> {1005
if value1006
.get(crate::legacy_card::HTML_TEXT)1007
.and_then(Value::as_bool)1008
== Some(true)1009
{1010
html_to_spans(text)1011
} else {1012
html_to_spans(&card_markdown_to_html(text))1013
}1014
}1015
1004
fn parse_fact_set(value: &Value) -> Option<CardElement> {1016
fn parse_fact_set(value: &Value) -> Option<CardElement> {1005
let facts: Vec<CardFact> = value1017
let facts: Vec<CardFact> = value1006
.get("facts")?1018
.get("facts")?@@ -1011,7 +1023,7 @@ fn parse_fact_set(value: &Value) -> Option<CardElement> {1011
let text = string_field(fact, "value").unwrap_or_default();1023
let text = string_field(fact, "value").unwrap_or_default();1012
Some(CardFact {1024
Some(CardFact {1013
title: format_card_dates(title),1025
title: format_card_dates(title),1014
value: html_to_spans(&card_markdown_to_html(&format_card_dates(text))),1026
value: card_text_spans(fact, &format_card_dates(text)),1015
})1027
})1016
})1028
})1017
.collect();1029
.collect();@@ -2,8 +2,18 @@
2
2
//! webhooks, legacy workflows) and hero or thumbnail cards. Teams draws them with title,
3
3
//! sections, facts and buttons; they are rewritten into an Adaptive Card here.
4
4
5
use std::sync::LazyLock;
6
7
use regex::Regex;
5
8
use serde_json::{Value, json};
6
9
10
/// Set on a converted `TextBlock` or fact whose text is HTML, which connector cards allow
11
/// besides markdown. The card parser then reads it as HTML instead of escaping the tags.
12
pub(crate) const HTML_TEXT: &str = "x-legacyHtml";
13
14
static HTML_TAG: LazyLock<Regex> =
15
LazyLock::new(|| Regex::new(r"</?[a-zA-Z][a-zA-Z0-9]*(\s[^<>]*)?/?>").unwrap());
16
7
17
const OPEN_URI: &str = "OpenUri";
8
18
const VIEW_ACTION: &str = "ViewAction";
9
19
const OPEN_URL_BUTTON: &str = "openUrl";
@@ -69,7 +79,29 @@ pub(crate) fn as_adaptive_card(root: &Value) -> Option<Value> {
69
79
if body.is_empty() && actions.is_empty() {
70
80
return None;
71
81
}
72
Some(json!({"type": "AdaptiveCard", "version": "1.4", "body": body, "actions": actions}))
82
let mut card =
83
json!({"type": "AdaptiveCard", "version": "1.4", "body": body, "actions": actions});
84
mark_html_text(&mut card);
85
Some(card)
86
}
87
88
fn mark_html_text(value: &mut Value) {
89
match value {
90
Value::Array(items) => items.iter_mut().for_each(mark_html_text),
91
Value::Object(object) => {
92
let html_in = |key: &str| {
93
object
94
.get(key)
95
.and_then(Value::as_str)
96
.is_some_and(|text| HTML_TAG.is_match(text))
97
};
98
if html_in("text") || html_in("value") {
99
object.insert(HTML_TEXT.to_owned(), Value::Bool(true));
100
}
101
object.values_mut().for_each(mark_html_text);
102
}
103
_ => {}
104
}
73
105
}
74
106
75
107
fn section_items(section: &Value) -> Vec<Value> {
@@ -214,6 +246,17 @@ mod tests {
214
246
assert_eq!(card.actions.len(), 1);
215
247
}
216
248
249
#[test]
250
fn html_in_connector_text_and_facts_is_read_as_html() {
251
let html_card = r#"{"@type": "MessageCard", "text": "Deploy <b>done</b><br>see <a href=\"https://example.com/run\">run</a>", "sections": [{"facts": [{"name": "Status", "value": "<i>green</i>"}]}]}"#;
252
let text = AdaptiveCard::parse(html_card)
253
.expect("connector card is shown")
254
.plain_text()
255
.unwrap_or_default();
256
assert!(text.contains("done") && text.contains("green"));
257
assert!(!text.contains('<'), "tags leaked: {text}");
258
}
259
217
260
#[test]
218
261
fn an_object_without_card_shape_stays_no_card() {
219
262
assert!(AdaptiveCard::parse("{}").is_none());
@@ -2,8 +2,18 @@2
//! webhooks, legacy workflows) and hero or thumbnail cards. Teams draws them with title,2
//! webhooks, legacy workflows) and hero or thumbnail cards. Teams draws them with title,3
//! sections, facts and buttons; they are rewritten into an Adaptive Card here.3
//! sections, facts and buttons; they are rewritten into an Adaptive Card here.4
4
5
use std::sync::LazyLock;6
7
use regex::Regex;5
use serde_json::{Value, json};8
use serde_json::{Value, json};6
9
10
/// Set on a converted `TextBlock` or fact whose text is HTML, which connector cards allow11
/// besides markdown. The card parser then reads it as HTML instead of escaping the tags.12
pub(crate) const HTML_TEXT: &str = "x-legacyHtml";13
14
static HTML_TAG: LazyLock<Regex> =15
LazyLock::new(|| Regex::new(r"</?[a-zA-Z][a-zA-Z0-9]*(\s[^<>]*)?/?>").unwrap());16
7
const OPEN_URI: &str = "OpenUri";17
const OPEN_URI: &str = "OpenUri";8
const VIEW_ACTION: &str = "ViewAction";18
const VIEW_ACTION: &str = "ViewAction";9
const OPEN_URL_BUTTON: &str = "openUrl";19
const OPEN_URL_BUTTON: &str = "openUrl";@@ -69,7 +79,29 @@ pub(crate) fn as_adaptive_card(root: &Value) -> Option<Value> {69
if body.is_empty() && actions.is_empty() {79
if body.is_empty() && actions.is_empty() {70
return None;80
return None;71
}81
}72
Some(json!({"type": "AdaptiveCard", "version": "1.4", "body": body, "actions": actions}))82
let mut card =83
json!({"type": "AdaptiveCard", "version": "1.4", "body": body, "actions": actions});84
mark_html_text(&mut card);85
Some(card)86
}87
88
fn mark_html_text(value: &mut Value) {89
match value {90
Value::Array(items) => items.iter_mut().for_each(mark_html_text),91
Value::Object(object) => {92
let html_in = |key: &str| {93
object94
.get(key)95
.and_then(Value::as_str)96
.is_some_and(|text| HTML_TAG.is_match(text))97
};98
if html_in("text") || html_in("value") {99
object.insert(HTML_TEXT.to_owned(), Value::Bool(true));100
}101
object.values_mut().for_each(mark_html_text);102
}103
_ => {}104
}73
}105
}74
106
75
fn section_items(section: &Value) -> Vec<Value> {107
fn section_items(section: &Value) -> Vec<Value> {@@ -214,6 +246,17 @@ mod tests {214
assert_eq!(card.actions.len(), 1);246
assert_eq!(card.actions.len(), 1);215
}247
}216
248
249
#[test]250
fn html_in_connector_text_and_facts_is_read_as_html() {251
let html_card = r#"{"@type": "MessageCard", "text": "Deploy <b>done</b><br>see <a href=\"https://example.com/run\">run</a>", "sections": [{"facts": [{"name": "Status", "value": "<i>green</i>"}]}]}"#;252
let text = AdaptiveCard::parse(html_card)253
.expect("connector card is shown")254
.plain_text()255
.unwrap_or_default();256
assert!(text.contains("done") && text.contains("green"));257
assert!(!text.contains('<'), "tags leaked: {text}");258
}259
217
#[test]260
#[test]218
fn an_object_without_card_shape_stays_no_card() {261
fn an_object_without_card_shape_stays_no_card() {219
assert!(AdaptiveCard::parse("{}").is_none());262
assert!(AdaptiveCard::parse("{}").is_none());