-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown-index
More file actions
executable file
·46 lines (39 loc) · 1.5 KB
/
markdown-index
File metadata and controls
executable file
·46 lines (39 loc) · 1.5 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
#!/bin/bash
md_dir="$(sed 's/\([^\/]\)$/\1\//g' <<< ${1-"."})"
escaped_md_dir=$(echo "${md_dir}" | sed "s/[!@#$%^&*\/\.-]/\\\&/g")
dir_name=$(basename $(realpath "$md_dir"))
dir_display_name=$(sed "s/[_-]/ /g; s/\(^.\)/\U&/g" <<< "$dir_name")
echo "Writing index for: $md_dir ($dir_display_name)"
echo "# $dir_display_name" > "$md_dir/index.md"
# First, markdown files
md_files_count=$(\
find "$md_dir" -maxdepth 1 -type f -name '*.md' -not -name "index.md" \
| wc -l\
)
if [ "$md_files_count" -gt 0 ]
then
echo -e "\n## Pages" >> "$md_dir/index.md"
echo "Found pages:"
for file in $(find "$md_dir" -maxdepth 1 -type f -name '*.md' -not -name "index.md" ); do
page_name=$(sed "s/$escaped_md_dir//g; s/\.md//g" <<< "$file")
display_name=$(sed "s/[_-]/ /g; s/\(^.\)/\U&/g" <<< "$page_name")
echo "$display_name ($file)"
echo "### [$display_name]($page_name)" >> "$md_dir/index.md"
done
fi
# and then dirs
subdircount=$(\
find "$md_dir" -type f -name 'index.md' -not -path "${md_dir}index.md" \
| wc -l\
)
if [ "$subdircount" -gt 0 ]
then
echo -e "\n## Sections" >> "$md_dir/index.md"
echo "Found sub indexes:"
for file in $(find "$md_dir" -maxdepth 2 -type f -name 'index.md' -not -path "${md_dir}index.md"); do
section_index=$(sed "s/$escaped_md_dir//g; s/\.md//g" <<< "$file")
display_name=$(dirname "$section_index" | sed "s/\///g; s/[_-]/ /g; s/\(^.\)/\U&/g")
echo "$display_name ($file)"
echo "### [$display_name]($section_index)" >> "$md_dir/index.md"
done
fi