Skip to content
This repository was archived by the owner on May 21, 2022. It is now read-only.

Commit 7425d8d

Browse files
committed
feat: rendering bodytext and heading with style
using dfs at last commit to render
1 parent 5d6fe25 commit 7425d8d

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

md2docx/md2docx.cs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using DocumentFormat.OpenXml.Wordprocessing;
55
using DocumentFormat.OpenXml.Packaging;
66
using Ap = DocumentFormat.OpenXml.ExtendedProperties;
7-
using DocumentFormat.OpenXml.Wordprocessing;
87
using DocumentFormat.OpenXml;
98
using A = DocumentFormat.OpenXml.Drawing;
109
using Thm15 = DocumentFormat.OpenXml.Office2013.Theme;
@@ -174,11 +173,34 @@ static void GenerateMainDocumentPart1Content(MainDocumentPart mainDocumentPart1,
174173
if (element is ParagraphBlock mpara)
175174
{
176175
Paragraph dpara = new Paragraph();
176+
ParagraphProperties dparap = new ParagraphProperties
177+
{
178+
ParagraphStyleId = new ParagraphStyleId() { Val = "BodyText" }
179+
};
180+
dpara.ParagraphProperties = dparap;
177181
foreach (var inline in mpara.Inlines)
178182
{
179183
dfs(new RunProperties(), inline, ref dpara);
180184
}
181185
body1.Append(dpara);
186+
}else if (element is HeaderBlock mhead)
187+
{
188+
Paragraph dpara = new Paragraph();
189+
ParagraphProperties dparap = new ParagraphProperties();
190+
switch (mhead.HeaderLevel)
191+
{
192+
case int i when i < 4:
193+
dparap.ParagraphStyleId = new ParagraphStyleId() { Val = $"Heading{i}" };
194+
break;
195+
default:
196+
throw new Exception($"Rendering {element.GetType()} not implement yet");
197+
}
198+
dpara.ParagraphProperties = dparap;
199+
foreach(var inline in mhead.Inlines)
200+
{
201+
dfs(new RunProperties(), inline, ref dpara);
202+
}
203+
body1.Append(dpara);
182204
}
183205
else if(!(element is YamlHeaderBlock))
184206
{
@@ -214,7 +236,8 @@ static void dfs(RunProperties rp, MarkdownInline inline, ref Paragraph dpara)
214236
Run trun = new Run();
215237
Text dtext = new Text
216238
{
217-
Text = mtxt.Text
239+
Text = mtxt.Text,
240+
Space = SpaceProcessingModeValues.Preserve
218241
};
219242
trun.Append(newtrp);
220243
trun.Append(dtext);
@@ -1853,6 +1876,7 @@ static void GenerateStyleDefinitionsPart1Content(StyleDefinitionsPart styleDefin
18531876
SpacingBetweenLines spacingBetweenLines2 = new SpacingBetweenLines() { Before = "100", BeforeLines = 100, After = "100", AfterLines = 100 };
18541877
Justification justification1 = new Justification() { Val = JustificationValues.Center };
18551878
OutlineLevel outlineLevel1 = new OutlineLevel() { Val = 0 };
1879+
Indentation indentation1 = new Indentation() { FirstLine = "200", FirstLineChars = 200 };
18561880

18571881
styleParagraphProperties1.Append(keepNext1);
18581882
styleParagraphProperties1.Append(keepLines1);
@@ -1862,7 +1886,7 @@ static void GenerateStyleDefinitionsPart1Content(StyleDefinitionsPart styleDefin
18621886
styleParagraphProperties1.Append(outlineLevel1);
18631887

18641888
StyleRunProperties styleRunProperties2 = new StyleRunProperties();
1865-
RunFonts runFonts3 = new RunFonts() { Ascii = "Times New Roman", HighAnsi = "Times New Roman", EastAsia = "黑体", ComplexScriptTheme = ThemeFontValues.MajorBidi };
1889+
RunFonts runFonts3 = new RunFonts() { Ascii = "Times New Roman", HighAnsi = "Times New Roman", EastAsia = "黑体", ComplexScript = "Times New Roman" };
18661890
BoldComplexScript boldComplexScript1 = new BoldComplexScript();
18671891
Color color2 = new Color() { Val = "000000", ThemeColor = ThemeColorValues.Text1 };
18681892
FontSize fontSize2 = new FontSize() { Val = "32" };
@@ -1901,14 +1925,17 @@ static void GenerateStyleDefinitionsPart1Content(StyleDefinitionsPart styleDefin
19011925
styleParagraphProperties2.Append(keepLines2);
19021926
styleParagraphProperties2.Append(spacingBetweenLines3);
19031927
styleParagraphProperties2.Append(outlineLevel2);
1928+
styleParagraphProperties2.Indentation = new Indentation() { FirstLine = "200", FirstLineChars = 200 };
19041929

19051930
StyleRunProperties styleRunProperties3 = new StyleRunProperties();
1906-
RunFonts runFonts4 = new RunFonts() { EastAsia = "黑体", ComplexScriptTheme = ThemeFontValues.MajorBidi };
1931+
RunFonts runFonts4 = new RunFonts() { EastAsia = "黑体", Ascii = "Times New Roman", HighAnsi = "Times New Roman", ComplexScript = "Times New Roman" };
19071932
BoldComplexScript boldComplexScript2 = new BoldComplexScript();
1908-
FontSizeComplexScript fontSizeComplexScript3 = new FontSizeComplexScript() { Val = "32" };
1933+
FontSize fontSize3 = new FontSize() { Val = "24" };
1934+
FontSizeComplexScript fontSizeComplexScript3 = new FontSizeComplexScript() { Val = "24" };
19091935

19101936
styleRunProperties3.Append(runFonts4);
19111937
styleRunProperties3.Append(boldComplexScript2);
1938+
styleRunProperties3.Append(fontSize3);
19121939
styleRunProperties3.Append(fontSizeComplexScript3);
19131940

19141941
style3.Append(styleName3);
@@ -1940,14 +1967,17 @@ static void GenerateStyleDefinitionsPart1Content(StyleDefinitionsPart styleDefin
19401967
styleParagraphProperties3.Append(keepLines3);
19411968
styleParagraphProperties3.Append(spacingBetweenLines4);
19421969
styleParagraphProperties3.Append(outlineLevel3);
1970+
styleParagraphProperties3.Indentation = new Indentation() { FirstLine = "200", FirstLineChars = 200 };
19431971

19441972
StyleRunProperties styleRunProperties4 = new StyleRunProperties();
1945-
RunFonts runFonts5 = new RunFonts() { EastAsia = "楷体", AsciiTheme = ThemeFontValues.MajorHighAnsi, HighAnsiTheme = ThemeFontValues.MajorHighAnsi, ComplexScriptTheme = ThemeFontValues.MajorBidi };
1973+
RunFonts runFonts5 = new RunFonts() { EastAsia = "楷体", Ascii = "Times New Roman", HighAnsi = "Times New Roman", ComplexScript = "Times New Roman" };
19461974
BoldComplexScript boldComplexScript3 = new BoldComplexScript();
1947-
FontSizeComplexScript fontSizeComplexScript4 = new FontSizeComplexScript() { Val = "28" };
1975+
FontSize fontSize4 = new FontSize() { Val = "24" };
1976+
FontSizeComplexScript fontSizeComplexScript4 = new FontSizeComplexScript() { Val = "24" };
19481977

19491978
styleRunProperties4.Append(runFonts5);
19501979
styleRunProperties4.Append(boldComplexScript3);
1980+
styleRunProperties4.Append(fontSize4);
19511981
styleRunProperties4.Append(fontSizeComplexScript4);
19521982

19531983
style4.Append(styleName4);
@@ -1968,17 +1998,21 @@ static void GenerateStyleDefinitionsPart1Content(StyleDefinitionsPart styleDefin
19681998

19691999
StyleParagraphProperties styleParagraphProperties10 = new StyleParagraphProperties();
19702000
SpacingBetweenLines spacingBetweenLines11 = new SpacingBetweenLines() { Before = "180", After = "180" };
1971-
Indentation indentation1 = new Indentation() { FirstLine = "200", FirstLineChars = 200 };
19722001

19732002
styleParagraphProperties10.Append(spacingBetweenLines11);
19742003
styleParagraphProperties10.Append(indentation1);
2004+
styleParagraphProperties10.Indentation = new Indentation() { FirstLine = "200", FirstLineChars = 200 };
19752005

19762006
StyleRunProperties styleRunProperties11 = new StyleRunProperties();
19772007
RunFonts runFonts12 = new RunFonts() { Ascii = "Times New Roman", HighAnsi = "Times New Roman", EastAsia = "宋体", ComplexScript = "Times New Roman" };
19782008
Color color9 = new Color() { Val = "000000", ThemeColor = ThemeColorValues.Text1 };
2009+
FontSize fontSize5 = new FontSize() { Val = "24" };
2010+
FontSizeComplexScript fontSizeComplexScript5 = new FontSizeComplexScript() { Val = "24" };
19792011

19802012
styleRunProperties11.Append(runFonts12);
19812013
styleRunProperties11.Append(color9);
2014+
styleRunProperties11.Append(fontSize5);
2015+
styleRunProperties11.Append(fontSizeComplexScript5);
19822016

19832017
style14.Append(styleName14);
19842018
style14.Append(linkedStyle1);

0 commit comments

Comments
 (0)