Skip to content

feat(toc): add --depth option to limit heading level - #174

Merged
ekalinin merged 1 commit into
masterfrom
feat/toc-depth
Sep 28, 2026
Merged

ekalinin merged 1 commit into
masterfrom
feat/toc-depth

Conversation

@ekalinin

Copy link
Copy Markdown
Owner

Summary

Fixes #25: a way to keep only the top heading levels in the TOC.

--depth <NUM> keeps only headings with level <= NUM. The level is absolute, as
--depth in github-markdown-toc.go: --depth 2 keeps # and ##. An empty value or
0 means no limit, which is the current behavior.

  • gh-md-toc:
    • gh_toc_grab: one condition at the start of the awk script shared by the default
      and OS/390 branches, so it applies to stdin, local files, remote URLs, several
      inputs and --insert. The value is passed with awk -v and compared as a number.
    • gh_toc_app: --depth is parsed right after --indent and before -, in the
      existing fixed order of options. gh_toc gets it as a new last parameter.
    • --help: a new line right after --indent.
  • README.md: an example with --depth 1 in "Local files", without a new heading,
    so the README TOC asserted in the tests does not change.
  • tests/tests.bats: tests for a local file (--depth 2) and stdin (--depth 1 -)
    with a new fixture tests/test directory/test_depth.md; test_help updated.
  • openspec/changes/add-toc-depth: proposal, design, spec and tasks.

Not included: --start-depth, the --depth=NUM syntax, validation of the value,
changes to indentation (#30, #75), options in any order.

Testing

  • The new tests fail without the change; all 17 tests pass with it.
  • --depth 0 gives the same output as without the option.
  • Checked manually: a remote URL with --depth 1 and --insert with --depth 1.
  • make lint passes.

@ekalinin
ekalinin merged commit 9bf1b6b into master Sep 28, 2026
1 check passed
@ekalinin
ekalinin deleted the feat/toc-depth branch September 29, 2026 09:14
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.

New feature to cut TOC items down to a given level

1 participant