-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown-append-chapter
More file actions
executable file
·219 lines (177 loc) · 5.9 KB
/
Copy pathmarkdown-append-chapter
File metadata and controls
executable file
·219 lines (177 loc) · 5.9 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/bin/bash
SCRIPT_NAME="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
LAST_LINES_TO_SHOW=20
LAST_HEADERS_LINES_TO_SHOW=7
MARKDOWN_EDIT_LAST_FILES_PATH="$HOME/.markdown-edit-last-files"
MAX_HISTORY_LINES=10
function echoerr {
printf "%s\n" "$*" >&2;
}
function help_exit { EXIT_CODE="$1";
if [[ -z "$EXIT_CODE" ]]; then
EXIT_CODE=1
fi
echoerr "Usage: ${SCRIPT_NAME} [-c|--clipboard] [TARGET_FILENAME]"
exit "$EXIT_CODE"
}
function ask_continue {
local ACTION_NAME=$1
if [[ -z "${ACTION_NAME}" ]]; then
ACTION_NAME="continue"
fi
read -n 1 -r -p "Press \"y\" to ${ACTION_NAME}: " input
echo >&2
if [ "$input" != "Y" ] && [ "$input" != "y" ] && [ "$input" != "д" ] && [ "$input" != "Д" ]; then
exit
fi
echo 1
}
function select_file_via_fzf {
local TX_DIR="$HOME/tx"
cd "$TX_DIR" || {
echoerr "Error: directory $TX_DIR not found"
exit 100
}
# Get all .md and .txt files
local ALL_FILES=$(find . -type f \( -name "*.md" -o -name "*.txt" \) | sort)
# Reorder files based on history
local REORDERED_FILES="$ALL_FILES"
if [[ -f "$MARKDOWN_EDIT_LAST_FILES_PATH" ]]; then
while read HISTORY_LINE; do
if [[ -n "$HISTORY_LINE" ]]; then
local MATCHING=$(echo "$REORDERED_FILES" | grep -F "$HISTORY_LINE")
local NON_MATCHING=$(echo "$REORDERED_FILES" | grep -vF "$HISTORY_LINE")
if [[ -n "$MATCHING" ]]; then
REORDERED_FILES=$(printf "%s\n%s" "$MATCHING" "$NON_MATCHING")
fi
fi
done < "$MARKDOWN_EDIT_LAST_FILES_PATH"
fi
# Show fzf prompt
local SELECTED=$(echo "$REORDERED_FILES" | fzf --prompt="Select markdown/txt file to append fragment into: ")
if [[ -z "$SELECTED" ]]; then
echoerr "No file selected"
exit 200
fi
# Return relative path (remove leading ./)
echo "${SELECTED#./}"
}
function add_modified_file_to_history {
local FILEPATH="$1"
# Create history file if it doesn't exist
if [[ ! -f "$MARKDOWN_EDIT_LAST_FILES_PATH" ]]; then
touch "$MARKDOWN_EDIT_LAST_FILES_PATH"
fi
# Remove the filepath if it already exists (to avoid duplicates)
CURRENT_HISTORY_CONTENTS=$(echo "$CURRENT_HISTORY_CONTENTS" | grep -vF "$FILEPATH")
# Read current contents, keep only last MAX_LINES-1
local CURRENT_HISTORY_CONTENTS=$(cat "$MARKDOWN_EDIT_LAST_FILES_PATH" | tail -n $((MAX_HISTORY_LINES - 1)))
# Write back and append new filepath
echo "$CURRENT_HISTORY_CONTENTS" > "$MARKDOWN_EDIT_LAST_FILES_PATH"
echo "$FILEPATH" >> "$MARKDOWN_EDIT_LAST_FILES_PATH"
}
CLIPBOARD_MODE=
TARGET_FILENAME=
while [[ $# -gt 0 ]]; do
OPTION="$1"
case "$OPTION" in
-c|--clipboard)
CLIPBOARD_MODE=1
;;
-h|--help)
help_exit
;;
*)
if [[ "${OPTION:0:1}" == "-" ]]; then
echoerr "Error: unknown option \"$OPTION\""
help_exit
else
break
fi
;;
esac
shift
done
# Get TARGET_FILENAME from argument or fzf
TARGET_FILENAME="$1"
# Read input fragment
if [[ "$CLIPBOARD_MODE" -eq 1 ]]; then
FRAGMENT=$(xclip -selection c -o)
else
if [ ! -t 0 ]; then
FRAGMENT=$(< /dev/stdin)
else
echoerr "Error: pipe input is empty. Use --clipboard flag if you want to get fragment from clipboard"
help_exit 1
fi
fi
if [[ -z "$TARGET_FILENAME" ]]; then
# cd to TX_DIR for fzf selection (relative paths)
TX_DIR="$HOME/tx"
cd "$TX_DIR" || {
echoerr "Error: directory $TX_DIR not found"
exit 300
}
TARGET_FILENAME=$(select_file_via_fzf)
if [[ -z "$TARGET_FILENAME" ]]; then
echoerr "Error: no file selected. Exitting"
exit 350
fi
echo "You selected \"$TARGET_FILENAME\""
fi
# Check if file exists
if [[ ! -f "$TARGET_FILENAME" ]]; then
echoerr "Error: file \"$TARGET_FILENAME\" does not exist"
exit 400
fi
# Remove empty lines at the beginning and end
FRAGMENT=$(echo "$FRAGMENT" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}')
# Find FIRST_URL in the fragment
FIRST_URL=$(echo "$FRAGMENT" | grep -oE 'https?://[^ ]+' | head -n 1)
# If fragment doesn't start with '#', process it through markdown-fix-header
if [[ "${FRAGMENT:0:1}" != "#" ]]; then
# Save to clipboard
echo "$FRAGMENT" | xclip -selection c
# Pass through markdown-fix-header
# FRAGMENT_FIXED_HEADER=$($SCRIPT_DIR/markdown-fix-header -c <<< "$FRAGMENT")
FRAGMENT_FIXED_HEADER=$(echo -n -e "$FRAGMENT" | $SCRIPT_DIR/markdown-fix-header --force)
if [[ $? -ne 0 ]]; then
echoerr "Warning: markdown-fix-header failed"
# exit 200
else
FRAGMENT="$FRAGMENT_FIXED_HEADER"
fi
# Re-read from clipboard after markdown-fix-header processing
# FRAGMENT=$(xclip -selection c -o)
# Find FIRST_URL again after processing
FIRST_URL=$(echo "$FRAGMENT" | grep -oE 'https?://[^ ]+' | head -n 1)
fi
# Check if FIRST_URL already exists in target file
if [[ -n "$FIRST_URL" ]]; then
if grep -qF "$FIRST_URL" "$TARGET_FILENAME"; then
ask_continue "add fragment with existing URL \"$FIRST_URL\"" > /dev/null
fi
fi
# Write fragment to file
# Remove trailing empty lines using for loop (1 to 10 iterations)
for TRAILING_CHAR_NUM in $(seq 1 10); do
FILE_LAST_SYMBOL=$(tail -c 1 "$TARGET_FILENAME")
if [[ "$FILE_LAST_SYMBOL" != "" ]]; then
break
fi
# Remove last character (empty line)
truncate -s -1 "$TARGET_FILENAME"
done
# Add new line and fragment
echo -e "\n" >> "$TARGET_FILENAME"
echo -n "$FRAGMENT" >> "$TARGET_FILENAME"
add_modified_file_to_history "$TARGET_FILENAME"
echo -e "\n=== Last $LAST_HEADERS_LINES_TO_SHOW: lines of \"$TARGET_FILENAME\" after fragment add: ==="
echo ". . . . ."
"$SCRIPT_DIR/markdown-headers.sh" -n "$TARGET_FILENAME" \
| tail -n "$LAST_HEADERS_LINES_TO_SHOW"
echo -e "\n=== Last $LAST_LINES_TO_SHOW: lines of \"$TARGET_FILENAME\" after fragment add: ==="
echo ". . . . ."
tail -n "$LAST_LINES_TO_SHOW" "$TARGET_FILENAME"
"$SCRIPT_DIR/obsidian-open" "$TARGET_FILENAME"