Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add categoryLayout option for metadata generation #9965

Merged
merged 1 commit into from
May 30, 2024

Conversation

filzrev
Copy link
Contributor

@filzrev filzrev commented May 29, 2024

This PR intended to fix #9934 feature request issue by adding new categoryLayout option.

Description
This PR add following options to docfx metadata config.


categoryLayout

Specifies how categories in TOC are organized:

  • flattened (default): Renders the namespaces as a plain label.
  • nested: Renders the categories in a nested tree form.
  • none: Don't render category labels. (It's added to support similar layout as mref output format)

Note

This setting is valid when using apiPage or markdown output format. mref format don't support categories.


Unit Tests
It's manually tested by using docfx\samples\seed docs with both samePage / separatedPages options.
And it pass following unit tests. (It require slow code analysis operation. So it's excluded from this commit)

MetadataCommandApiPageTocCategoryTest.cs
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Docfx.Common;
using Docfx.DataContracts.Common;
using Docfx.Dotnet;
using Docfx.Tests.Common;
using FluentAssertions;

namespace Docfx.Tests;

public partial class MetadataCommandApiPageTocCategoryTest : TestBase
{
    /// <summary>
    /// Use MetadataCommand to generate YAML files from a c# project and a VB project separately
    /// </summary>
    private readonly string _outputFolder;
    private readonly string _projectFolder;

    public MetadataCommandApiPageTocCategoryTest()
    {
        _outputFolder = GetRandomFolder();
        _projectFolder = GetRandomFolder();
    }

    [Fact]
    [Trait("Related", "docfx")]
    public async Task TestSeparatedPagesTocWithDefaulCategories()
    {
        var projectFile = Path.Combine(_projectFolder, "test.csproj");
        var sourceFile = Path.Combine(_projectFolder, "test.cs");
        File.Copy("Assets/test.csproj.sample.1", projectFile);
        File.Copy("Assets/test-multinamespace.cs.sample.1", sourceFile);

        await DotnetApiCatalog.Exec(
            new(new MetadataJsonItemConfig
            {
                Dest = _outputFolder,
                Src = new(new FileMappingItem(projectFile)) { Expanded = true },
                NamespaceLayout = NamespaceLayout.Nested,
                CategoryLayout = CategoryLayout.Flattened,
                MemberLayout = MemberLayout.SeparatePages,
                OutputFormat = MetadataOutputFormat.ApiPage,
            }),
            new(), Directory.GetCurrentDirectory());

        var file = Path.Combine(_outputFolder, "toc.yml");
        Assert.True(File.Exists(file));

        var tocViewModel = YamlUtility.Deserialize<TocItemViewModel[]>(file);

        var expected = YamlUtility.Deserialize<TocItemViewModel[]>(new StringReader(ExpectedData.Default));

        // Assert
        tocViewModel.Should().BeEquivalentTo(expected);
    }

    [Fact]
    [Trait("Related", "docfx")]
    public async Task TestSeparatedPagesTocWithNestedCategories()
    {
        var projectFile = Path.Combine(_projectFolder, "test.csproj");
        var sourceFile = Path.Combine(_projectFolder, "test.cs");
        File.Copy("Assets/test.csproj.sample.1", projectFile);
        File.Copy("Assets/test-multinamespace.cs.sample.1", sourceFile);

        await DotnetApiCatalog.Exec(
            new(new MetadataJsonItemConfig
            {
                Dest = _outputFolder,
                Src = new(new FileMappingItem(projectFile)) { Expanded = true },
                NamespaceLayout = NamespaceLayout.Nested,
                CategoryLayout = CategoryLayout.Nested,
                MemberLayout = MemberLayout.SeparatePages,
                OutputFormat = MetadataOutputFormat.ApiPage,
            }),
            new(), Directory.GetCurrentDirectory());

        var file = Path.Combine(_outputFolder, "toc.yml");
        Assert.True(File.Exists(file));

        var tocViewModel = YamlUtility.Deserialize<TocItemViewModel[]>(file);

        var expected = YamlUtility.Deserialize<TocItemViewModel[]>(new StringReader(ExpectedData.Nested));

        // Assert
        tocViewModel.Should().BeEquivalentTo(expected);
    }

    [Fact]
    [Trait("Related", "docfx")]
    public async Task TestSeparatedPagesTocWithNoneCategories()
    {
        var projectFile = Path.Combine(_projectFolder, "test.csproj");
        var sourceFile = Path.Combine(_projectFolder, "test.cs");
        File.Copy("Assets/test.csproj.sample.1", projectFile);
        File.Copy("Assets/test-multinamespace.cs.sample.1", sourceFile);

        await DotnetApiCatalog.Exec(
            new(new MetadataJsonItemConfig
            {
                Dest = _outputFolder,
                Src = new(new FileMappingItem(projectFile)) { Expanded = true },
                NamespaceLayout = NamespaceLayout.Nested,
                CategoryLayout = CategoryLayout.None,
                MemberLayout = MemberLayout.SeparatePages,
                OutputFormat = MetadataOutputFormat.ApiPage,
            }),
            new(), Directory.GetCurrentDirectory());

        var file = Path.Combine(_outputFolder, "toc.yml");
        Assert.True(File.Exists(file));

        var tocViewModel = YamlUtility.Deserialize<TocItemViewModel[]>(file);

        var expected = YamlUtility.Deserialize<TocItemViewModel[]>(new StringReader(ExpectedData.None));

        // Assert
        tocViewModel.Should().BeEquivalentTo(expected);
    }

    private static class ExpectedData
    {
        public static readonly string Default = @"
- name: OtherNamespace
  href: OtherNamespace.yml
  items:
  - name: Classes
  - name: OtherBar
    href: OtherNamespace.OtherBar.yml
    items:
    - name: Methods
    - name: FooBar<TArg>
      href: OtherNamespace.OtherBar.FooBar.yml
- name: Samples
  href: Samples.yml
  items:
  - name: Foo
    href: Samples.Foo.yml
    items:
    - name: Sub
      href: Samples.Foo.Sub.yml
      items:
      - name: Classes
      - name: SubBar
        href: Samples.Foo.Sub.SubBar.yml
        items:
        - name: Methods
        - name: FooBar<TArg>
          href: Samples.Foo.Sub.SubBar.FooBar.yml
    - name: Classes
    - name: Bar
      href: Samples.Foo.Bar.yml
      items:
      - name: Methods
      - name: FooBar<TArg>
        href: Samples.Foo.Bar.FooBar.yml
".Trim();

        public static readonly string Nested = @"
- name: OtherNamespace
  href: OtherNamespace.yml
  items:
  - name: Classes
    items:
    - name: OtherBar
      href: OtherNamespace.OtherBar.yml
      items:
      - name: Methods
        items:
        - name: FooBar<TArg>
          href: OtherNamespace.OtherBar.FooBar.yml
- name: Samples
  href: Samples.yml
  items:
  - name: Foo
    href: Samples.Foo.yml
    items:
    - name: Sub
      href: Samples.Foo.Sub.yml
      items:
      - name: Classes
        items:
        - name: SubBar
          href: Samples.Foo.Sub.SubBar.yml
          items:
          - name: Methods
            items:
            - name: FooBar<TArg>
              href: Samples.Foo.Sub.SubBar.FooBar.yml
    - name: Classes
      items:
      - name: Bar
        href: Samples.Foo.Bar.yml
        items:
        - name: Methods
          items:
          - name: FooBar<TArg>
            href: Samples.Foo.Bar.FooBar.yml
".Trim();

        public static readonly string None = @"
- name: OtherNamespace
  href: OtherNamespace.yml
  items:
  - name: OtherBar
    href: OtherNamespace.OtherBar.yml
    items:
    - name: FooBar<TArg>
      href: OtherNamespace.OtherBar.FooBar.yml
- name: Samples
  href: Samples.yml
  items:
  - name: Foo
    href: Samples.Foo.yml
    items:
    - name: Sub
      href: Samples.Foo.Sub.yml
      items:
      - name: SubBar
        href: Samples.Foo.Sub.SubBar.yml
        items:
        - name: FooBar<TArg>
          href: Samples.Foo.Sub.SubBar.FooBar.yml
    - name: Bar
      href: Samples.Foo.Bar.yml
      items:
      - name: FooBar<TArg>
        href: Samples.Foo.Bar.FooBar.yml
".Trim();
    }
}

Copy link

codecov bot commented May 29, 2024

Codecov Report

Attention: Patch coverage is 50.00000% with 10 lines in your changes are missing coverage. Please review.

Project coverage is 78.90%. Comparing base (fe673ec) to head (64af69e).
Report is 179 commits behind head on main.

Files Patch % Lines
src/Docfx.Dotnet/DotnetApiCatalog.Toc.cs 40.00% 8 Missing and 1 partial ⚠️
src/docfx/Models/MetadataCommand.cs 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9965      +/-   ##
==========================================
+ Coverage   74.31%   78.90%   +4.59%     
==========================================
  Files         536      540       +4     
  Lines       23189    23428     +239     
  Branches     4056     4056              
==========================================
+ Hits        17234    18487    +1253     
+ Misses       4853     3804    -1049     
- Partials     1102     1137      +35     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yufeih yufeih added the new-feature Makes the pull request to appear in "New Features" section of the next release note label May 29, 2024
Copy link
Contributor

@yufeih yufeih left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Thank you @filzrev !

@yufeih yufeih merged commit 6ab4c6b into dotnet:main May 30, 2024
7 checks passed
p-kostov pushed a commit to ErpNetDocs/docfx that referenced this pull request Jun 28, 2024
renovate bot added a commit to buehler/dotnet-operator-sdk that referenced this pull request Jul 1, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [docfx](https://togithub.com/dotnet/docfx) | `2.76.0` -> `2.77.0` |
[![age](https://developer.mend.io/api/mc/badges/age/nuget/docfx/2.77.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/nuget/docfx/2.77.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/nuget/docfx/2.76.0/2.77.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/nuget/docfx/2.76.0/2.77.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>dotnet/docfx (docfx)</summary>

### [`v2.77.0`](https://togithub.com/dotnet/docfx/releases/tag/v2.77.0)

<!-- Release notes generated using configuration in .github/release.yml
at main -->

#### What's Changed

##### 💥 Breaking Changes

- chore: Drop .NET 7 SDK supports by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9911

##### 🎉 New Features

- feat: Enable PreferCSSPageSize option for PDF generation by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9850
- feat: Add docfx JSON Schema files and related tests by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9852
- feat: Add `toc.json` transform logics using `toc.extension.js` by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9953
- feat: Better error when link not found by
[@&#8203;Patrick8639](https://togithub.com/Patrick8639) in
[dotnet/docfx#9957
- feat: Add `categoryLayout` option for metadata generation by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9965
- feat: Permits to specify the placement of overwrites by
[@&#8203;Patrick8639](https://togithub.com/Patrick8639) in
[dotnet/docfx#9937
- feat: Support private symbols by
[@&#8203;Patrick8639](https://togithub.com/Patrick8639) in
[dotnet/docfx#9972
- feat: Add support for gzipped xrefmap that stored as local file by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9966
- feat: Enable view transitions feature by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9909

##### 🐞 Bug Fixes

- fix: PDF `Producer` document information by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9851
- fix: Xrefmap baseUrl problem reported at
[#&#8203;9866](https://togithub.com/dotnet/docfx/issues/9866) by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9869
- fix: Xref links are not resolved on docs site by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9880
- fix: Change same URL check logic to case invariant by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9883
- fix: Improve unresolved xref messages by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9884
- fix: Fix nightly build errors by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9913
- fix: TOC filter value is not shared between pages by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9912
- fix: Build problems when running .NET 6 version of docfx on some
environment by [@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9922
- fix: `docfx metadata` command throw `ArgumentException` when
referencing empty namespace by doc comment by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#10023
- fix: serve url link by
[@&#8203;WeihanLi](https://togithub.com/WeihanLi) in
[dotnet/docfx#10035

##### 🚀 Performance Improvements

- perf: Change serializer for XrefMap from NewtonsoftJson to
System.Text.Json by [@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9872
- perf: Remove some enum boxing in GlobMatcher by
[@&#8203;lahma](https://togithub.com/lahma) in
[dotnet/docfx#10051
- perf: Optimize CountWordInText by
[@&#8203;lahma](https://togithub.com/lahma) in
[dotnet/docfx#10050

##### 🔧 Engineering

- chore: Skip unstable SVG content check that returned from PlantUML
Online Server by [@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9858
- deps: Update Spectre.Console package versions by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9894
- chore: fix NU5129 warning on `dotnet pack` command by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9942
- chore: Add PolySharp libarary to use latest C# syntax by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9960
- chore: Add snapshot update workflow by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9969
- chore: Remove unused workflow settings by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#10030

##### 📄 Documentation

- docs: Fix typo by
[@&#8203;carlos-regis](https://togithub.com/carlos-regis) in
[dotnet/docfx#9871
- docs: Fix URL in markdown and match to html example by
[@&#8203;si618](https://togithub.com/si618) in
[dotnet/docfx#9881
- docs: Fix documentation site build warnings by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9895
- docs: Fix missing docfx.json config docs by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9935
- docs: Fix Docfx.App nuget package usage document by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#9994
- docs: Fix plugin related docs and logs by
[@&#8203;filzrev](https://togithub.com/filzrev) in
[dotnet/docfx#10029

#### New Contributors

- [@&#8203;carlos-regis](https://togithub.com/carlos-regis) made their
first contribution in
[dotnet/docfx#9871
- [@&#8203;si618](https://togithub.com/si618) made their first
contribution in
[dotnet/docfx#9881
- [@&#8203;Patrick8639](https://togithub.com/Patrick8639) made their
first contribution in
[dotnet/docfx#9957

**Full Changelog**:
dotnet/docfx@v2.76.0...v2.77.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "after 9pm,before 6am" in timezone
Europe/Zurich, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/buehler/dotnet-operator-sdk).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40MjEuMCIsInVwZGF0ZWRJblZlciI6IjM3LjQyMS4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJkZXBlbmRlbmNpZXMiXX0=-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-feature Makes the pull request to appear in "New Features" section of the next release note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request] Create Class, Interface and Enums as expanders in the TOC for .NET Documentation
2 participants