Skip to content

Commit 6aa697f

Browse files
authored
feat: Include API summary in <meta description> tag (#9611)
1 parent faa53eb commit 6aa697f

File tree

92 files changed

+2038
-1873
lines changed

Some content is hidden

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

92 files changed

+2038
-1873
lines changed

src/Docfx.Build/ApiPage/ApiPageProcessor.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,18 @@ public FileModel Load(FileAndType file, ImmutableDictionary<string, object> meta
4141
var yml = EnvironmentContext.FileAbstractLayer.ReadAllText(file.File);
4242
var json = JsonSerializer.Serialize(deserializer.Deserialize<object>(yml));
4343
var data = JsonSerializer.Deserialize<ApiPage>(json, ApiPage.JsonSerializerOptions);
44-
var content = new Dictionary<string, object>(metadata.OrderBy(item => item.Key))
44+
var content = new Dictionary<string, object>(metadata.OrderBy(item => item.Key));
45+
46+
if (data.metadata is not null)
4547
{
46-
["title"] = data.title,
47-
["content"] = ApiPageHtmlTemplate.Render(data, Markup).ToString(),
48-
["yamlmime"] = "ApiPage",
49-
["_disableNextArticle"] = true,
50-
};
48+
foreach (var (key, value) in data.metadata.OrderBy(item => item.Key))
49+
content[key] = value.Value;
50+
}
51+
52+
content["title"] = data.title;
53+
content["content"] = ApiPageHtmlTemplate.Render(data, Markup).ToString();
54+
content["yamlmime"] = "ApiPage";
55+
content["_disableNextArticle"] = true;
5156

5257
var localPathFromRoot = PathUtility.MakeRelativePath(EnvironmentContext.BaseDirectory, EnvironmentContext.FileAbstractLayer.GetPhysicalPath(file.File));
5358

src/Docfx.Dotnet/Docfx.Dotnet.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
</ItemGroup>
3030

3131
<ItemGroup>
32+
<PackageReference Include="HtmlAgilityPack" />
3233
<PackageReference Include="ICSharpCode.Decompiler" />
3334
<PackageReference Include="IgnoresAccessChecksToGenerator" PrivateAssets="All" />
3435
<PackageReference Include="OneOf" />

src/Docfx.Dotnet/DotnetApiCatalog.ApiPage.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Docfx.Common.Git;
1111
using Docfx.DataContracts.ManagedReference;
1212
using Docfx.Plugins;
13+
using HtmlAgilityPack;
1314
using Microsoft.CodeAnalysis;
1415
using Microsoft.CodeAnalysis.Shared.Extensions;
1516
using OneOf;
@@ -96,9 +97,14 @@ _ when SymbolHelper.IsMember(method) => "Method",
9697
throw new NotSupportedException($"Unknown symbol type kind {symbols[0].symbol}");
9798
}
9899

100+
var metadata = new Dictionary<string, OneOf<string, string[]>>();
101+
if (!string.IsNullOrEmpty(comment?.Summary))
102+
metadata["description"] = HtmlInnerText(comment.Summary);
103+
99104
output(config.OutputFolder, id, new ApiPage
100105
{
101106
title = title,
107+
metadata = metadata.Count > 0 ? metadata : null,
102108
languageId = "csharp",
103109
body = body.ToArray(),
104110
});
@@ -760,6 +766,13 @@ Span Link(string text, string? url)
760766
});
761767
}
762768
}
769+
770+
static string HtmlInnerText(string html)
771+
{
772+
var doc = new HtmlDocument();
773+
doc.LoadHtml(html);
774+
return doc.DocumentNode.InnerText;
775+
}
763776
}
764777

765778
#endif

templates/common/ManagedReference.common.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ exports.transform = function (model) {
3636
}
3737
}
3838

39+
if (model.summary && !model.description) {
40+
model.description = model.summary.replace(/<.*?>/gi, '').replace(/(\r\n|\n|\r)/gm, ' ').trim();
41+
}
42+
3943
return model;
4044
}
4145

test/docfx.Snapshot.Tests/SamplesTest.CSharp.Linux/api/CSharp8.Issue4007.html.view.verified.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,7 @@
10061006
"hideSubtitle": false,
10071007
"isClass": true,
10081008
"inClass": true,
1009+
"description": "This works: . This does not work: .",
10091010
"_disableToc": false,
10101011
"_disableNextArticle": true
10111012
}

test/docfx.Snapshot.Tests/SamplesTest.CSharp.Windows/api/CSharp8.Issue4007.html.view.verified.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,7 @@
10061006
"hideSubtitle": false,
10071007
"isClass": true,
10081008
"inClass": true,
1009+
"description": "This works: . This does not work: .",
10091010
"_disableToc": false,
10101011
"_disableNextArticle": true
10111012
}

test/docfx.Snapshot.Tests/SamplesTest.Seed.Linux/api/BuildFromAssembly.Class1.html.view.verified.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,7 @@
847847
"hideSubtitle": false,
848848
"isClass": true,
849849
"inClass": true,
850+
"description": "This is a test class.",
850851
"_disableToc": false,
851852
"_disableNextArticle": true
852853
}

test/docfx.Snapshot.Tests/SamplesTest.Seed.Linux/api/BuildFromProject.Inheritdoc.Issue7484.html.view.verified.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,7 @@
11441144
"hideSubtitle": false,
11451145
"isClass": true,
11461146
"inClass": true,
1147+
"description": "This is a test class to have something for DocFX to document.",
11471148
"_disableToc": false,
11481149
"_disableNextArticle": true
11491150
}

test/docfx.Snapshot.Tests/SamplesTest.Seed.Linux/api/BuildFromProject.Issue8725.html.view.verified.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,7 @@
904904
"hideSubtitle": false,
905905
"isClass": true,
906906
"inClass": true,
907+
"description": "A nice class",
907908
"_disableToc": false,
908909
"_disableNextArticle": true
909910
}

test/docfx.Snapshot.Tests/SamplesTest.Seed.Linux/api/BuildFromVBSourceCode.BaseClass1.html.view.verified.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,7 @@
911911
"hideSubtitle": false,
912912
"isClass": true,
913913
"inClass": true,
914+
"description": "This is the BaseClass",
914915
"_disableToc": false,
915916
"_disableNextArticle": true
916917
}

0 commit comments

Comments
 (0)