Skip to content

[Repo Assist] Add (*** include-toc ***) directive for inline table of contents in literate F# scripts#1058

Open
github-actions[bot] wants to merge 5 commits intomainfrom
repo-assist/fix-issue-163-include-toc-eac5d5e355b2e64c
Open

[Repo Assist] Add (*** include-toc ***) directive for inline table of contents in literate F# scripts#1058
github-actions[bot] wants to merge 5 commits intomainfrom
repo-assist/fix-issue-163-include-toc-eac5d5e355b2e64c

Conversation

@github-actions
Copy link
Contributor

🤖 This PR was created by Repo Assist, an automated AI assistant.

Closes #163

Summary

Implements the long-requested in-page table of contents directive for literate F# scripts (.fsx files).

Usage

In any literate .fsx file, place one of these commands where you want the TOC to appear:

(*** include-toc ***)        (* list all headings up to depth 3 (default) *)
(*** include-toc:2 ***)      (* list only H1 and H2 headings *)
(*** include-toc:1 ***)      (* list only H1 headings *)

The directive generates an unordered list of hyperlinks pointing to the heading anchors in the rendered HTML. It requires generateAnchors = true (the default in fsdocs build).

Example

(**
# My Document

(*** include-toc:2 ***)

## Section One

Some content.

## Section Two

More content.
*)

Renders the TOC as links to #Section-One and #Section-Two.

Implementation

  • Document.fs: New TableOfContents of maxDepth: int * paragraphOptions: LiterateParagraphOptions case on LiterateParagraph.
  • ParseScript.fs: Handles (*** include-toc ***) and (*** include-toc:N ***) commands in transformBlocks, defaulting depth to 3.
  • Transformations.fs: New generateTableOfContents function that collects all Heading nodes, computes anchor names using the same algorithm as HtmlFormatting.formatAnchor (including UniqueNameGenerator simulation for duplicate headings), and replaces each TableOfContents paragraph with a ListBlock of DirectLink spans.
  • Formatting.fs: generateTableOfContents is called as the first step in transformDocument, before replaceLiterateParagraphs.

Test Status

  • Build: ✅ succeeded (0 warnings, 0 errors)
  • Tests: ✅ 116/116 passed (FSharp.Literate.Tests)

Generated by Repo Assist for issue #163

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@afb00b92a9514fee9a14c583f059a03d05738f70

…iterate F# scripts

Implements issue #163. Adds a new LiterateParagraph case TableOfContents and
a new transformation generateTableOfContents that replaces it with a ListBlock
of DirectLink elements pointing to all heading anchors in the document.

Usage in .fsx literate scripts:
  (*** include-toc ***)       - list all headings up to depth 3
  (*** include-toc:2 ***)     - list headings up to depth 2

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dsyme
Copy link
Contributor

dsyme commented Mar 3, 2026

/repo-assist add tests and documentation for this, and show us an example by replying in a comment here

@dsyme dsyme marked this pull request as ready for review March 3, 2026 00:52
dsyme and others added 2 commits March 3, 2026 00:52
…nb/ToFsx paths

- Add 5 NUnit tests covering parsing, HTML output, depth filtering,
  and duplicate anchor handling for (*** include-toc ***) and
  (*** include-toc:N ***)
- Fix bug: generateTableOfContents was only called in the
  transformDocument path (Formatting.fs) but not in the direct
  Literate.ToHtml, WriteHtml, ToLatex, WriteLatex, ToPynb, ToFsx
  methods in Literate.fs
- Add documentation in docs/literate.fsx explaining the directive,
  usage syntax, and correct placement as a standalone command

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions
Copy link
Contributor Author

github-actions bot commented Mar 3, 2026

Commit pushed: 98c54e9

Generated by Repo Assist

@github-actions
Copy link
Contributor Author

github-actions bot commented Mar 3, 2026

🤖 This is an automated response from Repo Assist.

Done! I've pushed a commit that adds tests and documentation. Here's a summary of what was added — and a bug fix I found along the way.


Example

The (*** include-toc ***) command must be placed as a standalone command between (**...*) blocks (not inside one, since ***) would close the preceding (**):

(**
# My Document
*)
(*** include-toc:2 ***)
(**
## Section One

Some content here.

## Section Two

More content here.
*)

This renders an unordered list of links at the position of (*** include-toc:2 ***):

Use (*** include-toc ***) for the default depth of 3, or (*** include-toc:N ***) to limit to headings up to depth N.


What was added

5 new tests in LiterateTests.fs:

  1. include-toc parses as TableOfContents with default depth 3
  2. include-toc:2 parses as TableOfContents with depth 2
  3. include-toc generates anchor links for all headings in HTML output
  4. include-toc:N filters out headings deeper than N
  5. include-toc handles duplicate heading names with unique anchors

All 121 tests pass.

Documentation added to docs/literate.fsx — a new "Table of Contents" section with a command table and example.


Bug fix

I found that generateTableOfContents was only called in the transformDocument path (Formatting.fs) but not in the direct Literate.ToHtml, WriteHtml, ToLatex, WriteLatex, ToPynb, and ToFsx methods. This meant the (*** include-toc ***) command would be silently dropped when using those APIs directly. I've fixed all six call sites in Literate.fs.

Generated by Repo Assist for issue #1058

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@da02dddebe71d9a937665abdcd1f5214dab852a7

@nojaf
Copy link
Collaborator

nojaf commented Mar 3, 2026

@dsyme devil's advocate but do we still needs this since we got the sidebar?

Example https://fsprojects.github.io/FSharp.Formatting/apidocs.html

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Would be nice to be able to auto generate a toc inside a page

2 participants