diff --git a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs index ec8d7ad183..3254882f7c 100644 --- a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs +++ b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs @@ -101,7 +101,7 @@ namespace Volo.Docs.Pages.Documents.Project public DocumentNavigationsDto DocumentNavigationsDto { get; private set; } private const int MaxDescriptionMetaTagLength = 200; - private const int MaxTocLevel = 3; + private const int TocLevelCount = 2; private readonly IDocumentAppService _documentAppService; private readonly IDocumentToHtmlConverterFactory _documentToHtmlConverterFactory; private readonly IProjectAppService _projectAppService; @@ -544,7 +544,7 @@ namespace Volo.Docs.Pages.Documents.Project if (Document != null && !Document.Content.IsNullOrEmpty()) { - TocItems = _tocGeneratorService.GenerateTocItems(Document.Content, MaxTocLevel); + TocItems = _tocGeneratorService.GenerateTocItems(Document.Content, TocLevelCount); } await ConvertDocumentContentToHtmlAsync(); diff --git a/modules/docs/src/Volo.Docs.Web/TableOfContents/ITocGeneratorService.cs b/modules/docs/src/Volo.Docs.Web/TableOfContents/ITocGeneratorService.cs index a33fb92e69..7a57597efe 100644 --- a/modules/docs/src/Volo.Docs.Web/TableOfContents/ITocGeneratorService.cs +++ b/modules/docs/src/Volo.Docs.Web/TableOfContents/ITocGeneratorService.cs @@ -7,9 +7,9 @@ public interface ITocGeneratorService : IApplicationService { List GenerateTocHeadings(string markdownContent); - List GenerateTocItems(List tocHeadings, int topLevel, int maxLevel); + List GenerateTocItems(List tocHeadings, int topLevel, int levelCount); int GetTopLevel(List tocHeadings); - List GenerateTocItems(string markdownContent, int maxLevel, int? topLevel = null); + List GenerateTocItems(string markdownContent, int levelCount, int? topLevel = null); } diff --git a/modules/docs/src/Volo.Docs.Web/TableOfContents/TocGeneratorService.cs b/modules/docs/src/Volo.Docs.Web/TableOfContents/TocGeneratorService.cs index e9f70c60c7..5b7a1eeb23 100644 --- a/modules/docs/src/Volo.Docs.Web/TableOfContents/TocGeneratorService.cs +++ b/modules/docs/src/Volo.Docs.Web/TableOfContents/TocGeneratorService.cs @@ -32,8 +32,10 @@ public class TocGeneratorService : ITocGeneratorService, ITransientDependency .ToList(); } - public virtual List GenerateTocItems(List tocHeadings, int topLevel, int maxLevel) + public virtual List GenerateTocItems(List tocHeadings, int topLevel, int levelCount) { + var maxLevel = GetMaxLevel(tocHeadings, topLevel, levelCount); + var filteredHeadings = tocHeadings .Where(heading => heading.Level >= topLevel && heading.Level <= maxLevel) .ToList(); @@ -47,7 +49,7 @@ public class TocGeneratorService : ITocGeneratorService, ITransientDependency .FirstOrDefault(level => headings.Count(h => h.Level == level) > 1, MinHeadingLevel); } - public virtual List GenerateTocItems(string markdownContent, int maxLevel, int? topLevel = null) + public virtual List GenerateTocItems(string markdownContent, int levelCount, int? topLevel = null) { var headings = GenerateTocHeadings(markdownContent); if (headings.Count == 0) @@ -56,7 +58,7 @@ public class TocGeneratorService : ITocGeneratorService, ITransientDependency } var resolvedTopLevel = topLevel ?? GetTopLevel(headings); - return GenerateTocItems(headings, resolvedTopLevel, maxLevel); + return GenerateTocItems(headings, resolvedTopLevel, levelCount); } protected virtual MarkdownPipeline CreateMarkdownPipeline() @@ -180,6 +182,16 @@ public class TocGeneratorService : ITocGeneratorService, ITransientDependency } } + protected virtual int GetMaxLevel(List tocHeadings, int topLevel, int levelCount) + { + return tocHeadings.Where(h => h.Level >= topLevel) + .Select(h => h.Level) + .Distinct() + .OrderBy(level => level) + .Skip(levelCount - 1) + .FirstOrDefault(topLevel); + } + protected virtual bool HasExactCount(IEnumerable enumerable, int count) { var itemCount = 0;