Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/GitLabWikiStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Kampute.DocToolkit
{
using Kampute.DocToolkit.Routing;
using Kampute.DocToolkit.Support;
using Kampute.DocToolkit.Topics;
using System;

/// <summary>
Expand All @@ -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.
/// </para>
/// <para>
/// GitLab serves the page at the path <c>home</c> as the front page of a wiki. When <see cref="MarkdownAddressingOptions.MainTopicId"/>
/// 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.
/// </para>
/// <note type="tip" title="Tip">
/// 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 <c>title</c> field of the page's front matter, which
Expand All @@ -44,6 +50,11 @@ namespace Kampute.DocToolkit
/// <seealso href="https://docs.gitlab.com/user/project/wiki/">GitLab Wiki</seealso>
public class GitLabWikiStrategy : MarkdownAddressingStrategy
{
/// <summary>
/// The path of the page that GitLab serves as the front page of a wiki.
/// </summary>
private const string HomePagePath = "home";

/// <overloads>
/// <summary>
/// Initializes a new instance of the <see cref="GitLabWikiStrategy"/> class.
Expand Down Expand Up @@ -74,6 +85,13 @@ public GitLabWikiStrategy(GitLabWikiOptions options)
/// </remarks>
protected override string EncodePageName(string title) => EncodeWikiPath(title);

/// <inheritdoc/>
/// <remarks>
/// The page of the main topic is placed at the path <c>home</c>, which GitLab serves as the front page of a wiki, regardless
/// of the topic's title.
/// </remarks>
protected override string GetTopicPath(ITopic topic) => IsMainTopic(topic) ? HomePagePath : base.GetTopicPath(topic);

/// <summary>
/// Converts the given title to a GitLab Wiki page name that is valid as a file name and as a URL path segment.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Kampute.DocToolkit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.1</TargetFramework>
<Title>Kampute.DocToolkit</Title>
<Description>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.</Description>
<Version>3.1.0</Version>
<Version>3.1.1</Version>
<Company>Kampute</Company>
<Authors>Kambiz Khojasteh</Authors>
<Copyright>Copyright (C) Kampute</Copyright>
Expand Down
47 changes: 34 additions & 13 deletions src/Routing/MarkdownAddressingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string?> mainPath;
private readonly Lazy<string?> apiPath;

Expand All @@ -65,11 +67,11 @@ protected MarkdownAddressingStrategy(MarkdownAddressingOptions options)
{
Language = options.Language;

var mainTopicId = options.MainTopicId;
mainPath = new Lazy<string?>(() => ResolveMainPath(mainTopicId));
mainTopicId = options.MainTopicId;
mainPath = new(ResolveMainPath);

var apiTopicId = options.ApiTopicId;
apiPath = new Lazy<string?>(() => ResolveApiPath(apiTopicId));
apiTopicId = options.ApiTopicId;
apiPath = new(ResolveApiPath);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -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;
Expand All @@ -235,14 +237,33 @@ protected virtual string GetTopicPath(ITopic topic)
/// </remarks>
protected virtual string EncodeFragmentIdentifier(string heading) => Markdown.ToHeadingAnchor(heading);

/// <summary>
/// Determines whether the specified topic is the main topic of the documentation.
/// </summary>
/// <param name="topic">The topic to check.</param>
/// <returns>
/// <see langword="true"/> if <paramref name="topic"/> is a top-level topic whose identifier matches <see cref="MarkdownAddressingOptions.MainTopicId"/>;
/// otherwise, <see langword="false"/>.
/// </returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="topic"/> is <see langword="null"/>.</exception>
/// <remarks>
/// Topic identifiers are compared case-insensitively, as the topic collection of a documentation context compares them.
/// </remarks>
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);
}

/// <summary>
/// Resolves the main topic path.
/// </summary>
/// <param name="mainTopicId">The identifier of the main topic.</param>
/// <returns>The resolved main path or <see langword="null"/> if not found.</returns>
/// <exception cref="ArgumentException">Thrown when <paramref name="mainTopicId"/> does not exist in the documentation context.</exception>
/// <returns>The resolved main path, or <see langword="null"/> if <see cref="MarkdownAddressingOptions.MainTopicId"/> is not set.</returns>
/// <exception cref="ArgumentException">Thrown when the main topic does not exist in the documentation context.</exception>
/// <exception cref="InvalidOperationException">Thrown when the addressing strategy is not bound to a documentation context.</exception>
private string? ResolveMainPath(string mainTopicId)
private string? ResolveMainPath()
{
if (string.IsNullOrEmpty(mainTopicId))
return null;
Expand All @@ -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);
}

/// <summary>
/// Resolves the API topic path.
/// </summary>
/// <param name="apiTopicId">The identifier of the API topic.</param>
/// <returns>The resolved API path or <see langword="null"/> if not resolvable.</returns>
/// <exception cref="ArgumentException">Thrown when <paramref name="apiTopicId"/> does not exist in the documentation context.</exception>
/// <exception cref="ArgumentException">Thrown when the API topic does not exist in the documentation context.</exception>
/// <exception cref="InvalidOperationException">Thrown when the addressing strategy is not bound to a documentation context.</exception>
private string? ResolveApiPath(string apiTopicId)
private string? ResolveApiPath()
{
if (string.IsNullOrEmpty(apiTopicId))
return MainPath;
Expand Down
31 changes: 24 additions & 7 deletions tests/GitLabWikiStrategyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,41 @@ 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<MarkdownFormat>(
[
MockTopicBuilder.Topic("home", "Home").Build(),
MockTopicBuilder.Topic("welcome", "Welcome").Build(),
MockTopicBuilder.Topic("api", "API Reference").Build(),
MockTopicBuilder.Topic("guide", "User Guide").Build(),
]);
((IContextualAddressingStrategy)strategy).InitializeWithContext(context);

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<MarkdownFormat>(
[
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"));
}
}
}
Loading