diff --git a/README.md b/README.md index a0c209c..f552806 100644 --- a/README.md +++ b/README.md @@ -183,6 +183,14 @@ Table of Contents ``` +To keep the inline code of the headings in backticks, use `--keep-code`: + +```bash +➥ printf '# The command `foo1`\n\n## The command `foo2` is better\n' | ./gh-md-toc --keep-code - +* [The command `foo1`](#the-command-foo1) + * [The command `foo2` is better](#the-command-foo2-is-better) +``` + Remote files ------------ diff --git a/gh-md-toc b/gh-md-toc index 95bc850..60d7a7e 100755 --- a/gh-md-toc +++ b/gh-md-toc @@ -199,6 +199,7 @@ gh_toc(){ local numbered=$9 local bullet=${10} local skip_no_markers=${11} + local keep_code=${12} if [ "$gh_src" = "" ]; then echo "Please, enter URL or local path for a README.md" @@ -225,7 +226,7 @@ gh_toc(){ fi if [ "$(gh_is_url "$gh_src")" == "yes" ]; then - gh_toc_load "$gh_src" | gh_toc_grab "$gh_src_copy" "$indent" "$depth" "$numbered" "$bullet" + gh_toc_load "$gh_src" | gh_toc_grab "$gh_src_copy" "$indent" "$depth" "$numbered" "$bullet" "" "$keep_code" if [ "${PIPESTATUS[0]}" != "0" ]; then echo "Could not load remote document." echo "Please check your url or network connectivity" @@ -242,7 +243,7 @@ gh_toc(){ rawhtml=$(gh_toc_md2html "$gh_src" "$skip_header") gh_toc_check_api_error "$rawhtml" local toc - toc=$(echo "$rawhtml" | gh_toc_grab "$gh_src_copy" "$indent" "$depth" "$numbered" "$bullet" "$need_replace") + toc=$(echo "$rawhtml" | gh_toc_grab "$gh_src_copy" "$indent" "$depth" "$numbered" "$bullet" "$need_replace" "$keep_code") echo "$toc" if [ "$need_replace" = "yes" ]; then if grep -Eq '^[[:space:]]*$' "$gh_src" && grep -Eq '^[[:space:]]*$' "$gh_src"; then @@ -308,6 +309,7 @@ gh_toc_stdout(){ local depth=$5 local numbered=$6 local bullet=$7 + local keep_code=$8 # global: the trap runs on exit, after this function has returned gh_tmp_stdin="" @@ -329,7 +331,7 @@ gh_toc_stdout(){ rawhtml=$(gh_toc_md2html "$gh_src" "$skip_header") gh_toc_check_api_error "$rawhtml" >&2 local toc - toc=$(echo "$rawhtml" | gh_toc_grab "" "$indent" "$depth" "$numbered" "$bullet") + toc=$(echo "$rawhtml" | gh_toc_grab "" "$indent" "$depth" "$numbered" "$bullet" "" "$keep_code") # the same TOC block and sed expressions as --insert, without -i local ts="<\!--ts-->" @@ -359,15 +361,44 @@ gh_toc_stdout(){ # $4 - numbering type: "list", "outline" or empty for bullets. # $5 - bullet character: "*", "-", "+" or empty for "*". # $6 - "yes" if the TOC goes into the document itself (--insert). +# $7 - "yes" to keep inline code in backticks (--keep-code). # gh_toc_grab() { # the heading's own anchor, not a link in the heading text, see #40 href_regex="/class=\"anchor\"[^>]*href=\"[^\"]+\"/" + # shellcheck disable=SC2016 common_awk_script=' if (depth+0 > 0 && level+0 > depth+0) next + out = "" + # keep inline code in backticks, see #153 + while (keep_code == "yes" && match(text, /]*>[^<]*<\/code>/)) { + pre = substr(text, 1, RSTART - 1) + code = substr(text, RSTART, RLENGTH) + text = substr(text, RSTART + RLENGTH) + gsub(/<[^>]*>/, "", pre) + sub(/^]*>/, "", code) + sub(/<\/code>$/, "", code) + gsub(/</, "<", code) + gsub(/>/, ">", code) + gsub(/&/, "\\&", code) + # one backtick more than the longest run inside + fence = "`" + s = code + while (match(s, /`+/)) { + if (RLENGTH >= length(fence)) { + fence = "`" + for (n = 0; n < RLENGTH; n++) fence = fence "`" + } + s = substr(s, RSTART + RLENGTH) + } + # a space on each side keeps the spaces and backticks at the edges + pad = (code ~ /[^ ]/ && (code ~ /^[ `]/ || code ~ /[ `]$/)) ? " " : "" + out = out pre fence pad code pad fence + } # remove tags of links, formatting and inline html, see #40 gsub(/<[^>]*>/, "", text) + text = out text # outline number: pop the entries down to the parent, # the last popped one is the previous sibling prev = 0 @@ -456,8 +487,8 @@ gh_toc_grab() { # find strings that corresponds to template $grepcmd ']*class="heading-element".*//g' | sed 's/<\/code>//g' | + # remove code tags, unless --keep-code, see #153 + if [ "$7" = "yes" ]; then cat; else sed 's///g' | sed 's/<\/code>//g'; fi | # remove g-emoji sed 's/]*[^<]*<\/g-emoji> //g' | @@ -467,7 +498,7 @@ gh_toc_grab() { # format result line # * $0 - whole string # * last element of each row: " Max heading level to include into TOC. Default: 0 (all levels)." echo " --numbered Number TOC entries: list (ordered list) or outline (1.1. in text). Default: none." echo " --bullet Bullet of TOC entries: *, - or +. Default: *." + echo " --keep-code Keep inline code of headings in backticks. Default: false." echo " --insert Insert new TOC into original file. For local files only. Default: false." 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." @@ -533,6 +565,7 @@ gh_toc_app() { local depth=0 local numbered="" local bullet="" + local keep_code="" local to_stdout="" local skip_no_markers="" @@ -566,6 +599,11 @@ gh_toc_app() { shift 2 fi + if [ "$1" = '--keep-code' ]; then + keep_code="yes" + shift + fi + case $numbered in "" | list | outline) ;; *) @@ -598,7 +636,7 @@ gh_toc_app() { local rawhtml rawhtml=$(gh_toc_md2html "$gh_tmp_md") gh_toc_check_api_error "$rawhtml" - echo "$rawhtml" | gh_toc_grab "" "$indent" "$depth" "$numbered" "$bullet" + echo "$rawhtml" | gh_toc_grab "" "$indent" "$depth" "$numbered" "$bullet" "" "$keep_code" return fi @@ -644,7 +682,7 @@ gh_toc_app() { echo "--stdout works with one local file or '-' (STDIN) only." >&2 exit 1 fi - gh_toc_stdout "$1" "$no_footer" "$indent" "$skip_header" "$depth" "$numbered" "$bullet" + gh_toc_stdout "$1" "$no_footer" "$indent" "$skip_header" "$depth" "$numbered" "$bullet" "$keep_code" return fi @@ -652,7 +690,7 @@ gh_toc_app() { for md in "$@" do echo "" - gh_toc "$md" "$#" "$need_replace" "$no_backup" "$no_footer" "$indent" "$skip_header" "$depth" "$numbered" "$bullet" "$skip_no_markers" + gh_toc "$md" "$#" "$need_replace" "$no_backup" "$no_footer" "$indent" "$skip_header" "$depth" "$numbered" "$bullet" "$skip_no_markers" "$keep_code" done echo "" diff --git a/openspec/changes/add-keep-code/.openspec.yaml b/openspec/changes/add-keep-code/.openspec.yaml new file mode 100644 index 0000000..c0ad5ed --- /dev/null +++ b/openspec/changes/add-keep-code/.openspec.yaml @@ -0,0 +1,2 @@ +schema: spec-driven +created: 2026-11-08 diff --git a/openspec/changes/add-keep-code/design.md b/openspec/changes/add-keep-code/design.md new file mode 100644 index 0000000..bff8685 --- /dev/null +++ b/openspec/changes/add-keep-code/design.md @@ -0,0 +1,242 @@ +# Design + +## Context + +See proposal.md - Why for the symptoms and specs/toc-keep-code/spec.md for the +requirements. + +`gh_toc_grab` turns the HTML into the TOC with one pipeline: + +``` +sed: join "\n..., since #13 + | +sed: remove ... + | +awk: text = from the first "> to ]*>/, "", text) since #185 + skipped level warning, numbers, bullet, link +``` + +Since #185, the `` `sed` step is redundant: the awk `gsub` removes every tag of +the text. The OS/390 branch takes the text with `/<\/span><\/a>[^<]*<\/h/`, an +expression for the old GitHub HTML layout, so its entries have no text on the current +markup (#204). + +The inline code in the HTML, both from the markdown API and on a remote GitHub page +(checked on `tests/test directory/test_backquote.md` on github.com): + +- a `` element without attributes; +- inside it, GitHub escapes `<`, `>` and `&` as `<`, `>`, `&`, and writes `"` + and `'` as they are (`` `"q'` `` gives `"q'`), so there are no tags + inside; +- `:rocket:` inside the code stays text, there is no `` in it. + +`gh_toc_grab` is called in four places, and only the local file call passes `$6` +(`need_replace`): + +``` +gh_toc remote URL gh_toc_grab "$gh_src_copy" ... "$bullet" +gh_toc local file gh_toc_grab "$gh_src_copy" ... "$bullet" "$need_replace" +gh_toc_stdout --stdout gh_toc_grab "" ... "$bullet" +gh_toc_app stdin (-) gh_toc_grab "" ... "$bullet" +``` + +`gh_toc_app` parses `--indent`, `--depth`, `--numbered`, `--bullet` and checks their +values. Then it handles `-` (stdin) and returns, and only after that parses `--insert` +and the rest. + +Constraints: BSD awk on macOS (20200816), mawk in the Docker image (Debian) and on Debian +and Ubuntu, GNU awk, the OS/390 branch, `make lint` (shellcheck). + +## Goals / Non-Goals + +**Goals:** + +- Without `--keep-code`, the same output byte for byte, on every branch. +- The code spans built in one place, `common_awk_script`, for both branches. + +**Non-Goals:** + +- Removing the redundant `` `sed` step for the default path. +- Passing the options of `gh_toc_grab` in another way than positional parameters. + +## Decisions + +### The name `--keep-code` + +The name says what the user gets: the code stays code. `--no-code-tag-removal` from #153 +describes the implementation, while the option also decodes the entities and picks the +backticks. It is also 21 characters long, and the option column of `--help` has 20, so +the description would not line up with the other options. + +### Parsing: right after `--bullet` + +`--keep-code` changes the text of the entries, as `--bullet` changes their marker, and it +has to work with stdin, which is handled right after the value checks. So `gh_toc_app` +gets `local keep_code=""` next to `local bullet=""`, and right after the `--bullet` +check: + +```bash +if [ "$1" = '--keep-code' ]; then + keep_code="yes" + shift +fi +``` + +### Passing the option: a new last parameter + +- `gh_toc_grab`: `$7`, passed to awk as `-v "keep_code=$7"`. The three calls without + `need_replace` pass `""` as `$6`, so `$7` is in place and `need_replace` stays empty + for them as now (the remote call relies on it for the link prefix). +- `gh_toc`: a 12th parameter, `local keep_code=${12}`. +- `gh_toc_stdout`: an 8th parameter, `local keep_code=$8`. + +This is how `--bullet` and `--skip-no-markers` were added. + +Alternative: rely on the dynamic scope of `local` in bash, so `gh_toc_grab` sees +`keep_code` of `gh_toc_app`. No other option works that way, and the calls would no +longer show what `gh_toc_grab` depends on. + +### The `` `sed` step is skipped with the option + +```bash +# remove code tags, unless --keep-code, see #153 +if [ "$7" = "yes" ]; then cat; else sed 's///g' | sed 's/<\/code>//g'; fi | +``` + +Without the option, the step runs as before. + +Alternatives considered: + +- Remove the step: it is redundant since #185, but `--keep-code` does not need it + removed. It stays to keep the diff small, as `fix-heading-markup` (#185) decided. +- Replace `` and `` with a backtick in `sed`, as #153 suggests: no entity + decoding and always one backtick, so `` `a&c` `` would give `` `a<b>&c` `` + (rendered as is inside a code span), and ``` `` a`b `` ``` would give `` `a`b` ``. + +### The code spans in `common_awk_script` + +Right after the depth filter, before the existing `gsub`: + +```awk +out = "" +# keep inline code in backticks, see #153 +while (keep_code == "yes" && match(text, /]*>[^<]*<\/code>/)) { + pre = substr(text, 1, RSTART - 1) + code = substr(text, RSTART, RLENGTH) + text = substr(text, RSTART + RLENGTH) + gsub(/<[^>]*>/, "", pre) + sub(/^]*>/, "", code) + sub(/<\/code>$/, "", code) + gsub(/</, "<", code) + gsub(/>/, ">", code) + gsub(/&/, "\\&", code) + # one backtick more than the longest run inside + fence = "`" + s = code + while (match(s, /`+/)) { + if (RLENGTH >= length(fence)) { + fence = "`" + for (n = 0; n < RLENGTH; n++) fence = fence "`" + } + s = substr(s, RSTART + RLENGTH) + } + # a space on each side keeps the spaces and backticks at the edges + pad = (code ~ /[^ ]/ && (code ~ /^[ `]/ || code ~ /[ `]$/)) ? " " : "" + out = out pre fence pad code pad fence +} +# remove tags of links, formatting and inline html, see #40 +gsub(/<[^>]*>/, "", text) +text = out text +``` + +- The tags are removed from the text around each code element, not from the whole text + after it: the decoded code can have `` in it, which the `gsub` would remove + (`a&c` would become `a&c`). So `out` collects the done part, and the existing + `gsub` runs only on the rest. +- Without the option, the loop does not run and `out` is empty, so the text is the same + as before. +- `&` is decoded last, so a literal `<` in the code (`&lt;`) stays `<`. + `"\\&"` is a literal `&` in the replacement of `gsub`. +- `pre`, `code` and the rest of `text` are taken before the inner `match`, which + overwrites `RSTART` and `RLENGTH`. +- `[^<]*` is enough for the content: there are no tags inside the code. +- The padding follows CommonMark: one space is stripped from each side of a code span + that starts and ends with a space, unless it is only spaces. So a code of only spaces + gets no padding. +- New awk variables: `out`, `pre`, `code`, `fence`, `s`, `n`, `pad`. None of them is used + by the script now (`num` is, so it is not reused). +- The skipped level warning (#75) prints `text`, so it shows the backticks too, as the + TOC entry does. + +### Help, README + +`show_help` gets +` --keep-code Keep inline code of headings in backticks. Default: false.` +right after the `--bullet` line; the description starts at the same column as the other +options. + +README, section "Local files": after the `--bullet` example, a sentence and an example: + +```` +To keep the inline code of the headings in backticks, use `--keep-code`: + +```bash +➥ printf '# The command `foo1`\n\n## The command `foo2` is better\n' | ./gh-md-toc --keep-code - +* [The command `foo1`](#the-command-foo1) + * [The command `foo2` is better](#the-command-foo2-is-better) +``` +```` + +The example is in a code block and adds no heading, so the README TOC in +`tests/tests.bats` does not change. + +### Tests + +- A new fixture `tests/test directory/test_keepcode.md` with the headings of the spec + scenario, separated by empty lines. For none of them GitHub puts a line break after the + heading tag (#201), checked with the markdown API; ``## *em `code`* and ...`` had one, + so the heading starts with `A`. +- "TOC with inline code in backticks for local file, #153": every entry and the footer. +- "TOC with inline code without --keep-code for local file, #153": the same fixture + without the option; it passes before the implementation and pins down the default. +- "TOC with inline code in backticks for markdown from stdin, #153": + `--bullet - --keep-code -` on `test_backquote.md`, 5 lines. +- "TOC with inline code in backticks for remote file, #153": the URL of the spec + scenario, pinned to a commit like the `sitemap.js` URL of the other remote tests. +- "Whole file with inline code in backticks to stdout, #153": `a.md` written into + `$BATS_TEST_TMPDIR` with `printf`, `run_to_files`, stdout and the file compared with + `cmp`, as the #62 tests do. +- `test_help`: the new line at index 11, the following lines one index further, 20 lines + in total. + +Before the implementation, `--keep-code` is taken as an input, a missing file, so the +four tests with the option and `test_help` fail. + +### Checked + +On a temporary copy of the script with the parsing replaced by an environment variable: + +- The fixture gives the entries of both spec scenarios, byte for byte the same with BSD + awk 20200816 (macOS), mawk 1.3.4 and GNU awk 5.2.1 (Debian container). +- The TOC entries made with `--keep-code`, rendered by the markdown API, have the same + `` content as the headings, for all 13 code spans of the fixture. +- Without the option, `README.md` and every fixture in `tests/test directory/` give the + same output as master. + +## Risks / Trade-offs + +- [GitHub adds attributes to ``] → `]*>` matches them with the option. + Without it, the `sed` step and the awk `gsub` remove the tags as now. +- [GitHub puts tags inside ``] → The element does not match, and its tags are + removed as without the option, so the entry has the code without backticks. +- [GitHub escapes `"` or `'` inside code in the future] → They would be written as + `"` or `'` in the code span. Today they are written as they are. +- [Five new tests call the GitHub API or load a page from github.com] → Same as the + existing tests: `GH_TOC_TOKEN` locally, `secrets.GITHUB_TOKEN` in CI. diff --git a/openspec/changes/add-keep-code/proposal.md b/openspec/changes/add-keep-code/proposal.md new file mode 100644 index 0000000..0d96767 --- /dev/null +++ b/openspec/changes/add-keep-code/proposal.md @@ -0,0 +1,77 @@ +# Proposal + +## Why + +The TOC entry of a heading with inline code has no backticks (#153, open since 2024): +`` # `Headline` `` gives `* [Headline](#headline)`. The reporter's linter fails on such +entries in CI. The `` tags have been removed from the entries since the fix of #13 +(1056956), and since #185 every tag of the heading text is removed in awk as well. + +Without the backticks, the code is read as markdown in the TOC. Reproduced with the +current master: `` ## `**kwargs**` `` gives `* [**kwargs**](#kwargs)`, which GitHub +renders as a bold "kwargs" without the asterisks. + +The reporter asks for an option, so that the entries do not change for the users who want +the code without backticks. + +## What Changes + +- New option `--keep-code`. With it, every inline code element of the heading text is + written into the TOC entry as a markdown code span with the same content: + - `<`, `>` and `&` inside the code are written as `<`, `>` and `&`: in a code + span they are not decoded; + - the span is enclosed in one backtick more than the longest run of backticks inside + the code; + - one space is added on each side when the code starts or ends with a space or a + backtick and is not only spaces. + + For example, `` # `Headline` `` gives `` * [`Headline`](#headline) ``, and + `` ## `a&c` `` gives `` * [`a&c`](#abc) ``. The rest of the text and the link of + the entry stay as without the option. +- `--keep-code` applies to every input: stdin, a local file, a remote URL, several + inputs, `--insert` and `--stdout`. +- `--keep-code` is parsed right after `--bullet` and before `-`, so it works with stdin. +- `--help`: a new line for `--keep-code` right after `--bullet`. +- README: a sentence and an example after the `--bullet` example, without a new heading. +- Tests: a new fixture with the cases above, the option for a local file, stdin, a + remote URL and `--stdout`, the same fixture without the option, and an updated + `test_help`. + +Without `--keep-code`, the output does not change. + +Non-goals: + +- Keeping the backticks by default: #153 asks for an option. +- Writing other markup back as markdown (`*em*`, `**strong**`, links): #153 asks for the + code only. +- Headings that are missing from the TOC when GitHub puts a line break after the heading + tag (#201): a separate bug, also without `--keep-code`. +- Backslash sequences in the heading text, such as `\n`, which the final `echo -e` of + `gh_toc_grab` turns into control characters: the same with and without `--keep-code`. +- Changes to the landing page (`site/`) or `openspec/specs/landing`. +- A version bump. + +## Capabilities + +### New Capabilities + +- `toc-keep-code`: the `--keep-code` option: how the inline code of a heading is + written into its TOC entry, the inputs it applies to, the option order, and `--help`. + +### Modified Capabilities + +- `toc-heading-markup`: "Plain text of the heading" states that all HTML tags of the + heading text are removed; with `--keep-code`, the inline code is written as described + in `toc-keep-code`. + +## Impact + +- `gh-md-toc`: `gh_toc_app` (parse `--keep-code`, pass it on), `gh_toc` and + `gh_toc_stdout` (a new last parameter), `gh_toc_grab` (a new parameter, the `` + `sed` step and the awk script), `show_help`. +- `tests/tests.bats`: new tests, a new fixture `tests/test directory/test_keepcode.md`, + and an updated `test_help` (20 lines instead of 19). +- `README.md`: a sentence and an example only, no new heading, so the README TOC + asserted in `tests/tests.bats` does not change. +- The Docker image (Debian, `awk` is mawk) gets the same output as macOS (BSD awk). +- Closes #153. diff --git a/openspec/changes/add-keep-code/specs/toc-heading-markup/spec.md b/openspec/changes/add-keep-code/specs/toc-heading-markup/spec.md new file mode 100644 index 0000000..42e3556 --- /dev/null +++ b/openspec/changes/add-keep-code/specs/toc-heading-markup/spec.md @@ -0,0 +1,28 @@ +# Spec Delta + +## MODIFIED Requirements + +### Requirement: Plain text of the heading + +The text of every TOC entry SHALL be the heading text as GitHub renders it, with all +HTML tags removed. The text inside link, image, formatting and inline HTML elements +(such as ``, ``, ``, ``) SHALL be kept. HTML entities SHALL stay as +GitHub renders them (`&`, `<`), and the spaces around a removed element SHALL +stay as they are. Entries of headings without HTML tags SHALL NOT change. This SHALL +apply to every input: stdin, a local file, a remote URL, several inputs, `--insert` and +`--stdout`. With `--keep-code`, the inline code of the heading SHALL be written as in +`toc-keep-code`, and the rest of the text SHALL follow this requirement. + +#### Scenario: Escaped characters are not tags + +- **WHEN** the user runs `gh-md-toc "tests/test directory/test_headingmarkup.md"` +- **THEN** the entry of the heading `## Tom & Jerry <3` is + ` * [Tom & Jerry <3](#tom--jerry-3)` + +#### Scenario: Formatted setext heading with --insert + +- **WHEN** the user runs + `gh-md-toc --no-backup --hide-footer "tests/test directory/test_setextwithformatting.md"`, + where the file has the setext heading `` *Title `two`* `` underlined with `=` +- **THEN** the entry of that heading is `* [Title two](#title-two)`, in the output and + in the TOC between `` and `` in the file diff --git a/openspec/changes/add-keep-code/specs/toc-keep-code/spec.md b/openspec/changes/add-keep-code/specs/toc-keep-code/spec.md new file mode 100644 index 0000000..03a7bf1 --- /dev/null +++ b/openspec/changes/add-keep-code/specs/toc-keep-code/spec.md @@ -0,0 +1,138 @@ +# Spec Delta + +## Purpose + +Lets the TOC entry of a heading with inline code keep that code in backticks, so the +entry shows the code as the heading does and linters that compare them pass. + +## ADDED Requirements + +### Requirement: Inline code in backticks + +With `--keep-code`, every inline code element of the heading text, as GitHub renders it, +SHALL be written into the TOC entry as a markdown code span with the same content: + +- `<`, `>` and `&` inside the code SHALL be written as `<`, `>` and `&`; +- the code SHALL be enclosed in a run of backticks one longer than the longest run of + backticks inside the code; +- when the code starts or ends with a space or a backtick and is not only spaces, one + space SHALL be added on each side, between the backticks and the code. + +The text outside the inline code SHALL be the same as without `--keep-code`, as in +`toc-heading-markup`: tags removed and HTML entities as GitHub renders them. The link of +the entry SHALL be the same as without `--keep-code`. + +#### Scenario: Headings with inline code + +- **WHEN** the user runs `gh-md-toc --keep-code "tests/test directory/test_keepcode.md"`, + where the file has these headings, separated by empty lines: + + ```` + # `Headline` + ## The command `foo1` is better + ## `a&c` + ## `` a`b `` + ## `` `a` `` + ## ``` a``b ``` + ## `< ©` + ## ` spaced ` + ## x ` ` y + ## `**kwargs**` + ## Tom & `Jerry` <3 + ## A *em `code`* and [`link`](#headline) + ```` + +- **THEN** the TOC entries are these lines, followed by + ``: + + ```` + * [`Headline`](#headline) + * [The command `foo1` is better](#the-command-foo1-is-better) + * [`a&c`](#abc) + * [``a`b``](#ab) + * [`` `a` ``](#a) + * [```a``b```](#ab-1) + * [`< ©`](#lt-copy) + * [` spaced `](#--spaced--) + * [x ` ` y](#x---y) + * [`**kwargs**`](#kwargs) + * [Tom & `Jerry` <3](#tom--jerry-3) + * [A em `code` and `link`](#a-em-code-and-link) + ```` + +### Requirement: Every input + +`--keep-code` SHALL apply to every input: stdin, a local file, a remote URL, several +inputs, `--insert` and `--stdout`. + +`--keep-code` SHALL be accepted after `--indent `, `--depth `, +`--numbered ` and `--bullet ` (when given) and before `-`, `--insert`, +`--no-backup`, `--hide-footer`, `--skip-no-markers`, `--stdout`, `--skip-header` and the +inputs. + +#### Scenario: Stdin with a bullet + +- **WHEN** the user runs + `cat "tests/test directory/test_backquote.md" | gh-md-toc --bullet - --keep-code -` +- **THEN** the output is these lines: + + ```` + - [The command `foo1`](#the-command-foo1) + - [The command `foo2` is better](#the-command-foo2-is-better) + - [The command `bar1`](#the-command-bar1) + - [The command `bar2` is better](#the-command-bar2-is-better) + - [The command `bar3` is the best](#the-command-bar3-is-the-best) + ```` + +#### Scenario: Remote URL + +- **WHEN** the user runs + `gh-md-toc --keep-code https://github.com/ekalinin/github-markdown-toc/blob/a28f86dcb616ce17103d1dfc28f6c3ac423c388c/tests/test%20directory/test_backquote.md` +- **THEN** the TOC entries are the lines of the scenario "Stdin with a bullet" with `*` + instead of `-`, and the exit code is 0 + +#### Scenario: Whole file to stdout + +- **WHEN** the user runs `gh-md-toc --keep-code --hide-footer --stdout /a.md`, where + `a.md` is ``\n\n\n# The command `foo1`\n`` +- **THEN** stdout is + ``\n* [The command `foo1`](#the-command-foo1)\n\n\n# The command `foo1`\n``, + `a.md` does not change, and the exit code is 0 + +### Requirement: Without the option + +Without `--keep-code`, the TOC entries SHALL be the same as before this change: the +inline code is written without backticks, as in `toc-heading-markup`. + +#### Scenario: Headings with inline code without the option + +- **WHEN** the user runs `gh-md-toc "tests/test directory/test_keepcode.md"` +- **THEN** the TOC entries are these lines, followed by + ``: + + ```` + * [Headline](#headline) + * [The command foo1 is better](#the-command-foo1-is-better) + * [a<b>&c](#abc) + * [a`b](#ab) + * [`a`](#a) + * [a``b](#ab-1) + * [&lt; &copy;](#lt-copy) + * [ spaced ](#--spaced--) + * [x y](#x---y) + * [**kwargs**](#kwargs) + * [Tom & Jerry <3](#tom--jerry-3) + * [A em code and link](#a-em-code-and-link) + ```` + +### Requirement: Help lists the option + +`--help` SHALL list `--keep-code` in the "Options" section right after +`--bullet `. + +#### Scenario: Help output + +- **WHEN** the user runs `gh-md-toc --help` +- **THEN** the line after + ` --bullet Bullet of TOC entries: *, - or +. Default: *.` + is ` --keep-code Keep inline code of headings in backticks. Default: false.` diff --git a/openspec/changes/add-keep-code/tasks.md b/openspec/changes/add-keep-code/tasks.md new file mode 100644 index 0000000..e8546c1 --- /dev/null +++ b/openspec/changes/add-keep-code/tasks.md @@ -0,0 +1,67 @@ +# Tasks + +## 1. Tests + +- [x] 1.1 Add the fixture `tests/test directory/test_keepcode.md` with the headings of the + spec scenario "Inline code in backticks - Headings with inline code", separated by + empty lines; verify with the markdown API that no heading has a line break right after + the heading tag (#201) +- [x] 1.2 Add the test "TOC with inline code without --keep-code for local file, #153" + in `tests/tests.bats`, as in the spec scenario "Headings with inline code without the + option": every entry and the footer; verify it passes before the implementation + (`bats --filter "" tests`) +- [x] 1.3 Add the test "TOC with inline code in backticks for local file, #153", as in + the spec scenario "Headings with inline code": every entry and the footer; verify it + fails before the implementation +- [x] 1.4 Add the test "TOC with inline code in backticks for markdown from stdin, #153", + as in the spec scenario "Stdin with a bullet": `cat` of `test_backquote.md` into + `--bullet - --keep-code -`, the 5 lines and `${#lines[@]}`; verify it fails before the + implementation +- [x] 1.5 Add the test "TOC with inline code in backticks for remote file, #153", as in + the spec scenario "Remote URL": the 5 entries with `*`; verify it fails before the + implementation +- [x] 1.6 Add the test "Whole file with inline code in backticks to stdout, #153", as in + the spec scenario "Whole file to stdout": `a.md` in `$BATS_TEST_TMPDIR` written with + `printf`, `run_to_files`, stdout and `a.md` compared with `cmp`; verify it fails before + the implementation +- [x] 1.7 Update `test_help`: the `--keep-code` line at index 11, the `--insert`, + `--no-backup`, `--hide-footer`, `--skip-no-markers`, `--stdout` and `--skip-header` + lines at 12, 14, 15, 16, 17 and 18, and 20 lines in total; verify `--help` and + `no arguments` fail before the implementation + +## 2. Implementation + +- [x] 2.1 `gh_toc_grab`: take `$7` (document it in the comment above the function), pass + it to awk as `-v "keep_code=$7"`, skip the `` `sed` step when it is `yes`, and + add the code span loop and `text = out text` to `common_awk_script` as in design.md; + verify `make lint` passes and the test from 1.2 still passes +- [x] 2.2 `gh_toc_app`: add `local keep_code=""` next to `local bullet=""` and the + `--keep-code` check right after `--bullet`; pass `"" "$keep_code"` to `gh_toc_grab` in + the stdin branch, `"$keep_code"` to `gh_toc_stdout` as the 8th parameter and to + `gh_toc` as the 12th. `gh_toc`: take `local keep_code=${12}`, pass `"" "$keep_code"` + in the remote call and `"$keep_code"` after `"$need_replace"` in the local call. + `gh_toc_stdout`: take `local keep_code=$8`, pass `"" "$keep_code"`; verify `make lint` + passes and the tests from 1.3 to 1.6 pass +- [x] 2.3 `show_help`: add + ` --keep-code Keep inline code of headings in backticks. Default: false.` + right after the `--bullet` line; verify the tests from 1.7 pass +- [x] 2.4 Check the cases not covered by the tests: render the TOC entries of + `--keep-code` on the fixture with the markdown API and verify every `` content + is the same as in the headings; run `--keep-code --insert --no-backup --hide-footer` on + a copy of the file from 1.6 and verify the TOC in it is the same as in the output of + 1.6; run `--keep-code` with several inputs (the fixture and `test_backquote.md`) and + verify the entries have the backticks and the link prefix + +## 3. Documentation + +- [x] 3.1 README, section "Local files": after the `--bullet` example, add the sentence + and the example from design.md; add no new heading; verify the example output with + `./gh-md-toc`, and 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 + +- [x] 4.1 Run `make lint` and `make test` with `GH_TOC_TOKEN` set; verify both pass +- [x] 4.2 Run `./gh-md-toc --keep-code` and `./gh-md-toc` on the fixture in a Debian + container with mawk and with GNU awk (the files passed in with `tar` on stdin); verify + both outputs are the same as on macOS diff --git a/tests/test directory/test_keepcode.md b/tests/test directory/test_keepcode.md new file mode 100644 index 0000000..a041f53 --- /dev/null +++ b/tests/test directory/test_keepcode.md @@ -0,0 +1,23 @@ +# `Headline` + +## The command `foo1` is better + +## `a&c` + +## `` a`b `` + +## `` `a` `` + +## ``` a``b ``` + +## `< ©` + +## ` spaced ` + +## x ` ` y + +## `**kwargs**` + +## Tom & `Jerry` <3 + +## A *em `code`* and [`link`](#headline) diff --git a/tests/tests.bats b/tests/tests.bats index 7b712a0..eccaaa4 100755 --- a/tests/tests.bats +++ b/tests/tests.bats @@ -129,13 +129,14 @@ test_help() { assert_equal "${lines[8]}" " --depth Max heading level to include into TOC. Default: 0 (all levels)." assert_equal "${lines[9]}" " --numbered Number TOC entries: list (ordered list) or outline (1.1. in text). Default: none." assert_equal "${lines[10]}" " --bullet Bullet of TOC entries: *, - or +. Default: *." - 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]}" " --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" + assert_equal "${lines[11]}" " --keep-code Keep inline code of headings in backticks. Default: false." + assert_equal "${lines[12]}" " --insert Insert new TOC into original file. For local files only. Default: false." + assert_equal "${lines[14]}" " --no-backup Remove backup file. Set --insert as well. Default: false." + assert_equal "${lines[15]}" " --hide-footer Do not write date & author of the last TOC update. Set --insert as well. Default: false." + assert_equal "${lines[16]}" " --skip-no-markers Skip files without or . Set --insert as well. Default: false." + assert_equal "${lines[17]}" " --stdout Print the whole file with the new TOC to stdout. The file is not changed. Default: false." + assert_equal "${lines[18]}" " --skip-header Hide entry of the topmost headlines. Default: false." + assert_equal "${#lines[@]}" "20" } @test "--help" { @@ -849,3 +850,78 @@ gh_md_toc_no_stderr() { [ ! -s "$BATS_TEST_TMPDIR/out.md" ] assert_equal "$(cat "$BATS_TEST_TMPDIR/err.txt")" "--skip-no-markers cannot be used with --stdout." } + +@test "TOC with inline code without --keep-code for local file, #153" { + run $BATS_TEST_DIRNAME/../gh-md-toc tests/test\ directory/test_keepcode.md + assert_success + + assert_equal "${lines[2]}" '* [Headline](#headline)' + assert_equal "${lines[3]}" ' * [The command foo1 is better](#the-command-foo1-is-better)' + assert_equal "${lines[4]}" ' * [a<b>&c](#abc)' + assert_equal "${lines[5]}" ' * [a`b](#ab)' + assert_equal "${lines[6]}" ' * [`a`](#a)' + assert_equal "${lines[7]}" ' * [a``b](#ab-1)' + assert_equal "${lines[8]}" ' * [&lt; &copy;](#lt-copy)' + assert_equal "${lines[9]}" ' * [ spaced ](#--spaced--)' + assert_equal "${lines[10]}" ' * [x y](#x---y)' + assert_equal "${lines[11]}" ' * [**kwargs**](#kwargs)' + assert_equal "${lines[12]}" ' * [Tom & Jerry <3](#tom--jerry-3)' + assert_equal "${lines[13]}" ' * [A em code and link](#a-em-code-and-link)' + assert_equal "${lines[14]}" '' +} + +@test "TOC with inline code in backticks for local file, #153" { + run $BATS_TEST_DIRNAME/../gh-md-toc --keep-code tests/test\ directory/test_keepcode.md + assert_success + + assert_equal "${lines[2]}" '* [`Headline`](#headline)' + assert_equal "${lines[3]}" ' * [The command `foo1` is better](#the-command-foo1-is-better)' + assert_equal "${lines[4]}" ' * [`a&c`](#abc)' + assert_equal "${lines[5]}" ' * [``a`b``](#ab)' + assert_equal "${lines[6]}" ' * [`` `a` ``](#a)' + assert_equal "${lines[7]}" ' * [```a``b```](#ab-1)' + assert_equal "${lines[8]}" ' * [`< ©`](#lt-copy)' + assert_equal "${lines[9]}" ' * [` spaced `](#--spaced--)' + assert_equal "${lines[10]}" ' * [x ` ` y](#x---y)' + assert_equal "${lines[11]}" ' * [`**kwargs**`](#kwargs)' + assert_equal "${lines[12]}" ' * [Tom & `Jerry` <3](#tom--jerry-3)' + assert_equal "${lines[13]}" ' * [A em `code` and `link`](#a-em-code-and-link)' + assert_equal "${lines[14]}" '' +} + +@test "TOC with inline code in backticks for markdown from stdin, #153" { + cat tests/test\ directory/test_backquote.md | { + run $BATS_TEST_DIRNAME/../gh-md-toc --bullet - --keep-code - + assert_success + + assert_equal "${lines[0]}" '- [The command `foo1`](#the-command-foo1)' + assert_equal "${lines[1]}" ' - [The command `foo2` is better](#the-command-foo2-is-better)' + assert_equal "${lines[2]}" '- [The command `bar1`](#the-command-bar1)' + assert_equal "${lines[3]}" ' - [The command `bar2` is better](#the-command-bar2-is-better)' + assert_equal "${lines[4]}" ' - [The command `bar3` is the best](#the-command-bar3-is-the-best)' + assert_equal "${#lines[@]}" "5" + } +} + +@test "TOC with inline code in backticks for remote file, #153" { + run $BATS_TEST_DIRNAME/../gh-md-toc --keep-code https://github.com/ekalinin/github-markdown-toc/blob/a28f86dcb616ce17103d1dfc28f6c3ac423c388c/tests/test%20directory/test_backquote.md + assert_success + + assert_equal "${lines[2]}" '* [The command `foo1`](#the-command-foo1)' + assert_equal "${lines[3]}" ' * [The command `foo2` is better](#the-command-foo2-is-better)' + assert_equal "${lines[4]}" '* [The command `bar1`](#the-command-bar1)' + assert_equal "${lines[5]}" ' * [The command `bar2` is better](#the-command-bar2-is-better)' + assert_equal "${lines[6]}" ' * [The command `bar3` is the best](#the-command-bar3-is-the-best)' +} + +@test "Whole file with inline code in backticks to stdout, #153" { + printf '\n\n\n# The command `foo1`\n' > "$BATS_TEST_TMPDIR/a.md" + local sum_before + sum_before=$(cksum < "$BATS_TEST_TMPDIR/a.md") + + run_to_files --keep-code --hide-footer --stdout "$BATS_TEST_TMPDIR/a.md" + assert_success + + printf '\n* [The command `foo1`](#the-command-foo1)\n\n\n# The command `foo1`\n' | cmp - "$BATS_TEST_TMPDIR/out.md" + assert_equal "$(cksum < "$BATS_TEST_TMPDIR/a.md")" "$sum_before" +}