diff --git a/src/GitLabWikiStrategy.cs b/src/GitLabWikiStrategy.cs index 02b5bf26..ee4d31c0 100644 --- a/src/GitLabWikiStrategy.cs +++ b/src/GitLabWikiStrategy.cs @@ -7,6 +7,7 @@ namespace Kampute.DocToolkit { using Kampute.DocToolkit.Routing; using Kampute.DocToolkit.Support; + using Kampute.DocToolkit.Topics; using System; /// @@ -25,6 +26,11 @@ namespace Kampute.DocToolkit /// Content organization for topics follows a simple hierarchical structure based on parent-child relationships. Topic /// pages are named after their titles, with child topics nested under their parents. /// + /// + /// GitLab serves the page at the path home as the front page of a wiki. When + /// is set, the page of the main topic is therefore placed at that path regardless of the topic's title, and all other pages are + /// nested under it. + /// /// /// GitLab displays the file name of a page as its title, showing hyphens as spaces. Because page names cannot contain every /// character a title can, renderers can write the original title in the title field of the page's front matter, which @@ -44,6 +50,11 @@ namespace Kampute.DocToolkit /// GitLab Wiki public class GitLabWikiStrategy : MarkdownAddressingStrategy { + /// + /// The path of the page that GitLab serves as the front page of a wiki. + /// + private const string HomePagePath = "home"; + /// /// /// Initializes a new instance of the class. @@ -74,6 +85,13 @@ public GitLabWikiStrategy(GitLabWikiOptions options) /// protected override string EncodePageName(string title) => EncodeWikiPath(title); + /// + /// + /// The page of the main topic is placed at the path home, which GitLab serves as the front page of a wiki, regardless + /// of the topic's title. + /// + protected override string GetTopicPath(ITopic topic) => IsMainTopic(topic) ? HomePagePath : base.GetTopicPath(topic); + /// /// Converts the given title to a GitLab Wiki page name that is valid as a file name and as a URL path segment. /// diff --git a/src/Kampute.DocToolkit.csproj b/src/Kampute.DocToolkit.csproj index 83791f3c..f1234f29 100644 --- a/src/Kampute.DocToolkit.csproj +++ b/src/Kampute.DocToolkit.csproj @@ -4,7 +4,7 @@ netstandard2.1 Kampute.DocToolkit Provides extensible pipeline for generating .NET API documentation by transforming assembly metadata and XML documentation into structured models, with automatic cross-reference resolution, support for multiple output formats (HTML, Markdown), and integration of conceptual topics. - 3.1.0 + 3.1.1 Kampute Kambiz Khojasteh Copyright (C) Kampute diff --git a/src/Routing/MarkdownAddressingStrategy.cs b/src/Routing/MarkdownAddressingStrategy.cs index d79153c2..57f1b16f 100644 --- a/src/Routing/MarkdownAddressingStrategy.cs +++ b/src/Routing/MarkdownAddressingStrategy.cs @@ -52,6 +52,8 @@ namespace Kampute.DocToolkit.Routing public abstract class MarkdownAddressingStrategy : AddressingStrategy, ILanguageSpecific, IContextualAddressingStrategy { private IDocumentationContext? context; + private readonly string mainTopicId; + private readonly string apiTopicId; private readonly Lazy mainPath; private readonly Lazy apiPath; @@ -65,11 +67,11 @@ protected MarkdownAddressingStrategy(MarkdownAddressingOptions options) { Language = options.Language; - var mainTopicId = options.MainTopicId; - mainPath = new Lazy(() => ResolveMainPath(mainTopicId)); + mainTopicId = options.MainTopicId; + mainPath = new(ResolveMainPath); - var apiTopicId = options.ApiTopicId; - apiPath = new Lazy(() => ResolveApiPath(apiTopicId)); + apiTopicId = options.ApiTopicId; + apiPath = new(ResolveApiPath); } /// @@ -208,7 +210,7 @@ protected virtual string GetTopicPath(ITopic topic) if (topic.ParentTopic is not null) path = GetTopicPath(topic.ParentTopic) + '/' + path; - else if (!string.IsNullOrEmpty(MainPath) && path != MainPath) + else if (!IsMainTopic(topic) && !string.IsNullOrEmpty(MainPath)) path = MainPath + '/' + path; return path; @@ -235,14 +237,33 @@ protected virtual string GetTopicPath(ITopic topic) /// protected virtual string EncodeFragmentIdentifier(string heading) => Markdown.ToHeadingAnchor(heading); + /// + /// Determines whether the specified topic is the main topic of the documentation. + /// + /// The topic to check. + /// + /// if is a top-level topic whose identifier matches ; + /// otherwise, . + /// + /// Thrown when is . + /// + /// Topic identifiers are compared case-insensitively, as the topic collection of a documentation context compares them. + /// + protected bool IsMainTopic(ITopic topic) + { + if (topic is null) + throw new ArgumentNullException(nameof(topic)); + + return topic.ParentTopic is null && !string.IsNullOrEmpty(mainTopicId) && string.Equals(topic.Id, mainTopicId, StringComparison.OrdinalIgnoreCase); + } + /// /// Resolves the main topic path. /// - /// The identifier of the main topic. - /// The resolved main path or if not found. - /// Thrown when does not exist in the documentation context. + /// The resolved main path, or if is not set. + /// Thrown when the main topic does not exist in the documentation context. /// Thrown when the addressing strategy is not bound to a documentation context. - private string? ResolveMainPath(string mainTopicId) + private string? ResolveMainPath() { if (string.IsNullOrEmpty(mainTopicId)) return null; @@ -253,17 +274,17 @@ protected virtual string GetTopicPath(ITopic topic) if (!context.Topics.TryGetById(mainTopicId, out var mainTopic)) throw new ArgumentException($"Could not find main topic '{mainTopicId}' in the documentation context.", nameof(mainTopicId)); - return EncodePageName(mainTopic.Name); + // A nested main topic is named after its title alone, because its path would depend on the main path itself. + return mainTopic.Source.ParentTopic is null ? GetTopicPath(mainTopic.Source) : EncodePageName(mainTopic.Name); } /// /// Resolves the API topic path. /// - /// The identifier of the API topic. /// The resolved API path or if not resolvable. - /// Thrown when does not exist in the documentation context. + /// Thrown when the API topic does not exist in the documentation context. /// Thrown when the addressing strategy is not bound to a documentation context. - private string? ResolveApiPath(string apiTopicId) + private string? ResolveApiPath() { if (string.IsNullOrEmpty(apiTopicId)) return MainPath; diff --git a/tests/GitLabWikiStrategyTests.cs b/tests/GitLabWikiStrategyTests.cs index 72bbeb45..de20dfa5 100644 --- a/tests/GitLabWikiStrategyTests.cs +++ b/tests/GitLabWikiStrategyTests.cs @@ -95,12 +95,12 @@ public string EncodeWikiPath_ReturnsExpectedPath(string input) } [Test] - public void MainTopicIdAndApiTopicId_PlacePagesUnderEncodedTopicPages() + public void MainTopicIdAndApiTopicId_PlaceMainTopicAtHomePathAndPagesUnderIt() { - var strategy = new GitLabWikiStrategy(new GitLabWikiOptions { MainTopicId = "home", ApiTopicId = "api", OmitExtensionInUrls = true }); + var strategy = new GitLabWikiStrategy(new GitLabWikiOptions { MainTopicId = "welcome", ApiTopicId = "api", OmitExtensionInUrls = true }); using var context = MockHelper.CreateDocumentationContext( [ - MockTopicBuilder.Topic("home", "Home").Build(), + MockTopicBuilder.Topic("welcome", "Welcome").Build(), MockTopicBuilder.Topic("api", "API Reference").Build(), MockTopicBuilder.Topic("guide", "User Guide").Build(), ]); @@ -108,11 +108,28 @@ public void MainTopicIdAndApiTopicId_PlacePagesUnderEncodedTopicPages() using (Assert.EnterMultipleScope()) { - Assert.That(strategy.TryResolveTopicAddress(context.Topics.First(static t => t.Id == "home").Source, out var main) ? main.RelativeUrl : null, Is.EqualTo("Home")); - Assert.That(strategy.TryResolveTopicAddress(context.Topics.First(static t => t.Id == "guide").Source, out var guide) ? guide.RelativeUrl : null, Is.EqualTo("Home/User-Guide")); - Assert.That(strategy.TryResolveAddressByCodeReference("N:System", out var ns) ? ns.RelativeUrl : null, Is.EqualTo("Home/API-Reference/System")); - Assert.That(strategy.TryResolveAddressByCodeReference("T:System.DateTime", out var type) ? type.RelativeUrl : null, Is.EqualTo("Home/API-Reference/System/DateTime")); + Assert.That(strategy.TryResolveTopicAddress(context.Topics.First(static t => t.Id == "welcome").Source, out var main) ? main.RelativeUrl : null, Is.EqualTo("home")); + Assert.That(strategy.TryResolveTopicAddress(context.Topics.First(static t => t.Id == "api").Source, out var api) ? api.RelativeUrl : null, Is.EqualTo("home/API-Reference")); + Assert.That(strategy.TryResolveTopicAddress(context.Topics.First(static t => t.Id == "guide").Source, out var guide) ? guide.RelativeUrl : null, Is.EqualTo("home/User-Guide")); + Assert.That(strategy.TryResolveAddressByCodeReference("N:System", out var ns) ? ns.RelativeUrl : null, Is.EqualTo("home/API-Reference/System")); + Assert.That(strategy.TryResolveAddressByCodeReference("T:System.DateTime", out var type) ? type.RelativeUrl : null, Is.EqualTo("home/API-Reference/System/DateTime")); } } + + [Test] + public void MainTopicId_TopicNamedLikeHomePage_IsNestedUnderHomePage() + { + var strategy = new GitLabWikiStrategy(new GitLabWikiOptions { MainTopicId = "welcome", OmitExtensionInUrls = true }); + using var context = MockHelper.CreateDocumentationContext( + [ + MockTopicBuilder.Topic("welcome", "Welcome").Build(), + MockTopicBuilder.Topic("other", "home").Build(), + ]); + ((IContextualAddressingStrategy)strategy).InitializeWithContext(context); + + var other = context.Topics.First(static t => t.Id == "other").Source; + + Assert.That(strategy.TryResolveTopicAddress(other, out var address) ? address.RelativeUrl : null, Is.EqualTo("home/home")); + } } }