/
wiki
executable file
·82 lines (70 loc) · 1.67 KB
/
wiki
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
#!/bin/bash
set -euo pipefail
# Don't set `IFS=$'\n\t'`, because a subshell appears to join multiple lines of
# output into a single space separated line (see the
# `format_staged_markdown_files`'s `FILES` variable and subshell command below)
function format_staged_markdown_files() {
STAGED=$(git diff --name-only --staged)
if [ -z "$STAGED" ]; then
return
fi
echo Formatting files...
FILES=$(echo $STAGED | grep --color=never -e ".md$")
# Still a few issues remaining before this can be enabled
# fr-md-format.ts $FILES
}
function commit() {
git add .
format_staged_markdown_files
git add .
git commit --allow-empty-message -m ""
if [ $? == 0 ]; then
git pull origin master
git push origin master
fi
}
function open_in_editor() {
FILE=$1
pushd . > /dev/null
cd ~/wiki
if [[ $OPEN_FZF -eq 1 ]]; then
nvim $FILE -c "FZF"
else
nvim $FILE
fi
commit
popd > /dev/null
}
function commit_changes() {
pushd . > /dev/null
cd ~/wiki
commit
popd > /dev/null
}
function print_help() {
echo "Usage: $0 [options] [file]"
echo
echo "Options:"
echo "-h print help"
echo "-c commit changes without editing"
echo "--fzf open fzf fuzzy file finder when editor opens"
}
# Set terminal title
PROMPT_COMMAND=
echo -en "\033]0;Wiki\a"
OPEN_FZF=
FILE=index.md
if [ $# -ge 1 ]; then
if [ "$1" == "-h" ]; then
print_help
exit 0
elif [ "$1" == "-c" ]; then
commit_changes
exit 0
elif [ "$1" == "--fzf" ]; then
OPEN_FZF=1
elif [ ! -z "$1" ]; then
FILE="$1"
fi
fi
open_in_editor $FILE