Skip to content

Commit 34828c5

Browse files
authored
feat(toc): add --skip-no-markers to skip files without markers (#199)
* docs(openspec): propose add-skip-no-markers change * chore(tests): add tests for --skip-no-markers * feat(toc): add --skip-no-markers to skip files without markers * docs(readme): add the --skip-no-markers example with find * chore(openspec): mark add-skip-no-markers tasks done
1 parent f757084 commit 34828c5

9 files changed

Lines changed: 562 additions & 4 deletions

File tree

‎README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,14 @@ Now check the same file:
372372
Next time when your file will be changed just repeat the command (`./gh-md-toc
373373
--insert ...`) and TOC will be refreshed again.
374374
375+
To refresh the TOC in all markdown files of a directory, add `--skip-no-markers`:
376+
the files without `<!--ts-->` or `<!--te-->` are skipped with a warning on stderr,
377+
instead of an error:
378+
379+
```bash
380+
$ find . -name '*.md' -exec ./gh-md-toc --insert --no-backup --hide-footer --skip-no-markers {} +
381+
```
382+
375383
To get the whole file with the new TOC on stdout instead, use `--stdout`. The
376384
file is not changed and no backup files are created. It takes one local file
377385
or `-` for STDIN, and works with `--hide-footer` and `--skip-header`:

‎gh-md-toc‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,21 @@ gh_toc(){
198198
local depth=$8
199199
local numbered=$9
200200
local bullet=${10}
201+
local skip_no_markers=${11}
201202

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

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

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

528539
if [ "$1" = '--help' ] || [ $# -eq 0 ] ; then
529540
show_help
@@ -608,6 +619,12 @@ gh_toc_app() {
608619
shift
609620
fi
610621

622+
if [ "$1" = '--skip-no-markers' ]; then
623+
need_replace="yes"
624+
skip_no_markers="yes"
625+
shift
626+
fi
627+
611628
if [ "$1" = '--stdout' ]; then
612629
to_stdout="yes"
613630
shift
@@ -619,6 +636,10 @@ gh_toc_app() {
619636
fi
620637

621638
if [ "$to_stdout" = "yes" ]; then
639+
if [ "$skip_no_markers" = "yes" ]; then
640+
echo "--skip-no-markers cannot be used with --stdout." >&2
641+
exit 1
642+
fi
622643
if [ $# -ne 1 ] || [ "$(gh_is_url "$1")" == "yes" ]; then
623644
echo "--stdout works with one local file or '-' (STDIN) only." >&2
624645
exit 1
@@ -631,7 +652,7 @@ gh_toc_app() {
631652
for md in "$@"
632653
do
633654
echo ""
634-
gh_toc "$md" "$#" "$need_replace" "$no_backup" "$no_footer" "$indent" "$skip_header" "$depth" "$numbered" "$bullet"
655+
gh_toc "$md" "$#" "$need_replace" "$no_backup" "$no_footer" "$indent" "$skip_header" "$depth" "$numbered" "$bullet" "$skip_no_markers"
635656
done
636657

637658
echo ""
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
schema: spec-driven
2+
created: 2026-11-07
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# Design
2+
3+
## Context
4+
5+
See proposal.md - Why for the symptoms and specs/toc-skip-no-markers/spec.md for the
6+
requirements.
7+
8+
`gh_toc_app` parses the options once each, in a fixed order: `--indent`, `--depth`,
9+
`--numbered`, `--bullet`, `-`, `--insert`, `--no-backup`, `--hide-footer`, `--stdout`,
10+
`--skip-header`, then the inputs. `--no-backup` and `--hide-footer` set
11+
`need_replace="yes"`. With `--stdout`, `gh_toc_app` calls `gh_toc_stdout` and returns;
12+
otherwise it calls `gh_toc` once per input, with `$#` as the number of inputs.
13+
14+
`gh_toc` takes 10 positional parameters, the last one is `bullet`. For a local file it
15+
works in this order:
16+
17+
```
18+
gh_toc <input> ...
19+
|
20+
+--> one input: print "Table of Contents", "=================", ""
21+
|
22+
+--> remote URL: gh_toc_load | gh_toc_grab -> stdout
23+
| with --insert: "!! '<url>' is not a local file"
24+
|
25+
+--> local file: gh_toc_md2html -> GitHub API request
26+
gh_toc_grab, echo "$toc" -> stdout
27+
with --insert: marker check -> "You don't have ..." + exit 1
28+
sed -i, backup and TOC files, "!! TOC was added"
29+
```
30+
31+
The `exit 1` of the marker check ends the whole program, so the loop in `gh_toc_app` does
32+
not reach the next inputs.
33+
34+
The marker lines are checked with
35+
`grep -Eq '^<!--ts-->[[:space:]]*$'` and `grep -Eq '^<!--te-->[[:space:]]*$'` in three
36+
places: `gh_toc_md2html` (`--skip-header`), `gh_toc` (`--insert`) and `gh_toc_stdout`
37+
(`--stdout`), see `toc-markers`.
38+
39+
Constraints: BSD and GNU tools, the OS/390 branch, `make lint` (shellcheck).
40+
41+
## Goals / Non-Goals
42+
43+
**Goals:**
44+
45+
- Decide about a skipped file before anything is printed or sent for it.
46+
- Keep the code paths without `--skip-no-markers` as they are.
47+
48+
**Non-Goals:**
49+
50+
- A shared function for the four marker checks: the three existing checks stay as they
51+
are.
52+
53+
## Decisions
54+
55+
### The check at the top of `gh_toc`
56+
57+
With `--skip-no-markers`, `gh_toc` checks the markers right after the check for an empty
58+
input and before the "Table of Contents" header, for a local input only
59+
(`gh_is_url` is not `yes`) that is an existing file (`[ -f "$gh_src" ]`). When a marker
60+
line is missing, it prints the warning to stderr and returns. The rest of `gh_toc` does
61+
not change.
62+
63+
```
64+
gh_toc <input> ...
65+
|
66+
+--> --skip-no-markers, local, existing file, no marker line:
67+
| "<input>: Warning: no <!--ts--> or <!--te--> in the file, skipped" >&2
68+
| return 0
69+
|
70+
+--> as before
71+
```
72+
73+
So a skipped file gets no header, no GitHub API request, no backup and TOC files, and
74+
the loop goes on to the next input. `return` gives 0, and the exit code of the program
75+
stays the one of its last command.
76+
77+
Alternatives considered:
78+
79+
- The check in the local branch, before `gh_toc_md2html`: the header is already printed,
80+
so one skipped file would leave "Table of Contents" with no entries on stdout.
81+
- Filter the inputs in `gh_toc_app` before the loop: `$#` would drop, and with it the
82+
"Table of Contents" header and the link prefix of the other inputs, e.g. a remote URL
83+
given with one skipped file would lose its prefix. The spec keeps the other inputs as
84+
before.
85+
- Replace the `exit 1` of the existing check with a `return`: the TOC is already printed
86+
and the API request is already sent by then.
87+
88+
### The same regexes, inline
89+
90+
The check uses the same two `grep -Eq` calls as `gh_toc_stdout`, so a file is skipped by
91+
exactly the lines `--insert` looks for (`toc-markers`). It is written inline, like the
92+
three existing checks.
93+
94+
Alternative: a function such as `gh_toc_has_markers` for all four checks. It would touch
95+
the three existing checks, which this change does not need.
96+
97+
### Only an existing file is skipped
98+
99+
The `[ -f "$gh_src" ]` part keeps a missing path, a directory and `-` out of the skip:
100+
they go the same way as before, and a missing path still exits with code 1. Without it,
101+
`grep` would fail on a missing path, the file would be skipped with exit code 0, and a
102+
typo in a CI command would pass unnoticed.
103+
104+
### Parsing and passing the option
105+
106+
`gh_toc_app` gets `local skip_no_markers=""` next to `local to_stdout=""`, and a check
107+
right after `--hide-footer`:
108+
109+
```bash
110+
if [ "$1" = '--skip-no-markers' ]; then
111+
need_replace="yes"
112+
skip_no_markers="yes"
113+
shift
114+
fi
115+
```
116+
117+
`need_replace="yes"` is what `--no-backup` and `--hide-footer` do to turn on `--insert`.
118+
The value goes to `gh_toc` as a new last (11th) parameter, read as
119+
`local skip_no_markers=${11}`.
120+
121+
### The error with `--stdout`
122+
123+
The option is parsed before `--stdout`, so the error is checked in the `--stdout` branch
124+
of `gh_toc_app`, before the existing check of the inputs: the message
125+
`--skip-no-markers cannot be used with --stdout.` goes to stderr and the program exits
126+
with 1. Stderr follows `toc-stdout` (Errors on stderr): with `--stdout`, every error goes
127+
to stderr. The check comes before `gh_toc_stdout`, so there is no network call.
128+
129+
### The warning
130+
131+
`<input>: Warning: no <!--ts--> or <!--te--> in the file, skipped` follows the form of the
132+
skipped level warning (#75), `<input>: Warning: ...`, and goes to stderr like it. Unlike
133+
that warning, it has the input also with one input: the point of the warning is which
134+
file was skipped. Stdout keeps only the TOC and the `!!` lines.
135+
136+
### Help, README
137+
138+
`show_help` gets
139+
` --skip-no-markers Skip files without <!--ts--> or <!--te-->. Set --insert as well. Default: false.`
140+
right after the `--hide-footer` line. The description starts at the same column as the
141+
other options, and "Set --insert as well" is the wording of `--no-backup` and
142+
`--hide-footer`.
143+
144+
README, section "Auto insert and update TOC": after the paragraph "Next time when your
145+
file will be changed just repeat the command ...", a sentence about
146+
`--skip-no-markers` and the example
147+
`find . -name '*.md' -exec ./gh-md-toc --insert --no-backup --hide-footer --skip-no-markers {} +`.
148+
No new heading, so the README TOC in `tests/tests.bats` does not change.
149+
150+
### Tests
151+
152+
The new tests write their files into `$BATS_TEST_TMPDIR` with `printf` and run
153+
`run_to_files`, as the #113 and #119 tests do:
154+
155+
- "Several files" and "Several files without the option": `a.md`, `b.md`, `c.md` as in
156+
the spec, the files compared with `cmp` and stdout with `grep -Fxc`. Two GitHub API
157+
requests each.
158+
- "One file with one marker": `GH_TOC_TOKEN=invalid`, the file in its own directory, so
159+
`ls` shows that no backup or TOC files were created. With a request to the API, the
160+
invalid token would give exit code 1, so the test also shows that there is no request.
161+
No network.
162+
- "Missing file", "With --stdout": no network.
163+
- "Without --insert": one GitHub API request; the backup and TOC files stay in
164+
`$BATS_TEST_TMPDIR`.
165+
- `test_help`: the new line at index 15, `--stdout` and `--skip-header` at 16 and 17, 19
166+
lines in total.
167+
168+
Before the implementation, `--skip-no-markers` is taken as an input, a missing file, so
169+
the program exits with 1 on it. So "Several files", "One file with one marker",
170+
"With --stdout", "Without --insert" and `test_help` fail before the implementation.
171+
"Missing file" and "Several files without the option" pass before it: they pin down the
172+
behavior that does not change.
173+
174+
## Risks / Trade-offs
175+
176+
- [A broken marker line, e.g. an indented one, makes a file skipped with exit code 0 in
177+
CI] → The warning on stderr names the file. Without `--skip-no-markers`, the error and
178+
exit code 1 stay.
179+
- [`find` also finds files in `node_modules`, `vendor` and the like] → They usually have
180+
no markers, so they are skipped without API requests. The README example is a starting
181+
point; `find` options such as `-not -path` narrow it.
182+
- [`--skip-no-markers` after `--stdout` or `--skip-header` is taken as an input] → The
183+
same limitation applies to every option; `--help` lists the options in the parse order.
184+
- [Three new tests call the GitHub API] → Same as the existing tests: `GH_TOC_TOKEN`
185+
locally, `secrets.GITHUB_TOKEN` in CI.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Proposal
2+
3+
## Why
4+
5+
`--insert` with several local files stops at the first file without the `<!--ts-->` and
6+
`<!--te-->` markers (#110, open since 2021). The files after it are not processed, even
7+
when they have the markers, and the exit code is 1. So `--insert` cannot be run on all
8+
markdown files of a directory, where most files have no TOC. Reproduced with the current
9+
master:
10+
11+
- `a.md` and `c.md` have the markers, `b.md` does not.
12+
`gh-md-toc --insert --no-backup --hide-footer a.md b.md c.md` inserts the TOC into
13+
`a.md`, prints `You don't have <!--ts--> or <!--te--> in your file...exiting` for
14+
`b.md`, exits with 1, and does not change `c.md`.
15+
- The markers are checked after the file is rendered by the GitHub API, so a request is
16+
spent on `b.md`. Without a token, the limit is 60 requests per hour.
17+
18+
The reporter of #110 works around it in CI with `grep -z` to find the files with both
19+
markers, and `xargs -L 1` to run `gh-md-toc` once per file. PRs #115 and #117 proposed
20+
`--all` to find all markdown files, and `--auto` to add the markers to a file without
21+
them; both were closed without a merge.
22+
23+
## What Changes
24+
25+
- New option `--skip-no-markers`. With it, a local file without a line that counts as
26+
`<!--ts-->` or without a line that counts as `<!--te-->` (see `toc-markers`) is
27+
skipped: it is not changed, no request is sent to the GitHub API for it, and a warning
28+
`<input>: Warning: no <!--ts--> or <!--te--> in the file, skipped` is printed to
29+
stderr. The other inputs are processed as before.
30+
- When every file is skipped, the exit code is 0.
31+
- `--skip-no-markers` turns on `--insert`, as `--no-backup` and `--hide-footer` do. It
32+
is parsed right after `--hide-footer` and before `--stdout`.
33+
- `--skip-no-markers` with `--stdout` is an error: the message
34+
`--skip-no-markers cannot be used with --stdout.` on stderr, exit code 1, before any
35+
network call.
36+
- A remote URL is not affected: its TOC is printed, followed by `!! '<url>' is not a
37+
local file`, as before.
38+
- `--help`: a new line for `--skip-no-markers` right after `--hide-footer`.
39+
- README: a sentence and a `find` example in the "Auto insert and update TOC" section,
40+
without a new heading:
41+
`find . -name '*.md' -exec ./gh-md-toc --insert --no-backup --hide-footer --skip-no-markers {} +`.
42+
This covers the first request of #110, the recursive folders.
43+
- Tests for several files, one file without markers, `--skip-no-markers` alone, the error
44+
with `--stdout`, and an updated `test_help`.
45+
46+
Without `--skip-no-markers`, the output, the exit codes and the stop at the first file
47+
without markers do not change.
48+
49+
Non-goals:
50+
51+
- A directory walk in `gh-md-toc` itself (`--all`, `--recursive`): `find` does it, and
52+
hidden folders, `node_modules` and symlinks would need their own rules.
53+
- Adding the markers to a file without them (`--auto`, `--head` of #117).
54+
- Skipping files without markers by default, or only when several inputs are given.
55+
- Changes to the landing page (`site/`) or `openspec/specs/landing`.
56+
- A version bump.
57+
58+
## Capabilities
59+
60+
### New Capabilities
61+
62+
- `toc-skip-no-markers`: the `--skip-no-markers` option: which files are skipped, the
63+
warning, the exit code, and its use with other options.
64+
65+
### Modified Capabilities
66+
67+
- `toc-stdout`: "Help lists the option" states that `--stdout` follows `--hide-footer` in
68+
`--help`; it follows `--skip-no-markers` now.
69+
70+
## Impact
71+
72+
- `gh-md-toc`: `gh_toc_app` (parse `--skip-no-markers`, the error with `--stdout`, pass
73+
it on), `gh_toc` (a new parameter and the check before the GitHub API call),
74+
`show_help`.
75+
- `tests/tests.bats`: new tests on files in `$BATS_TEST_TMPDIR`, and an updated
76+
`test_help` (19 lines instead of 18). No new fixtures.
77+
- `README.md`: a sentence and an example only, no new heading, so the README TOC asserted
78+
in `tests/tests.bats` does not change.
79+
- `toc-markers`: not changed. A file is skipped by the same marker lines that `--insert`
80+
looks for.
81+
- `toc-multiple-inputs`: not changed. The links of a local file with `--insert` and
82+
several inputs are already `#<anchor>` only (#192), so the TOC written by the `find`
83+
example works in every file.
84+
- Closes #110.

0 commit comments

Comments
 (0)