Skip to content

Commit 36f1639

Browse files
committed
Add BoldElement
1 parent 646d86d commit 36f1639

File tree

6 files changed

+91
-15
lines changed

6 files changed

+91
-15
lines changed

src/MdDocs.ApiReference/Model/XmlDocs/IVisitor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public interface IVisitor
1616

1717
void Visit(TextElement element);
1818

19+
void Visit(BoldElement element);
20+
1921
void Visit(SeeElement element);
2022

2123
void Visit(TextBlock textBlock);

src/MdDocs.ApiReference/Model/XmlDocs/XmlDocsReader.cs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ internal void ReadMemberContent(XElement xml, MemberElement member)
206206

207207
// ignore unknown elements
208208
default:
209-
m_Logger.LogWarning($"Encountered unknown element '{elementName}'. Element will be ignored.");
209+
m_Logger.LogWarning($"Encountered unknown element in member '{elementName}'. Element will be ignored.");
210210
break;
211211
}
212212
}
@@ -281,6 +281,20 @@ internal TextBlock ReadTextBlock(XElement xml)
281281
element = new CElement(elementNode.Value);
282282
break;
283283

284+
case "b":
285+
element = new BoldElement(ReadTextBlock(elementNode));
286+
break;
287+
288+
case "a":
289+
if (elementNode.TryGetAttributeValue("href", out var linkHref))
290+
{
291+
if (Uri.TryCreate(linkHref, UriKind.Absolute, out var target))
292+
{
293+
element = new SeeElement(target, ReadTextBlock(elementNode));
294+
}
295+
}
296+
break;
297+
284298
case "see":
285299
// <see /> allows adding links to the documentation
286300
//
@@ -313,7 +327,7 @@ internal TextBlock ReadTextBlock(XElement xml)
313327

