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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,14 @@ Now check the same file:
Next time when your file will be changed just repeat the command (`./gh-md-toc
--insert ...`) and TOC will be refreshed again.

To refresh the TOC in all markdown files of a directory, add `--skip-no-markers`:
the files without `<!--ts-->` or `<!--te-->` are skipped with a warning on stderr,
instead of an error:

```bash
$ find . -name '*.md' -exec ./gh-md-toc --insert --no-backup --hide-footer --skip-no-markers {} +
```

To get the whole file with the new TOC on stdout instead, use `--stdout`. The
file is not changed and no backup files are created. It takes one local file
or `-` for STDIN, and works with `--hide-footer` and `--skip-header`:
Expand Down
23 changes: 22 additions & 1 deletion gh-md-toc
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,21 @@ gh_toc(){
local depth=$8
local numbered=$9
local bullet=${10}
local skip_no_markers=${11}

if [ "$gh_src" = "" ]; then
echo "Please, enter URL or local path for a README.md"
exit 1
fi

# skip a local file without the markers before the API request, see #110
if [ "$skip_no_markers" = "yes" ] && [ "$(gh_is_url "$gh_src")" != "yes" ] && [ -f "$gh_src" ]; then
if ! grep -Eq '^<!--ts-->[[:space:]]*$' "$gh_src" || ! grep -Eq '^<!--te-->[[:space:]]*$' "$gh_src"; then
echo "$gh_src: Warning: no <!--ts--> or <!--te--> in the file, skipped" >&2
return
fi
fi


# Show "TOC" string only if working with one document
if [ "$gh_ttl_docs" = "1" ]; then
Expand Down Expand Up @@ -508,6 +517,7 @@ show_help() {
echo " See https://github.com/ekalinin/github-markdown-toc/issues/41 for details."
echo " --no-backup Remove backup file. Set --insert as well. Default: false."
echo " --hide-footer Do not write date & author of the last TOC update. Set --insert as well. Default: false."
echo " --skip-no-markers Skip files without <!--ts--> or <!--te-->. Set --insert as well. Default: false."
echo " --stdout Print the whole file with the new TOC to stdout. The file is not changed. Default: false."
echo " --skip-header Hide entry of the topmost headlines. Default: false."
echo " See https://github.com/ekalinin/github-markdown-toc/issues/125 for details."
Expand All @@ -524,6 +534,7 @@ gh_toc_app() {
local numbered=""
local bullet=""
local to_stdout=""
local skip_no_markers=""

if [ "$1" = '--help' ] || [ $# -eq 0 ] ; then
show_help
Expand Down Expand Up @@ -608,6 +619,12 @@ gh_toc_app() {
shift
fi

if [ "$1" = '--skip-no-markers' ]; then
need_replace="yes"
skip_no_markers="yes"
shift
fi

if [ "$1" = '--stdout' ]; then
to_stdout="yes"
shift
Expand All @@ -619,6 +636,10 @@ gh_toc_app() {
fi

if [ "$to_stdout" = "yes" ]; then
if [ "$skip_no_markers" = "yes" ]; then
echo "--skip-no-markers cannot be used with --stdout." >&2
exit 1
fi
if [ $# -ne 1 ] || [ "$(gh_is_url "$1")" == "yes" ]; then
echo "--stdout works with one local file or '-' (STDIN) only." >&2
exit 1
Expand All @@ -631,7 +652,7 @@ gh_toc_app() {
for md in "$@"
do
echo ""
gh_toc "$md" "$#" "$need_replace" "$no_backup" "$no_footer" "$indent" "$skip_header" "$depth" "$numbered" "$bullet"
gh_toc "$md" "$#" "$need_replace" "$no_backup" "$no_footer" "$indent" "$skip_header" "$depth" "$numbered" "$bullet" "$skip_no_markers"
done

echo ""
Expand Down
2 changes: 2 additions & 0 deletions openspec/changes/add-skip-no-markers/.openspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-11-07
185 changes: 185 additions & 0 deletions openspec/changes/add-skip-no-markers/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
# Design

## Context

See proposal.md - Why for the symptoms and specs/toc-skip-no-markers/spec.md for the
requirements.

`gh_toc_app` parses the options once each, in a fixed order: `--indent`, `--depth`,
`--numbered`, `--bullet`, `-`, `--insert`, `--no-backup`, `--hide-footer`, `--stdout`,
`--skip-header`, then the inputs. `--no-backup` and `--hide-footer` set
`need_replace="yes"`. With `--stdout`, `gh_toc_app` calls `gh_toc_stdout` and returns;
otherwise it calls `gh_toc` once per input, with `$#` as the number of inputs.

`gh_toc` takes 10 positional parameters, the last one is `bullet`. For a local file it
works in this order:

```
gh_toc <input> ...
|
+--> one input: print "Table of Contents", "=================", ""
|
+--> remote URL: gh_toc_load | gh_toc_grab -> stdout
| with --insert: "!! '<url>' is not a local file"
|
+--> local file: gh_toc_md2html -> GitHub API request
gh_toc_grab, echo "$toc" -> stdout
with --insert: marker check -> "You don't have ..." + exit 1
sed -i, backup and TOC files, "!! TOC was added"
```

The `exit 1` of the marker check ends the whole program, so the loop in `gh_toc_app` does
not reach the next inputs.

The marker lines are checked with
`grep -Eq '^<!--ts-->[[:space:]]*$'` and `grep -Eq '^<!--te-->[[:space:]]*$'` in three
places: `gh_toc_md2html` (`--skip-header`), `gh_toc` (`--insert`) and `gh_toc_stdout`
(`--stdout`), see `toc-markers`.

Constraints: BSD and GNU tools, the OS/390 branch, `make lint` (shellcheck).

## Goals / Non-Goals

**Goals:**

- Decide about a skipped file before anything is printed or sent for it.
- Keep the code paths without `--skip-no-markers` as they are.

**Non-Goals:**

- A shared function for the four marker checks: the three existing checks stay as they
are.

## Decisions

### The check at the top of `gh_toc`

With `--skip-no-markers`, `gh_toc` checks the markers right after the check for an empty
input and before the "Table of Contents" header, for a local input only
(`gh_is_url` is not `yes`) that is an existing file (`[ -f "$gh_src" ]`). When a marker
line is missing, it prints the warning to stderr and returns. The rest of `gh_toc` does
not change.

```
gh_toc <input> ...
|
+--> --skip-no-markers, local, existing file, no marker line:
| "<input>: Warning: no <!--ts--> or <!--te--> in the file, skipped" >&2
| return 0
|
+--> as before
```

So a skipped file gets no header, no GitHub API request, no backup and TOC files, and
the loop goes on to the next input. `return` gives 0, and the exit code of the program
stays the one of its last command.

Alternatives considered:

- The check in the local branch, before `gh_toc_md2html`: the header is already printed,
so one skipped file would leave "Table of Contents" with no entries on stdout.
- Filter the inputs in `gh_toc_app` before the loop: `$#` would drop, and with it the
"Table of Contents" header and the link prefix of the other inputs, e.g. a remote URL
given with one skipped file would lose its prefix. The spec keeps the other inputs as
before.
- Replace the `exit 1` of the existing check with a `return`: the TOC is already printed
and the API request is already sent by then.

### The same regexes, inline

The check uses the same two `grep -Eq` calls as `gh_toc_stdout`, so a file is skipped by
exactly the lines `--insert` looks for (`toc-markers`). It is written inline, like the
three existing checks.

Alternative: a function such as `gh_toc_has_markers` for all four checks. It would touch
the three existing checks, which this change does not need.

### Only an existing file is skipped

The `[ -f "$gh_src" ]` part keeps a missing path, a directory and `-` out of the skip:
they go the same way as before, and a missing path still exits with code 1. Without it,
`grep` would fail on a missing path, the file would be skipped with exit code 0, and a
typo in a CI command would pass unnoticed.

### Parsing and passing the option

`gh_toc_app` gets `local skip_no_markers=""` next to `local to_stdout=""`, and a check
right after `--hide-footer`:

```bash
if [ "$1" = '--skip-no-markers' ]; then
need_replace="yes"
skip_no_markers="yes"
shift
fi
```

`need_replace="yes"` is what `--no-backup` and `--hide-footer` do to turn on `--insert`.
The value goes to `gh_toc` as a new last (11th) parameter, read as
`local skip_no_markers=${11}`.

### The error with `--stdout`

The option is parsed before `--stdout`, so the error is checked in the `--stdout` branch
of `gh_toc_app`, before the existing check of the inputs: the message
`--skip-no-markers cannot be used with --stdout.` goes to stderr and the program exits
with 1. Stderr follows `toc-stdout` (Errors on stderr): with `--stdout`, every error goes
to stderr. The check comes before `gh_toc_stdout`, so there is no network call.

### The warning

`<input>: Warning: no <!--ts--> or <!--te--> in the file, skipped` follows the form of the
skipped level warning (#75), `<input>: Warning: ...`, and goes to stderr like it. Unlike
that warning, it has the input also with one input: the point of the warning is which
file was skipped. Stdout keeps only the TOC and the `!!` lines.

### Help, README

`show_help` gets
` --skip-no-markers Skip files without <!--ts--> or <!--te-->. Set --insert as well. Default: false.`
right after the `--hide-footer` line. The description starts at the same column as the
other options, and "Set --insert as well" is the wording of `--no-backup` and
`--hide-footer`.

README, section "Auto insert and update TOC": after the paragraph "Next time when your
file will be changed just repeat the command ...", a sentence about
`--skip-no-markers` and the example
`find . -name '*.md' -exec ./gh-md-toc --insert --no-backup --hide-footer --skip-no-markers {} +`.
No new heading, so the README TOC in `tests/tests.bats` does not change.

### Tests

The new tests write their files into `$BATS_TEST_TMPDIR` with `printf` and run
`run_to_files`, as the #113 and #119 tests do:

- "Several files" and "Several files without the option": `a.md`, `b.md`, `c.md` as in
the spec, the files compared with `cmp` and stdout with `grep -Fxc`. Two GitHub API
requests each.
- "One file with one marker": `GH_TOC_TOKEN=invalid`, the file in its own directory, so
`ls` shows that no backup or TOC files were created. With a request to the API, the
invalid token would give exit code 1, so the test also shows that there is no request.
No network.
- "Missing file", "With --stdout": no network.
- "Without --insert": one GitHub API request; the backup and TOC files stay in
`$BATS_TEST_TMPDIR`.
- `test_help`: the new line at index 15, `--stdout` and `--skip-header` at 16 and 17, 19
lines in total.

Before the implementation, `--skip-no-markers` is taken as an input, a missing file, so
the program exits with 1 on it. So "Several files", "One file with one marker",
"With --stdout", "Without --insert" and `test_help` fail before the implementation.
"Missing file" and "Several files without the option" pass before it: they pin down the
behavior that does not change.

## Risks / Trade-offs

- [A broken marker line, e.g. an indented one, makes a file skipped with exit code 0 in
CI] → The warning on stderr names the file. Without `--skip-no-markers`, the error and
exit code 1 stay.
- [`find` also finds files in `node_modules`, `vendor` and the like] → They usually have
no markers, so they are skipped without API requests. The README example is a starting
point; `find` options such as `-not -path` narrow it.
- [`--skip-no-markers` after `--stdout` or `--skip-header` is taken as an input] → The
same limitation applies to every option; `--help` lists the options in the parse order.
- [Three new tests call the GitHub API] → Same as the existing tests: `GH_TOC_TOKEN`
locally, `secrets.GITHUB_TOKEN` in CI.
84 changes: 84 additions & 0 deletions openspec/changes/add-skip-no-markers/proposal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Proposal

## Why

`--insert` with several local files stops at the first file without the `<!--ts-->` and
`<!--te-->` markers (#110, open since 2021). The files after it are not processed, even
when they have the markers, and the exit code is 1. So `--insert` cannot be run on all
markdown files of a directory, where most files have no TOC. Reproduced with the current
master:

- `a.md` and `c.md` have the markers, `b.md` does not.
`gh-md-toc --insert --no-backup --hide-footer a.md b.md c.md` inserts the TOC into
`a.md`, prints `You don't have <!--ts--> or <!--te--> in your file...exiting` for
`b.md`, exits with 1, and does not change `c.md`.
- The markers are checked after the file is rendered by the GitHub API, so a request is
spent on `b.md`. Without a token, the limit is 60 requests per hour.

The reporter of #110 works around it in CI with `grep -z` to find the files with both
markers, and `xargs -L 1` to run `gh-md-toc` once per file. PRs #115 and #117 proposed
`--all` to find all markdown files, and `--auto` to add the markers to a file without
them; both were closed without a merge.

## What Changes

- New option `--skip-no-markers`. With it, a local file without a line that counts as
`<!--ts-->` or without a line that counts as `<!--te-->` (see `toc-markers`) is
skipped: it is not changed, no request is sent to the GitHub API for it, and a warning
`<input>: Warning: no <!--ts--> or <!--te--> in the file, skipped` is printed to
stderr. The other inputs are processed as before.
- When every file is skipped, the exit code is 0.
- `--skip-no-markers` turns on `--insert`, as `--no-backup` and `--hide-footer` do. It
is parsed right after `--hide-footer` and before `--stdout`.
- `--skip-no-markers` with `--stdout` is an error: the message
`--skip-no-markers cannot be used with --stdout.` on stderr, exit code 1, before any
network call.
- A remote URL is not affected: its TOC is printed, followed by `!! '<url>' is not a
local file`, as before.
- `--help`: a new line for `--skip-no-markers` right after `--hide-footer`.
- README: a sentence and a `find` example in the "Auto insert and update TOC" section,
without a new heading:
`find . -name '*.md' -exec ./gh-md-toc --insert --no-backup --hide-footer --skip-no-markers {} +`.
This covers the first request of #110, the recursive folders.
- Tests for several files, one file without markers, `--skip-no-markers` alone, the error
with `--stdout`, and an updated `test_help`.

Without `--skip-no-markers`, the output, the exit codes and the stop at the first file
without markers do not change.

Non-goals:

- A directory walk in `gh-md-toc` itself (`--all`, `--recursive`): `find` does it, and
hidden folders, `node_modules` and symlinks would need their own rules.
- Adding the markers to a file without them (`--auto`, `--head` of #117).
- Skipping files without markers by default, or only when several inputs are given.
- Changes to the landing page (`site/`) or `openspec/specs/landing`.
- A version bump.

## Capabilities

### New Capabilities

- `toc-skip-no-markers`: the `--skip-no-markers` option: which files are skipped, the
warning, the exit code, and its use with other options.

### Modified Capabilities

- `toc-stdout`: "Help lists the option" states that `--stdout` follows `--hide-footer` in
`--help`; it follows `--skip-no-markers` now.

## Impact

- `gh-md-toc`: `gh_toc_app` (parse `--skip-no-markers`, the error with `--stdout`, pass
it on), `gh_toc` (a new parameter and the check before the GitHub API call),
`show_help`.
- `tests/tests.bats`: new tests on files in `$BATS_TEST_TMPDIR`, and an updated
`test_help` (19 lines instead of 18). No new fixtures.
- `README.md`: a sentence and an example only, no new heading, so the README TOC asserted
in `tests/tests.bats` does not change.
- `toc-markers`: not changed. A file is skipped by the same marker lines that `--insert`
looks for.
- `toc-multiple-inputs`: not changed. The links of a local file with `--insert` and
several inputs are already `#<anchor>` only (#192), so the TOC written by the `find`
example works in every file.
- Closes #110.
Loading
Loading