Skip to content

feat(core): add action dispatcher and source editing tools#96

Closed
svallory wants to merge 3 commits intodocmd-io:mainfrom
svallory:feat/action-dispatcher
Closed

feat(core): add action dispatcher and source editing tools#96
svallory wants to merge 3 commits intodocmd-io:mainfrom
svallory:feat/action-dispatcher

Conversation

@svallory
Copy link
Copy Markdown
Contributor

@svallory svallory commented Apr 5, 2026

Summary

  • Adds action-dispatcher.ts — routes WebSocket RPC calls to plugin action handlers with a sandboxed ActionContext
  • Adds source-tools.ts — translates rendered-output references (block positions, text offsets) to raw markdown source positions for safe file editing
  • All file I/O is async (fs.promises) and path-sandboxed (safePath rejects traversal)

Context

Part of the plugin API infrastructure discussed in #73. Depends on #95 (plugin actions/events hooks).

The action dispatcher creates a fresh ActionContext per call with:

  • readFile, writeFile, readFileLines — sandboxed to project root
  • source — block-level editing tools (getBlockAt, findText, wrapText, insertAfter, replaceBlock, removeBlock)
  • broadcast — push events to connected browser clients
  • Modification tracking for automatic reload signaling

Changes

File Change
packages/core/src/utils/action-dispatcher.ts New: action routing + sandboxed context
packages/core/src/utils/source-tools.ts New: markdown source editing tools
packages/core/src/index.ts Export new modules

Test plan

  • Unit test safePath: rejects ../../../etc/passwd, accepts docs/index.md
  • Unit test dispatcher: mock handler returns result, verify { result, reload }
  • Unit test source-tools with fixture markdown: getBlockAt, findText, wrapText
  • Run pnpm test

svallory added 2 commits April 3, 2026 17:19
Plugins can now export `actions` (for WebSocket RPC handlers) and
`events` (for fire-and-forget handlers) alongside existing build-time
hooks. Also defines PluginModule, ActionContext, and SourceTools types
for the plugin API contract.
Action dispatcher routes WebSocket RPC calls and events to plugin
handlers with a sandboxed ActionContext. Source-tools translate
rendered-output references (block positions, text offsets) back to
raw markdown source positions for safe file editing.

- Port action-dispatcher.js to TypeScript/ESM with async fs.promises
- Port source-tools.js to TypeScript/ESM with async fs.promises
- Import createMarkdownProcessor from @docmd/parser (upstream export)
- safePath rejects any path traversal outside projectRoot
- Export both modules from @docmd/core barrel
Fixes prefer-const lint error on the removal cleanup loop index.
@svallory svallory mentioned this pull request Apr 5, 2026
@mgks
Copy link
Copy Markdown
Member

mgks commented Apr 9, 2026

Closing.
These changes are included in #97 which has been merged. Thank you! 🙏

@mgks mgks closed this Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants