Skip to content

Commit bbe254b

Browse files
authored
fix: Doc comment <code> block is not rendered correctly (#10390)
* fix: code block is not rendered correctly * test(snapshot): update snapshots test(snapshot): update snapshots e1b692a * test(snapshot): update snapshots 887b6b6 --------- Co-authored-by: filzrev <[email protected]>
1 parent f70ab07 commit bbe254b

File tree

48 files changed

+6037
-1802
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+6037
-1802
lines changed

samples/seed/dotnet/project/Project/Inheritdoc.cs

+38-1
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,41 @@ public class Class2 : Class1<bool>
108108
public override bool TestMethod1(bool parm1, int parm2) => false;
109109
}
110110
}
111-
}
111+
112+
// Issue #9736 #9495 #9754
113+
public class Issue9736
114+
{
115+
public interface IJsonApiOptions
116+
{
117+
/// <summary>
118+
/// Whether to use relative links for all resources. <c>false</c> by default.
119+
/// </summary>
120+
/// <example>
121+
/// <code><![CDATA[
122+
/// options.UseRelativeLinks = true;
123+
/// ]]></code>
124+
/// <code><![CDATA[
125+
/// {
126+
/// "type": "articles",
127+
/// "id": "4309",
128+
/// "relationships": {
129+
/// "author": {
130+
/// "links": {
131+
/// "self": "/api/shopping/articles/4309/relationships/author",
132+
/// "related": "/api/shopping/articles/4309/author"
133+
/// }
134+
/// }
135+
/// }
136+
/// }
137+
/// ]]></code>
138+
/// </example>
139+
bool UseRelativeLinks { get; }
140+
}
141+
142+
public sealed class JsonApiOptions : IJsonApiOptions
143+
{
144+
/// <inheritdoc />
145+
public bool UseRelativeLinks { get; set; }
146+
}
147+
}
148+
}

src/Docfx.Dotnet/Parsers/XmlComment.cs

+2-5
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,6 @@ public static XmlComment Parse(string xml, XmlCommentParserContext context = nul
124124
}
125125
try
126126
{
127-
// Format xml with indentation.
128-
// It's needed to fix issue (https://github.com/dotnet/docfx/issues/9736)
129-
xml = XElement.Parse(xml).ToString(SaveOptions.None);
130-
131127
return new XmlComment(xml, context ?? new());
132128
}
133129
catch (XmlException)
@@ -173,7 +169,8 @@ private void ResolveCode(XDocument doc, XmlCommentParserContext context)
173169

174170
code.SetAttributeValue("class", $"lang-{lang}");
175171

176-
if (node.PreviousNode is null)
172+
if (node.PreviousNode is null
173+
|| node.PreviousNode is XText xText && xText.Value == $"\n{indent}")
177174
{
178175
// Xml writer formats <pre><code> with unintended identation
179176
// when there is no preceeding text node.

test/Docfx.Dotnet.Tests/XmlCommentUnitTest.cs

+50-6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public static void ParaNewLine()
2828
Assert.Equal(
2929
"""
3030
a
31-
<p>b</p><p>c</p>
31+
<p>b</p>
32+
<p>c</p>
3233
""",
3334
XmlComment.Parse("""
3435
<summary>
@@ -390,13 +391,14 @@ Classes in assemblies are by definition complete.
390391
<a href="https://example.org">example</a>
391392
<p>This is <code class="paramref">ref</code> a sample of exception node</p>
392393
<ul><li>
393-
<pre><code class="lang-c#">public class XmlElement
394-
: XmlLinkedNode</code></pre>
395-
<ol><li>
396-
word inside list-&gt;listItem-&gt;list-&gt;listItem-&gt;para.&gt;
394+
395+
<pre><code class="lang-c#">public class XmlElement
396+
: XmlLinkedNode</code></pre>
397+
<ol><li>
398+
word inside list->listItem->list->listItem->para.>
397399
the second line.
398400
</li><li>item2 in numbered list</li></ol>
399-
</li><li>item2 in bullet list</li><li>
401+
</li><li>item2 in bullet list</li><li>
400402
loose text <i>not</i> wrapped in description
401403
</li></ul>
402404
""", remarks, ignoreLineEndingDifferences: true);
@@ -519,6 +521,7 @@ public void Issue9495()
519521
Assert.Equal(
520522
"""
521523
<pre><code class="lang-csharp">options.UseRelativeLinks = true;</code></pre>
524+
522525
<pre><code class="lang-csharp">{
523526
"type": "articles",
524527
"id": "4309",
@@ -533,4 +536,45 @@ public void Issue9495()
533536
}</code></pre>
534537
""", comment.Examples[0], ignoreLineEndingDifferences: true);
535538
}
539+
540+
[Fact]
541+
public void Issue10385()
542+
{
543+
var comment = XmlComment.Parse(
544+
"""
545+
<remarks>
546+
<para>
547+
Paragraph.
548+
</para>
549+
<code lang="cs">
550+
public sealed class Issue10385
551+
{
552+
public int AAA {get;set;}
553+
554+
public int BBB {get;set;}
555+
556+
public int CCC {get;set;}
557+
}
558+
</code>
559+
</remarks>
560+
""");
561+
Assert.Equal(
562+
"""
563+
<p>
564+
Paragraph.
565+
</p>
566+
567+
<pre><code class="lang-csharp">public sealed class Issue10385
568+
{
569+
public int AAA {get;set;}
570+
571+
public int BBB {get;set;}
572+
573+
public int CCC {get;set;}
574+
}</code></pre>
575+
""", comment.Remarks, ignoreLineEndingDifferences: true);
576+
}
577+
578+
579+
536580
}

0 commit comments

Comments
 (0)