md-codeblock-patcher
v1.0.0
Published
Zero-dependency markdown parser to extract code blocks and apply them as direct file overwrites or SEARCH/REPLACE diff patches.
Maintainers
Readme
md-codeblock-patcher
LLM Code Block Applier: A zero-dependency utility that extracts fenced code blocks from markdown responses and applies them directly to the workspace as full overwrites or git-like SEARCH/REPLACE diff patches.
⚡ Features
- Zero Runtime Dependencies: Ultra lightweight and fast.
- FilePath Annotation Extraction: Automatically resolves targets from inline code comments (e.g.,
// filepath: path/to/fileor<!-- filepath: ... -->). - SEARCH/REPLACE Patch Parsing: Applies standard search/replace diff chunks dynamically onto local files without corrupting code.
- Whole File Overwrites: Safely creates or overwrites target files with clean code outputs.
📦 Installation
npm install md-codeblock-patcher🚀 Usage
1. CLI Usage
Extract and apply patches from a markdown log file:
npx md-codeblock-patcher response.md
# Set base workspace directory
npx md-codeblock-patcher response.md --dir ./packages/app2. Programmatic API
import { extractCodeBlocks, applyBlockPatch } from 'md-codeblock-patcher';
const llmResponse = `
Here is the fix for your utility file:
\`\`\`typescript
<<<<<<< SEARCH
export function add(a, b) {
return a + b;
}
=======
export function add(a: number, b: number): number {
return a + b;
}
>>>>>>> REPLACE
\`\`\`
`;
const blocks = extractCodeBlocks(llmResponse);
// Manually assign filepath if not present in markdown comments
blocks[0].filePath = "src/math.ts";
const success = await applyBlockPatch(blocks[0], process.cwd());
if (success) {
console.log("Successfully patched src/math.ts");
}📄 License
MIT License.
