-
Notifications
You must be signed in to change notification settings - Fork 576
/
new-page.sh
executable file
·88 lines (79 loc) · 2.09 KB
/
new-page.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
#!/usr/bin/env bash
delete_mode=0
if [ "${1}" = "-d" ]; then
delete_mode=1
shift
fi
if [ -z "${1}" ] || [ "${1}" = "-h" ] || [ "${1}" = "-help" ] || [ "${1}" = "--help" ]; then
echo "Usage: ${0} [-d] <page-path> [<page title>]"
echo "Examples:"
echo "${0} my-page \"My Page Title\""
echo "${0} post/my-post \"My Post Title\""
echo "${0} -d my-page"
exit 1
fi
path=$(dirname "${1}")
slug=$(basename "${1}")
filename="${slug}.md"
if [ "${delete_mode}" -eq 0 ]; then
if [ -z "${2}" ]; then
echo "Error: Page title is required for creating a page."
exit 1
fi
title="${2}"
date=$(date +%Y-%m-%d)
fi
for i in $(find . -type d -path './content/*' -maxdepth 2); do
if [ "${delete_mode}" -eq 1 ]; then
# Delete mode
if [ -f "${i}/${path}/${filename}" ]; then
rm "${i}/${path}/${filename}"
echo "Deleted page: ${i}/${path}/${filename}"
fi
# Optionally delete the directory if it's empty and not 'post'
if [ -d "${i}/${path}" ] && [ "${path}" != "post" ] && [ "$(ls -A "${i}/${path}")" = "" ]; then
rmdir "${i}/${path}"
echo "Deleted empty directory: ${i}/${path}"
fi
else
# Create mode
# Create directory if it doesn't exist and path is not 'post'
if [ ! -d "${i}/${path}" ] && [ "${path}" != "post" ]; then
mkdir -p "${i}/${path}"
echo "Created directory: ${i}/${path}"
fi
if [ "${i}" = "./content/en" ]; then
# Create the new page for English content
if [ ! -f "${i}/${path}/${filename}" ]; then
cat > "${i}/${path}/${filename}" << EOF
---
title: ${title}
slug: ${slug}
date: ${date}
lastmod: ${date}
show_lastmod: false
---
EOF
echo "Created page: ${i}/${path}/${filename}"
fi
else
# For non-English content (translations)
if [ "${path}" = "post" ]; then
continue
fi
if [ ! -f "${i}/${path}/${filename}" ]; then
cat > "${i}/${path}/${filename}" << EOF
---
title: ${title}
slug: ${slug}
date: ${date}
lastmod: ${date}
show_lastmod: false
untranslated: 1
---
EOF
echo "Created page: ${i}/${path}/${filename}"
fi
fi
fi
done