astro-grab
v0.2.1
Published
Grab context for coding agents directly from your astro site.
Downloads
168
Maintainers
Readme
Astro Grab (Alpha)
Visual element targeting for Astro projects - Hold Cmd/Ctrl+G to target any element and copy its source code to your clipboard.
What is Astro Grab?
Astro Grab is a dev-only tool that helps you quickly locate and grab source code from your Astro components. Simply hold Cmd+G (Mac) or Ctrl+G (Windows/Linux) for one second, then click any element to copy its source snippet to your clipboard - perfect for pasting into AI assistants like Claude.
Features (Alpha)
- 🎯 Visual targeting mode: Hold Cmd/Ctrl+G to enter targeting mode with visual highlights
- 📋 Instant clipboard copy: Click any element to copy its source code snippet
- 🔍 Source attribution: Automatically tracks elements to their .astro files
- ⚡ Dev-only: Zero impact on production builds
- 🎨 Non-intrusive: Overlay doesn't interfere with page interaction
Quick Start
Installation
bun installDevelopment
Start all package watchers and the demo app:
bun run devThis will:
- Build and watch all packages in
packages/ - Start the Astro dev server for the demo in
apps/demo - Enable hot reload for client code changes
Builds are optimized with Turborepo:
- Automatic parallel execution when dependencies allow
- Local caching speeds up subsequent builds
- Dependency graph ensures correct build order
Visit http://localhost:4321 and try the targeting mode:
- Hold Cmd+G (Mac) or Ctrl+G (Win/Linux) for 1 second
- Move your mouse over elements to see them highlighted
- Click an element to copy its source code
- Press Escape to exit targeting mode
Building
Build all packages with Turborepo:
bun run buildTurborepo automatically:
- Builds packages in dependency order (shared → server/client → integration)
- Caches build outputs for faster rebuilds
- Runs packages in parallel when dependencies allow
Build specific packages:
bun run turbo run build --filter astro-grab-sharedTesting
Run all tests:
bun run testWatch mode:
bun run test:watchRun tests in specific package:
bun run turbo run test --filter astro-grab-sharedProject Structure
This is a Bun monorepo with the following packages:
packages/astro-grab: Main Astro integration (what users install)packages/astro-grab-client: Client-side overlay and keybind logicpackages/astro-grab-server: Vite dev server middleware and snippet endpointpackages/astro-grab-shared: Shared types and utilitiesapps/demo: Demo Astro site for testing
Usage in Your Project
- Install the integration:
bun add astro-grab- Add to your
astro.config.mjs:
import { defineConfig } from "astro/config";
import { astroGrab } from "astro-grab";
export default defineConfig({
integrations: [
astroGrab({
enabled: true, // Enable in dev mode (default: true)
holdDuration: 1000, // Hold time in ms (default: 1000)
contextLines: 10, // Lines of context around target (default: 5)
template: `Source: {{file}}:{{targetLine}}\n\n\`\`\`{{language}}\n{{snippet}}\n\`\`\``, // Custom clipboard template
}),
],
});Custom Clipboard Template
You can customize the format of the copied snippet using the template option with {{variable}} interpolation:
astroGrab({
template: `File: {{file}}
Line: {{targetLine}}
{{snippet}}`,
});Available variables:
| Variable | Description | Example |
| ---------------- | -------------------------- | --------------------------- |
| {{file}} | Path to the .astro file | src/components/Card.astro |
| {{snippet}} | The extracted code snippet | <div>...</div> |
| {{startLine}} | First line of snippet | 12 |
| {{endLine}} | Last line of snippet | 18 |
| {{targetLine}} | The line clicked on | 15 |
| {{language}} | Language identifier | astro |
- Run your dev server:
npm run dev- Hold Cmd/Ctrl+G and click elements to grab their source!
Production Usage (Demo Sites)
By default, astro-grab only works in development mode. For demo sites or production deployments that need to showcase the feature, you can force-enable it:
# Build with astro-grab enabled
ASTRO_GRAB_DANGEROUSLY_FORCE_ENABLE=true npm run build⚠️ Security Warning: This exposes source code snippets to end users. Only enable this on demo sites where sharing component source is intentional.
How It Works
- Instrumentation: During dev, the Vite plugin transforms
.astrofiles to adddata-astro-grab="file:line:col"attributes to HTML elements - Client injection: The integration automatically injects a client script that listens for Cmd/Ctrl+G
- Targeting mode: Holding the keybind for 1s activates an overlay that highlights elements under the cursor
- Snippet extraction: Clicking an element fetches a code snippet from the dev server and copies it to clipboard
Known Limitations (Alpha)
- HMR: Client changes require page reload; server/integration changes require dev server restart
- Parser: Only
.astrofiles (no.jsx,.vue, etc.) - Snippets: Fixed context window (±40 lines), no dependency resolution
- Browser: Requires modern browser with Clipboard API (HTTPS or localhost)
Development
See DEVELOPMENT.md for detailed development instructions, architecture notes, and contribution guidelines.
License
MIT
