Skip to content

Commit 48578d4

Browse files
committed
Only add styles if they are not the default
1 parent 85cc43d commit 48578d4

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

crates/markdown_preview/src/markdown_parser.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -826,20 +826,24 @@ impl<'a> MarkdownParser<'a> {
826826
}
827827
markup5ever_rcdom::NodeData::Comment { .. } => {}
828828
markup5ever_rcdom::NodeData::Element { name, attrs, .. } => {
829+
let mut styles = if let Some(styles) = Self::markdown_style_from_html_styles(
830+
Self::extract_styles_from_attributes(attrs),
831+
) {
832+
vec![MarkdownHighlight::Style(styles)]
833+
} else {
834+
Vec::default()
835+
};
836+
829837
if local_name!("img") == name.local {
830838
if let Some(image) = self.extract_image(source_range, attrs) {
831839
elements.push(ParsedMarkdownElement::Image(image));
832840
}
833841
} 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-
838842
self.parse_paragraph(
839843
source_range,
840844
node,
841845
&mut MarkdownParagraph::new(),
842-
&mut vec![style],
846+
&mut styles,
843847
elements,
844848
);
845849
} else {
@@ -1003,7 +1007,9 @@ impl<'a> MarkdownParser<'a> {
10031007
styles
10041008
}
10051009

1006-
fn markdown_style_from_html_styles(styles: HashMap<String, String>) -> MarkdownHighlightStyle {
1010+
fn markdown_style_from_html_styles(
1011+
styles: HashMap<String, String>,
1012+
) -> Option<MarkdownHighlightStyle> {
10071013
let mut markdown_style = MarkdownHighlightStyle::default();
10081014

10091015
if let Some(text_decoration) = styles.get("text-decoration") {
@@ -1032,9 +1038,6 @@ impl<'a> MarkdownParser<'a> {
10321038

10331039
if let Some(font_weight) = styles.get("font-weight") {
10341040
match font_weight.to_lowercase().as_str() {
1035-
"normal" => {
1036-
markdown_style.weight = FontWeight::NORMAL;
1037-
}
10381041
"bold" => {
10391042
markdown_style.weight = FontWeight::BOLD;
10401043
}
@@ -1049,7 +1052,11 @@ impl<'a> MarkdownParser<'a> {
10491052
}
10501053
}
10511054

1052-
markdown_style
1055+
if markdown_style != MarkdownHighlightStyle::default() {
1056+
Some(markdown_style)
1057+
} else {
1058+
None
1059+
}
10531060
}
10541061

10551062
fn extract_image(

0 commit comments

Comments
 (0)