From f4d22d3c76d4d12f6294e9b47e4b959de48dd919 Mon Sep 17 00:00:00 2001 From: Eugene Kalinin Date: Sat, 7 Nov 2026 23:44:14 +0300 Subject: [PATCH 1/5] docs(openspec): propose add-skip-no-markers change --- .../add-skip-no-markers/.openspec.yaml | 2 + .../changes/add-skip-no-markers/design.md | 185 ++++++++++++++++++ .../changes/add-skip-no-markers/proposal.md | 84 ++++++++ .../specs/toc-skip-no-markers/spec.md | 112 +++++++++++ .../specs/toc-stdout/spec.md | 15 ++ openspec/changes/add-skip-no-markers/tasks.md | 65 ++++++ 6 files changed, 463 insertions(+) create mode 100644 openspec/changes/add-skip-no-markers/.openspec.yaml create mode 100644 openspec/changes/add-skip-no-markers/design.md create mode 100644 openspec/changes/add-skip-no-markers/proposal.md create mode 100644 openspec/changes/add-skip-no-markers/specs/toc-skip-no-markers/spec.md create mode 100644 openspec/changes/add-skip-no-markers/specs/toc-stdout/spec.md create mode 100644 openspec/changes/add-skip-no-markers/tasks.md diff --git a/openspec/changes/add-skip-no-markers/.openspec.yaml b/openspec/changes/add-skip-no-markers/.openspec.yaml new file mode 100644 index 0000000..e582ee4 --- /dev/null +++ b/openspec/changes/add-skip-no-markers/.openspec.yaml @@ -0,0 +1,2 @@ +schema: spec-driven +created: 2026-11-07 diff --git a/openspec/changes/add-skip-no-markers/design.md b/openspec/changes/add-skip-no-markers/design.md new file mode 100644 index 0000000..9e582ee --- /dev/null +++ b/openspec/changes/add-skip-no-markers/design.md @@ -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 ... + | + +--> one input: print "Table of Contents", "=================", "" + | + +--> remote URL: gh_toc_load | gh_toc_grab -> stdout + | with --insert: "!! '' 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 '^[[:space:]]*$'` and `grep -Eq '^[[: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 ... + | + +--> --skip-no-markers, local, existing file, no marker line: + | ": Warning: no or 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 + +`: Warning: no or in the file, skipped` follows the form of the +skipped level warning (#75), `: 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 or . 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. diff --git a/openspec/changes/add-skip-no-markers/proposal.md b/openspec/changes/add-skip-no-markers/proposal.md new file mode 100644 index 0000000..1ac3ec1 --- /dev/null +++ b/openspec/changes/add-skip-no-markers/proposal.md @@ -0,0 +1,84 @@ +# Proposal + +## Why + +`--insert` with several local files stops at the first file without the `` and +`` 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 or 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 + `` or without a line that counts as `` (see `toc-markers`) is + skipped: it is not changed, no request is sent to the GitHub API for it, and a warning + `: Warning: no or 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 `!! '' 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 `#` only (#192), so the TOC written by the `find` + example works in every file. +- Closes #110. diff --git a/openspec/changes/add-skip-no-markers/specs/toc-skip-no-markers/spec.md b/openspec/changes/add-skip-no-markers/specs/toc-skip-no-markers/spec.md new file mode 100644 index 0000000..b133928 --- /dev/null +++ b/openspec/changes/add-skip-no-markers/specs/toc-skip-no-markers/spec.md @@ -0,0 +1,112 @@ +# Spec Delta + +## Purpose + +Lets `--insert` run on many local files, such as all markdown files found by `find`, by +skipping the files without the `` and `` markers instead of stopping +with an error. + +## ADDED Requirements + +### Requirement: Skip a file without markers + +With `--skip-no-markers`, a local file SHALL be skipped when it has no line that counts as +the `` marker or no line that counts as the `` marker, as defined in +`toc-markers`. A skipped file SHALL NOT be changed, no backup or TOC files SHALL be +created for it, no request SHALL be sent to the GitHub API for it, and stdout SHALL have +no TOC entries of it. For a skipped file, the program SHALL print +`: Warning: no or in the file, skipped` to stderr, where +`` is the input as given on the command line, also with one input. The other +inputs SHALL be processed as before this change. + +The exit code SHALL be 0 when every local file is skipped. An error of another input, +such as a GitHub API error, SHALL exit with code 1 as before this change. A local input +that is not an existing file, such as a missing path, SHALL NOT be skipped and SHALL give +the same output and exit code as before this change. + +#### Scenario: Several files + +- **WHEN** the user runs + `gh-md-toc --insert --no-backup --hide-footer --skip-no-markers /a.md /b.md /c.md`, + where `a.md` is `\n\n\n# Page a\n`, `b.md` is `# Page b\n`, and + `c.md` is `\n\n\n# Page c\n` +- **THEN** `a.md` is `\n* [Page a](#page-a)\n\n\n# Page a\n`, `c.md` + is `\n* [Page c](#page-c)\n\n\n# Page c\n`, `b.md` does not change, + stdout has the lines `* [Page a](#page-a)` and `* [Page c](#page-c)` and no line + `* [Page b](#page-b)`, stderr is + `/b.md: Warning: no or in the file, skipped`, and the exit + code is 0 + +#### Scenario: One file with one marker + +- **WHEN** the user runs `GH_TOC_TOKEN=invalid gh-md-toc --insert --skip-no-markers /b.md`, + where `` has only `b.md`, and `b.md` is `\n\n# Page b\n` +- **THEN** `b.md` does not change, `` has only `b.md`, stdout has no line + `Parsing local markdown file requires access to github API`, stderr is + `/b.md: Warning: no or in the file, skipped`, and the exit + code is 0 + +#### Scenario: Missing file + +- **WHEN** the user runs `gh-md-toc --skip-no-markers /missing.md`, where + `missing.md` does not exist +- **THEN** stderr has no line that ends with `in the file, skipped`, and the exit code + is 1 + +### Requirement: Turns on --insert + +`--skip-no-markers` SHALL turn on `--insert`, as `--no-backup` and `--hide-footer` do. A +local file with the markers and a remote URL SHALL get the same output and the same +changes as with `--insert` without `--skip-no-markers`. + +`--skip-no-markers` SHALL be accepted after `--indent `, `--depth `, +`--numbered `, `--bullet `, `--insert`, `--no-backup` and `--hide-footer` +(when given) and before `--stdout`, `--skip-header` and the inputs. + +#### Scenario: Without --insert + +- **WHEN** the user runs `gh-md-toc --skip-no-markers /a.md`, where `a.md` is + `\n\n\n# Page a\n` +- **THEN** line 2 of `a.md` is `* [Page a](#page-a)`, stdout has the line + `!! TOC was added into: '/a.md'`, and the exit code is 0 + +### Requirement: Without the option + +Without `--skip-no-markers`, `--insert` on a local file without the markers SHALL print +`You don't have or in your file...exiting` to stdout and exit with +code 1, and the inputs after that file SHALL NOT be processed, as before this change. + +#### Scenario: Several files without the option + +- **WHEN** the user runs + `gh-md-toc --insert --no-backup --hide-footer /a.md /b.md /c.md`, with + the files of the scenario "Several files" +- **THEN** line 2 of `a.md` is `* [Page a](#page-a)`, stdout has the line + `You don't have or in your file...exiting`, `b.md` and `c.md` do + not change, and the exit code is 1 + +### Requirement: Invalid usage with --stdout + +With `--skip-no-markers` and `--stdout`, the program SHALL print +`--skip-no-markers cannot be used with --stdout.` to stderr, print nothing to stdout, and +exit with code 1 before any network call. + +#### Scenario: With --stdout + +- **WHEN** the user runs + `gh-md-toc --skip-no-markers --stdout "tests/test directory/test_stdout.md"` +- **THEN** stderr is `--skip-no-markers cannot be used with --stdout.`, stdout is empty, + and the exit code is 1 + +### Requirement: Help lists the option + +`--help` SHALL list `--skip-no-markers` in the "Options" section right after +`--hide-footer`. + +#### Scenario: Help output + +- **WHEN** the user runs `gh-md-toc --help` +- **THEN** the line after + ` --hide-footer Do not write date & author of the last TOC update. Set --insert as well. Default: false.` + is + ` --skip-no-markers Skip files without or . Set --insert as well. Default: false.` diff --git a/openspec/changes/add-skip-no-markers/specs/toc-stdout/spec.md b/openspec/changes/add-skip-no-markers/specs/toc-stdout/spec.md new file mode 100644 index 0000000..3bd362f --- /dev/null +++ b/openspec/changes/add-skip-no-markers/specs/toc-stdout/spec.md @@ -0,0 +1,15 @@ +# Spec Delta + +## MODIFIED Requirements + +### Requirement: Help lists the option + +`--help` SHALL list `--stdout` in the "Options" section right after `--skip-no-markers`. + +#### Scenario: Help output + +- **WHEN** the user runs `gh-md-toc --help` +- **THEN** the line after + ` --skip-no-markers Skip files without or . Set --insert as well. Default: false.` + is + ` --stdout Print the whole file with the new TOC to stdout. The file is not changed. Default: false.` diff --git a/openspec/changes/add-skip-no-markers/tasks.md b/openspec/changes/add-skip-no-markers/tasks.md new file mode 100644 index 0000000..6a2d175 --- /dev/null +++ b/openspec/changes/add-skip-no-markers/tasks.md @@ -0,0 +1,65 @@ +# Tasks + +## 1. Tests + +- [ ] 1.1 Add the test "Insert into several files skips a file without markers, #110" in + `tests/tests.bats`, as in the spec scenario "Skip a file without markers - Several + files": `a.md`, `b.md`, `c.md` in `$BATS_TEST_TMPDIR`, `run_to_files`, the files + compared with `cmp`, stdout with `grep -Fxc`, `err.txt` with `assert_equal`; verify it + fails before the implementation (`bats --filter "" tests`) +- [ ] 1.2 Add the test "Skip a file with one marker without a GitHub API request, #110", + as in the spec scenario "One file with one marker": `b.md` in its own directory under + `$BATS_TEST_TMPDIR`, `GH_TOC_TOKEN=invalid`, `ls` of the directory is `b.md`, no + `Parsing local markdown file requires access to github API` in `out.md`; verify it + fails before the implementation +- [ ] 1.3 Add the test "No skip for a missing file, #110", as in the spec scenario + "Missing file": `assert_fail` and no `in the file, skipped` in `err.txt`; verify it + passes before the implementation +- [ ] 1.4 Add the test "--skip-no-markers turns on --insert, #110", as in the spec + scenario "Without --insert": line 2 of `a.md` and the `!! TOC was added into` line in + `out.md`; verify it fails before the implementation +- [ ] 1.5 Add the test "Insert into several files stops at a file without markers, #110", + as in the spec scenario "Several files without the option": line 2 of `a.md`, the + `You don't have ...` line in `out.md`, `b.md` and `c.md` compared with `cmp`, + `assert_fail`; verify it passes before the implementation +- [ ] 1.6 Add the test "Error for --skip-no-markers with --stdout, #110", as in the spec + scenario "With --stdout": `assert_fail`, `err.txt` is the message, `out.md` is empty; + verify it fails before the implementation +- [ ] 1.7 Update `test_help`: the `--skip-no-markers` line at index 15, the `--stdout` + and `--skip-header` lines at 16 and 17, and 19 lines in total; verify `--help` and + `no arguments` fail before the implementation + +## 2. Implementation + +- [ ] 2.1 `gh_toc_app`: add `local skip_no_markers=""` next to `local to_stdout=""`, the + `--skip-no-markers` check right after `--hide-footer` (sets `need_replace="yes"` and + `skip_no_markers="yes"`), and pass `"$skip_no_markers"` to `gh_toc` as the 11th + parameter. `gh_toc`: take `local skip_no_markers=${11}`, and right after the check for + an empty input add the check from design.md (local input, `[ -f "$gh_src" ]`, the two + `grep -Eq` of `gh_toc_stdout`, the warning to stderr, `return`); verify `make lint` + passes, the tests from 1.1, 1.2 and 1.4 pass, and the tests from 1.3 and 1.5 still pass +- [ ] 2.2 `gh_toc_app`, the `--stdout` branch: before the check of the inputs, print + `--skip-no-markers cannot be used with --stdout.` to stderr and `exit 1` when + `skip_no_markers` is `yes`; verify the test from 1.6 passes +- [ ] 2.3 `show_help`: add + ` --skip-no-markers Skip files without or . Set --insert as well. Default: false.` + right after the `--hide-footer` line; verify the tests from 1.7 pass +- [ ] 2.4 Check the cases not covered by the tests: in a temporary directory with + `a.md` and `sub/c.md` (with markers) and `b.md` (without), run the README example + `find . -name '*.md' -exec /gh-md-toc --insert --no-backup --hide-footer --skip-no-markers {} +`; + verify `a.md` and `sub/c.md` get their TOC with `#` links, `b.md` does not + change, stderr is the warning for `./b.md`, and the exit code is 0. Run + `./gh-md-toc --skip-no-markers https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md`; + verify the TOC and `!! '' is not a local file` are printed and the exit code is 0 + +## 3. Documentation + +- [ ] 3.1 README, section "Auto insert and update TOC": after the paragraph "Next time + when your file will be changed just repeat the command ...", add a sentence about + `--skip-no-markers` and the `find` example from design.md; add no new heading; verify + the README TOC tests (`TOC for local README.md`, `TOC for local README.md with skip + headers`, `TOC for markdown from stdin`) still pass + +## 4. Verification + +- [ ] 4.1 Run `make lint` and `make test` with `GH_TOC_TOKEN` set; verify both pass From 807b3f5cbf94a0c3a2bee6246e392a53b3ccbc0e Mon Sep 17 00:00:00 2001 From: Eugene Kalinin Date: Sat, 7 Nov 2026 23:46:42 +0300 Subject: [PATCH 2/5] chore(tests): add tests for --skip-no-markers --- openspec/changes/add-skip-no-markers/tasks.md | 14 ++-- tests/tests.bats | 72 ++++++++++++++++++- 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/openspec/changes/add-skip-no-markers/tasks.md b/openspec/changes/add-skip-no-markers/tasks.md index 6a2d175..24c031a 100644 --- a/openspec/changes/add-skip-no-markers/tasks.md +++ b/openspec/changes/add-skip-no-markers/tasks.md @@ -2,30 +2,30 @@ ## 1. Tests -- [ ] 1.1 Add the test "Insert into several files skips a file without markers, #110" in +- [x] 1.1 Add the test "Insert into several files skips a file without markers, #110" in `tests/tests.bats`, as in the spec scenario "Skip a file without markers - Several files": `a.md`, `b.md`, `c.md` in `$BATS_TEST_TMPDIR`, `run_to_files`, the files compared with `cmp`, stdout with `grep -Fxc`, `err.txt` with `assert_equal`; verify it fails before the implementation (`bats --filter "" tests`) -- [ ] 1.2 Add the test "Skip a file with one marker without a GitHub API request, #110", +- [x] 1.2 Add the test "Skip a file with one marker without a GitHub API request, #110", as in the spec scenario "One file with one marker": `b.md` in its own directory under `$BATS_TEST_TMPDIR`, `GH_TOC_TOKEN=invalid`, `ls` of the directory is `b.md`, no `Parsing local markdown file requires access to github API` in `out.md`; verify it fails before the implementation -- [ ] 1.3 Add the test "No skip for a missing file, #110", as in the spec scenario +- [x] 1.3 Add the test "No skip for a missing file, #110", as in the spec scenario "Missing file": `assert_fail` and no `in the file, skipped` in `err.txt`; verify it passes before the implementation -- [ ] 1.4 Add the test "--skip-no-markers turns on --insert, #110", as in the spec +- [x] 1.4 Add the test "--skip-no-markers turns on --insert, #110", as in the spec scenario "Without --insert": line 2 of `a.md` and the `!! TOC was added into` line in `out.md`; verify it fails before the implementation -- [ ] 1.5 Add the test "Insert into several files stops at a file without markers, #110", +- [x] 1.5 Add the test "Insert into several files stops at a file without markers, #110", as in the spec scenario "Several files without the option": line 2 of `a.md`, the `You don't have ...` line in `out.md`, `b.md` and `c.md` compared with `cmp`, `assert_fail`; verify it passes before the implementation -- [ ] 1.6 Add the test "Error for --skip-no-markers with --stdout, #110", as in the spec +- [x] 1.6 Add the test "Error for --skip-no-markers with --stdout, #110", as in the spec scenario "With --stdout": `assert_fail`, `err.txt` is the message, `out.md` is empty; verify it fails before the implementation -- [ ] 1.7 Update `test_help`: the `--skip-no-markers` line at index 15, the `--stdout` +- [x] 1.7 Update `test_help`: the `--skip-no-markers` line at index 15, the `--stdout` and `--skip-header` lines at 16 and 17, and 19 lines in total; verify `--help` and `no arguments` fail before the implementation diff --git a/tests/tests.bats b/tests/tests.bats index dee70aa..7b712a0 100755 --- a/tests/tests.bats +++ b/tests/tests.bats @@ -132,9 +132,10 @@ test_help() { assert_equal "${lines[11]}" " --insert Insert new TOC into original file. For local files only. Default: false." assert_equal "${lines[13]}" " --no-backup Remove backup file. Set --insert as well. Default: false." assert_equal "${lines[14]}" " --hide-footer Do not write date & author of the last TOC update. Set --insert as well. Default: false." - assert_equal "${lines[15]}" " --stdout Print the whole file with the new TOC to stdout. The file is not changed. Default: false." - assert_equal "${lines[16]}" " --skip-header Hide entry of the topmost headlines. Default: false." - assert_equal "${#lines[@]}" "18" + assert_equal "${lines[15]}" " --skip-no-markers Skip files without or . Set --insert as well. Default: false." + assert_equal "${lines[16]}" " --stdout Print the whole file with the new TOC to stdout. The file is not changed. Default: false." + assert_equal "${lines[17]}" " --skip-header Hide entry of the topmost headlines. Default: false." + assert_equal "${#lines[@]}" "19" } @test "--help" { @@ -783,3 +784,68 @@ gh_md_toc_no_stderr() { assert_equal "${lines[3]}" "Error: GitHub API request failed (HTTP 403)" [[ "${lines[4]}" == *"too large"* ]] } + +@test "Insert into several files skips a file without markers, #110" { + printf '\n\n\n# Page a\n' > "$BATS_TEST_TMPDIR/a.md" + printf '# Page b\n' > "$BATS_TEST_TMPDIR/b.md" + printf '\n\n\n# Page c\n' > "$BATS_TEST_TMPDIR/c.md" + run_to_files --insert --no-backup --hide-footer --skip-no-markers "$BATS_TEST_TMPDIR/a.md" "$BATS_TEST_TMPDIR/b.md" "$BATS_TEST_TMPDIR/c.md" + assert_success + + printf '\n* [Page a](#page-a)\n\n\n# Page a\n' | cmp - "$BATS_TEST_TMPDIR/a.md" + printf '\n* [Page c](#page-c)\n\n\n# Page c\n' | cmp - "$BATS_TEST_TMPDIR/c.md" + printf '# Page b\n' | cmp - "$BATS_TEST_TMPDIR/b.md" + assert_equal "$(grep -Fxc '* [Page a](#page-a)' "$BATS_TEST_TMPDIR/out.md")" "1" + assert_equal "$(grep -Fxc '* [Page c](#page-c)' "$BATS_TEST_TMPDIR/out.md")" "1" + assert_equal "$(grep -Fxc '* [Page b](#page-b)' "$BATS_TEST_TMPDIR/out.md")" "0" + assert_equal "$(cat "$BATS_TEST_TMPDIR/err.txt")" "$BATS_TEST_TMPDIR/b.md: Warning: no or in the file, skipped" +} + +@test "Skip a file with one marker without a GitHub API request, #110" { + mkdir "$BATS_TEST_TMPDIR/dir" + printf '\n\n# Page b\n' > "$BATS_TEST_TMPDIR/dir/b.md" + GH_TOC_TOKEN=invalid run_to_files --insert --skip-no-markers "$BATS_TEST_TMPDIR/dir/b.md" + assert_success + + printf '\n\n# Page b\n' | cmp - "$BATS_TEST_TMPDIR/dir/b.md" + assert_equal "$(ls "$BATS_TEST_TMPDIR/dir")" "b.md" + assert_equal "$(grep -Fxc 'Parsing local markdown file requires access to github API' "$BATS_TEST_TMPDIR/out.md")" "0" + assert_equal "$(cat "$BATS_TEST_TMPDIR/err.txt")" "$BATS_TEST_TMPDIR/dir/b.md: Warning: no or in the file, skipped" +} + +@test "No skip for a missing file, #110" { + run_to_files --skip-no-markers "$BATS_TEST_TMPDIR/missing.md" + assert_fail + + assert_equal "$(grep -c 'in the file, skipped$' "$BATS_TEST_TMPDIR/err.txt")" "0" +} + +@test "--skip-no-markers turns on --insert, #110" { + printf '\n\n\n# Page a\n' > "$BATS_TEST_TMPDIR/a.md" + run_to_files --skip-no-markers "$BATS_TEST_TMPDIR/a.md" + assert_success + + assert_equal "$(sed -n 2p "$BATS_TEST_TMPDIR/a.md")" "* [Page a](#page-a)" + assert_equal "$(grep -Fxc "!! TOC was added into: '$BATS_TEST_TMPDIR/a.md'" "$BATS_TEST_TMPDIR/out.md")" "1" +} + +@test "Insert into several files stops at a file without markers, #110" { + printf '\n\n\n# Page a\n' > "$BATS_TEST_TMPDIR/a.md" + printf '# Page b\n' > "$BATS_TEST_TMPDIR/b.md" + printf '\n\n\n# Page c\n' > "$BATS_TEST_TMPDIR/c.md" + run_to_files --insert --no-backup --hide-footer "$BATS_TEST_TMPDIR/a.md" "$BATS_TEST_TMPDIR/b.md" "$BATS_TEST_TMPDIR/c.md" + assert_fail + + assert_equal "$(sed -n 2p "$BATS_TEST_TMPDIR/a.md")" "* [Page a](#page-a)" + assert_equal "$(grep -Fxc "You don't have or in your file...exiting" "$BATS_TEST_TMPDIR/out.md")" "1" + printf '# Page b\n' | cmp - "$BATS_TEST_TMPDIR/b.md" + printf '\n\n\n# Page c\n' | cmp - "$BATS_TEST_TMPDIR/c.md" +} + +@test "Error for --skip-no-markers with --stdout, #110" { + run_to_files --skip-no-markers --stdout tests/test\ directory/test_stdout.md + assert_fail + + [ ! -s "$BATS_TEST_TMPDIR/out.md" ] + assert_equal "$(cat "$BATS_TEST_TMPDIR/err.txt")" "--skip-no-markers cannot be used with --stdout." +} From ae4f0512705674a1878bb71ff7ea91c243d89ac3 Mon Sep 17 00:00:00 2001 From: Eugene Kalinin Date: Sat, 7 Nov 2026 23:48:02 +0300 Subject: [PATCH 3/5] feat(toc): add --skip-no-markers to skip files without markers --- gh-md-toc | 23 ++++++++++++++++++- openspec/changes/add-skip-no-markers/tasks.md | 8 +++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/gh-md-toc b/gh-md-toc index 86ed222..95bc850 100755 --- a/gh-md-toc +++ b/gh-md-toc @@ -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 '^[[:space:]]*$' "$gh_src" || ! grep -Eq '^[[:space:]]*$' "$gh_src"; then + echo "$gh_src: Warning: no or in the file, skipped" >&2 + return + fi + fi + # Show "TOC" string only if working with one document if [ "$gh_ttl_docs" = "1" ]; then @@ -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 or . 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." @@ -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 @@ -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 @@ -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 @@ -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 "" diff --git a/openspec/changes/add-skip-no-markers/tasks.md b/openspec/changes/add-skip-no-markers/tasks.md index 24c031a..19f7653 100644 --- a/openspec/changes/add-skip-no-markers/tasks.md +++ b/openspec/changes/add-skip-no-markers/tasks.md @@ -31,20 +31,20 @@ ## 2. Implementation -- [ ] 2.1 `gh_toc_app`: add `local skip_no_markers=""` next to `local to_stdout=""`, the +- [x] 2.1 `gh_toc_app`: add `local skip_no_markers=""` next to `local to_stdout=""`, the `--skip-no-markers` check right after `--hide-footer` (sets `need_replace="yes"` and `skip_no_markers="yes"`), and pass `"$skip_no_markers"` to `gh_toc` as the 11th parameter. `gh_toc`: take `local skip_no_markers=${11}`, and right after the check for an empty input add the check from design.md (local input, `[ -f "$gh_src" ]`, the two `grep -Eq` of `gh_toc_stdout`, the warning to stderr, `return`); verify `make lint` passes, the tests from 1.1, 1.2 and 1.4 pass, and the tests from 1.3 and 1.5 still pass -- [ ] 2.2 `gh_toc_app`, the `--stdout` branch: before the check of the inputs, print +- [x] 2.2 `gh_toc_app`, the `--stdout` branch: before the check of the inputs, print `--skip-no-markers cannot be used with --stdout.` to stderr and `exit 1` when `skip_no_markers` is `yes`; verify the test from 1.6 passes -- [ ] 2.3 `show_help`: add +- [x] 2.3 `show_help`: add ` --skip-no-markers Skip files without or . Set --insert as well. Default: false.` right after the `--hide-footer` line; verify the tests from 1.7 pass -- [ ] 2.4 Check the cases not covered by the tests: in a temporary directory with +- [x] 2.4 Check the cases not covered by the tests: in a temporary directory with `a.md` and `sub/c.md` (with markers) and `b.md` (without), run the README example `find . -name '*.md' -exec /gh-md-toc --insert --no-backup --hide-footer --skip-no-markers {} +`; verify `a.md` and `sub/c.md` get their TOC with `#` links, `b.md` does not From 2f8f3502f094b42406390c4b84bd5ff47fc24243 Mon Sep 17 00:00:00 2001 From: Eugene Kalinin Date: Sat, 7 Nov 2026 23:48:23 +0300 Subject: [PATCH 4/5] docs(readme): add the --skip-no-markers example with find --- README.md | 8 ++++++++ openspec/changes/add-skip-no-markers/tasks.md | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8bf08de..a0c209c 100644 --- a/README.md +++ b/README.md @@ -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 `` or `` 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`: diff --git a/openspec/changes/add-skip-no-markers/tasks.md b/openspec/changes/add-skip-no-markers/tasks.md index 19f7653..d59f058 100644 --- a/openspec/changes/add-skip-no-markers/tasks.md +++ b/openspec/changes/add-skip-no-markers/tasks.md @@ -54,7 +54,7 @@ ## 3. Documentation -- [ ] 3.1 README, section "Auto insert and update TOC": after the paragraph "Next time +- [x] 3.1 README, section "Auto insert and update TOC": after the paragraph "Next time when your file will be changed just repeat the command ...", add a sentence about `--skip-no-markers` and the `find` example from design.md; add no new heading; verify the README TOC tests (`TOC for local README.md`, `TOC for local README.md with skip From 339fa0a39f2b62171bef08800aab2542513fb01f Mon Sep 17 00:00:00 2001 From: Eugene Kalinin Date: Sat, 7 Nov 2026 23:50:03 +0300 Subject: [PATCH 5/5] chore(openspec): mark add-skip-no-markers tasks done --- openspec/changes/add-skip-no-markers/tasks.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openspec/changes/add-skip-no-markers/tasks.md b/openspec/changes/add-skip-no-markers/tasks.md index d59f058..5c71ad5 100644 --- a/openspec/changes/add-skip-no-markers/tasks.md +++ b/openspec/changes/add-skip-no-markers/tasks.md @@ -62,4 +62,4 @@ ## 4. Verification -- [ ] 4.1 Run `make lint` and `make test` with `GH_TOC_TOKEN` set; verify both pass +- [x] 4.1 Run `make lint` and `make test` with `GH_TOC_TOKEN` set; verify both pass