Skip to content

Commit

Permalink
feat: support language attribute on C# documentation comment (#9159)
Browse files Browse the repository at this point in the history
  • Loading branch information
yufeih authored Sep 2, 2023
1 parent c64cd92 commit 463b3f7
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 10 deletions.
13 changes: 12 additions & 1 deletion src/Docfx.Dotnet/Parsers/XmlComment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,18 @@ private void ResolveCode(XDocument doc, XmlCommentParserContext context)
var (lang, value) = ResolveCodeSource(node, context);
value = TrimEachLine(value ?? node.Value, new(' ', indent));
var code = new XElement("code", value);
code.SetAttributeValue("class", $"lang-{lang ?? "csharp"}");

if (node.Attribute("language") is { } languageAttribute)
{
lang = languageAttribute.Value;
}

if (string.IsNullOrEmpty(lang))
{
lang = "csharp";
}

code.SetAttributeValue("class", $"lang-{lang}");
node.ReplaceWith(new XElement("pre", code));
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/Docfx.Dotnet.Tests/XmlCommentUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ Classes in assemblies are by definition complete.
<a href="https://example.org">example</a>
<p>This is <code class="paramref">ref</code> a sample of exception node</p>
<ul><li>
<pre><code class="lang-csharp">public class XmlElement
<pre><code class="lang-c#">public class XmlElement
: XmlLinkedNode</code></pre>
<ol><li>
word inside list-&gt;listItem-&gt;list-&gt;listItem-&gt;para.&gt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3693,7 +3693,7 @@
"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",
"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",
"example": [
"<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"
"<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"
],
"syntax": {
"content": [
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,9 @@ <h4 class="section" id="type-parameters">Type Parameters<a class="anchorjs-link

<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>
<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>
<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>)();
<span class="hljs-built_in">int</span> catNumber = <span class="hljs-keyword">new</span> <span class="hljs-built_in">int</span>();
<span class="hljs-keyword">unsafe</span>
<pre><code class="lang-c# hljs language-c">var a = new Cat(object, <span class="hljs-type">int</span>)();
<span class="hljs-type">int</span> catNumber = new <span class="hljs-type">int</span>();
unsafe
{
a.GetFeetLength(catNumber);
}</code><a class="btn border-0 code-action" href="#" title="Copy"><i class="bi bi-clipboard"></i></a></pre>
Expand Down

0 comments on commit 463b3f7

Please sign in to comment.