diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/ContentWithDetailsDto.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/ContentWithDetailsDto.cs index 06edbd7d88..6c8c7fae47 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/ContentWithDetailsDto.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/ContentWithDetailsDto.cs @@ -42,6 +42,8 @@ namespace Volo.Docs.Documents [JsonProperty("items")] public List Items { get; set; } + public bool IsLeaf => !HasChildItems; + public bool HasChildItems => Items != null && Items.Any(); public bool IsEmpty => Text == null && Path == null; diff --git a/modules/docs/src/Volo.Docs.Web/Areas/Documents/Helpers/TagHelpers/TreeTagHelper.cs b/modules/docs/src/Volo.Docs.Web/Areas/Documents/Helpers/TagHelpers/TreeTagHelper.cs index ac26d00d68..c5b28ec25d 100644 --- a/modules/docs/src/Volo.Docs.Web/Areas/Documents/Helpers/TagHelpers/TreeTagHelper.cs +++ b/modules/docs/src/Volo.Docs.Web/Areas/Documents/Helpers/TagHelpers/TreeTagHelper.cs @@ -9,19 +9,13 @@ namespace Volo.Docs.Areas.Documents.Helpers.TagHelpers [HtmlTargetElement("ul", Attributes = "root-node")] public class TreeTagHelper : TagHelper { - private const string LiItemTemplateWithLink = @"
  • - - {2} - {3} -
  • "; + private const string LiItemTemplateWithLink = @"
  • {2}{3}
  • "; + private const string ListItemAnchor = @"{2}"; private const string ListItemSpan = @"{1}"; - - private const string UlItemTemplate = @""; + private const string UlItemTemplate = @""; [HtmlAttributeName("root-node")] public NavigationNode RootNode { get; set; } @@ -98,61 +92,46 @@ namespace Volo.Docs.Areas.Documents.Helpers.TagHelpers { var anchorCss = node.Path.IsNullOrEmpty() ? "tree-toggle" : ""; var isNodeSelected = node.IsSelected(SelectedDocumentName); - - var normalizedPath = node.Path != null && node.Path.EndsWith("." + ProjectFormat) - ? node.Path.Left(node.Path.Length - ProjectFormat.Length - 1) - : node.Path; - - if (node.Path.IsNullOrEmpty() && !node.HasChildItems) + var listItemCss = node.HasChildItems ? "nav-header" : "last-link"; + if (isNodeSelected) { - //span - var span = string.Format(ListItemSpan, anchorCss, node.Text.IsNullOrEmpty() ? "?" : node.Text); - var listItemCss = node.HasChildItems ? "nav-header" : "last-link"; - if (isNodeSelected) - { - listItemCss += " selected-tree"; - } - - return string.Format(LiItemTemplateWithLink, - listItemCss, - node.HasChildItems ? "chevron-right" : "long-arrow-right", - span, - content); + listItemCss += " selected-tree"; } - else if (node.Path.IsNullOrEmpty() && node.HasChildItems) + + string listInnerItem; + if (node.Path.IsNullOrEmpty() && node.IsLeaf) { - //anchor - var path = "javascript:;"; - var anchor = string.Format(ListItemAnchor, path, anchorCss, node.Text.IsNullOrEmpty() ? "?" : node.Text); - var listItemCss = node.HasChildItems ? "nav-header" : "last-link"; - if (isNodeSelected) - { - listItemCss += " selected-tree"; - } - - return string.Format(LiItemTemplateWithLink, - listItemCss, - node.HasChildItems ? "chevron-right" : "long-arrow-right", - anchor, - content); + listInnerItem = string.Format(ListItemSpan, anchorCss, node.Text.IsNullOrEmpty() ? "?" : node.Text); } else { - //anchor - var path = "/documents/" + ProjectName + "/" + Version + "/" + normalizedPath; - var anchor = string.Format(ListItemAnchor, path, anchorCss, node.Text.IsNullOrEmpty() ? "?" : node.Text); - var listItemCss = node.HasChildItems ? "nav-header" : "last-link"; - if (isNodeSelected) - { - listItemCss += " selected-tree"; - } - - return string.Format(LiItemTemplateWithLink, - listItemCss, - node.HasChildItems ? "chevron-right" : "long-arrow-right", - anchor, - content); + listInnerItem = string.Format(ListItemAnchor, NormalizePath(node.Path, node.HasChildItems), anchorCss, node.Text.IsNullOrEmpty() ? "?" : node.Text); + } + + return string.Format(LiItemTemplateWithLink, + listItemCss, + node.HasChildItems ? "chevron-right" : "long-arrow-right", + listInnerItem, + content); + } + + private string NormalizePath(string path, bool hasChildItems) + { + var pathWithoutFileExtension = RemoveFileExtensionFromPath(path); + + return hasChildItems ? "javascript:;" : "/documents/" + ProjectName + "/" + Version + "/" + pathWithoutFileExtension; + } + + private string RemoveFileExtensionFromPath(string path) + { + if (path == null) + { + return null; } + + return path.EndsWith("." + ProjectFormat) + ? path.Left(path.Length - ProjectFormat.Length - 1) + : path; } }