This file provides guidance to AI agents when working with code in this repository.
Before making architectural changes, consult .docs/summary.md
This comprehensive document (658 lines, 13 sections) provides deep analysis of:
- Project Overview (tech stack, features, purpose)
- Architecture Analysis (dual build system, component structure)
- Build System Deep Dive (Trunk vs. wasm-pack+Vite)
- Core Functionality Analysis (markdown conversion, live preview)
- Security Analysis (XSS risks, WASM sandboxing)
- Testing Strategy (unit, integration, benchmarks)
- Performance Considerations (WASM benefits, optimization opportunities)
- Development Workflow (prerequisites, common commands)
- Distribution & Integration (NPM package, module formats)
- Business Value & Use Cases (target applications)
- Technical Debt & Improvements (critical issues, missing features)
- File Organization & Conventions (structure, naming)
- Cross-Reference Map (data flows, dependencies)
Individual summaries available in .docs/summary-<path>.md
Currently documented files:
- Rust Sources:
summary-src-lib-rs.md,summary-tests-web-rs.md,summary-benches-markdown-bench-rs.md - Configuration:
summary-cargo-toml.md,summary-trunk-toml.md,summary-package-json.md,summary-vite-config-js.md,summary-build-sh.md - Templates:
summary-templates-editor-html.md - JavaScript:
summary-public-js-config-js.md
Read .docs/summary.md sections 2-3 to understand:
- Dual build system (Trunk for dev, wasm-pack+Vite for distribution)
- Component architecture (Rust/WASM layer, JS wrapper, config, UI)
- Data flow (user input → Rust conversion → DOM update)
Determine which layer your change affects:
- Rust/WASM (
src/lib.rs): Markdown conversion logic, template rendering - JavaScript Wrapper (
public/js/terraphim-editor.js): Library API, initialization - Configuration (
public/js/config.js): Shortcuts, commands, initial content - UI Template (
templates/editor.html): Layout, Shoelace components - Build System: Cargo.toml, Trunk.toml, package.json, vite.config.js, build.sh
Before implementing features, review .docs/summary.md section 11:
- Critical Issues: XSS vulnerability, memory leaks, error handling gaps
- Code Quality: Hard-coded selectors, inconsistent error handling
- Missing Features: Undo/redo, accessibility, mobile responsiveness
Plan testing approach based on .docs/summary.md section 6:
- Unit Tests:
cargo testfor Rust logic - Browser Tests:
wasm-pack test --chromefor integration - Benchmarks:
cargo benchfor performance validation - Test Gaps: No accessibility, cross-browser, edge case, or visual regression tests
- Separation of Concerns: Rust handles computation, JS handles UI orchestration
- Minimal Dependencies: Only add essential crates/packages
- Configuration Over Code: Centralize settings in
config.js - Performance First: Target < 50ms markdown conversion
- Security Conscious: Be aware of XSS risks (see
.docs/summary.mdsection 5)
- Rust: Use
cargo fmtandcargo clippybefore committing - Error Handling: Prefer
Result<T, JsValue>for WASM-compatible errors - Memory Safety: Avoid
.forget()on closures (creates leak), use proper cleanup - Testing: Write tests for new functionality (unit + browser integration)
- Documentation: Update
.docs/summaries when making architectural changes
- Development: Use
trunk servefor fast iteration - Distribution: Run
./build.shto verify full build pipeline - Version Sync: Version in
Cargo.tomlis source of truth (propagated to NPM) - Module Formats: Ensure changes work with ESM, UMD, and IIFE outputs
New Keyboard Shortcut:
- Edit
public/js/config.js→shortcutsarray - Test in
trunk serveenvironment - Document in README if user-facing
New Toolbar Command:
- Edit
public/js/config.js→commandsarray - Use valid Shoelace icon names
- Test text wrapping behavior
New Markdown Feature:
- Check if
markdowncrate supports it - Update
src/lib.rsconversion options if needed - Add tests in
tests/web.rs - Benchmark performance impact with
cargo bench
Security Issues (High Priority):
- Review
.docs/summary.mdsection 5 (Security Analysis) - For XSS: Add input sanitization before
.set_inner_html() - Test with malicious markdown inputs
Memory Leaks:
- Search for
.forget()insrc/lib.rs - Implement proper cleanup mechanism
- Test with long-running sessions
Performance Issues:
- Run
cargo benchto establish baseline - Implement optimization (see
.docs/summary.mdsection 7) - Re-benchmark to validate improvement
- Ensure still < 50ms conversion time
Before Refactoring:
- Read relevant file summary in
.docs/summary-*.md - Understand integration points and dependencies
- Plan backwards-compatible changes when possible
After Refactoring:
- Update affected
.docs/summary-*.mdfiles - Run full test suite (
cargo test,wasm-pack test --chrome,cargo bench) - Verify both build systems work (
trunk serve,./build.sh) - Update
.docs/summary.mdif architecture changed
- ❌ Remove or modify the dual build system without deep analysis
- ❌ Change
Cargo.tomlcrate-type (breaks both dev and dist workflows) - ❌ Bundle Shoelace directly (it's a peer dependency)
- ❌ Add heavy JavaScript frameworks (defeats WASM performance benefits)
- ❌ Use
.innerHTMLwithout sanitization (XSS risk) - ❌ Modify version in
package.jsondirectly (sync fromCargo.toml)
- ✅ Test in both Trunk and Vite builds
- ✅ Validate all 3 module formats (ESM, UMD, IIFE) work
- ✅ Run benchmarks after performance-related changes
- ✅ Update documentation when making architectural changes
- ✅ Consider mobile/accessibility when adding UI features
- ✅ Check browser compatibility for new Web APIs
#[wasm_bindgen(start)]onrun()— Auto-execution#[wasm_bindgen]onrender_markdown()— Public APIJsValuefor error propagation- See
.docs/summary-src-lib-rs.mdfor details
src/lib.rs
→ cargo/rustc → WASM binary
→ wasm-pack → pkg/ (bindings + .wasm)
→ Vite → dist/ (ESM/UMD/IIFE)
→ build.sh → package/ (NPM-ready)
index.html
→ Loads config.js (EditorConfig)
→ Loads Shoelace components
→ WASM module loads
→ run() executes
→ Renders template
→ Sets up events
→ editor.js initializes
→ Toolbar/shortcuts setup
See .docs/summary.md section 13 for complete cross-reference map.
- Markdown Conversion: < 50ms (validated by benchmarks)
- WASM Binary Size: ~100KB (can optimize with LTO)
- Initial Load: Fast (WASM compiles/caches quickly)
- Memory: Minimal footprint (no large dependencies)
Before submitting changes:
-
cargo fmt— Rust formatting -
cargo clippy -- -D warnings— Rust linting -
cargo test— Unit tests pass -
wasm-pack test --chrome— Browser tests pass -
cargo bench— Benchmarks run (check for regressions) -
trunk serve— Dev build works -
./build.sh— Production build succeeds - Test ESM, UMD, IIFE examples in
package/ - Update
.docs/if architecture changed - Update WARP.md if commands/workflow changed
Shoelace Components: https://shoelace.style/
wasm-bindgen Guide: https://rustwasm.github.io/wasm-bindgen/
Trunk Documentation: https://trunkrs.dev/
Vite Library Mode: https://vitejs.dev/guide/build.html#library-mode
Remember: This project demonstrates best practices for Rust+WASM integration. Maintain the clean architecture, minimal dependencies, and performance-first approach when making changes.