The best way to control Claude Code, Codex and more from any computer or phone
| 1 | # copy-editor |
| 2 | |
| 3 | Edit hardcoded copy in the browser while `vite dev` runs, and have each edit |
| 4 | written back to the source file. |
| 5 | |
| 6 | ```ts |
| 7 | // vite.config.ts (or `vite.plugins` in astro.config.ts) |
| 8 | import { copyEditor } from "./src/copy-editor/index.ts"; |
| 9 | |
| 10 | export default defineConfig({ |
| 11 | // Register before framework plugins so it sees the raw source. |
| 12 | plugins: [copyEditor(), react()], |
| 13 | }); |
| 14 | ``` |
| 15 | |
| 16 | Mark any piece of hardcoded copy with a bare `data-copy` attribute: |
| 17 | |
| 18 | ```html |
| 19 | <h1 data-copy>Reach for the better primitives.</h1> |
| 20 | <p data-copy>Snap together cloud resources with <code>Alchemy</code>.</p> |
| 21 | ``` |
| 22 | |
| 23 | - **Dev** (`vite serve`): every `data-copy` element is editable. Click and type; |
| 24 | leaving the element or pressing Enter saves, Escape reverts. Only the changed |
| 25 | words are rewritten, so tags, entities, and line wrapping are preserved. |
| 26 | Ctrl/Cmd+click still follows links inside editable copy. |
| 27 | - **Build**: the attribute is stripped and none of the editor ships. |
| 28 | |
| 29 | Works with `.astro`, `.html`, `.jsx`, `.tsx`, `.vue`, and `.svelte` files |
| 30 | (configure with `copyEditor({ include: /\.(tsx|html)$/ })`). An editable |
| 31 | element may contain only text and plain HTML tags; elements containing |
| 32 | `{expressions}` or components are rejected with an explanation in the browser. |
| 33 | |
| 34 | Limit editing to some pages with `pages`, a predicate over the URL pathname |
| 35 | (e.g. `copyEditor({ pages: (p) => p === "/" })`); other pages load no editor. |
| 36 | |
| 37 | ## Markdown sections |
| 38 | |
| 39 | Content that comes from markdown (docs pages, a CMS, generated reference) can |
| 40 | be edited as markdown instead of rendered text. Mark the section's container |
| 41 | and supply a handler for its id scheme: |
| 42 | |
| 43 | ```html |
| 44 | <div data-copy="docs:intro" data-copy-format="markdown" data-copy-style="docs"> |
| 45 | …rendered markdown… |
| 46 | </div> |
| 47 | ``` |
| 48 | |
| 49 | ```ts |
| 50 | copyEditor({ |
| 51 | handlers: { |
| 52 | docs: { |
| 53 | readSource: async (id) => loadMarkdown(id), |
| 54 | writeSource: async ({ id, source, base }) => saveMarkdown(id, source, base), |
| 55 | }, |
| 56 | }, |
| 57 | markdownStyles: { |
| 58 | docs: { |
| 59 | elements: { |
| 60 | code: { class: "inline-code" }, // inline code |
| 61 | pre: { class: "code-block" }, // code blocks |
| 62 | a: { class: "link" }, |
| 63 | ul: { class: "bullets" }, |
| 64 | }, |
| 65 | css: `[data-copy-style="docs"] .code-block { background: #111; }`, |
| 66 | }, |
| 67 | }, |
| 68 | }); |
| 69 | ``` |
| 70 | |
| 71 | Focusing the section swaps in its markdown source (Enter adds a line, |
| 72 | Ctrl/Cmd+Enter or leaving the section saves, Escape cancels). One-line |
| 73 | template copy is edited as inline markdown: Enter saves, and Shift+Enter |
| 74 | adds a line break (`<br>`). After saving, |
| 75 | the section shows a preview rendered with its `data-copy-style` preset. |
| 76 | Once the handler's follow-up work (for example regenerating pages) finishes, |
| 77 | the page's own rendering replaces the preview. If the handler reports a |
| 78 | structural change (`reload: true`), the page reloads instead. |
| 79 | |
| 80 | Content that isn't rendered through Vite (markdown files) can mark a single |
| 81 | element with a trailing `<!--copy:<scheme>:<id>-->` comment for inline |
| 82 | editing; handlers implement `edit` for those. |