Skip to content

Commit bcf2338

Browse files
authored
doc(AttributeTable): parse XML see cref references (#8494)
1 parent 33041d0 commit bcf2338

1 file changed

Lines changed: 36 additions & 6 deletions

File tree

‎src/BootstrapBlazor.Server/Services/ComponentAttributeCacheService.cs‎

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ private static List<AttributeItem> GetAttributeCore(Type type)
6767
}
6868

6969
/// <summary>
70-
/// <para lang="zh">从 XML 注释获取 summary(支持多语言)</para>
71-
/// <para lang="en">Get the summary from XML comments (supports multiple languages)</para>
70+
/// <para lang="zh">从 XML 注释获取 summary(支持多语言和 see cref 引用)</para>
71+
/// <para lang="en">Get the summary from XML comments (supports multiple languages and see cref references)</para>
7272
/// </summary>
7373
private static string? GetSummary(XDocument? xmlDoc, PropertyInfo property)
7474
{
7575
var type = property.DeclaringType ?? property.PropertyType;
7676
var typeName = $"BootstrapBlazor.Components.{type.Name}";
7777
var memberName = $"P:{typeName}.{property.Name}";
7878
var summaryElement = FindSummaryElement(xmlDoc, memberName);
79-
return summaryElement == null ? null : GetLocalizedSummary(summaryElement);
79+
return summaryElement == null ? null : GetLocalizedSummary(summaryElement, typeName);
8080
}
8181

8282
private static XElement? FindSummaryElement(XDocument? xmlDoc, string memberName)
@@ -97,7 +97,7 @@ private static List<AttributeItem> GetAttributeCore(Type type)
9797
return v != null ? FindSummaryElement(xmlDoc, v) : summaryElement;
9898
}
9999

100-
private static string? GetLocalizedSummary(XElement? summaryElement)
100+
private static string? GetLocalizedSummary(XElement? summaryElement, string? typeName)
101101
{
102102
if (summaryElement == null)
103103
{
@@ -109,12 +109,42 @@ private static List<AttributeItem> GetAttributeCore(Type type)
109109
.FirstOrDefault(p => p.Attribute("lang")?.Value == currentLanguage);
110110
if (langPara != null)
111111
{
112-
return langPara.Value.Trim();
112+
return GetSummaryText(langPara, typeName).Trim();
113113
}
114114

115115
var firstLangPara = summaryElement.Elements("para")
116116
.FirstOrDefault(p => p.Attribute("lang") != null);
117-
return firstLangPara != null ? firstLangPara.Value.Trim() : summaryElement.Value.Trim();
117+
return GetSummaryText(firstLangPara ?? summaryElement, typeName).Trim();
118+
}
119+
120+
private static string GetSummaryText(XElement element, string? typeName)
121+
{
122+
if (element.Name == "see" && !element.Nodes().Any() && element.Attribute("cref") is { } cref)
123+
{
124+
var reference = cref.Value;
125+
var lastDotIndex = reference.LastIndexOf('.');
126+
if (typeName != null && lastDotIndex > 0 && reference[..lastDotIndex] == $"P:{typeName}")
127+
{
128+
return reference[(lastDotIndex + 1)..];
129+
}
130+
131+
if (reference.Length > 1 && reference[1] == ':')
132+
{
133+
reference = reference[2..];
134+
}
135+
136+
const string namespacePrefix = "BootstrapBlazor.Components.";
137+
return reference.StartsWith(namespacePrefix, StringComparison.Ordinal)
138+
? reference[namespacePrefix.Length..]
139+
: reference;
140+
}
141+
142+
return string.Concat(element.Nodes().Select(node => node switch
143+
{
144+
XText text => text.Value,
145+
XElement child => GetSummaryText(child, typeName),
146+
_ => ""
147+
}));
118148
}
119149

120150
/// <summary>

0 commit comments

Comments
 (0)