-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-post.sh
executable file
·34 lines (28 loc) · 1.08 KB
/
find-post.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
#!/bin/bash
# Generated by Google Gemini 1.5 on 2025-01-02
# Function to extract the title from a file
extract_title() {
grep "^title =" "$1" | sed 's/^title = "//' | sed 's/"$//' | tr -d '\r'
}
# Check if the "content" directory exists
if [[ ! -d "content/posts" ]]; then
echo "Error: 'content/posts' directory not found." >&2
exit 1
fi
# Find all .md files in the "content" directory recursively
find content -type f -name "*.md" 2>/dev/null | while read file; do
title=$(extract_title "$file")
# Skip files with 4 or 6 character ISO date titles
if [[ ! -z "$title" ]] && [[ ! "$title" =~ ^[0-9]{4}(-[0-9]{2})?$ ]]; then
printf "%s\t%s\n" "$title" "$file"
fi
done | {
selected=$(fzf --delimiter='\t' --layout=reverse --preview 'cat {2}' --preview-window='right:50%')
if [[ -n "$selected" ]]; then
filepath=$(echo "$selected" | awk -F'\t' '{print $2}')
# Open the file in the editor using exec or by redirecting to /dev/tty
#exec "$EDITOR" "$filepath" # Most robust solution
#OR
"$EDITOR" "$filepath" </dev/tty >/dev/tty 2>/dev/tty # Alternative solution
fi
}