Skip to content

Commit

Permalink
feat: Include API summary in <meta description> tag (#9611)
Browse files Browse the repository at this point in the history
  • Loading branch information
yufeih authored Jan 13, 2024
1 parent faa53eb commit 6aa697f
Show file tree
Hide file tree
Showing 92 changed files with 2,038 additions and 1,873 deletions.
17 changes: 11 additions & 6 deletions src/Docfx.Build/ApiPage/ApiPageProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,18 @@ public FileModel Load(FileAndType file, ImmutableDictionary<string, object> meta
var yml = EnvironmentContext.FileAbstractLayer.ReadAllText(file.File);
var json = JsonSerializer.Serialize(deserializer.Deserialize<object>(yml));
var data = JsonSerializer.Deserialize<ApiPage>(json, ApiPage.JsonSerializerOptions);
var content = new Dictionary<string, object>(metadata.OrderBy(item => item.Key))
var content = new Dictionary<string, object>(metadata.OrderBy(item => item.Key));

if (data.metadata is not null)
{
["title"] = data.title,
["content"] = ApiPageHtmlTemplate.Render(data, Markup).ToString(),
["yamlmime"] = "ApiPage",
["_disableNextArticle"] = true,
};
foreach (var (key, value) in data.metadata.OrderBy(item => item.Key))
content[key] = value.Value;
}

content["title"] = data.title;
content["content"] = ApiPageHtmlTemplate.Render(data, Markup).ToString();
content["yamlmime"] = "ApiPage";
content["_disableNextArticle"] = true;

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

Expand Down
1 change: 1 addition & 0 deletions src/Docfx.Dotnet/Docfx.Dotnet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="HtmlAgilityPack" />
<PackageReference Include="ICSharpCode.Decompiler" />
<PackageReference Include="IgnoresAccessChecksToGenerator" PrivateAssets="All" />
<PackageReference Include="OneOf" />
Expand Down
13 changes: 13 additions & 0 deletions src/Docfx.Dotnet/DotnetApiCatalog.ApiPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Docfx.Common.Git;
using Docfx.DataContracts.ManagedReference;
using Docfx.Plugins;
using HtmlAgilityPack;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Shared.Extensions;
using OneOf;
Expand Down Expand Up @@ -96,9 +97,14 @@ _ when SymbolHelper.IsMember(method) => "Method",
throw new NotSupportedException($"Unknown symbol type kind {symbols[0].symbol}");
}

var metadata = new Dictionary<string, OneOf<string, string[]>>();
if (!string.IsNullOrEmpty(comment?.Summary))
metadata["description"] = HtmlInnerText(comment.Summary);

output(config.OutputFolder, id, new ApiPage
{
title = title,
metadata = metadata.Count > 0 ? metadata : null,
languageId = "csharp",
body = body.ToArray(),
});
Expand Down Expand Up @@ -760,6 +766,13 @@ Span Link(string text, string? url)
});
}
}

static string HtmlInnerText(string html)
{
var doc = new HtmlDocument();
doc.LoadHtml(html);
return doc.DocumentNode.InnerText;
}
}

#endif
4 changes: 4 additions & 0 deletions templates/common/ManagedReference.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ exports.transform = function (model) {
}
}

if (model.summary && !model.description) {
model.description = model.summary.replace(/<.*?>/gi, '').replace(/(\r\n|\n|\r)/gm, ' ').trim();
}

return model;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,7 @@
"hideSubtitle": false,
"isClass": true,
"inClass": true,
"description": "This works: . This does not work: .",
"_disableToc": false,
"_disableNextArticle": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,7 @@
"hideSubtitle": false,
"isClass": true,
"inClass": true,
"description": "This works: . This does not work: .",
"_disableToc": false,
"_disableNextArticle": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,7 @@
"hideSubtitle": false,
"isClass": true,
"inClass": true,
"description": "This is a test class.",
"_disableToc": false,
"_disableNextArticle": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,7 @@
"hideSubtitle": false,
"isClass": true,
"inClass": true,
"description": "This is a test class to have something for DocFX to document.",
"_disableToc": false,
"_disableNextArticle": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,7 @@
"hideSubtitle": false,
"isClass": true,
"inClass": true,
"description": "A nice class",
"_disableToc": false,
"_disableNextArticle": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,7 @@
"hideSubtitle": false,
"isClass": true,
"inClass": true,
"description": "This is the BaseClass",
"_disableToc": false,
"_disableNextArticle": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,7 @@
"hideSubtitle": false,
"isClass": true,
"inClass": true,
"description": "This is summary from vb class...",
"_disableToc": false,
"_disableNextArticle": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -4296,6 +4296,7 @@
"hideSubtitle": false,
"isClass": true,
"inClass": true,
"description": "Here's main class of this Demo. You can see mostly type of article within this class and you for more detail, please see the remarks. this class is a template class. It has two Generic parameter. they are: T and K. The extension method of this class can refer to class",
"_disableToc": false,
"_disableNextArticle": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@
"hideSubtitle": false,
"isClass": false,
"inEnum": true,
"description": "Enumeration ColorType",
"isEnum": true,
"_disableToc": false,
"_disableNextArticle": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@
"hideSubtitle": false,
"isClass": true,
"inDelegate": true,
"description": "Delegate ContainersRefTypeDelegate",
"_disableToc": false,
"_disableNextArticle": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,7 @@
"hideSubtitle": false,
"isClass": true,
"inStruct": true,
"description": "Struct ContainersRefType",
"_disableToc": false,
"_disableNextArticle": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@
"hideSubtitle": false,
"isClass": true,
"inDelegate": true,
"description": "Fake delegate",
"_disableToc": false,
"_disableNextArticle": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,7 @@
"hideSubtitle": false,
"isClass": true,
"inInterface": true,
"description": "This is basic interface of all animal.",
"_disableToc": false,
"_disableNextArticle": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@
"hideSubtitle": false,
"isClass": true,
"inInterface": true,
"description": "Cat's interface",
"_disableToc": false,
"_disableNextArticle": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,7 @@
"hideSubtitle": false,
"isClass": true,
"inClass": true,
"description": "It's the class that contains ICat interface's extension method. This class must be public and static. Also it shouldn't be a geneic class",
"_disableToc": false,
"_disableNextArticle": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@
"hideSubtitle": false,
"isClass": true,
"inDelegate": true,
"description": "Generic delegate with many constrains.",
"_disableToc": false,
"_disableNextArticle": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@
"hideSubtitle": false,
"isClass": true,
"inDelegate": true,
"description": "Delegate in the namespace",
"_disableToc": false,
"_disableNextArticle": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,7 @@
"hideSubtitle": false,
"isClass": true,
"inClass": true,
"description": "Tom class is only inherit from Object. Not any member inside itself.",
"_disableToc": false,
"_disableNextArticle": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,7 @@
"hideSubtitle": false,
"isClass": true,
"inClass": true,
"description": "TomFromBaseClass inherits from @",
"_disableToc": false,
"_disableNextArticle": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@
"hideSubtitle": false,
"isClass": false,
"inEnum": true,
"description": "Enumeration ColorType",
"isEnum": true,
"_disableToc": false,
"_disableNextArticle": true
Expand Down
Loading

0 comments on commit 6aa697f

Please sign in to comment.