@humanonlyweb/writer 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +62 -2
  3. package/dist/components/WriterContent.d.ts +57 -0
  4. package/dist/components/WriterContent.js +114 -0
  5. package/dist/components/WriterEditor.js +232 -0
  6. package/dist/components/WriterEditor.vue.d.ts +70 -0
  7. package/dist/components/WriterToolbar.js +135 -0
  8. package/dist/components/WriterToolbar.vue.d.ts +27 -0
  9. package/dist/config.d.ts +39 -0
  10. package/dist/config.js +41 -0
  11. package/dist/docs/README.md +24 -0
  12. package/dist/docs/blocks.md +64 -0
  13. package/dist/docs/changelog.md +17 -0
  14. package/dist/docs/components/content.md +51 -0
  15. package/dist/docs/components/editor.md +236 -0
  16. package/dist/docs/components/toolbar.md +146 -0
  17. package/dist/docs/images.md +80 -0
  18. package/dist/docs/index.md +86 -0
  19. package/dist/docs/installation.md +74 -0
  20. package/dist/docs/limitations.md +11 -0
  21. package/dist/docs/localisation.md +35 -0
  22. package/dist/docs/parsing.md +47 -0
  23. package/dist/docs/security.md +16 -0
  24. package/dist/docs/storing.md +105 -0
  25. package/dist/docs/styling.md +47 -0
  26. package/dist/docs/word-count.md +16 -0
  27. package/dist/editor/commands.d.ts +1 -0
  28. package/dist/editor/commands.js +359 -0
  29. package/dist/editor/images.d.ts +1 -0
  30. package/dist/editor/images.js +400 -0
  31. package/dist/editor/live.d.ts +2 -0
  32. package/dist/editor/live.js +411 -0
  33. package/dist/editor/setup.d.ts +9 -0
  34. package/dist/editor/setup.js +485 -0
  35. package/dist/index.d.ts +11 -0
  36. package/dist/index.js +8 -0
  37. package/dist/load-parser.js +12 -0
  38. package/dist/nuxt/module.d.ts +35 -0
  39. package/dist/nuxt/module.js +114 -0
  40. package/dist/nuxt/runtime/plugin.d.ts +2 -0
  41. package/dist/nuxt/runtime/plugin.js +25 -0
  42. package/dist/nuxt/runtime/server/parse.d.ts +12 -0
  43. package/dist/nuxt/runtime/server/parse.js +15 -0
  44. package/dist/package.js +3 -0
  45. package/dist/parse.d.ts +16 -0
  46. package/dist/parse.js +62 -0
  47. package/dist/parser-options.d.ts +19 -0
  48. package/dist/placeholder.js +5 -0
  49. package/dist/render.d.ts +10 -0
  50. package/dist/render.js +97 -0
  51. package/dist/sanitize.d.ts +11 -0
  52. package/dist/sanitize.js +139 -0
  53. package/dist/stats.d.ts +8 -0
  54. package/dist/stats.js +17 -0
  55. package/dist/styles/core.css +275 -0
  56. package/dist/translations.d.ts +26 -0
  57. package/dist/translations.js +47 -0
  58. package/dist/types.d.ts +9 -0
  59. package/package.json +101 -4
