@usememos/editor 26.10.28 → 26.10.30
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/README.md +51 -1
- package/dist/index.js +85 -85
- package/dist/index.js.map +4 -4
- package/dist/react.js +2 -2
- package/dist/react.js.map +3 -3
- package/dist/types/editor.d.ts +16 -0
- package/dist/types/extension.d.ts +6 -2
- package/dist/types/index.d.ts +6 -1
- package/dist/types/markdown/htmlSection.d.ts +2 -0
- package/dist/types/markdown/tree.d.ts +26 -0
- package/dist/types/react/index.d.ts +1 -1
- package/dist/types/view/html.d.ts +29 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -63,6 +63,7 @@ The same names work in `new Editor(options)` and as React props, except that Rea
|
|
|
63
63
|
| `headingIds` | `(headings) => (string \| undefined)[]` | Element ids for headings, for in-page links and an outline. Called with every heading's `{ text, level }` in order, again when the document changes. |
|
|
64
64
|
| `codeCopy` | `boolean` | A copy button at the end of each code block's bar, shown on hover or focus, that copies the code. Default `true`. Its text is `labels.copyCode` and `labels.copied`, its icons `icons.copyCode` and `icons.copied`. |
|
|
65
65
|
| `referrerPolicy` | `ReferrerPolicy` | `referrerpolicy` for images, such as `no-referrer`. Images always load lazily. |
|
|
66
|
+
| `sanitizeHtml` | `(tree, { kind, editor }) => Node \| null \| Promise` | Renders raw HTML. Without it, HTML blocks and inline HTML show as their source: the editor renders no HTML on its own. See [Raw HTML](#raw-html). |
|
|
66
67
|
| `onLinkClick` | `({ editor, href, event }) => boolean` | Called when a link is clicked to be opened: a plain click while read-only, Mod+click while editing, or Open on the link card. Return `true` to handle it, for example to route an in-app link or scroll to an anchor; otherwise the editor opens `href` in a new tab. |
|
|
67
68
|
| `theme` | `{ classes?, tokens?, colorScheme? }` | Your own classes on rendered elements, colors and fonts, and light or dark. See [Theming](#theming). |
|
|
68
69
|
| `toolbar` | `boolean \| { items? }` | Shows the formatting toolbar on selection, and the image toolbar on a selected image (see [Images](#images)). Default `true`. `items` lists the formatting toolbar's controls; they keep the built-in order: `textStyle`, `bold`, `italic`, `strike`, `inlineCode`, `link`, `quote`, `codeBlock`, `list` (exported as `TOOLBAR_ITEMS`). For example, `{ items: ['bold', 'italic', 'strike', 'inlineCode', 'link'] }` keeps it to inline formatting. `false` turns both toolbars off. |
|
|
@@ -127,6 +128,30 @@ function Composer() {
|
|
|
127
128
|
|
|
128
129
|
`insertToken(text)` inserts a token such as `#tag` at the caret as a word of its own: a space before it unless the caret is at the start of the text or after whitespace, and a space after it, reusing one that is already there; the caret ends after that space. Selected text is kept, the token takes the format at the caret as typed text would (plain text in a code block), and it returns `false` without inserting while an input method is composing.
|
|
129
130
|
|
|
131
|
+
### Syntax tree
|
|
132
|
+
|
|
133
|
+
A host that reads a document as data, without an editor, uses the parser directly. `parseAst(markdown, options?)` returns the CommonMark and GFM syntax tree the editor itself loads from, with the same `markdown` options and extension syntax, so a walk over the tree sees the document the editor shows. Link and image references are resolved in it (`destination` and `title` are final), escapes and entities are decoded, and `Root.definitions` keeps the link reference definitions as source text. The tree is read-only: edits go through the document (`parseMarkdown`, `serializeMarkdown`) or through the source text at the positions below.
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
import { inlineNodesText, parseAst, sourceRange, visit } from '@usememos/editor';
|
|
137
|
+
|
|
138
|
+
const root = parseAst(markdown, { breaks: true });
|
|
139
|
+
const tasks: { checked: boolean; line: number }[] = [];
|
|
140
|
+
const images: string[] = [];
|
|
141
|
+
let excerpt = '';
|
|
142
|
+
visit(root, (node) => {
|
|
143
|
+
if (node.type === 'listItem' && node.task) tasks.push({ checked: node.checked, line: node.lines!.start });
|
|
144
|
+
if (node.type === 'image') images.push(node.destination);
|
|
145
|
+
if (node.type === 'paragraph' && !excerpt) excerpt = inlineNodesText(node.children, '\n');
|
|
146
|
+
});
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
- `visit(node, visitor)` calls the visitor for every node in document order with its ancestors, root first: list items, the blocks in them, a table's cells row by row, an image's description. Returning `false` skips a node's children. `nodeChildren(node)` gives one node's children in that order.
|
|
150
|
+
- Every block the parser builds has `lines`, its 1-based inclusive source lines; a list item's first line holds its marker, so a task can be toggled by rewriting `[ ]` on that line. The root's `lines` has them by child index.
|
|
151
|
+
- Every inline node the parser builds has `pos`, a range in its block's content as the block parser built it, after quote markers and list indentation were taken off. `sourceRange(markdown, block.source, node.pos)` maps it to offsets in the Markdown; for a table cell, the block's source is `table.sources[row][cell]`. Pass `lineTable(markdown)` instead of the text for many lookups in one document.
|
|
152
|
+
- `inlineNodesText(nodes, soft)` is the text without markup, with `soft` for a soft line break (a space, or `"\n"` with `markdown.breaks`) and `"\n"` for a hard one, as in image alt text.
|
|
153
|
+
- An HTML block's `value` is its source; an image inside one is read as HTML, not from the tree. Nodes also carry `syntax`, `pieces` and the details of `source`, which source mode uses; those may change between versions.
|
|
154
|
+
|
|
130
155
|
### Extensions
|
|
131
156
|
|
|
132
157
|
An extension is a plain object. All fields except `name` are optional.
|
|
@@ -243,7 +268,7 @@ Options: `limit` (default 8), `minLength`, the letters typed before the popup op
|
|
|
243
268
|
|
|
244
269
|
`markdown` adds syntax; see `math()`, `footnotes()`, [docs/syntax.md](docs/syntax.md) and [docs/architecture.md](docs/architecture.md).
|
|
245
270
|
|
|
246
|
-
`views` shows a syntax rendered instead of as source, keyed by syntax name. A key of `code:` and a language (`code:mermaid`) renders code blocks of that language, such as diagrams.
|
|
271
|
+
`views` shows a syntax rendered instead of as source, keyed by syntax name. A key of `code:` and a language (`code:mermaid`) renders code blocks of that language, such as diagrams. Raw HTML is not a view an extension provides: the editor parses it and the `sanitizeHtml` option decides what is shown (see [Raw HTML](#raw-html)), so `html` is not a key here. While editing, a token shows its source when the caret touches it, and a block when the selection is in it; clicking either does the same. Read-only always shows the rendered view. A view that finishes later, such as a diagram library that loads on demand, fills in a placeholder and returns a promise: the editor shows the placeholder, then the finished element; a promise that rejects or resolves to `false` shows the source. A view's element is not in the page yet, so a view that lays out text, such as a diagram, measures it with its fourth argument, the editor: `getComputedStyle(ctx.editor.root)` gives the font, for a canvas `measureText`. Rendered views are cached by source; `editor.refreshViews(syntax?)` renders them again when what they show depends on something else that changed, such as a renderer that finished loading, and the editor calls it itself when the theme's font tokens change. `math()` uses this when given a render function; the core has no math renderer of its own, so pass one such as KaTeX. A render function may return a promise, for a renderer loaded on demand; the formula's source shows until it resolves:
|
|
247
272
|
|
|
248
273
|
```ts
|
|
249
274
|
import katex from 'katex';
|
|
@@ -438,6 +463,31 @@ Make one with Collapsible section in the slash menu, Mod+Shift+6, or `>>> ` at t
|
|
|
438
463
|
|
|
439
464
|
Only a `<details>` written the way the editor saves it loads as a section: the opening tag and a `<summary>` of plain text, a blank line, Markdown, and `</details>` at the start of a line. Other `<details>`, such as one on a single line, stay raw HTML.
|
|
440
465
|
|
|
466
|
+
## Raw HTML
|
|
467
|
+
|
|
468
|
+
Markdown may hold HTML: an HTML block such as `<div align="center">` or `<!-- comment -->`, an HTML section whose tags enclose Markdown across blank lines (see [HTML blocks](docs/syntax.md#html-blocks)), and inline HTML such as `<kbd>Ctrl</kbd>`. All of it is edited as source and saved verbatim. The editor renders none of it on its own: without `sanitizeHtml`, HTML shows as its source.
|
|
469
|
+
|
|
470
|
+
`sanitizeHtml` renders it. The editor parses each HTML block and each inline run (an opening tag through its closing tag, or a single tag) with the browser's parser into a `DocumentFragment` that is not in the page, so nothing in it has run or loaded, and calls the function with the fragment and `{ kind, editor }`, where `kind` is `'block'` or `'inline'`. Return the tree to attach, after removing what your policy does not allow, or `null` to show the source instead. The function may return a promise, for a sanitizer loaded on demand: the HTML shows nothing until it settles, then the tree, and a rejection shows the source. Results are cached by source; `editor.refreshViews('html')` runs the sanitizer again.
|
|
471
|
+
|
|
472
|
+
```ts
|
|
473
|
+
// A small allowlist. An app with richer HTML passes the tree to a sanitizer library and returns the result.
|
|
474
|
+
const ALLOWED = new Set(['details', 'summary', 'kbd', 'sup', 'sub', 'br', 'div', 'p', 'span']);
|
|
475
|
+
const editor = new Editor({
|
|
476
|
+
element,
|
|
477
|
+
sanitizeHtml: (tree) => {
|
|
478
|
+
for (const el of Array.from(tree.querySelectorAll('*'))) {
|
|
479
|
+
if (!ALLOWED.has(el.localName)) el.remove();
|
|
480
|
+
else for (const name of el.getAttributeNames()) if (name !== 'align') el.removeAttribute(name);
|
|
481
|
+
}
|
|
482
|
+
return tree;
|
|
483
|
+
},
|
|
484
|
+
});
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
The Markdown between the tags is the editor's. The fragment holds a marker (private-use characters) in the text where it goes; keep the text, and the editor renders that Markdown itself, as blocks inside a section or inline text inside a run, with footnotes, tags, math and code highlighting as anywhere else, and puts it in place of the marker after the sanitizer has run. A sanitizer that removes the element around a marker loses nothing: that Markdown follows the rest.
|
|
488
|
+
|
|
489
|
+
The sanitizer is also the place for product policy: which hosts an `<iframe>` may load from, `target` and `rel` on links, `referrerpolicy` on images, classes. The editor keeps no such policy of its own. While editing, an HTML block shows its source when the selection is in it, and an inline run when the caret touches it; a click does the same. Read-only always shows the rendered HTML. The rendered element has the class `mbe-view-html`.
|
|
490
|
+
|
|
441
491
|
## Source mode
|
|
442
492
|
|
|
443
493
|
With `mode: 'source'`, the editor shows the whole document as Markdown text, as Obsidian's source mode and GitLab's plain text editor do. The editor has no button or key for it: the app switches with `setOptions({ mode })` or the React `mode` prop, and remembers the choice if it wants to.
|