Skip to content

Commit 463b3f7

Browse files
authored
feat: support language attribute on C# documentation comment (#9159)
1 parent c64cd92 commit 463b3f7

File tree

6 files changed

+21
-10
lines changed

6 files changed

+21
-10
lines changed

src/Docfx.Dotnet/Parsers/XmlComment.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,18 @@ private void ResolveCode(XDocument doc, XmlCommentParserContext context)
142142
var (lang, value) = ResolveCodeSource(node, context);
143143
value = TrimEachLine(value ?? node.Value, new(' ', indent));
144144
var code = new XElement("code", value);
145-
code.SetAttributeValue("class", $"lang-{lang ?? "csharp"}");
145+
146+
if (node.Attribute("language") is { } languageAttribute)
147+
{
148+
lang = languageAttribute.Value;
149+
}
150+
151+
if (string.IsNullOrEmpty(lang))
152+
{
153+
lang = "csharp";
154+
}
155+
156+
code.SetAttributeValue("class", $"lang-{lang}");
146157
node.ReplaceWith(new XElement("pre", code));
147158
}
148159
}

test/Docfx.Dotnet.Tests/XmlCommentUnitTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ Classes in assemblies are by definition complete.
325325
<a href="https://example.org">example</a>
326326
<p>This is <code class="paramref">ref</code> a sample of exception node</p>
327327
<ul><li>
328-
<pre><code class="lang-csharp">public class XmlElement
328+
<pre><code class="lang-c#">public class XmlElement
329329
: XmlLinkedNode</code></pre>
330330
<ol><li>
331331
word inside list-&gt;listItem-&gt;list-&gt;listItem-&gt;para.&gt;

test/docfx.Snapshot.Tests/SamplesTest.Seed/api/CatLibrary.Cat-2.html.view.verified.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3693,7 +3693,7 @@
36933693
"summary": "<p>Here's main class of this <i>Demo</i>.</p>\n<p>You can see mostly type of article within this class and you for more detail, please see the remarks.</p>\n<p></p>\n<p>this class is a template class. It has two Generic parameter. they are: <code class=\"typeparamref\">T</code> and <code class=\"typeparamref\">K</code>.</p>\n<p>The extension method of this class can refer to <xref href=\"CatLibrary.ICatExtension\" data-throw-if-not-resolved=\"false\"></xref> class</p>\n",
36943694
"remarks": "<p sourcefile=\"specs/Cat.md\" sourcestartlinenumber=\"1\"><em sourcefile=\"specs/Cat.md\" sourcestartlinenumber=\"1\">THIS</em> is remarks overridden in <em sourcefile=\"specs/Cat.md\" sourcestartlinenumber=\"1\">MARKDWON</em> file</p>\n",
36953695
"example": [
3696-
"<p>Here's example of how to create an instance of this class. As T is limited with <code>class</code> and K is limited with <code>struct</code>.</p>\n<pre><code class=\"lang-csharp\">var a = new Cat(object, int)();\nint catNumber = new int();\nunsafe\n{\n a.GetFeetLength(catNumber);\n}</code></pre>\n<p>As you see, here we bring in <b>pointer</b> so we need to add <code class=\"languageKeyword\">unsafe</code> keyword.</p>\n"
3696+
"<p>Here's example of how to create an instance of this class. As T is limited with <code>class</code> and K is limited with <code>struct</code>.</p>\n<pre><code class=\"lang-c#\">var a = new Cat(object, int)();\nint catNumber = new int();\nunsafe\n{\n a.GetFeetLength(catNumber);\n}</code></pre>\n<p>As you see, here we bring in <b>pointer</b> so we need to add <code class=\"languageKeyword\">unsafe</code> keyword.</p>\n"
36973697
],
36983698
"syntax": {
36993699
"content": [
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading

test/docfx.Snapshot.Tests/SamplesTest.SeedHtml/html/api-CatLibrary.Cat-2.html-q-cat.verified.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,9 @@ <h4 class="section" id="type-parameters">Type Parameters<a class="anchorjs-link
458458

459459
<h2 id="CatLibrary_Cat_2_examples">Examples<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="#" href="#CatLibrary_Cat_2_examples" style="margin-left: 0.1875em; padding-right: 0.1875em; padding-left: 0.1875em;"></a></h2>
460460
<p>Here's example of how to create an instance of this class. As T is limited with <code>class</code> and K is limited with <code>struct</code>.</p>
461-
<pre><code class="lang-csharp hljs language-csharp"><span class="hljs-keyword">var</span> a = <span class="hljs-keyword">new</span> Cat(<span class="hljs-built_in">object</span>, <span class="hljs-built_in">int</span>)();
462-
<span class="hljs-built_in">int</span> catNumber = <span class="hljs-keyword">new</span> <span class="hljs-built_in">int</span>();
463-
<span class="hljs-keyword">unsafe</span>
461+
<pre><code class="lang-c# hljs language-c">var a = new Cat(object, <span class="hljs-type">int</span>)();
462+
<span class="hljs-type">int</span> catNumber = new <span class="hljs-type">int</span>();
463+
unsafe
464464
{
465465
a.GetFeetLength(catNumber);
466466
}</code><a class="btn border-0 code-action" href="#" title="Copy"><i class="bi bi-clipboard"></i></a></pre>

0 commit comments

Comments
 (0)