-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtool-add-seo-to-frontmatter.sh
More file actions
executable file
·47 lines (44 loc) · 1.39 KB
/
tool-add-seo-to-frontmatter.sh
File metadata and controls
executable file
·47 lines (44 loc) · 1.39 KB
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
#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: $0 <directory>"
exit 1
fi
DIR="$1"
find "$DIR" -type f \( \
-name "index.de.md" -o \
-name "index.en.md" -o \
-name "index.es.md" -o \
-name "index.fr.md" -o \
-name "index.pt.md" -o \
-name "index.ru.md" \
\) | while read -r file; do
# Check if the file contains frontmatter
if grep -q '^---' "$file"; then
# Check if 'seo:' is in the frontmatter (between first two ---)
if awk '/^---/{i++} i==1 && /seo:/{found=1} END{exit !found}' "$file"; then
echo "seo: already present in $file"
else
echo "Adding seo: block to $file"
awk '
BEGIN {in_front=0; inserted=0}
/^---/ {
if (in_front == 0) {
in_front=1
print
next
} else if (in_front == 1 && inserted == 0) {
# Insert seo block before closing ---
print "seo:\n title:\n description:"
inserted=1
print
in_front=2
next
}
}
{print}
' "$file" > "${file}.tmp" && mv "${file}.tmp" "$file"
fi
else
echo "No frontmatter in $file, skipping."
fi
done