Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ Table of Contents
<!-- Created by https://github.com/ekalinin/github-markdown-toc -->
```

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
------------

Expand Down
56 changes: 47 additions & 9 deletions gh-md-toc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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 '^<!--ts-->[[:space:]]*$' "$gh_src" && grep -Eq '^<!--te-->[[:space:]]*$' "$gh_src"; then
Expand Down Expand Up @@ -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=""
Expand All @@ -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-->"
Expand Down Expand Up @@ -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[^>]*>[^<]*<\/code>/)) {
pre = substr(text, 1, RSTART - 1)
code = substr(text, RSTART, RLENGTH)
text = substr(text, RSTART + RLENGTH)
gsub(/<[^>]*>/, "", pre)
sub(/^<code[^>]*>/, "", code)
sub(/<\/code>$/, "", code)
gsub(/&lt;/, "<", code)
gsub(/&gt;/, ">", code)
gsub(/&amp;/, "\\&", 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
Expand Down Expand Up @@ -456,8 +487,8 @@ gh_toc_grab() {
# find strings that corresponds to template
$grepcmd '<h[1-6][^>]*class="heading-element".*</a' |

# remove code tags
sed 's/<code>//g' | sed 's/<\/code>//g' |
# remove code tags, unless --keep-code, see #153
if [ "$7" = "yes" ]; then cat; else sed 's/<code>//g' | sed 's/<\/code>//g'; fi |

# remove g-emoji
sed 's/<g-emoji[^>]*[^<]*<\/g-emoji> //g' |
Expand All @@ -467,7 +498,7 @@ gh_toc_grab() {
# format result line
# * $0 - whole string
# * last element of each row: "</hN" where N in (1,2,3,...)
echo $echoargs "$(awk -v "gh_url=$1" -v "depth=$3" -v "numbered=$4" -v "bullet=$5" -v "need_replace=$6" "$awkscript")"
echo $echoargs "$(awk -v "gh_url=$1" -v "depth=$3" -v "numbered=$4" -v "bullet=$5" -v "need_replace=$6" -v "keep_code=$7" "$awkscript")"
}

# perl -lpE 's/(\[[^\]]*\]\()(.*?)(\))/my ($pre, $in, $post)=($1, $2, $3) ; $in =~ s{\+}{ }g; $in =~ s{%}{\\x}g; $pre.$in.$post/ems')"
Expand Down Expand Up @@ -513,6 +544,7 @@ show_help() {
echo " --depth <NUM> Max heading level to include into TOC. Default: 0 (all levels)."
echo " --numbered <TYPE> Number TOC entries: list (ordered list) or outline (1.1. in text). Default: none."
echo " --bullet <CHAR> 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."
Expand All @@ -533,6 +565,7 @@ gh_toc_app() {
local depth=0
local numbered=""
local bullet=""
local keep_code=""
local to_stdout=""
local skip_no_markers=""

Expand Down Expand Up @@ -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) ;;
*)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -644,15 +682,15 @@ 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


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 ""
Expand Down
2 changes: 2 additions & 0 deletions openspec/changes/add-keep-code/.openspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-11-08
Loading
Loading