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

Feature - Autotoc generation #10574

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft

Feature - Autotoc generation #10574

wants to merge 8 commits into from

Conversation

saipramod
Copy link
Contributor

@saipramod saipramod commented Mar 4, 2025

This pull request introduces the ability to generate a Table of Contents (TOC) based on the underlying file system. The feature works as follows:

When auto TOC generation is enabled:

  • Add auto: true to the toc.yml located alongside the docfx.json.
  • This triggers the addition of items to the toc.yml in the order specified by the file system.
  • The process is repeated recursively across all folders and subfolders, starting at the root of the project.
  • If auto: false is encountered in any TOC, the auto TOC generation for that TOC will be halted.
  • Auto TOC generation will check for the presence of an href before adding one, thus avoiding any duplicates.

@rwrife
Copy link

rwrife commented Mar 4, 2025

What naming conventions are you following for generating the human-readable names in the TOC? I assume you'd want to use something similar to what ADO Wiki follows or give the end-user options for detecting camel case, snake case, etc. Also, should there be an option to output toc.yml to the end user so they could use autotoc generation for the first pass, then easily switch to providing manually updated toc.yml files?

@rwrife
Copy link

rwrife commented Mar 4, 2025

Here are some rules I was following (in Powershell) to convert the folder name to human readable to handle these scenarios:

hello-world = Hello World
helloWorld = Hello World
MicrosoftAI = Microsoft AI

function Convert-HyphenToHumanReadable {
  param (
    [string]$InputString
  )

  # Replace hyphens (-) with spaces
  $readableName = $InputString -replace '-', ' '

  # Replace URL-encoded symbols using System.Uri to decode
  $decodedName = [System.Uri]::UnescapeDataString($readableName)

  return $decodedName
}

function Convert-CamelCaseToHumanReadable {
  param (
    [string]$InputString
  )

  # Step 1: Add space before capital letters, except for consecutive capitals
  $result = $InputString -creplace '(?<!^)(?<![A-Z\s_-])(?=[A-Z])', ' '

  # Step 2: Add space between a group of capitals and a lowercase letter
  $result = $result -creplace '(?<=[A-Z])(?=[A-Z][a-z])', ' '

  # Step 3: Special handling for words ending with 's' or 'S'
  $result = $result -creplace '\s([A-Z]+s)(?=$|\s)', '$1'

  # Step 4: Remove any extra spaces
  $result = $result -replace '\s+', ' '

  return $result.Trim()
}

@saipramod
Copy link
Contributor Author

saipramod commented Mar 5, 2025

Here are some rules I was following (in Powershell) to convert the folder name to human readable to handle these scenarios:

hello-world = Hello World helloWorld = Hello World MicrosoftAI = Microsoft AI

function Convert-HyphenToHumanReadable {
  param (
    [string]$InputString
  )

  # Replace hyphens (-) with spaces
  $readableName = $InputString -replace '-', ' '

  # Replace URL-encoded symbols using System.Uri to decode
  $decodedName = [System.Uri]::UnescapeDataString($readableName)

  return $decodedName
}

function Convert-CamelCaseToHumanReadable {
  param (
    [string]$InputString
  )

  # Step 1: Add space before capital letters, except for consecutive capitals
  $result = $InputString -creplace '(?<!^)(?<![A-Z\s_-])(?=[A-Z])', ' '

  # Step 2: Add space between a group of capitals and a lowercase letter
  $result = $result -creplace '(?<=[A-Z])(?=[A-Z][a-z])', ' '

  # Step 3: Special handling for words ending with 's' or 'S'
  $result = $result -creplace '\s([A-Z]+s)(?=$|\s)', '$1'

  # Step 4: Remove any extra spaces
  $result = $result -replace '\s+', ' '

  return $result.Trim()
}

i see this an extension and not a part of the auto toc feature. so the auto toc does the job of generating toc using the underlying file system. you can apply an extension to massage those titles to fit whatever need. we can easily write one i think ryan. @yufeih can weigh in .

Found one, we already have a contribution for this.

massage-title-adowiki.zip

@saipramod
Copy link
Contributor Author

@dotnet-policy-service agree company="Microsoft"

@dotnet-policy-service agree company="Microsoft"

Sai Pramod Upadhyayula added 2 commits March 5, 2025 10:22
@saipramod saipramod requested a review from yufeih March 6, 2025 12:37
@yufeih
Copy link
Contributor

yufeih commented Mar 8, 2025

It makes sense to have a default naming convention for folders. Typically, folder names for URL segments uses either hyphens (-) or underscores (_) for spaces. We could follow the ADO wiki convention.

!m.LocalPathFromRoot.Contains("..")).OrderBy(f => f.LocalPathFromRoot.Split('/').Count()).FirstOrDefault();

// If there is no toc along side docfx.json, skip tocgen - validate behavior with yuefi
if (rootTocModel != null && rootTocModel.LocalPathFromRoot.Equals(tocFileName))
Copy link
Contributor

Choose a reason for hiding this comment

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

auto should allow gen TOC based on the directory structure for any TOC file, not just the root

Copy link
Contributor Author

@saipramod saipramod Mar 10, 2025

Choose a reason for hiding this comment

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

i am glad i paused to ask this. what would be the scenario where if the root toc.yml is not present and the auto toc still need to populate the toc.yml in sub folders ? my understanding is if the root toc.yml is missing, there is no nav i.e there is no way for users to reach the toc unless they remember the url. Is my understanding off ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants