|
71 | 71 | PAGES_URL="https://${OWNER}.github.io/${REPO_NAME}/" |
72 | 72 | fi |
73 | 73 |
|
74 | | - # Minimal Pandoc template (now includes $toc$) |
| 74 | + # Minimal Pandoc template — CORRECT TOC PLACEHOLDER |
75 | 75 | cat > pandoc_template.html <<'TPL' |
76 | 76 | <!doctype html> |
77 | 77 | <html> |
|
84 | 84 | $for(include-before)$ |
85 | 85 | $include-before$ |
86 | 86 | $endfor$ |
87 | | - $toc$ |
| 87 | +
|
| 88 | + $if(toc)$ |
| 89 | + <nav id="TOC" role="doc-toc"> |
| 90 | + $table-of-contents$ |
| 91 | + </nav> |
| 92 | + $endif$ |
| 93 | +
|
88 | 94 | $body$ |
| 95 | +
|
89 | 96 | $for(include-after)$ |
90 | 97 | $include-after$ |
91 | 98 | $endfor$ |
@@ -133,7 +140,7 @@ jobs: |
133 | 140 | if [ -f README.md ]; then |
134 | 141 | echo "Generating index.html from README.md with a TOC..." |
135 | 142 | rm -f index.html |
136 | | - pandoc README.md -f markdown -t html -s \ |
| 143 | + pandoc README.md -f gfm+yaml_metadata_block -t html -s \ |
137 | 144 | --template=pandoc_template.html \ |
138 | 145 | -o index.html \ |
139 | 146 | --metadata title="$REPO_HTML_TITLE" \ |
@@ -183,30 +190,25 @@ jobs: |
183 | 190 | grep -niE '<nav[^>]+(id="TOC"|role="doc-toc"|class="[^"]*toc[^"]*")' index.html || echo "TOC not found pre-extraction" |
184 | 191 |
|
185 | 192 | # === Extract Pandoc TOC and move it into the sidebar === |
186 | | - # Try several known selectors for Pandoc's TOC output |
187 | 193 | TOC_INNER=$(perl -0777 -ne 'if (m|<(nav|div)\s+[^>]*(id="TOC"|role="doc-toc")[^>]*>(.*?)</\1>|si){print $3} elsif (m|<nav\s+id="TOC"[^>]*>(.*?)</nav>|si){print $1} elsif (m|<nav\s+role="doc-toc"[^>]*>(.*?)</nav>|si){print $1}' index.html || true) |
188 | 194 |
|
189 | | - # Count how many README headings will be eligible (for diagnostics) |
| 195 | + # Count README headings (diagnostics) |
190 | 196 | if [ -f README.md ]; then |
191 | 197 | HCOUNT=$(grep -E '^\s{0,3}#{1,6}\s' README.md | wc -l | tr -d ' ') |
192 | 198 | else |
193 | 199 | HCOUNT=0 |
194 | 200 | fi |
195 | 201 |
|
196 | 202 | if [ -n "$TOC_INNER" ]; then |
197 | | - # Remove any heading labels inside the toc block |
198 | 203 | TOC_CLEAN=$(printf "%s" "$TOC_INNER" | perl -0777 -pe 's|<h[1-6][^>]*>.*?</h[1-6]>||si') |
199 | 204 |
|
200 | | - # Inject into sidebar: prefer placeholder; otherwise replace nav.toc contents |
201 | 205 | if grep -q "<!--SIDEBAR_LINKS-->" index.html; then |
202 | 206 | awk -v block="$TOC_CLEAN" 'BEGIN{IGNORECASE=1} |
203 | 207 | { sub(/<!--SIDEBAR_LINKS-->/, block); print }' index.html > index.html.tmp && mv index.html.tmp index.html |
204 | 208 | else |
205 | | - # Replace whatever is inside <nav class="toc">...</nav> with the fresh TOC |
206 | 209 | perl -0777 -pe 's|(<nav\s+class="toc"[^>]*>).*?(</nav>)|$1'"$TOC_CLEAN"'$2|si' -i index.html |
207 | 210 | fi |
208 | 211 |
|
209 | | - # Remove original TOC from main content if it exists |
210 | 212 | perl -0777 -pe 's|<(nav|div)\s+(id="TOC"|role="doc-toc")[^>]*>.*?</\1>||si' -i index.html |
211 | 213 |
|
212 | 214 | echo "TOC extracted and injected; README headings: $HCOUNT" |
@@ -250,7 +252,6 @@ jobs: |
250 | 252 | OWNER: ${{ github.repository_owner }} |
251 | 253 | run: | |
252 | 254 | set -e |
253 | | -
|
254 | 255 | REPO_NAME="${REPO#*/}" |
255 | 256 | REPO_URL="https://github.com/$REPO" |
256 | 257 | if [ "$REPO_NAME" = "${OWNER}.github.io" ]; then |
|
0 commit comments