Skip to content

Commit 882a6f3

Browse files
committed
feat: Enhance code block file path resolution and parsing
Improve handling of file paths in code blocks by normalizing repository root paths and refining markdown parsing logic. This ensures more accurate and robust file reference detection and processing.
1 parent e801fa2 commit 882a6f3

File tree

1 file changed

+47
-15
lines changed

1 file changed

+47
-15
lines changed

docs/apply-md.md

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Overview
44

5-
The `apply-md` tool extracts code blocks from markdown (typically LLM responses) and applies them to your filesystem.
5+
The `apply-md` tool extracts code blocks from markdown (typically LLM responses) and applies them to your filesystem. It supports both relative and absolute paths, with absolute paths resolved relative to the git repository root when run within a git repository.
66

77
## Usage
88

@@ -20,17 +20,19 @@ cat llm_response.md | ./apply-md --dry-run --verbose
2020
| `--backup-dir=<dir>` | Directory for backups | "./.backups" |
2121
| `--file-marker=<regex>` | Regex to identify target files from markdown | "```[a-z]+ (.+)" |
2222
| `--skip-unchanged` | Skip files with no changes | False |
23-
| `--verbose` | Show detailed output about changes | False |
23+
| `--verbose` | Show detailed output about changes, including git root detection | False |
2424
| `--confirm` | Prompt for confirmation before applying each change | False |
2525
| `--only-files=<pattern>` | Only apply changes to files matching pattern | None |
2626
| `--ignore-files=<pattern>` | Ignore changes for files matching pattern | None |
2727

2828
## How It Works
2929

3030
1. The tool reads markdown content from stdin
31-
2. It looks for code blocks that specify a filename (e.g., ```javascript src/utils.js)
32-
3. It extracts the code from these blocks
33-
4. It applies the code to the corresponding files in your filesystem
31+
2. If run within a git repository, it detects the repository root
32+
3. It looks for code blocks that specify a filename (e.g., ```javascript src/utils.js or ```bash /apply-md)
33+
4. Absolute paths (starting with /) are resolved relative to the git root when applicable
34+
5. It extracts the code from these blocks
35+
6. It applies the code to the corresponding files in your filesystem
3436

3537
## Examples
3638

@@ -74,23 +76,53 @@ Only apply changes to specific files:
7476
cat llm_response.md | ./apply-md --only-files="*.js"
7577
```
7678

79+
### Using Absolute Paths
80+
81+
Apply changes using absolute paths relative to git root:
82+
83+
```bash
84+
echo -e "```bash /scripts/test.sh\necho 'test'\n```" | ./apply-md --create-missing
85+
```
86+
7787
## Expected Markdown Format
7888

79-
The script expects code blocks in this format:
89+
The script expects code blocks in these formats:
8090

81-
````
82-
```javascript src/utils.js
83-
function add(a, b) {
84-
return a + b;
85-
}
91+
### Relative Path
92+
```markdown
93+
```javascript src/utils.js
94+
function add(a, b) {
95+
return a + b;
96+
}
97+
```
8698
```
87-
````
8899

89-
The language specification is optional, but the filename is required.
100+
### Absolute Path (from git root)
101+
```markdown
102+
```bash /scripts/deploy.sh
103+
#!/bin/bash
104+
echo "Deploying..."
105+
```
106+
```
107+
108+
The language specification is optional, but the filename is required. Absolute paths (starting with /) will be resolved relative to the git repository root when run within a git repository.
90109

91110
## Tips
92111

93112
- Always use `--dry-run` first to preview changes
113+
- Use `--verbose` to see the detected git root and normalized paths
94114
- Consider using `--backup` for important changes
95-
- Use `--verbose` to see details about what's changing
96-
- For complex changes, use `--confirm` to review each change individually
115+
- Use `--confirm` to review each change individually
116+
- When using absolute paths, ensure you're running from within a git repository
117+
- If not in a git repository, absolute paths will be treated as system paths
118+
119+
## Git Integration
120+
121+
When run within a git repository:
122+
- Detects the repository root automatically
123+
- Converts absolute paths (e.g., `/src/main.js`) to relative paths from the git root
124+
- Shows the detected git root in verbose mode
125+
126+
If not in a git repository:
127+
- Absolute paths are treated as system absolute paths
128+
- A warning is shown in verbose mode

0 commit comments

Comments
 (0)