1+ #! /bin/bash
2+
3+ # This script adds TOC markers to markdown files that have a "## Table of Contents" heading
4+
5+ # Check if a file path is provided
6+ if [ " $# " -eq 0 ]; then
7+ echo " Usage: $0 <file.md> [file2.md ...]"
8+ echo " Example: $0 group-syllabus/*.md"
9+ exit 1
10+ fi
11+
12+ # Process each file provided as an argument
13+ for file in " $@ " ; do
14+ # Check if file exists
15+ if [ -f " $file " ]; then
16+ echo " Processing $file ..."
17+
18+ # Check if the file already has TOC markers
19+ if grep -q " <!-- toc -->" " $file " ; then
20+ echo " TOC markers already exist in $file , skipping..."
21+ continue
22+ fi
23+
24+ # Find the line with "## Table of Contents"
25+ toc_line=$( grep -n " ^## Table of Contents" " $file " | cut -d: -f1)
26+
27+ if [ -n " $toc_line " ]; then
28+ # Create a temporary file
29+ temp_file=$( mktemp)
30+
31+ # Extract content before TOC line
32+ head -n " $toc_line " " $file " > " $temp_file "
33+
34+ # Add TOC markers
35+ echo " <!-- toc -->" >> " $temp_file "
36+ echo " <!-- tocstop -->" >> " $temp_file "
37+
38+ # Extract content after TOC line
39+ tail -n +$(( toc_line + 1 )) " $file " >> " $temp_file "
40+
41+ # Replace original file
42+ mv " $temp_file " " $file "
43+
44+ echo " Added TOC markers to $file "
45+ else
46+ echo " No '## Table of Contents' found in $file "
47+ fi
48+ else
49+ echo " File $file does not exist"
50+ fi
51+ done
52+
53+ echo " Done adding TOC markers"
0 commit comments