Skip to content

Commit 6ec0601

Browse files
committed
Allow specifying styles inside style attribute
1 parent 6a0d366 commit 6ec0601

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

crates/markdown_preview/src/markdown_parser.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,11 +831,15 @@ impl<'a> MarkdownParser<'a> {
831831
elements.push(ParsedMarkdownElement::Image(image));
832832
}
833833
} else if local_name!("p") == name.local {
834+
let style = MarkdownHighlight::Style(Self::markdown_style_from_html_styles(
835+
Self::extract_styles_from_attributes(attrs),
836+
));
837+
834838
self.parse_paragraph(
835839
source_range,
836840
node,
837841
&mut MarkdownParagraph::new(),
838-
&mut Vec::new(),
842+
&mut vec![style],
839843
elements,
840844
);
841845
} else {
@@ -999,6 +1003,59 @@ impl<'a> MarkdownParser<'a> {
9991003
styles
10001004
}
10011005

1006+
fn markdown_style_from_html_styles(styles: HashMap<String, String>) -> MarkdownHighlightStyle {
1007+
let mut markdown_style = MarkdownHighlightStyle::default();
1008+
1009+
if let Some(text_decoration) = styles.get("text-decoration") {
1010+
match text_decoration.to_lowercase().as_str() {
1011+
"underline" => {
1012+
markdown_style.underline = true;
1013+
}
1014+
"line-through" => {
1015+
markdown_style.strikethrough = true;
1016+
}
1017+
_ => {}
1018+
}
1019+
}
1020+
1021+
if let Some(font_style) = styles.get("font-style") {
1022+
match font_style.to_lowercase().as_str() {
1023+
"normal" => {
1024+
markdown_style.emphasized = false;
1025+
markdown_style.italic = false;
1026+
}
1027+
"italic" => {
1028+
markdown_style.italic = true;
1029+
}
1030+
"oblique" => {
1031+
markdown_style.emphasized = true;
1032+
}
1033+
_ => {}
1034+
}
1035+
}
1036+
1037+
if let Some(font_weight) = styles.get("font-weight") {
1038+
match font_weight.to_lowercase().as_str() {
1039+
"normal" => {
1040+
markdown_style.weight = FontWeight::NORMAL;
1041+
}
1042+
"bold" => {
1043+
markdown_style.weight = FontWeight::BOLD;
1044+
}
1045+
"lighter" => {
1046+
markdown_style.weight = FontWeight::THIN;
1047+
}
1048+
_ => {
1049+
if let Some(weight) = font_weight.parse::<f32>().ok() {
1050+
markdown_style.weight = FontWeight(weight);
1051+
}
1052+
}
1053+
}
1054+
}
1055+
1056+
markdown_style
1057+
}
1058+
10021059
fn extract_image(
10031060
&self,
10041061
source_range: Range<usize>,

0 commit comments

Comments
 (0)