Skip to content

Commit 5040fe3

Browse files
committed
Improve styles logic in DOCX to RTF/HTML/MD converters
1 parent 912770e commit 5040fe3

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Improvements to styles and lists handling when appending Markdown to an existing DOCX document (issue #12)
88
- Heading styles used in Markdown to DOCX converter now inherit from default Word heading styles, so that collapsing/expanding is available.
99
- Fix: SVG images are now preserved in Markdown to DOCX renderer
10+
- Improve styles logic in DOCX to RTF/HTML/MD converters
1011

1112
## 0.13.1 - 2025.08.31
1213

src/DocSharp.Docx/Helpers/OpenXmlHelpers.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,9 @@ private static void MergeAttributes(OpenXmlElement? target, OpenXmlElement? sour
446446
{
447447
var res = new SpacingBetweenLines();
448448
var attributes = new List<string> { "before", "beforeLines",
449-
"after", "afterLines",
450-
"beforeAutoSpacing", "afterAutoSpacing",
451-
"line", "lineRule" };
449+
"after", "afterLines",
450+
"beforeAutoSpacing", "afterAutoSpacing",
451+
"line", "lineRule" };
452452

453453
MergeAttributes(res, paragraph.ParagraphProperties?.SpacingBetweenLines, attributes);
454454

@@ -630,8 +630,7 @@ private static void MergeAttributes(OpenXmlElement? target, OpenXmlElement? sour
630630
}
631631

632632
// Check run style
633-
var runStyle = stylesPart.GetStyleFromId(run.RunProperties?.RunStyle?.Val, StyleValues.Character) ??
634-
stylesPart.GetStyleFromId(run.RunProperties?.RunStyle?.Val, StyleValues.Paragraph);
633+
var runStyle = stylesPart.GetStyleFromId(run.RunProperties?.RunStyle?.Val, StyleValues.Character);
635634
while (runStyle != null)
636635
{
637636
propertyValue = runStyle.StyleRunProperties?.GetFirstChild<T>();
@@ -645,16 +644,32 @@ private static void MergeAttributes(OpenXmlElement? target, OpenXmlElement? sour
645644
}
646645

647646
// Check paragraph style
648-
var paragraphProperties = run.GetFirstAncestor<Paragraph>()?.ParagraphProperties;
647+
var paragraphProperties = paragraph?.ParagraphProperties;
649648
var paragraphStyle = stylesPart.GetStyleFromId(paragraphProperties?.ParagraphStyleId?.Val, StyleValues.Paragraph);
650649
while (paragraphStyle != null)
651650
{
651+
// Check paragraph style run properties
652652
propertyValue = paragraphStyle.StyleRunProperties?.GetFirstChild<T>();
653653
if (propertyValue != null)
654654
{
655655
return propertyValue;
656656
}
657657

658+
// Check linked style, if any
659+
var linkedStyleId = paragraphStyle.LinkedStyle?.Val;
660+
if (linkedStyleId != null)
661+
{
662+
var linkedStyle = stylesPart.GetStyleFromId(linkedStyleId, StyleValues.Character);
663+
if (linkedStyle != null)
664+
{
665+
propertyValue = linkedStyle.StyleRunProperties?.GetFirstChild<T>();
666+
if (propertyValue != null)
667+
{
668+
return propertyValue;
669+
}
670+
}
671+
}
672+
658673
// Check styles from which the current style inherits
659674
paragraphStyle = stylesPart.GetBaseStyle(paragraphStyle);
660675
}

0 commit comments

Comments
 (0)