Skip to content

Commit 4afd9e6

Browse files
committed
fix: issue 10965
1 parent 8b43a7a commit 4afd9e6

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private static string GetMarkdownText(XElement elem)
4343
node.InsertEmptyLineBefore();
4444

4545
if (node.NeedEmptyLineAfter())
46-
node.AddAfterSelf(new XText("\n"));
46+
node.InsertEmptyLineAfter();
4747
}
4848

4949
return elem.GetInnerXml();
@@ -143,7 +143,23 @@ public static void InsertEmptyLineBefore(this XElement elem)
143143
}
144144
else
145145
{
146-
elem.AddBeforeSelf(new XText("\n"));
146+
if (prevTextNode.Value.EndsWith('\n'))
147+
{
148+
elem.AddBeforeSelf(new XText("\n"));
149+
}
150+
else
151+
{
152+
// HTML block tag is adjacent to markdown without new line.
153+
// In this case, it need to append `\n\n` and copy last line indent chars.
154+
int startIndex = lastLine.IndexOfAnyExcept(' ', '\t');
155+
ReadOnlySpan<char> indent = startIndex switch
156+
{
157+
< 0 => lastLine,
158+
0 => [],
159+
_ => lastLine.Slice(0, startIndex),
160+
};
161+
elem.AddBeforeSelf(new XText($"\n\n{indent}"));
162+
}
147163
}
148164
}
149165

@@ -203,6 +219,21 @@ public static bool NeedEmptyLineAfter(this XElement node)
203219
return false;
204220
}
205221
}
222+
223+
public static void InsertEmptyLineAfter(this XElement elem)
224+
{
225+
if (!elem.TryGetNonWhitespaceNextNode(out var nextNode))
226+
return;
227+
228+
Debug.Assert(nextNode.NodeType == XmlNodeType.Text);
229+
230+
var nextTextNode = (XText)nextNode;
231+
if (nextTextNode.Value.StartsWith('\n'))
232+
elem.AddAfterSelf(new XText("\n"));
233+
else
234+
elem.AddAfterSelf(new XText("\n\n"));
235+
}
236+
206237
private static bool StartsWithEmptyLine(this ReadOnlySpan<char> span)
207238
{
208239
var index = span.IndexOfAnyExcept([' ', '\t']);

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,28 @@ public void Code_Block()
2828
""");
2929
}
3030

31+
[Fact]
32+
public void Code_Block_WithoutNewLine()
33+
{
34+
ValidateSummary(
35+
// Input XML
36+
"""
37+
<summary>
38+
Paragraph1<code><![CDATA[
39+
DELETE /articles/1 HTTP/1.1
40+
]]></code>Paragraph2
41+
</summary>
42+
""",
43+
// Expected Markdown
44+
"""
45+
Paragraph1
46+
47+
<pre><code class="lang-csharp">DELETE /articles/1 HTTP/1.1</code></pre>
48+
49+
Paragraph2
50+
""");
51+
}
52+
3153
[Fact]
3254
public void Code_Inline()
3355
{

0 commit comments

Comments
 (0)