314328
// ignore unknown elements
315329
default:
316-
m_Logger.LogWarning($"Encountered unknown element '{elementNode.Name}'. Element will be ignored.");
330+
m_Logger.LogWarning($"Encountered unknown element in text block '{elementNode.Name}'. Element will be ignored.");
317331
break;
318332
}
319333
break;
@@ -451,8 +465,8 @@ private static string TrimLines(string content, StringSplitOptions splitOptions,
451465
if (lines.Count == 0)
452466
return String.Empty;
453467

454-
// Indent in generated XML doc files is greater than 4 always.
455-
// This allows us to optimize the case where the author actually placed
468+
// Indent in generated XML doc files is greater than 4 always.
469+
// This allows us to optimize the case where the author actually placed
456470
// whitespace inline in between tags.
457471
if (indent <= 4 && !String.IsNullOrEmpty(lines[0]) && lines[0][0] != '\t')
458472
indent = 0;
@@ -477,13 +491,13 @@ private static string TrimLines(string content, StringSplitOptions splitOptions,
477491
/// </summary>
478492
private static int? DetermineIndentation(string content)
479493
{
480-
// When inline documentation is written to a XML file by the compiler,
494+
// When inline documentation is written to a XML file by the compiler,
481495
// all text is indented by the same number of character:
482496
//
483497
// Entire block is indented
484498
// │
485-
// │ <member name="F:DemoProject.DemoClass.Field1"> Empty leading line
486-
// │ <remarks> <───────┘
499+
// │ <member name="F:DemoProject.DemoClass.Field1"> Empty leading line
500+
// │ <remarks> <───────┘
487501
// └───> Remarks allow specification of more detailed information about a member, in this case a field
488502
// supplementing the information specified in the summary
489503
// ┌───> </remarks>
@@ -493,10 +507,10 @@ private static string TrimLines(string content, StringSplitOptions splitOptions,
493507
// Empty trailing line
494508
//
495509
// In order to properly render the documentation, the leading whitespace
496-
// needs to be removed.
497-
//
510+
// needs to be removed.
511+
//
498512
// This methods determines the number of character that have to be removed
499-
// from all lines by
513+
// from all lines by
500514
// - removing the first line (probably only whitespace, as can be seen in the example above)
501515
// - removing the last line (probably only whitespace, as can be seen in the example above)
502516
// - counting the number of whitespace characters in the first non-whitespace line
@@ -521,9 +535,9 @@ private static string TrimLines(string content, StringSplitOptions splitOptions,
521535
if (lines.Count == 0)
522536
return null;
523537

524-
// The indent of the first line of content determines the base
525-
// indent for all the lines, which we should remove since it's just
526-
// a doc gen artifact.
538+
// The indent of the first line of content determines the base
539+
// indent for all the lines, which we should remove since it's just
540+
// a doc gen artifact.
527541
return lines[0].TakeWhile(c => Char.IsWhiteSpace(c)).Count();
528542

529543
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
3+
namespace Grynwald.MdDocs.ApiReference.Model.XmlDocs
4+
{
5+
/// <summary>
6+
/// Represents a bold text element in XML documentation comments.
7+
/// </summary>
8+
/// <remarks>
9+
/// For a list of tags in documentation comments, see
10+
/// https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/xmldoc/recommended-tags-for-documentation-comments
11+
/// </remarks>
12+
public class BoldElement : Element, IEquatable<BoldElement>
13+
{
14+
/// <summary>
15+
/// Gets the bold text elements content
16+
/// </summary>
17+
public TextBlock Content { get; }
18+
19+
/// <summary>
20+
/// Initializes a new instance of <see cref="BoldElement"/>.
21+
/// </summary>
22+
/// <param name="content">The text element's content.</param>
23+
/// <exception cref="ArgumentNullException">Thrown when <paramref name="content"/> is <c>null</c>.</exception>
24+
public BoldElement(TextBlock content)
25+
{
26+
Content = content ?? throw new ArgumentNullException(nameof(content));
27+
}
28+
29+
/// <inheritdoc />
30+
public override void Accept(IVisitor visitor) => visitor.Visit(this);
31+
32+
/// <inheritdoc />
33+
public override int GetHashCode() => StringComparer.Ordinal.GetHashCode(Content);
34+
35+
/// <inheritdoc />
36+
public override bool Equals(object? obj) => Equals(obj as BoldElement);
37+
38+
/// <inheritdoc />
39+
public bool Equals(BoldElement? other) => other != null && StringComparer.Ordinal.Equals(Content, other.Content);
40+
}
41+
}

src/MdDocs.ApiReference/Model/XmlDocs/_TextElements/TextElement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Grynwald.MdDocs.ApiReference.Model.XmlDocs
44
{
55
/// <summary>
6-
/// Represents a plaint text element in XML documentation comments.
6+
/// Represents a plain text element in XML documentation comments.
77
/// </summary>
88
/// <remarks>
99
/// For a list of tags in documentation comments, see

src/MdDocs.ApiReference/Templates/Default/_Helpers/ConvertToBlockVisitor.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Reflection.Metadata.Ecma335;
45
using Grynwald.MarkdownGenerator;
56
using Grynwald.MdDocs.ApiReference.Model.XmlDocs;
67

@@ -22,7 +23,7 @@ internal class ConvertToBlockVisitor : IVisitor
2223
public MdContainerBlock Result { get; }
2324

2425
/// <summary>
25-
/// Gets the markdown block currently being appended to
26+
/// Gets the markdown block currently being appended to
2627
/// </summary>
2728
private MdContainerBlockBase CurrentBlock => m_Blocks.Peek();
2829

@@ -59,6 +60,15 @@ public void Visit(CodeElement element)
5960

6061
public void Visit(TextElement element) => AddToCurrentParagraph(new MdTextSpan(element.Content));
6162

63+
public void Visit(BoldElement element)
64+
{
65+
if (!element.Content.IsEmpty)
66+
{
67+
var text = TextBlockToMarkdownConverter.ConvertToSpan(element.Content, m_SpanFactory);
68+
AddToCurrentParagraph(new MdStrongEmphasisSpan(text));
69+
}
70+
}
71+
6272
public void Visit(SeeElement element)
6373
{
6474
if (element is null)

src/MdDocs.ApiReference/Templates/Default/_Helpers/ConvertToSpanVisitor.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ public void Visit(CodeElement element)
3939

4040
public void Visit(TextElement element) => Result.Add(new MdTextSpan(element.Content));
4141

42+
public void Visit(BoldElement element)
43+
{
44+
if (!element.Content.IsEmpty)
45+
{
46+
var text = TextBlockToMarkdownConverter.ConvertToSpan(element.Content, m_SpanFactory);
47+
Result.Add(new MdStrongEmphasisSpan(text));
48+
}
49+
}
50+
4251
public void Visit(SeeElement element)
4352
{
4453
MdSpan span;

0 commit comments

Comments
 (0)