-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathadd-update-sidebar-label.sh
executable file
·105 lines (90 loc) · 2.71 KB
/
add-update-sidebar-label.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env bash
# Check if both arguments are provided
if [ $# -ne 2 ]; then
echo "Usage: $0 <file_path> <label>"
exit 1
fi
file_path="$1"
new_label="$2"
# Check if label is empty or just quotes
if [ -z "$new_label" ] || [ "$new_label" = "\"\"" ] || [ "$new_label" = "''" ]; then
echo "Error: Label cannot be empty"
exit 1
fi
# Strip existing quotes if present, store in variable
cleaned_label=$(echo "$new_label" | sed "s/^[\"']//;s/[\"']$//")
if [ -z "$cleaned_label" ]; then
echo "Error: Label cannot be empty"
exit 1
fi
# Check if file exists
if [ ! -f "$file_path" ]; then
echo "Error: File does not exist"
exit 1
fi
# Check if filename is index.md
filename=$(basename "$file_path")
if [ "$filename" != "index.md" ]; then
echo "Error: File must be named index.md"
exit 1
fi
# Create a temporary file
temp_file=$(mktemp)
# Initialize variables
in_frontmatter=false
frontmatter_end_found=false
first_line_checked=false
sidebar_label_found=false
line_count=0
# Process the file line by line
while IFS= read -r line || [ -n "$line" ]; do
((line_count++))
# Check first line
if [ $line_count -eq 1 ]; then
if [ "$line" != "---" ]; then
echo "Error: First line must be '---'"
rm "$temp_file"
exit 1
fi
first_line_checked=true
in_frontmatter=true
echo "$line" >> "$temp_file"
continue
fi
# Process frontmatter
if $in_frontmatter; then
if [ "$line" = "---" ]; then
# If no sidebar_label was found, add it before closing delimiter
if ! $sidebar_label_found; then
printf 'sidebar_label: "%s"\n' "$cleaned_label" >> "$temp_file"
fi
echo "$line" >> "$temp_file"
in_frontmatter=false
frontmatter_end_found=true
else
# Check if line contains sidebar_label
if [[ "$line" =~ ^[[:space:]]*sidebar_label:[[:space:]]* ]]; then
# Replace existing sidebar_label with new one
printf 'sidebar_label: "%s"\n' "$cleaned_label" >> "$temp_file"
sidebar_label_found=true
else
echo "$line" >> "$temp_file"
fi
fi
else
echo "$line" >> "$temp_file"
fi
done < "$file_path"
# Check if we found the closing frontmatter delimiter
if ! $frontmatter_end_found; then
echo "Error: No closing '---' found for frontmatter"
rm "$temp_file"
exit 1
fi
# Replace original file with modified content
mv "$temp_file" "$file_path"
if $sidebar_label_found; then
echo "Successfully updated existing sidebar_label in frontmatter"
else
echo "Successfully added new sidebar_label to frontmatter"
fi