Skip to content

Commit e400c6b

Browse files
committed
Properly handle code formatting
1 parent 4d3ab9d commit e400c6b

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

src/content/style.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub(crate) enum Style {
44
Italic,
55
Underline,
66
Strike,
7+
Code,
78
TextColor(String),
89
BackgroundColor(String),
910
}
@@ -15,6 +16,7 @@ impl Style {
1516
Style::Italic => "italic",
1617
Style::Underline => "underline",
1718
Style::Strike => "strike",
19+
Style::Code => "code",
1820
Style::TextColor(_) => "textColor",
1921
Style::BackgroundColor(_) => "backgroundColor",
2022
}
@@ -30,6 +32,7 @@ impl TryFrom<&str> for Style {
3032
"italic" => Ok(Style::Italic),
3133
"underline" => Ok(Style::Underline),
3234
"strike" => Ok(Style::Strike),
35+
"code" => Ok(Style::Code),
3336
_ => Err(format!("Cannot create Style from {}", s)),
3437
}
3538
}

src/converter.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ fn convert_content(node: Node, styles: &mut Vec<Style>) -> Result<BasicContent,
9898
Ok(content)
9999
}
100100
NodeType::Element => match node.tag_name().name() {
101-
"bold" | "italic" | "underline" | "strike" | "textColor" | "backgroundColor" => {
101+
"bold" | "italic" | "underline" | "strike" | "code" | "textColor"
102+
| "backgroundColor" => {
102103
// Style tags can have either one text child or one element child.
103104
// In the case of an element child, the tag could be surrounded by whitespace.
104105
// This seems to only happen when the XML is formatted with newlines,

tests/converter_test.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,49 @@ fn test_convert_formatted_content() {
134134
assert_json_incl(&expected, &result);
135135
}
136136

137+
#[test]
138+
fn test_convert_code() {
139+
let input = r#"
140+
<blockGroup>
141+
<blockContainer textColor="default" backgroundColor="default"
142+
id="f6596d68-4414-48f3-b502-eb54c9a00b17">
143+
<paragraph textAlignment="left">here is <code>some code</code> yay</paragraph>
144+
</blockContainer>
145+
<blockContainer textColor="default" id="30bd3f34-0288-43fe-9045-c58b71305e9d"
146+
backgroundColor="default">
147+
<paragraph textAlignment="left"></paragraph>
148+
</blockContainer>
149+
</blockGroup>
150+
"#;
151+
152+
let expected: Value = serde_json::from_str(
153+
r#"
154+
[
155+
{
156+
"id": "f6596d68-4414-48f3-b502-eb54c9a00b17",
157+
"type": "paragraph",
158+
"props": {
159+
"textColor": "default",
160+
"backgroundColor": "default",
161+
"textAlignment": "left"
162+
},
163+
"content": [
164+
{ "type": "text", "text": "here is ", "styles": {} },
165+
{ "type": "text", "text": "some code", "styles": { "code": true } },
166+
{ "type": "text", "text": " yay", "styles": {} }
167+
],
168+
"children": []
169+
}
170+
]
171+
"#,
172+
)
173+
.unwrap();
174+
175+
let result = convert_to_value(input.to_string()).unwrap();
176+
177+
assert_json_incl(&expected, &result);
178+
}
179+
137180
#[test]
138181
fn test_convert_nested_block() {
139182
let input = r#"

0 commit comments

Comments
 (0)