Skip to content

Commit e5dea16

Browse files
committed
fix(setup): search PATH for .md files in suffix alias handler
The _handle_md function now searches PATH directories when a .md file isn't found in the current directory, enabling agents in ~/.mdflow to be run by name from anywhere. Also fixes the PATH cleanup regex to preserve ~/.mdflow (3 segments) while removing project .mdflow dirs (4+ segments).
1 parent bba7092 commit e5dea16

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

src/setup.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,48 @@ const SHELL_SNIPPET = `
77
# mdflow: Treat .md files as executable agents
88
alias -s md='_handle_md'
99
_handle_md() {
10-
local file="$1"
10+
local input="$1"
1111
shift
12-
# Pass file and any remaining args (--model, --silent, etc.) to handler
12+
13+
# Resolve input: URLs pass through, files check current dir then PATH
14+
local resolved=""
15+
if [[ "$input" =~ ^https?:// ]]; then
16+
# URL - pass through as-is
17+
resolved="$input"
18+
elif [[ -f "$input" ]]; then
19+
resolved="$input"
20+
else
21+
# Search PATH for the .md file
22+
local dir
23+
for dir in \${(s/:/)PATH}; do
24+
if [[ -f "$dir/$input" ]]; then
25+
resolved="$dir/$input"
26+
break
27+
fi
28+
done
29+
fi
30+
31+
if [[ -z "$resolved" ]]; then
32+
echo "File not found: $input (checked current dir and PATH)"
33+
return 1
34+
fi
35+
36+
# Pass resolved file/URL and any remaining args to handler
1337
if command -v mdflow &>/dev/null; then
14-
mdflow "$file" "$@"
38+
mdflow "$resolved" "$@"
1539
else
16-
echo "mdflow not installed. Install with: bun add -g mdflow"
17-
echo "Attempting to install now..."
18-
if command -v bun &>/dev/null; then
19-
bun add -g mdflow && mdflow "$file" "$@"
20-
elif command -v npm &>/dev/null; then
21-
npm install -g mdflow && mdflow "$file" "$@"
40+
echo "mdflow not installed."
41+
read -q "REPLY?Would you like to run \\\`bun add -g mdflow\\\` now? [y/N] "
42+
echo
43+
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
44+
bun add -g mdflow
45+
echo
46+
read -q "REPLY?Would you like to attempt to run this again? [y/N] "
47+
echo
48+
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
49+
mdflow "$resolved" "$@"
50+
fi
2251
else
23-
echo "Neither bun nor npm found. Please install mdflow manually."
2452
return 1
2553
fi
2654
fi
@@ -41,9 +69,9 @@ export PATH="$HOME/.mdflow:$PATH"
4169
# This function runs on each directory change to update PATH dynamically
4270
_mdflow_chpwd() {
4371
# Remove project .mdflow paths from PATH, but keep ~/.mdflow (user agents)
44-
# Project paths have format: /path/to/project/.mdflow (more than 3 segments)
45-
# User path has format: /Users/name/.mdflow (exactly 3 segments)
46-
PATH=$(echo "$PATH" | tr ':' '\\n' | grep -vE '^(/[^/]+){3,}/\\.mdflow$' | tr '\\n' ':' | sed 's/:$//')
72+
# Project paths: /path/to/project/.mdflow (4+ segments)
73+
# User path: /Users/name/.mdflow (3 segments) - keep this one
74+
PATH=$(echo "$PATH" | tr ':' '\\n' | grep -vE '^(/[^/]+){4,}/\\.mdflow$' | tr '\\n' ':' | sed 's/:$//')
4775
# Add current directory's .mdflow if it exists
4876
if [[ -d ".mdflow" ]]; then
4977
export PATH="$PWD/.mdflow:$PATH"

0 commit comments

Comments
 (0)