@@ -0,0 +1,135 @@
1
+ import { useTranslations } from "../config.js";
2
+ import { Fragment, computed, createElementBlock, createTextVNode, defineComponent, openBlock, renderList, renderSlot, shallowRef, toDisplayString, unref, useTemplateRef, withModifiers } from "vue";
3
+ // src/components/WriterToolbar.vue
4
+ const _hoisted_1 = ["aria-label"];
5
+ const _hoisted_2 = [
6
+ "data-command",
7
+ "tabindex",
8
+ "aria-label",
9
+ "aria-pressed",
10
+ "aria-disabled",
11
+ "aria-keyshortcuts",
12
+ "title",
13
+ "onClick"
14
+ ];
15
+ const _sfc_main = /*@__PURE__*/ defineComponent({
16
+ __name: "WriterToolbar",
17
+ props: {
18
+ editor: {},
19
+ items: { default: () => [
20
+ "heading2",
21
+ "heading3",
22
+ "bold",
23
+ "italic",
24
+ "link",
25
+ "code",
26
+ "quote",
27
+ "bulletList",
28
+ "orderedList",
29
+ "codeBlock",
30
+ "image"
31
+ ] },
32
+ label: {}
33
+ },
34
+ setup(__props) {
35
+ const t = useTranslations();
36
+ const root = useTemplateRef("root");
37
+ const TOGGLES = /* @__PURE__ */ new Set([
38
+ "bold",
39
+ "italic",
40
+ "strikethrough",
41
+ "code",
42
+ "link",
43
+ "heading1",
44
+ "heading2",
45
+ "heading3",
46
+ "quote",
47
+ "bulletList",
48
+ "orderedList",
49
+ "taskList",
50
+ "codeBlock"
51
+ ]);
52
+ const SHORTCUTS = {
53
+ bold: "B",
54
+ italic: "I",
55
+ link: "K",
56
+ code: "E",
57
+ strikethrough: "Shift+X",
58
+ heading1: "Alt+1",
59
+ heading2: "Alt+2",
60
+ heading3: "Alt+3",
61
+ orderedList: "Shift+7",
62
+ bulletList: "Shift+8",
63
+ taskList: "Shift+9",
64
+ footnote: "Alt+F",
65
+ undo: "Z",
66
+ redo: "Shift+Z"
67
+ };
68
+ const buttons = computed(() => __props.items.map((name) => ({
69
+ name,
70
+ label: t.value.commands[name],
71
+ pressed: TOGGLES.has(name) ? !!__props.editor?.active.has(name) : void 0,
72
+ shortcut: SHORTCUTS[name],
73
+ unavailable: name === "undo" && !__props.editor?.canUndo || name === "redo" && !__props.editor?.canRedo
74
+ })));
75
+ const disabled = computed(() => !__props.editor?.ready);
76
+ function buttonsIn(el) {
77
+ return [...el.querySelectorAll("button")];
78
+ }
79
+ const last = shallowRef();
80
+ const tabStop = computed(() => last.value && __props.items.includes(last.value) ? last.value : __props.items[0]);
81
+ function rove(target) {
82
+ last.value = target.dataset.command;
83
+ target.focus();
84
+ }
85
+ function onKeydown(event) {
86
+ if (!root.value || !(event.target instanceof HTMLButtonElement)) return;
87
+ const list = buttonsIn(root.value);
88
+ const index = list.indexOf(event.target);
89
+ const rtl = getComputedStyle(root.value).direction === "rtl";
90
+ const step = {
91
+ ArrowRight: rtl ? -1 : 1,
92
+ ArrowLeft: rtl ? 1 : -1
93
+ }[event.key];
94
+ const target = event.key === "Home" ? list[0] : event.key === "End" ? list.at(-1) : step ? list[(index + step + list.length) % list.length] : void 0;
95
+ if (!target) return;
96
+ event.preventDefault();
97
+ rove(target);
98
+ }
99
+ function run(name) {
100
+ last.value = name;
101
+ if (!disabled.value) __props.editor?.run(name);
102
+ }
103
+ const mac = (shortcut) => `⌘${shortcut.replace("Alt+", "⌥").replace("Shift+", "⇧")}`;
104
+ return (_ctx, _cache) => {
105
+ return openBlock(), createElementBlock("div", {
106
+ ref_key: "root",
107
+ ref: root,
108
+ role: "toolbar",
109
+ "data-part": "writer-toolbar",
110
+ "aria-label": __props.label ?? unref(t).toolbar,
111
+ onKeydown
112
+ }, [(openBlock(true), createElementBlock(Fragment, null, renderList(buttons.value, (button) => {
113
+ return openBlock(), createElementBlock("button", {
114
+ key: button.name,
115
+ type: "button",
116
+ "data-part": "writer-toolbar-item",
117
+ "data-command": button.name,
118
+ tabindex: button.name === tabStop.value ? 0 : -1,
119
+ "aria-label": button.label,
120
+ "aria-pressed": button.pressed,
121
+ "aria-disabled": disabled.value || button.unavailable || void 0,
122
+ "aria-keyshortcuts": button.shortcut && `Control+${button.shortcut} Meta+${button.shortcut}`,
123
+ title: button.shortcut ? `${button.label} (Ctrl+${button.shortcut} / ${mac(button.shortcut)})` : void 0,
124
+ onMousedown: _cache[0] || (_cache[0] = withModifiers(() => {}, ["prevent"])),
125
+ onClick: ($event) => run(button.name)
126
+ }, [renderSlot(_ctx.$slots, "item", {
127
+ command: button.name,
128
+ label: button.label,
129
+ active: !!button.pressed
130
+ }, () => [createTextVNode(toDisplayString(button.label), 1)])], 40, _hoisted_2);
131
+ }), 128))], 40, _hoisted_1);
132
+ };
133
+ }
134
+ });
135
+ export { _sfc_main as default };
@@ -0,0 +1,27 @@
1
+ import { CommandName } from "../translations.js";
2
+ import { ToolbarEditor } from "../types.js";
3
+ type __VLS_Props = {
4
+ /** The editor to format. */
5
+ editor: ToolbarEditor | null | undefined;
6
+ /** Commands to show, in order. */
7
+ items?: readonly CommandName[];
8
+ /** Accessible name. Defaults to the "toolbar" translation. */
9
+ label?: string;
10
+ };
11
+ type __VLS_Slots = {
12
+ /** A button's content, e.g. an icon. Defaults to its label, which stays its accessible name. */
13
+ item?(props: {
14
+ command: CommandName;
15
+ label: string;
16
+ active: boolean;
17
+ }): unknown;
18
+ };
19
+ declare const __VLS_base: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
20
+ declare const __VLS_export: __VLS_WithSlots<typeof __VLS_base, __VLS_Slots>;
21
+ declare const _default: typeof __VLS_export;
22
+ type __VLS_WithSlots<T, S> = T & {
23
+ new (): {
24
+ $slots: S;
25
+ };
26
+ };
27
+ export { _default as default };
@@ -0,0 +1,39 @@
1
+ import { WriterTranslations, WriterTranslationsInput } from "./translations.js";
2
+ import { ParserOptions } from "./parser-options.js";
3
+ import "./parse.js";
4
+ import { App, Component, ComputedRef, InjectionKey, Ref } from "vue";
5
+ export interface WriterBlock {
6
+ /** Rendered for `::name` in content. Receives the attributes as props and the body as slots. */
7
+ component: Component;
8
+ /** Slash menu entry. Defaults to the block name. */
9
+ label?: string;
10
+ /**
11
+ * Inserted from the slash menu, in CodeMirror snippet syntax: `${}` marks where the
12
+ * cursor goes, `${name}` a field Tab moves to. Defaults to an empty block.
13
+ */
14
+ snippet?: string;
15
+ }
16
+ export interface WriterOptions {
17
+ /** Components authors can use, keyed by their Markdown name: `{ callout }` for `::callout`. */
18
+ blocks?: Record<string, Component | WriterBlock>;
19
+ /** Replace how native elements render, e.g. `{ a: AppLink, img: AppImage }`. */
20
+ components?: Record<string, Component>;
21
+ /** Used by `<WriterContent>` for Markdown strings. Parse stored documents with the same options. */
22
+ parser?: ParserOptions;
23
+ /** The components' built-in text. Pass only what you change; a ref keeps it live. */
24
+ translations?: WriterTranslationsInput | Ref<WriterTranslationsInput>;
25
+ }
26
+ export interface WriterConfig {
27
+ blocks: Readonly<Record<string, Required<WriterBlock>>>;
28
+ components: Readonly<Record<string, Component>>;
29
+ parser: ParserOptions;
30
+ translations: Ref<WriterTranslationsInput>;
31
+ }
32
+ export declare const WRITER_CONFIG_KEY: InjectionKey<WriterConfig>;
33
+ /** `app.use(createWriter({ blocks }))`. Scoped to the app, so nothing leaks between SSR requests. */
34
+ export declare function createWriter(options?: WriterOptions): {
35
+ install(app: App): void;
36
+ };
37
+ export declare function useWriterConfig(): WriterConfig;
38
+ /** The built-in text: English, with the app's `translations` on top. */
39
+ export declare function useTranslations(): ComputedRef<WriterTranslations>;
package/dist/config.js ADDED
@@ -0,0 +1,41 @@
1
+ import { resolveTranslations } from "./translations.js";
2
+ import { kebabCase } from "comark/utils";
3
+ import { computed, inject, isRef, shallowRef } from "vue";
4
+ // src/config.ts
5
+ const WRITER_CONFIG_KEY = Symbol("humanonlyweb-writer");
6
+ const isBlock = (value) => typeof value === "object" && value !== null && "component" in value;
7
+ function resolveConfig(options = {}) {
8
+ const blocks = {};
9
+ for (const [key, value] of Object.entries(options.blocks ?? {})) {
10
+ const name = kebabCase(key);
11
+ const block = isBlock(value) ? value : { component: value };
12
+ blocks[name] = {
13
+ component: block.component,
14
+ label: block.label ?? name,
15
+ snippet: block.snippet ?? `::${name}\n\${}\n::`
16
+ };
17
+ }
18
+ const { translations = {} } = options;
19
+ return {
20
+ blocks,
21
+ components: options.components ?? {},
22
+ parser: options.parser ?? {},
23
+ translations: isRef(translations) ? translations : shallowRef(translations)
24
+ };
25
+ }
26
+ const DEFAULT_CONFIG = resolveConfig();
27
+ /** `app.use(createWriter({ blocks }))`. Scoped to the app, so nothing leaks between SSR requests. */
28
+ function createWriter(options = {}) {
29
+ return { install(app) {
30
+ app.provide(WRITER_CONFIG_KEY, resolveConfig(options));
31
+ } };
32
+ }
33
+ function useWriterConfig() {
34
+ return inject(WRITER_CONFIG_KEY, DEFAULT_CONFIG);
35
+ }
36
+ /** The built-in text: English, with the app's `translations` on top. */
37
+ function useTranslations() {
38
+ const { translations } = useWriterConfig();
39
+ return computed(() => resolveTranslations(translations.value));
40
+ }
41
+ export { WRITER_CONFIG_KEY, createWriter, useTranslations, useWriterConfig };
@@ -0,0 +1,24 @@
1
+ # @humanonlyweb/writer
2
+
3
+ > Markdown editor and renderer for Vue 3 and Nuxt: CodeMirror editing with a live preview, Comark parsing, blocks as your own Vue components. SSR-safe; store the Markdown, render a document parsed on save. Nuxt names: `<WriterEditor>`, `<WriterToolbar>`, `<WriterContent>`; in plain Vue, import them from `@humanonlyweb/writer`.
4
+
5
+ ## Guides
6
+
7
+ - [Introduction](./index.md): A Markdown editor and renderer for Vue 3 and Nuxt.
8
+ - [Installation](./installation.md): Nuxt module or Vue plugin.
9
+ - [Blocks](./blocks.md): Your Vue components, written in Markdown.
10
+ - [Images](./images.md): Images, figures and uploads.
11
+ - [Storing documents](./storing.md): Store the Markdown, render a document parsed on save.
12
+ - [Parsing](./parsing.md): Markdown to a document, anywhere.
13
+ - [Word count](./word-count.md): Words a reader sees, and minutes to read them.
14
+ - [Styling](./styling.md): Tokens, hooks, and your site's CSS.
15
+ - [Localisation](./localisation.md): Every string the components show or announce.
16
+ - [Security](./security.md): Content is untrusted.
17
+ - [Limitations](./limitations.md): What it doesn't do yet.
18
+ - [Changelog](./changelog.md): Changes in each release.
19
+
20
+ ## Components
21
+
22
+ - [Editor](./components/editor.md): Markdown in, a live preview while you write.
23
+ - [Toolbar](./components/toolbar.md): Formatting buttons for an editor.
24
+ - [Content](./components/content.md): Markdown, rendered with your components.
@@ -0,0 +1,64 @@
1
+ # Blocks
2
+
3
+ > Your Vue components, written in Markdown.
4
+
5
+ ## Writing one
6
+
7
+ Attributes become props, `#name` sections become slots:
8
+
9
+ ```md
10
+ ::callout{type="warning"}
11
+ #title
12
+ Heads up
13
+ #default
14
+ Body with **Markdown**.
15
+ ::
16
+ ```
17
+
18
+ ```vue
19
+ <script setup lang="ts">
20
+ // `::callout` renders components/blocks/Callout.vue, in the editor and on the page.
21
+ // Type / on an empty line for the block menu.
22
+ const body = ref(`::callout{type="warning"}
23
+ Click me to edit my source.
24
+ ::
25
+
26
+ `);
27
+ </script>
28
+
29
+ <template>
30
+ <div class="writer-demo">
31
+ <WriterEditor v-model="body" label="Body" />
32
+ </div>
33
+ </template>
34
+ ```
35
+
36
+ ## Registering
37
+
38
+ In Nuxt, every component in `components/blocks/` is a block: `Callout.vue` is `::callout`, `media/Video.vue` is `::media-video`. They load on demand.
39
+
40
+ In Vue, pass them to `createWriter({ blocks: { callout: Callout } })`.
41
+
42
+ The slash menu's labels and snippets go in `app.config.ts` (or `{ component, label, snippet }` in Vue):
43
+
44
+ ```ts
45
+ export default defineAppConfig({
46
+ writer: {
47
+ blocks: {
48
+ callout: { label: "Callout", snippet: '::callout{type="${note}"}\n${}\n::' },
49
+ },
50
+ },
51
+ });
52
+ ```
53
+
54
+ Snippets use CodeMirror's syntax: `${}` is where the cursor lands, `${name}` a field Tab moves to.
55
+
56
+ ## Unknown blocks
57
+
58
+ Only registered blocks render. Anything else renders its content alone, so a removed block never breaks a page.
59
+
60
+ GitHub alerts (`> [!NOTE]`) render as a blockquote, or as your `note` block if you register one.
61
+
62
+ ## In the editor
63
+
64
+ Blocks render inside the editor through the app, not the page around it: they get app-level plugins and `provide`s, but nothing provided by a parent of `<WriterEditor>`. Declare a `__node` prop to receive the block's raw node.
@@ -0,0 +1,17 @@
1
+ # Changelog
2
+
3
+ > Changes in each release.
4
+
5
+ ## 0.1.1 (2026-11-02)
6
+
7
+ - chore: bump version to 0.1.1 in package.json
8
+ - Editor: italic works inside words, marks skip edge spaces, code fences fit backticks, blocks keep blank lines around them, read-only cards stay read-only; docs demos for every command and read-only
9
+ - Editor: headings, quotes and lists from the toolbar leave the cursor after their marks
10
+ - fix: remove Nuxt auto-import reference from code example in storing.md
11
+ - chore: update @humanonlyweb/ui dependency version to allow minor updates
12
+ - Update favicon and logo assets; modify site manifest for branding consistency
13
+ - Add a docs site at writer.humanonlyweb.com, built from the playground; the package ships its pages as Markdown in dist/docs
14
+
15
+ ## 0.1.0 (2026-10-28)
16
+
17
+ First release.
@@ -0,0 +1,51 @@
1
+ # Content
2
+
3
+ > Markdown, rendered with your components.
4
+
5
+ `<WriterContent>` renders Markdown, or a document parsed ahead of time. In plain Vue, import `WriterContent`.
6
+
7
+ ## Examples
8
+
9
+ ### Live preview
10
+
11
+ Markdown renders as you type; `debounce` waits a beat before parsing again.
12
+
13
+ ```vue
14
+ <script setup lang="ts">
15
+ const body = ref(`## Rendered
16
+
17
+ Type on the left: **Markdown** renders on the right, blocks included.
18
+
19
+ ::callout
20
+ A callout block.
21
+ ::`);
22
+ </script>
23
+
24
+ <template>
25
+ <div class="writer-demo writer-demo-split">
26
+ <UiTextarea v-model="body" label="Markdown" />
27
+ <WriterContent :value="body" :debounce="100" />
28
+ </div>
29
+ </template>
30
+ ```
31
+
32
+ ## Props
33
+
34
+ | Prop | Type | Description |
35
+ | ------------ | ---------------------------- | -------------------------------------------------------------------- |
36
+ | `value` | `string \| MarkdownDocument` | Markdown, or a document parsed ahead of time (no parser download). |
37
+ | `components` | `Record<string, Component>` | Native element overrides for this render, e.g. `{ a: NuxtLink }`. |
38
+ | `debounce` | `number` | Ms to wait before parsing changed Markdown again. ~100 for previews. |
39
+ | `tag` | `string` | Wrapper element. Default `div`. |
40
+
41
+ Give it a parsed document on public pages: they render without loading the parser. A string works anywhere, server rendering included, and loads the parser on demand.
42
+
43
+ ## What renders
44
+
45
+ - Markdown's own elements, and registered [blocks](/blocks). Anything else renders its content only.
46
+ - Images load lazily. An image alone in its paragraph, with a title, becomes a `<figure>`. See [Images](/images).
47
+ - Content is sanitized again before rendering. See [Security](/security).
48
+
49
+ ## Your own wrapper
50
+
51
+ `renderDocument(document, { blocks, components })` returns the rendered Vue nodes, for when `<WriterContent>` doesn't fit. These docs render their pages with it.
@@ -0,0 +1,236 @@
1
+ # Editor
2
+
3
+ > Markdown in, a live preview while you write.
4
+
5
+ `<WriterEditor>` edits Markdown, held in its `v-model`. In plain Vue, import `WriterEditor`.
6
+
7
+ ## Examples
8
+
9
+ ### Basic
10
+
11
+ ```vue
12
+ <script setup lang="ts">
13
+ const body = ref(`## A heading
14
+
15
+ Some **bold**, some _italic_, a [link](https://example.com) and \`code\`.
16
+
17
+ - A list
18
+ - [x] A done task
19
+
20
+ > A quote.`);
21
+ const editor = useTemplateRef("editor");
22
+ </script>
23
+
24
+ <template>
25
+ <div class="writer-demo">
26
+ <WriterToolbar
27
+ :editor="editor"
28
+ :items="['heading2', 'bold', 'italic', 'link', 'quote', 'bulletList', 'image']"
29
+ />
30
+ <WriterEditor ref="editor" v-model="body" label="Body" placeholder="Start writing…" />
31
+ </div>
32
+ </template>
33
+ ```
34
+
35
+ ### Source mode
36
+
37
+ Plain Markdown throughout, with the source highlighted.
38
+
39
+ ```vue
40
+ <script setup lang="ts">
41
+ const body = ref(`## Plain Markdown
42
+
43
+ Every **mark** stays where you can see it.
44
+
45
+ ::callout
46
+ Blocks too.
47
+ ::`);
48
+ </script>
49
+
50
+ <template>
51
+ <div class="writer-demo">
52
+ <WriterEditor v-model="body" mode="source" label="Body" />
53
+ </div>
54
+ </template>
55
+ ```
56
+
57
+ ### Uploads
58
+
59
+ Pass `upload` and files can be pasted, dropped or picked. It gets the `File` and resolves to its URL.
60
+
61
+ ```vue
62
+ <script setup lang="ts">
63
+ const body = ref("Drop, paste or choose an image:\n\n![]()\n");
64
+ const editor = useTemplateRef("editor");
65
+ const error = ref("");
66
+
67
+ // Your endpoint stores the file and answers with its URL. Here: the image inlined,
68
+ // after a moment, so you can see it upload.
69
+ async function upload(file: File) {
70
+ await new Promise((resolve) => setTimeout(resolve, 1500));
71
+ return new Promise<string>((resolve) => {
72
+ const reader = new FileReader();
73
+ reader.addEventListener("load", () => resolve(String(reader.result)));
74
+ reader.readAsDataURL(file);
75
+ });
76
+ }
77
+ </script>
78
+
79
+ <template>
80
+ <div class="writer-demo">
81
+ <WriterToolbar :editor="editor" :items="['bold', 'italic', 'image']" />
82
+ <WriterEditor
83
+ ref="editor"
84
+ v-model="body"
85
+ label="Body"
86
+ :upload="upload"
87
+ @upload-error="(_, file) => (error = `Couldn't upload ${file.name}.`)"
88
+ />
89
+ <p class="writer-demo-stats">
90
+ {{ error || (editor?.uploading ? "Uploading…" : "Ready to save") }}
91
+ </p>
92
+ </div>
93
+ </template>
94
+ ```
95
+
96
+ ### Read-only
97
+
98
+ `readonly` locks everything: the text, task checkboxes and image cards.
99
+
100
+ ```vue
101
+ <script setup lang="ts">
102
+ const body = ref(`## Read-only
103
+
104
+ Nothing here can change: not the text, not the task,
105
+ and not the image card below.
106
+
107
+ - [x] A done task
108
+
109
+ ![A snowy street](https://picsum.photos/id/1036/900/360 "Look, don't touch."){width="900" height="360"}`);
110
+ const readonly = ref(true);
111
+ </script>
112
+
113
+ <template>
114
+ <div class="writer-demo">
115
+ <div class="writer-demo-bar">
116
+ <span class="writer-demo-stats">{{ readonly ? "Read-only" : "Editable" }}</span>
117
+ <button
118
+ type="button"
119
+ class="writer-demo-toggle"
120
+ :aria-pressed="readonly"
121
+ @click="readonly = !readonly"
122
+ >
123
+ Read-only
124
+ </button>
125
+ </div>
126
+ <WriterEditor v-model="body" :readonly="readonly" label="Body" />
127
+ </div>
128
+ </template>
129
+ ```
130
+
131
+ ### Link suggestions
132
+
133
+ `links` suggests targets as the author types `](`, e.g. your site's pages.
134
+
135
+ ```vue
136
+ <script setup lang="ts">
137
+ const body = ref("Type `](` after some text in brackets: [the council](");
138
+
139
+ const pages = [
140
+ { label: "City council", url: "/topics/council" },
141
+ { label: "Winter", url: "/topics/winter" },
142
+ { label: "Dale", url: "/people/dale" },
143
+ ];
144
+ const links = (query: string) => pages.filter((page) => page.url.includes(query));
145
+ </script>
146
+
147
+ <template>
148
+ <div class="writer-demo">
149
+ <WriterEditor v-model="body" label="Body" :links="links" />
150
+ </div>
151
+ </template>
152
+ ```
153
+
154
+ ## Live preview
155
+
156
+ The document is always Markdown, but it reads like the page:
157
+
158
+ - **Formatting:** marks hide except in the element being edited. `**bold**` shows its asterisks only while the cursor is in it. Heading, quote and list marks show on the cursor's line.
159
+ - **Blocks** render with your components. Click or arrow into one and its source opens, framed, in the same spot. Set `--writer-block-inset` and `--writer-block-background` to your blocks' padding and background, and the text doesn't jump.
160
+ - **Images** on their own line are cards: the published figure, with caption and alt text typed in place. Drop, paste or pick a file to fill or replace one.
161
+ - **Leaving the editor** shows everything as published. The cursor stays put for when you come back.
162
+
163
+ `mode="source"` turns it off.
164
+
165
+ ## Props
166
+
167
+ | Prop | Type | Description |
168
+ | --------------- | -------------------------------------------- | ----------------------------------------------------------------------------------- |
169
+ | `v-model` | `string` | The Markdown. |
170
+ | `label` | `string` | Accessible name. Default: "Markdown editor". |
171
+ | `labelledby` | `string` | Id of the element naming it, instead of `label`. |
172
+ | `describedby` | `string` | Id of a hint or error. |
173
+ | `name` | `string` | Submits the Markdown with its form. |
174
+ | `placeholder` | `string` | |
175
+ | `readonly` | `boolean` | |
176
+ | `mode` | `"live" \| "source"` | Live preview (default) or plain Markdown. |
177
+ | `upload` | `(file: File) => Promise<string>` | Enables pasting, dropping and picking files. Resolve to the URL. |
178
+ | `links` | `(query) => { label, url }[]` (or a promise) | Suggests link targets after `](`. |
179
+ | `codeLanguages` | `LanguageDescription[]` | Highlighting inside fenced code, e.g. `languages` from `@codemirror/language-data`. |
180
+ | `extensions` | `Extension` | Extra CodeMirror extensions. Read once, on mount. |
181
+
182
+ ## Events
183
+
184
+ | Event | Payload | When |
185
+ | -------------- | ------------------------------ | ------------------------------------------------------------------------------- |
186
+ | `focus` | | The editor gets focus. |
187
+ | `blur` | | It loses it. |
188
+ | `upload-error` | `(error: unknown, file: File)` | An `upload` rejected. Its placeholder is gone, or the card's old image is back. |
189
+
190
+ ## Exposed
191
+
192
+ Through a template ref:
193
+
194
+ | Member | Description |
195
+ | -------------------- | --------------------------------------------------------------------------------------------------------------------------- |
196
+ | `ready` | CodeMirror has loaded. |
197
+ | `active` | Formatting at the cursor, a `Set` of command names. |
198
+ | `canUpload` | `upload` is set. |
199
+ | `canUndo`, `canRedo` | |
200
+ | `uploading` | Uploads in progress. Save when it's `0`. |
201
+ | `run(command)` | Applies a [command](/components/toolbar#commands). `image` opens the file picker with `upload`, else inserts an empty card. |
202
+ | `insert(text)` | Replaces the selection. |
203
+ | `insertFiles(files)` | Uploads and inserts files at the cursor. |
204
+ | `focus()` | |
205
+ | `view()` | The CodeMirror `EditorView`, for anything else. |
206
+
207
+ ## Keyboard
208
+
209
+ `Mod` is Ctrl, or ⌘ on a Mac.
210
+
211
+ | Keys | Description |
212
+ | ------------------------------- | ------------------------------------------------------------ |
213
+ | `Mod+B` `Mod+I` `Mod+E` `Mod+K` | Bold, italic, code, link |
214
+ | `Mod+Shift+X` | Strikethrough |
215
+ | `Mod+Alt+1` … `3` | Heading 1–3 |
216
+ | `Mod+Shift+7` `8` `9` | Numbered, bulleted, task list |
217
+ | `Mod+Alt+F` | Footnote |
218
+ | `Mod+Z` `Mod+Shift+Z` | Undo, redo |
219
+ | `Enter` | New paragraph (in a quote, inside it); in a list, a new item |
220
+ | `Shift+Enter` | Line break inside the paragraph (`\` at the line end) |
221
+ | `/` on an empty line | Block menu |
222
+ | `Mod+]` `Mod+[` | Indent, outdent |
223
+
224
+ `Tab` leaves the editor, so keyboard users aren't trapped. Pasting a URL over a selection links it.
225
+
226
+ ## Uploads
227
+
228
+ While a file uploads, the Markdown holds an `#upload-…` placeholder where its URL will go, and `uploading` counts what's in flight. Wait for `0` before saving: the URL landing changes the Markdown, so a save-on-change runs again by itself. A placeholder saved anyway renders nothing, and in the editor it's an empty card to fill again.
229
+
230
+ ## Loading another document
231
+
232
+ Change the editor's `key`. An outside `v-model` change is applied as an edit, keeping undo history; a new document should start fresh.
233
+
234
+ ## Server rendering
235
+
236
+ The server renders the content as it will read (a `<textarea>` in source mode). CodeMirror loads in the browser and takes over, keeping anything typed before it did. Pages without an editor never load CodeMirror.