summaryrefslogtreecommitdiff
path: root/bin/extra/notes_index
blob: 9ccb3b2e96617233392d1767c79203bf6220ae50 (plain)
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
#!/bin/sh

NOTES="$HOME"/notes

if ! [ -d "$NOTES" ]; then
    >&2 printf 'No notes directory.\n'
    exit 1
fi

cd "$NOTES"

find . -type d | while read -r d
do
    cd "$d"
    printf '# Notes\n' > index.md
    find . \
		-maxdepth 1 \
		-type f \
		-name '*.md' \
		-not \
		-name 'index.md' \
		-printf '%f ' \
		-exec grep '^# ' -m1 {} \; |
            sed 's/\(.\+\.md\) # \(.\+\)/- [\2](\1)/' |
            sort -t '[' -k 2 >> index.md

    if [ "$(find . -mindepth 2 -maxdepth 2 -type f -name '*.md' | wc -l)" -gt 0 ]
    then
        printf '# Dirs\n' >> index.md
        find . \
            -mindepth 1 \
            -maxdepth 1 \
            -type d \
            -not -name '.*'\
            -printf '- [%f](%f/index.md)\n' | 
                sort -t '[' -k 2 >> index.md
    fi

    cd - > /dev/null
done