Skip to content

Commit 057387d

Browse files
committed
chore: fix code block layout corrupted issue.
1 parent 4afd9e6 commit 057387d

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

src/Docfx.Dotnet/Parsers/XmlComment.Extensions.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,15 @@ public static bool NeedEmptyLineBefore(this XElement node)
102102

103103
switch (prevNode.NodeType)
104104
{
105-
// If prev node is HTML element. No need to insert empty line.
106105
case XmlNodeType.Element:
107-
return false;
106+
107+
// No need to insert empty line, if prev node is HTML element and it's not <pre> tag. T
108+
if (node.Name != "pre")
109+
return false;
110+
111+
// <pre> tag needs empty line before. Without this setting, markdown parser treat code as markdown block.
112+
var prevElementNode = (XElement)prevNode;
113+
return prevElementNode.Name.LocalName != "pre";
108114

109115
// Ensure empty lines exists before text node.
110116
case XmlNodeType.Text:
@@ -126,6 +132,12 @@ public static void InsertEmptyLineBefore(this XElement elem)
126132
if (!elem.TryGetNonWhitespacePrevNode(out var prevNode))
127133
return;
128134

135+
if (prevNode.NodeType == XmlNodeType.Element)
136+
{
137+
elem.AddBeforeSelf(new XText("\n"));
138+
return;
139+
}
140+
129141
Debug.Assert(prevNode.NodeType == XmlNodeType.Text);
130142

131143
var prevTextNode = (XText)prevNode;

test/Docfx.Dotnet.Tests/XmlCommentTests/XmlCommentSummaryTest.Code.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,51 @@ public void Code_Inline()
6969
Paragraph2
7070
""");
7171
}
72+
73+
74+
[Fact]
75+
public void Code_HtmlTagExistBefore()
76+
{
77+
ValidateSummary(
78+
// Input XML
79+
"""
80+
<summary>
81+
paragraph1
82+
<para>paragraph2</para>
83+
<code><![CDATA[
84+
public class Sample
85+
{
86+
line1
87+
88+
line2
89+
}]]></code>
90+
<code><![CDATA[
91+
public class Sample2
92+
{
93+
line1
94+
95+
line2
96+
}]]></code>
97+
</summary>
98+
""",
99+
// Expected Markdown
100+
"""
101+
paragraph1
102+
103+
<p>paragraph2</p>
104+
105+
<pre><code class="lang-csharp">public class Sample
106+
{
107+
line1
108+
109+
line2
110+
}</code></pre>
111+
<pre><code class="lang-csharp">public class Sample2
112+
{
113+
line1
114+
115+
line2
116+
}</code></pre>
117+
""");
118+
}
72119
}

test/Docfx.Dotnet.Tests/XmlCommentUnitTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ public sealed class Issue10385
581581
<p>
582582
Paragraph.
583583
</p>
584+
584585
<pre><code class="lang-csharp">public sealed class Issue10385
585586
{
586587
public int AAA {get;set;}

0 commit comments

Comments
 (0)