@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.
- package/LICENSE +21 -0
- package/README.md +62 -2
- package/dist/components/WriterContent.d.ts +57 -0
- package/dist/components/WriterContent.js +114 -0
- package/dist/components/WriterEditor.js +232 -0
- package/dist/components/WriterEditor.vue.d.ts +70 -0
- package/dist/components/WriterToolbar.js +135 -0
- package/dist/components/WriterToolbar.vue.d.ts +27 -0
- package/dist/config.d.ts +39 -0
- package/dist/config.js +41 -0
- package/dist/docs/README.md +24 -0
- package/dist/docs/blocks.md +64 -0
- package/dist/docs/changelog.md +17 -0
- package/dist/docs/components/content.md +51 -0
- package/dist/docs/components/editor.md +236 -0
- package/dist/docs/components/toolbar.md +146 -0
- package/dist/docs/images.md +80 -0
- package/dist/docs/index.md +86 -0
- package/dist/docs/installation.md +74 -0
- package/dist/docs/limitations.md +11 -0
- package/dist/docs/localisation.md +35 -0
- package/dist/docs/parsing.md +47 -0
- package/dist/docs/security.md +16 -0
- package/dist/docs/storing.md +105 -0
- package/dist/docs/styling.md +47 -0
- package/dist/docs/word-count.md +16 -0
- package/dist/editor/commands.d.ts +1 -0
- package/dist/editor/commands.js +359 -0
- package/dist/editor/images.d.ts +1 -0
- package/dist/editor/images.js +400 -0
- package/dist/editor/live.d.ts +2 -0
- package/dist/editor/live.js +411 -0
- package/dist/editor/setup.d.ts +9 -0
- package/dist/editor/setup.js +485 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +8 -0
- package/dist/load-parser.js +12 -0
- package/dist/nuxt/module.d.ts +35 -0
- package/dist/nuxt/module.js +114 -0
- package/dist/nuxt/runtime/plugin.d.ts +2 -0
- package/dist/nuxt/runtime/plugin.js +25 -0
- package/dist/nuxt/runtime/server/parse.d.ts +12 -0
- package/dist/nuxt/runtime/server/parse.js +15 -0
- package/dist/package.js +3 -0
- package/dist/parse.d.ts +16 -0
- package/dist/parse.js +62 -0
- package/dist/parser-options.d.ts +19 -0
- package/dist/placeholder.js +5 -0
- package/dist/render.d.ts +10 -0
- package/dist/render.js +97 -0
- package/dist/sanitize.d.ts +11 -0
- package/dist/sanitize.js +139 -0
- package/dist/stats.d.ts +8 -0
- package/dist/stats.js +17 -0
- package/dist/styles/core.css +275 -0
- package/dist/translations.d.ts +26 -0
- package/dist/translations.js +47 -0
- package/dist/types.d.ts +9 -0
- package/package.json +101 -4
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# Toolbar
|
|
2
|
+
|
|
3
|
+
> Formatting buttons for an editor.
|
|
4
|
+
|
|
5
|
+
`<WriterToolbar>` formats an editor: pass it the editor's template ref. In plain Vue, import `WriterToolbar`.
|
|
6
|
+
|
|
7
|
+
## Examples
|
|
8
|
+
|
|
9
|
+
### Basic
|
|
10
|
+
|
|
11
|
+
```vue
|
|
12
|
+
<script setup lang="ts">
|
|
13
|
+
const body = ref("Select some text, then format it.");
|
|
14
|
+
const editor = useTemplateRef("editor");
|
|
15
|
+
</script>
|
|
16
|
+
|
|
17
|
+
<template>
|
|
18
|
+
<div class="writer-demo">
|
|
19
|
+
<WriterToolbar
|
|
20
|
+
:editor="editor"
|
|
21
|
+
:items="[
|
|
22
|
+
'heading2',
|
|
23
|
+
'bold',
|
|
24
|
+
'italic',
|
|
25
|
+
'strikethrough',
|
|
26
|
+
'link',
|
|
27
|
+
'bulletList',
|
|
28
|
+
'taskList',
|
|
29
|
+
'undo',
|
|
30
|
+
'redo',
|
|
31
|
+
]"
|
|
32
|
+
/>
|
|
33
|
+
<WriterEditor ref="editor" v-model="body" label="Body" />
|
|
34
|
+
</div>
|
|
35
|
+
</template>
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Every command
|
|
39
|
+
|
|
40
|
+
Try each one on an empty line, in the middle of a line and on a selection. Blocks (tables, dividers, images, code) get blank lines around them, so they never join the text next to them.
|
|
41
|
+
|
|
42
|
+
```vue
|
|
43
|
+
<script setup lang="ts">
|
|
44
|
+
import type { CommandName } from "@humanonlyweb/writer";
|
|
45
|
+
|
|
46
|
+
// Every command. Try each one on an empty line, in the middle of a line, and on a
|
|
47
|
+
// selection: the cursor should always land where the next word goes.
|
|
48
|
+
const items: CommandName[] = [
|
|
49
|
+
"heading1",
|
|
50
|
+
"heading2",
|
|
51
|
+
"heading3",
|
|
52
|
+
"bold",
|
|
53
|
+
"italic",
|
|
54
|
+
"strikethrough",
|
|
55
|
+
"code",
|
|
56
|
+
"link",
|
|
57
|
+
"quote",
|
|
58
|
+
"bulletList",
|
|
59
|
+
"orderedList",
|
|
60
|
+
"taskList",
|
|
61
|
+
"codeBlock",
|
|
62
|
+
"table",
|
|
63
|
+
"divider",
|
|
64
|
+
"image",
|
|
65
|
+
"footnote",
|
|
66
|
+
"undo",
|
|
67
|
+
"redo",
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
const body = ref("A line to format.\n\n");
|
|
71
|
+
const editor = useTemplateRef("editor");
|
|
72
|
+
const mode = ref<"live" | "source">("live");
|
|
73
|
+
</script>
|
|
74
|
+
|
|
75
|
+
<template>
|
|
76
|
+
<div class="writer-demo">
|
|
77
|
+
<div class="writer-demo-bar">
|
|
78
|
+
<WriterToolbar :editor="editor" :items="items" />
|
|
79
|
+
<button
|
|
80
|
+
type="button"
|
|
81
|
+
class="writer-demo-toggle"
|
|
82
|
+
:aria-pressed="mode === 'source'"
|
|
83
|
+
@click="mode = mode === 'source' ? 'live' : 'source'"
|
|
84
|
+
>
|
|
85
|
+
Markdown
|
|
86
|
+
</button>
|
|
87
|
+
</div>
|
|
88
|
+
<WriterEditor ref="editor" v-model="body" :mode="mode" label="Body" />
|
|
89
|
+
</div>
|
|
90
|
+
</template>
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Icons
|
|
94
|
+
|
|
95
|
+
The `item` slot replaces a button's content. Its label stays the accessible name.
|
|
96
|
+
|
|
97
|
+
```vue
|
|
98
|
+
<script setup lang="ts">
|
|
99
|
+
const body = ref("Icons instead of words; the names stay for screen readers.");
|
|
100
|
+
const editor = useTemplateRef("editor");
|
|
101
|
+
const icons = {
|
|
102
|
+
bold: "bold",
|
|
103
|
+
italic: "italic",
|
|
104
|
+
link: "link",
|
|
105
|
+
quote: "quote",
|
|
106
|
+
image: "image",
|
|
107
|
+
} as const;
|
|
108
|
+
</script>
|
|
109
|
+
|
|
110
|
+
<template>
|
|
111
|
+
<div class="writer-demo">
|
|
112
|
+
<WriterToolbar :editor="editor" :items="Object.keys(icons) as (keyof typeof icons)[]">
|
|
113
|
+
<template #item="{ command }">
|
|
114
|
+
<Icon :name="icons[command as keyof typeof icons]" />
|
|
115
|
+
</template>
|
|
116
|
+
</WriterToolbar>
|
|
117
|
+
<WriterEditor ref="editor" v-model="body" label="Body" />
|
|
118
|
+
</div>
|
|
119
|
+
</template>
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Props
|
|
123
|
+
|
|
124
|
+
| Prop | Type | Description |
|
|
125
|
+
| -------- | --------------- | ----------------------------------------------------------------------------------------------------- |
|
|
126
|
+
| `editor` | editor ref | The `<WriterEditor>` to format: pass its template ref. |
|
|
127
|
+
| `items` | `CommandName[]` | Commands, in order. Default: headings 2–3, bold, italic, link, code, quote, lists, code block, image. |
|
|
128
|
+
| `label` | `string` | Accessible name. Default: "Formatting". |
|
|
129
|
+
|
|
130
|
+
## Slots
|
|
131
|
+
|
|
132
|
+
| Slot | Props | Description |
|
|
133
|
+
| ------ | ---------------------------- | --------------------------------- |
|
|
134
|
+
| `item` | `{ command, label, active }` | A button's content, e.g. an icon. |
|
|
135
|
+
|
|
136
|
+
## Commands
|
|
137
|
+
|
|
138
|
+
`bold`, `italic`, `strikethrough`, `code`, `link`, `heading1`, `heading2`, `heading3`, `quote`, `bulletList`, `orderedList`, `taskList`, `codeBlock`, `table`, `divider`, `image`, `footnote`, `undo`, `redo`.
|
|
139
|
+
|
|
140
|
+
Headings, quotes and lists replace each other on a line, and the cursor ends after their marks. Italic writes `*`, which works inside a word; spaces at a selection's edges stay outside the marks. `image` opens the file picker when the editor has `upload`, and otherwise inserts an empty image card.
|
|
141
|
+
|
|
142
|
+
For a different toolbar entirely, call the editor's `run(command)` yourself. `ToolbarEditor` is the type of what the toolbar needs from an editor.
|
|
143
|
+
|
|
144
|
+
## Accessibility
|
|
145
|
+
|
|
146
|
+
`role="toolbar"`, with one tab stop: arrow keys, Home and End move between buttons. Formatting buttons show `aria-pressed`; `undo` and `redo` are `aria-disabled` when there's nothing to undo or redo. Buttons don't take focus from the editor.
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# Images
|
|
2
|
+
|
|
3
|
+
> Images, figures and uploads.
|
|
4
|
+
|
|
5
|
+
## In Markdown
|
|
6
|
+
|
|
7
|
+
Plain Markdown, no block needed: ``. Alone in its paragraph with a title, it becomes a figure:
|
|
8
|
+
|
|
9
|
+
```md
|
|
10
|
+

|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```html
|
|
14
|
+
<figure>
|
|
15
|
+
<img src="/cat.png" alt="A cat on a sofa" loading="lazy" decoding="async" />
|
|
16
|
+
<figcaption>Our cat, 2026</figcaption>
|
|
17
|
+
</figure>
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Size it with attributes so the page doesn't shift: `{width="800" height="600"}`. Uploads add them for you.
|
|
21
|
+
|
|
22
|
+
- `data:image/…` URLs work, for images only.
|
|
23
|
+
- An image without a URL (`![]()`) doesn't render. In the editor it's an empty card to fill.
|
|
24
|
+
- For responsive images or a CDN, replace `img` through `components`, e.g. `{ img: "NuxtImg" }`.
|
|
25
|
+
|
|
26
|
+
## In the editor
|
|
27
|
+
|
|
28
|
+
Images on their own line are cards: the picture, its caption and its alt text, edited in place. Arrowing onto a card selects it; Backspace or Delete removes it, Enter adds a paragraph after it. A card never turns back into Markdown; switch to source mode for that.
|
|
29
|
+
|
|
30
|
+
## Uploads
|
|
31
|
+
|
|
32
|
+
Give the editor an `upload` function: it gets the `File` and resolves to its URL.
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
async function upload(file: File) {
|
|
36
|
+
const body = new FormData();
|
|
37
|
+
body.append("file", file);
|
|
38
|
+
const { url } = await $fetch<{ url: string }>("/api/uploads", { method: "POST", body });
|
|
39
|
+
return url;
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
While it uploads, the card shows the local file at its real size, so nothing moves when the URL arrives. Several files upload side by side, in order. A failed upload fires `upload-error` and removes its placeholder, or puts a replaced card back the way it was.
|
|
44
|
+
|
|
45
|
+
On the server, check what you're given: the file type by its first bytes, not its name, and a size limit. Don't accept SVG on your own origin: it can run script.
|
|
46
|
+
|
|
47
|
+
```vue
|
|
48
|
+
<script setup lang="ts">
|
|
49
|
+
const body = ref("Drop, paste or choose an image:\n\n![]()\n");
|
|
50
|
+
const editor = useTemplateRef("editor");
|
|
51
|
+
const error = ref("");
|
|
52
|
+
|
|
53
|
+
// Your endpoint stores the file and answers with its URL. Here: the image inlined,
|
|
54
|
+
// after a moment, so you can see it upload.
|
|
55
|
+
async function upload(file: File) {
|
|
56
|
+
await new Promise((resolve) => setTimeout(resolve, 1500));
|
|
57
|
+
return new Promise<string>((resolve) => {
|
|
58
|
+
const reader = new FileReader();
|
|
59
|
+
reader.addEventListener("load", () => resolve(String(reader.result)));
|
|
60
|
+
reader.readAsDataURL(file);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
</script>
|
|
64
|
+
|
|
65
|
+
<template>
|
|
66
|
+
<div class="writer-demo">
|
|
67
|
+
<WriterToolbar :editor="editor" :items="['bold', 'italic', 'image']" />
|
|
68
|
+
<WriterEditor
|
|
69
|
+
ref="editor"
|
|
70
|
+
v-model="body"
|
|
71
|
+
label="Body"
|
|
72
|
+
:upload="upload"
|
|
73
|
+
@upload-error="(_, file) => (error = `Couldn't upload ${file.name}.`)"
|
|
74
|
+
/>
|
|
75
|
+
<p class="writer-demo-stats">
|
|
76
|
+
{{ error || (editor?.uploading ? "Uploading…" : "Ready to save") }}
|
|
77
|
+
</p>
|
|
78
|
+
</div>
|
|
79
|
+
</template>
|
|
80
|
+
```
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# Introduction
|
|
2
|
+
|
|
3
|
+
> A Markdown editor and renderer for Vue 3 and Nuxt.
|
|
4
|
+
|
|
5
|
+
Authors write Markdown, but it reads like the published page: formatting marks tuck away, blocks render as your own components, and images are cards with their caption and alt text. Readers get the same Markdown rendered by the same components, server-side.
|
|
6
|
+
|
|
7
|
+
```vue
|
|
8
|
+
<script setup lang="ts">
|
|
9
|
+
import { countWords, readingMinutes } from "@humanonlyweb/writer";
|
|
10
|
+
|
|
11
|
+
const body =
|
|
12
|
+
ref(`City council voted 7–2 on Tuesday to approve this winter's snow-clearing budget, which officials describe as "ambitious, in a spiritual sense."
|
|
13
|
+
|
|
14
|
+
::callout{type="note"}
|
|
15
|
+
The **full budget** is available at city hall, between 2:00 and 2:05 p.m.
|
|
16
|
+
::
|
|
17
|
+
|
|
18
|
+
## What residents can do
|
|
19
|
+
|
|
20
|
+
- Shovel early, shovel often
|
|
21
|
+
- [x] Buy salt
|
|
22
|
+
- [ ] Lower expectations
|
|
23
|
+
|
|
24
|
+
> "It's not a plan, it's a vibe," said one resident.
|
|
25
|
+
|
|
26
|
+
{width="900" height="360"}
|
|
27
|
+
|
|
28
|
+
Drop a photo of your street below, or choose one:
|
|
29
|
+
|
|
30
|
+
![]()
|
|
31
|
+
`);
|
|
32
|
+
|
|
33
|
+
const editor = useTemplateRef("editor");
|
|
34
|
+
const mode = ref<"live" | "source">("live");
|
|
35
|
+
const icons = {
|
|
36
|
+
heading2: "heading",
|
|
37
|
+
bold: "bold",
|
|
38
|
+
italic: "italic",
|
|
39
|
+
link: "link",
|
|
40
|
+
quote: "quote",
|
|
41
|
+
bulletList: "list",
|
|
42
|
+
image: "image",
|
|
43
|
+
} as const;
|
|
44
|
+
const words = computed(() => countWords(body.value));
|
|
45
|
+
|
|
46
|
+
// A stand-in for your upload endpoint: the image, inlined, after a moment.
|
|
47
|
+
async function upload(file: File) {
|
|
48
|
+
await new Promise((resolve) => setTimeout(resolve, 1500));
|
|
49
|
+
return new Promise<string>((resolve) => {
|
|
50
|
+
const reader = new FileReader();
|
|
51
|
+
reader.addEventListener("load", () => resolve(String(reader.result)));
|
|
52
|
+
reader.readAsDataURL(file);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
</script>
|
|
56
|
+
|
|
57
|
+
<template>
|
|
58
|
+
<div class="writer-demo">
|
|
59
|
+
<div class="writer-demo-bar">
|
|
60
|
+
<WriterToolbar :editor="editor" :items="Object.keys(icons) as (keyof typeof icons)[]">
|
|
61
|
+
<template #item="{ command }">
|
|
62
|
+
<Icon :name="icons[command as keyof typeof icons]" />
|
|
63
|
+
</template>
|
|
64
|
+
</WriterToolbar>
|
|
65
|
+
<button
|
|
66
|
+
type="button"
|
|
67
|
+
class="writer-demo-toggle"
|
|
68
|
+
:aria-pressed="mode === 'source'"
|
|
69
|
+
@click="mode = mode === 'source' ? 'live' : 'source'"
|
|
70
|
+
>
|
|
71
|
+
Markdown
|
|
72
|
+
</button>
|
|
73
|
+
</div>
|
|
74
|
+
<WriterEditor ref="editor" v-model="body" :mode="mode" label="Article" :upload="upload" />
|
|
75
|
+
<p class="writer-demo-stats">{{ words }} words · {{ readingMinutes(words) }} min read</p>
|
|
76
|
+
</div>
|
|
77
|
+
</template>
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
- **Markdown is the source of truth.** Store it, diff it, edit it anywhere.
|
|
81
|
+
- **Blocks are your Vue components.** `::callout` renders `Callout.vue`, in the editor and on the page.
|
|
82
|
+
- **Fast pages.** Parse on save and render the stored document: visitors download neither the parser nor the editor.
|
|
83
|
+
- **Safe by default.** Raw HTML is text, and only Markdown's own elements and your blocks render.
|
|
84
|
+
- **SSR, accessible, unstyled.** Works with server rendering, follows the ARIA patterns, and takes your site's CSS.
|
|
85
|
+
|
|
86
|
+
Built on [CodeMirror 6](https://codemirror.net) and [Comark](https://comark.dev).
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Installation
|
|
2
|
+
|
|
3
|
+
> Nuxt module or Vue plugin.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @humanonlyweb/writer
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Nuxt
|
|
12
|
+
|
|
13
|
+
The components are auto-imported, `core.css` is added, and every component in `components/blocks/` becomes a block.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
// nuxt.config.ts
|
|
17
|
+
export default defineNuxtConfig({
|
|
18
|
+
modules: ["@humanonlyweb/writer/nuxt"],
|
|
19
|
+
writer: {
|
|
20
|
+
blocks: "blocks", // folder in components/; false for none
|
|
21
|
+
components: { a: "NuxtLink" }, // render native elements with your components
|
|
22
|
+
css: true, // inject core.css
|
|
23
|
+
parser: { highlight: true }, // see Parsing
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
On the server, `parseWriterDocument`, `writerParserVersion` and `countWords` are auto-imported.
|
|
29
|
+
|
|
30
|
+
## Vue
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { createWriter } from "@humanonlyweb/writer";
|
|
34
|
+
import "@humanonlyweb/writer/core.css";
|
|
35
|
+
|
|
36
|
+
createApp(App)
|
|
37
|
+
.use(createWriter({ blocks: { callout: Callout }, components: { a: RouterLink } }))
|
|
38
|
+
.mount("#app");
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
| Option | What it does |
|
|
42
|
+
| -------------- | -------------------------------------------------------------------------------------------------- |
|
|
43
|
+
| `blocks` | Components authors can use, by Markdown name: `{ callout }` is `::callout`. See [Blocks](/blocks). |
|
|
44
|
+
| `components` | Native elements to render with your components, e.g. `{ a: RouterLink, img: AppImage }`. |
|
|
45
|
+
| `parser` | [Parser options](/parsing) for Markdown parsed in the browser. |
|
|
46
|
+
| `translations` | The built-in text. See [Localisation](/localisation). |
|
|
47
|
+
|
|
48
|
+
`useWriterConfig()` reads the config in your own components, and `WRITER_CONFIG_KEY` provides one yourself.
|
|
49
|
+
|
|
50
|
+
## Use it
|
|
51
|
+
|
|
52
|
+
```vue
|
|
53
|
+
<script setup lang="ts">
|
|
54
|
+
const body = ref("");
|
|
55
|
+
const editor = useTemplateRef("editor");
|
|
56
|
+
</script>
|
|
57
|
+
|
|
58
|
+
<template>
|
|
59
|
+
<WriterToolbar :editor="editor" />
|
|
60
|
+
<WriterEditor ref="editor" v-model="body" label="Post body" />
|
|
61
|
+
<WriterContent :value="body" />
|
|
62
|
+
</template>
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
In plain Vue, import `WriterEditor`, `WriterToolbar` and `WriterContent` from `@humanonlyweb/writer`.
|
|
66
|
+
|
|
67
|
+
## AI agents
|
|
68
|
+
|
|
69
|
+
The package ships these docs as Markdown, matching the installed version. Point your agent at them in `AGENTS.md` or `CLAUDE.md`:
|
|
70
|
+
|
|
71
|
+
```md
|
|
72
|
+
For @humanonlyweb/writer, read node_modules/@humanonlyweb/writer/dist/docs/README.md
|
|
73
|
+
before using it. It indexes a page per topic: components, parsing, storing documents.
|
|
74
|
+
```
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Limitations
|
|
2
|
+
|
|
3
|
+
> What it doesn't do yet.
|
|
4
|
+
|
|
5
|
+
- **Live preview** renders blocks and images on their own line. Tables, footnote references and inline images stay Markdown. A block still being typed (no closing `::`) stays Markdown until it's closed.
|
|
6
|
+
- **Indented content** (four spaces in, outside a block) stays Markdown: it's code, or deeply nested list content.
|
|
7
|
+
- **Blocks in the editor** render through the app, not the page: they get app-level plugins and `provide`s, but nothing a parent of `<WriterEditor>` provides.
|
|
8
|
+
- **Footnotes inside a block** are numbered within that block in the editor; the page numbers them across the document.
|
|
9
|
+
- **An empty image card** is 16:9 until a file is chosen.
|
|
10
|
+
- **Chinese, Japanese and Korean input** (IME composition) hasn't been tested by hand yet.
|
|
11
|
+
- **Tested** in Chromium, Firefox and WebKit, automatically. Touch devices and screen readers haven't been tested by hand yet.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Localisation
|
|
2
|
+
|
|
3
|
+
> Every string the components show or announce.
|
|
4
|
+
|
|
5
|
+
Pass only what you change. In Nuxt, use `app.config.ts`:
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
export default defineAppConfig({
|
|
9
|
+
writer: {
|
|
10
|
+
translations: { editor: "Éditeur", commands: { bold: "Gras" } },
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
In Vue, `createWriter({ translations })`. A ref keeps it live, e.g. when the reader switches language.
|
|
16
|
+
|
|
17
|
+
| Key | Default |
|
|
18
|
+
| ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
19
|
+
| `editor` | Markdown editor |
|
|
20
|
+
| `toolbar` | Formatting |
|
|
21
|
+
| `commands` | A label per command: Bold, Italic, Strikethrough, Code, Link, Heading 1–3, Quote, Bulleted list, Numbered list, Task list, Code block, Table, Divider, Image, Footnote, Undo, Redo |
|
|
22
|
+
| `task` | Task (a task checkbox's name before it has text) |
|
|
23
|
+
| `imageAlt` | Alt text |
|
|
24
|
+
| `imageAltHint` | Describe the image for people who can't see it |
|
|
25
|
+
| `imageCaption` | Add a caption |
|
|
26
|
+
| `imageDrop` | Drop an image here, or |
|
|
27
|
+
| `imageChoose` | Choose an image |
|
|
28
|
+
| `imageUrl` | Image URL |
|
|
29
|
+
| `imageReplace` | Replace |
|
|
30
|
+
| `imageRemove` | Remove |
|
|
31
|
+
| `imageUploading` | Uploading… |
|
|
32
|
+
|
|
33
|
+
`defaultTranslations` has them all; `useTranslations()` gives the resolved set.
|
|
34
|
+
|
|
35
|
+
Each line takes its own text direction, so right-to-left and mixed text work.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# Parsing
|
|
2
|
+
|
|
3
|
+
> Markdown to a document, anywhere.
|
|
4
|
+
|
|
5
|
+
## Parser
|
|
6
|
+
|
|
7
|
+
`@humanonlyweb/writer/parse` has no Vue and no DOM: it runs on the server, in a worker, in a script.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { createParser } from "@humanonlyweb/writer/parse";
|
|
11
|
+
|
|
12
|
+
const parse = createParser({ highlight: true, toc: true });
|
|
13
|
+
const document = await parse(markdown);
|
|
14
|
+
document.meta.toc; // { links: [{ id, text, depth, children }] }
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`parse` is `createParser()` with the defaults. Use the same options everywhere you parse: the Nuxt module's `parser`, `createWriter({ parser })` and the server.
|
|
18
|
+
|
|
19
|
+
## Options
|
|
20
|
+
|
|
21
|
+
| Option | Default | What it does |
|
|
22
|
+
| ----------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
23
|
+
| `footnotes` | `true` | `[^1]` footnotes. An object sets `label` and `backRef`. |
|
|
24
|
+
| `highlight` | `false` | Highlights fenced code with [rangi](https://comark.dev/plugins/built-in/rangi). An object sets `theme`, `lineNumbers`. Loads only for documents with code. |
|
|
25
|
+
| `toc` | `false` | Table of contents in `meta.toc`. An object sets `depth`. |
|
|
26
|
+
| `linkify` | `true` | URL-like text becomes links. |
|
|
27
|
+
| `plugins` | `[]` | Other Comark plugins, e.g. `comark/plugins/shiki`. They run before sanitizing. Not in `nuxt.config`: they aren't serializable, so parse on the server. |
|
|
28
|
+
|
|
29
|
+
## Syntax
|
|
30
|
+
|
|
31
|
+
CommonMark and GFM (tables, task lists, strikethrough, autolinks), frontmatter, footnotes, GitHub alerts, [blocks](/blocks) and `{.class #id key="value"}` attributes. Raw HTML is text.
|
|
32
|
+
|
|
33
|
+
A blank line starts a paragraph; a `\` at the end of a line breaks it. The editor's Enter and Shift+Enter write those for you.
|
|
34
|
+
|
|
35
|
+
## Dark mode code
|
|
36
|
+
|
|
37
|
+
rangi colours code inline for its light theme and puts the dark one in `--shiki-dark`:
|
|
38
|
+
|
|
39
|
+
```css
|
|
40
|
+
@media (prefers-color-scheme: dark) {
|
|
41
|
+
.shiki span {
|
|
42
|
+
color: var(--shiki-dark) !important;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
This site does exactly that.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Security
|
|
2
|
+
|
|
3
|
+
> Content is untrusted.
|
|
4
|
+
|
|
5
|
+
- Raw HTML is text.
|
|
6
|
+
- Only Markdown's own elements and registered blocks render. App-wide components can't be reached from Markdown.
|
|
7
|
+
- `script`, `style`, `iframe`, `object` and similar are removed, with their content. So are `on*` handlers, `style` attributes (except a highlighter's colours inside code) and URLs that aren't relative, `http(s)`, `mailto` or `tel`.
|
|
8
|
+
- `<WriterContent>` sanitizes again before rendering, so a stored document is safe even if it didn't come from this parser.
|
|
9
|
+
|
|
10
|
+
The checks are exported from `@humanonlyweb/writer/parse`: `sanitize(nodes, isAllowed?)`, `isSafeUrl(url)` and `NATIVE_TAGS`.
|
|
11
|
+
|
|
12
|
+
## What's left to you
|
|
13
|
+
|
|
14
|
+
- Authors can set `class` and `id` with `{…}`. With untrusted authors and utility-class CSS (`fixed inset-0`), that's enough to restyle the page: strip them from parsed documents, or render those elements through `components`.
|
|
15
|
+
- Blocks get attributes as written. Validate any URL your block puts in an attribute other than `href` or `src`.
|
|
16
|
+
- CodeMirror injects `<style>` elements. Under a strict CSP, pass `EditorView.cspNonce.of(nonce)` in `extensions`.
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Storing documents
|
|
2
|
+
|
|
3
|
+
> Store the Markdown, render a document parsed on save.
|
|
4
|
+
|
|
5
|
+
## The short version
|
|
6
|
+
|
|
7
|
+
Store the Markdown: it's the source of truth. Parse it on the server when saving, store that too, and render it. Visitors then download neither the parser nor the editor.
|
|
8
|
+
|
|
9
|
+
| Column | Type | Description |
|
|
10
|
+
| --------------------- | ---- | -------------------------------------------------------------- |
|
|
11
|
+
| `body` | text | What the editor produces. |
|
|
12
|
+
| `body_document` | json | Optional cache: the parsed document, for rendering. |
|
|
13
|
+
| `body_parser_version` | text | `writerParserVersion` when parsed: tells you when to re-parse. |
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
// server/api/posts/[id].put.ts
|
|
17
|
+
await db.posts.update(id, {
|
|
18
|
+
body: input.body,
|
|
19
|
+
body_document: await parseWriterDocument(input.body),
|
|
20
|
+
body_parser_version: writerParserVersion,
|
|
21
|
+
word_count: countWords(input.body),
|
|
22
|
+
});
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```vue
|
|
26
|
+
<WriterContent :value="post.body_document" />
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Outside Nuxt, `createParser(options)` from `@humanonlyweb/writer/parse` with the options your app renders with.
|
|
30
|
+
|
|
31
|
+
## Re-parsing
|
|
32
|
+
|
|
33
|
+
The parsed document is derived, so it can go stale. `writerParserVersion` is this package's version plus a hash of your `parser` options. When a stored one differs, parse the `body` again: on read, or in a batch after deploying. Outside Nuxt, do the same with the package version and your options.
|
|
34
|
+
|
|
35
|
+
## Drafts and published versions
|
|
36
|
+
|
|
37
|
+
On a real site, an autosave shouldn't change the live page. Keep the working copy on the article, and freeze what readers see as a revision when publishing:
|
|
38
|
+
|
|
39
|
+
```sql
|
|
40
|
+
-- The working copy the editor autosaves. Readers see published_revision_id.
|
|
41
|
+
CREATE TABLE articles (
|
|
42
|
+
id TEXT PRIMARY KEY,
|
|
43
|
+
slug TEXT NOT NULL UNIQUE,
|
|
44
|
+
section_id TEXT NOT NULL REFERENCES sections (id),
|
|
45
|
+
byline_id TEXT NOT NULL REFERENCES personas (id),
|
|
46
|
+
headline TEXT NOT NULL,
|
|
47
|
+
dek TEXT NOT NULL,
|
|
48
|
+
body TEXT NOT NULL, -- the editor's Markdown
|
|
49
|
+
updated_at TEXT NOT NULL,
|
|
50
|
+
published_revision_id TEXT REFERENCES revisions (id),
|
|
51
|
+
published_at TEXT
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
-- Published versions, frozen: everything readers see.
|
|
55
|
+
CREATE TABLE revisions (
|
|
56
|
+
id TEXT PRIMARY KEY,
|
|
57
|
+
article_id TEXT NOT NULL REFERENCES articles (id) ON DELETE CASCADE,
|
|
58
|
+
section_id TEXT NOT NULL REFERENCES sections (id),
|
|
59
|
+
byline_id TEXT NOT NULL REFERENCES personas (id),
|
|
60
|
+
headline TEXT NOT NULL,
|
|
61
|
+
dek TEXT NOT NULL,
|
|
62
|
+
body TEXT NOT NULL,
|
|
63
|
+
body_document TEXT NOT NULL, -- JSON: parseWriterDocument(body)
|
|
64
|
+
body_parser_version TEXT NOT NULL, -- writerParserVersion when parsed
|
|
65
|
+
word_count INTEGER NOT NULL,
|
|
66
|
+
created_at TEXT NOT NULL
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
-- Uploads. The files live in storage; the Markdown holds their url.
|
|
70
|
+
CREATE TABLE assets (
|
|
71
|
+
id TEXT PRIMARY KEY,
|
|
72
|
+
url TEXT NOT NULL UNIQUE,
|
|
73
|
+
type TEXT NOT NULL,
|
|
74
|
+
bytes INTEGER NOT NULL,
|
|
75
|
+
created_at TEXT NOT NULL
|
|
76
|
+
);
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Publishing parses the working copy into a new revision, then points the article at it:
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
async function publish(article: Article) {
|
|
83
|
+
const revision = {
|
|
84
|
+
id: crypto.randomUUID(),
|
|
85
|
+
articleId: article.id,
|
|
86
|
+
// …section, byline, headline, dek and body, copied from the article
|
|
87
|
+
body: article.body,
|
|
88
|
+
bodyDocument: await parseWriterDocument(article.body),
|
|
89
|
+
bodyParserVersion: writerParserVersion,
|
|
90
|
+
wordCount: countWords(article.body),
|
|
91
|
+
createdAt: new Date().toISOString(),
|
|
92
|
+
};
|
|
93
|
+
await db.revisions.insert(revision);
|
|
94
|
+
await db.articles.update(article.id, {
|
|
95
|
+
publishedRevisionId: revision.id,
|
|
96
|
+
publishedAt: article.publishedAt ?? revision.createdAt,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Before saving or publishing, wait for the editor's `uploading` to be `0`: until then the Markdown holds placeholders.
|
|
102
|
+
|
|
103
|
+
## Keep metadata out of the body
|
|
104
|
+
|
|
105
|
+
Headlines, slugs, dates and authors go in their own columns, not frontmatter, so you can query and index them. Things templates add around the body (related links, ads) stay out of it too. If editors place something by hand mid-article, make it a [block](/blocks).
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# Styling
|
|
2
|
+
|
|
3
|
+
> Tokens, hooks, and your site's CSS.
|
|
4
|
+
|
|
5
|
+
## Your CSS wins
|
|
6
|
+
|
|
7
|
+
`core.css` is in `@layer humanonlyweb-writer`, so anything you write overrides it. It styles the editor's Markdown source and gives the editor and rendered content one type scale: a heading is the same size in both.
|
|
8
|
+
|
|
9
|
+
Style rendered content (`[data-part="writer-content"]`) as your site's prose. The editor's previews use the same wrapper, so they pick it up too.
|
|
10
|
+
|
|
11
|
+
## Tokens
|
|
12
|
+
|
|
13
|
+
Set them on any ancestor; the editor and content both follow.
|
|
14
|
+
|
|
15
|
+
| Token | Default | Description |
|
|
16
|
+
| ------------------------------------------------------------ | ------------------------- | ----------------------------------------------------------------------------------- |
|
|
17
|
+
| `--writer-h1` … `--writer-h6` | The browser's | Heading sizes. |
|
|
18
|
+
| `--writer-heading-weight` | `bold` | |
|
|
19
|
+
| `--writer-code-font`, `--writer-code-size` | `ui-monospace`, `0.875em` | Inline and block code. |
|
|
20
|
+
| `--writer-quote-indent` | `1em` | Quotes. |
|
|
21
|
+
| `--writer-quote-border-width`, `--writer-quote-border-color` | `3px`, text at 25% | |
|
|
22
|
+
| `--writer-block-inset`, `--writer-block-background` | `0.75em`, text at 4% | An open block's padding and background: match your blocks' and the text won't jump. |
|
|
23
|
+
|
|
24
|
+
```css
|
|
25
|
+
:root {
|
|
26
|
+
--writer-h2: 1.6rem;
|
|
27
|
+
--writer-code-font: "JetBrains Mono", monospace;
|
|
28
|
+
--writer-block-inset: 1.25rem;
|
|
29
|
+
--writer-block-background: #fbf6e3;
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
If your prose CSS sets heading sizes directly, set these to the same values, or the editor won't match.
|
|
34
|
+
|
|
35
|
+
## Hooks
|
|
36
|
+
|
|
37
|
+
| Selector | Description |
|
|
38
|
+
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------ |
|
|
39
|
+
| `[data-part="writer-editor"]` | Editor root: `[data-mode]`, `[data-ready]`, `[data-readonly]`. |
|
|
40
|
+
| `[data-part="writer-fallback"]` | What shows before CodeMirror loads. |
|
|
41
|
+
| `[data-part="writer-host"] .cm-editor` | CodeMirror. |
|
|
42
|
+
| `[data-part="writer-content"]` | Rendered content, on the page and in the editor's previews. |
|
|
43
|
+
| `.cm-md-preview`, `.cm-md-block-open`, `.cm-md-block-fence` | Rendered blocks, and the lines of a block open for editing. |
|
|
44
|
+
| `[data-part="writer-image"]`, `.cm-md-image-frame`, `.cm-md-image-caption`, `.cm-md-image-alt` | Image cards: `[data-selected]`; the frame `[data-empty]`, `[data-uploading]`, `[data-dropping]`. |
|
|
45
|
+
| `.cm-md-bullet`, `.cm-md-task`, `.cm-md-quote-line`, `.cm-md-hr` | Live preview's stand-ins for list, task, quote and rule syntax. |
|
|
46
|
+
| `.cm-md-h1`…`.cm-md-h6`, `.cm-md-codeblock`, `.cm-md-strong`, `.cm-md-em`, `.cm-md-strike`, `.cm-md-code`, `.cm-md-link`, `.cm-md-quote`, `.cm-md-mark`, `.cm-md-comment` | Markdown source. |
|
|
47
|
+
| `[data-part="writer-toolbar"]`, `[data-part="writer-toolbar-item"][data-command]` | Toolbar and buttons: `[aria-pressed]`, `[aria-disabled]`. |
|