Skip to content

Commit d4c2f4c

Browse files
testac974claude
andcommitted
Fix PDF TOC for chapters 3-5: handle inconsistent heading levels
- Add strip_first_h1 to remove redundant H1 titles from section content - Update shift_headings to shift ## to #### (skip ### level) to avoid conflicts with injected section titles - Chapters 3-5 use ## for internal headings vs ### in chapters 1-2 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent b9729ba commit d4c2f4c

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

scripts/build-pdf.sh

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,26 @@ strip_frontmatter() {
6161
' "$file"
6262
}
6363

64-
# Function to shift markdown headings down by one level (## -> ###, ### -> ####, etc.)
65-
# This ensures content headings are subordinate to section headings
64+
# Function to strip the first H1 heading from content if it exists
65+
# Some section files have "# Title" at the top which is redundant with our injected heading
66+
strip_first_h1() {
67+
awk '
68+
found==0 && /^# / { found=1; next } # Skip first H1 heading found
69+
{ print }
70+
'
71+
}
72+
73+
# Function to shift markdown headings down to be subordinate to section headings (###)
74+
# - ## becomes #### (skip ### to avoid conflict with section titles)
75+
# - Other headings shift by one level
6676
shift_headings() {
67-
sed 's/^#/##/'
77+
awk '
78+
/^#### / { sub(/^#### /, "##### "); print; next }
79+
/^### / { sub(/^### /, "##### "); print; next }
80+
/^## / { sub(/^## /, "#### "); print; next }
81+
/^# / { sub(/^# /, "## "); print; next }
82+
{ print }
83+
'
6884
}
6985

7086
# Initialize combined.md with book metadata for Pandoc
@@ -159,8 +175,8 @@ for part_dir in book/part1-foundations book/part2-playbook book/part3-patterns-t
159175
echo "### ${section_title}" >> output/combined.md
160176
echo "" >> output/combined.md
161177

162-
# Add section content (without frontmatter, with headings shifted down one level)
163-
strip_frontmatter "$section_file" | shift_headings >> output/combined.md
178+
# Add section content (without frontmatter, strip redundant H1, shift headings down)
179+
strip_frontmatter "$section_file" | strip_first_h1 | shift_headings >> output/combined.md
164180
done
165181
fi
166182
rm -f "$temp_file"
@@ -174,7 +190,7 @@ for part_dir in book/part1-foundations book/part2-playbook book/part3-patterns-t
174190
echo "" >> output/combined.md
175191
echo "## ${chapter_title}" >> output/combined.md
176192
echo "" >> output/combined.md
177-
strip_frontmatter "$chapter_file" | shift_headings >> output/combined.md
193+
strip_frontmatter "$chapter_file" | strip_first_h1 | shift_headings >> output/combined.md
178194
done
179195
done
180196

0 commit comments

Comments
 (0)