@usememos/editor 26.10.12 → 26.10.13

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 CHANGED
@@ -66,6 +66,7 @@ The same names work in `new Editor(options)` and as React props, except that Rea
66
66
  | `toolbar` | `boolean \| { items? }` | Shows the formatting toolbar on selection. Default `true`. `items` lists its 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. |
67
67
  | `popupContainer` | `HTMLElement` | Where menus, the toolbar and the autocomplete popup are appended. Default: the nearest open `<dialog>` or `aria-modal` ancestor of `element`, else `document.body`. |
68
68
  | `slashMenu` | `false \| string[]` | Built-in slash menu items to show, in order, or `false` for none: `text`, `heading1`, `heading2`, `heading3`, `bulletList`, `numberedList`, `todoList`, `quote`, `code`, `table`, `divider`. |
69
+ | `slashHint` | `boolean` | Shows `labels.slashHint` ("Type '/' for commands") in the empty paragraph that has the caret. Default `false`. No effect when `slashMenu` is `false`. When the document is a single empty paragraph, `placeholder` shows instead. |
69
70
  | `labels` | `Partial<Labels>` | Overrides UI strings, for localization. See `src/labels.ts`. |
70
71
  | `icons` | `Partial<Record<IconName, string \| Element>>` | Replaces built-in icons, which are [Lucide](https://lucide.dev) icons. A string is shown as text; an element, such as an `<svg>`, is cloned for every place it appears. Names match the `labels` keys of the same controls: the slash menu items, the block actions (`duplicate`, `delete`, the table cell actions, `altText` and `resetImageSize`), `bold`, `italic`, `strike`, `inlineCode`, `link`, `chevronDown` and `selected` (the formatting toolbar's menu buttons and their check mark), `chevronRight` (a menu row that opens a submenu), `imageAlignLeft`, `imageAlignCenter` and `imageAlignRight` (an image block's Align choices), `blockHandle` and `imageUnavailable` (shown in place of an image that did not load). |
71
72
  | `onChange` | `(markdown: string, editor) => void` | Called after document changes, at most once per animation frame. |
@@ -221,7 +222,7 @@ The popup lines up with the trigger under the caret's line, follows scrolling, r
221
222
 
222
223
  `markdown` adds syntax; see `math()`, `footnotes()`, [docs/syntax.md](docs/syntax.md) and [docs/architecture.md](docs/architecture.md).
223
224
 
224
- `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. The key `html` renders raw HTML: HTML blocks (`kind` `'block'`), and inline HTML (`'inline'`) one run at a time, an opening tag through its closing tag with the Markdown between (`<kbd>**Ctrl**</kbd>`), or a single tag (`<br>`). The editor never renders HTML itself, so sanitize it in the view. 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. `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:
225
+ `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. The key `html` renders raw HTML: HTML blocks (`kind` `'block'`), and inline HTML (`'inline'`) one run at a time, an opening tag through its closing tag (`<kbd>**Ctrl**</kbd>`), or a single tag (`<br>`). The view gets HTML only: the editor renders the Markdown between the tags itself, inline text and the blocks inside an HTML section such as `<details>` (see [HTML blocks](docs/syntax.md#html-blocks)), and puts it in place of a marker it leaves in the HTML, so parse the source as HTML and keep its text. The editor never renders HTML itself, so sanitize it in the view. 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. `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:
225
226
 
226
227
  ```ts
227
228
  import katex from 'katex';
package/dist/base.css CHANGED
@@ -29,7 +29,7 @@
29
29
  margins collapse, so blocks sit one gap apart and a block with larger
30
30
  margins (a heading, a divider) wins. No sibling selectors: in Chrome they
31
31
  make every edit restyle and re-lay out all blocks after the edited one. */
32
- .mbe-root > .mbe-block {
32
+ :is(.mbe-root, .mbe-flow) > .mbe-block {
33
33
  margin: var(--mbe-block-gap) 0;
34
34
  position: relative;
35
35
  /* Margins inside a block never merge with the block's own, so a block lays
@@ -52,31 +52,31 @@
52
52
  }
53
53
 
54
54
  /* Headings: more room above than below. */
55
- .mbe-root > :is(h1, h2).mbe-block { margin-top: 20px; }
56
- .mbe-root > h3.mbe-block { margin-top: 20px; margin-bottom: 6px; }
57
- .mbe-root > :is(h4, h5, h6).mbe-block { margin-top: 16px; margin-bottom: 4px; }
55
+ :is(.mbe-root, .mbe-flow) > :is(h1, h2).mbe-block { margin-top: 20px; }
56
+ :is(.mbe-root, .mbe-flow) > h3.mbe-block { margin-top: 20px; margin-bottom: 6px; }
57
+ :is(.mbe-root, .mbe-flow) > :is(h4, h5, h6).mbe-block { margin-top: 16px; margin-bottom: 4px; }
58
58
 
59
59
  /* Quote: the renderer marks a quote block that follows another (see
60
60
  updateNeighborMarks). The negative margin cancels the gap, and padding
61
61
  keeps a border unbroken, so consecutive quote blocks read as one quote. */
62
- .mbe-root > blockquote[data-quote-joined] { margin-top: calc(-1 * var(--mbe-block-gap)); padding-top: var(--mbe-block-gap); }
62
+ :is(.mbe-root, .mbe-flow) > blockquote[data-quote-joined] { margin-top: calc(-1 * var(--mbe-block-gap)); padding-top: var(--mbe-block-gap); }
63
63
 
64
64
  /* Lists: items sit close together; the list as a whole gets the block gap.
65
65
  The renderer marks the first item of each list with data-list-start
66
66
  (see updateNeighborMarks). Item margins of 2px collapse with a neighbor's
67
67
  gap, so only the gap shows at either end of a list. */
68
- .mbe-root > .mbe-list-item {
68
+ :is(.mbe-root, .mbe-flow) > .mbe-list-item {
69
69
  display: flex;
70
70
  align-items: flex-start;
71
71
  margin: 2px 0;
72
72
  }
73
- .mbe-root > .mbe-list-item[data-list-start] { margin-top: var(--mbe-block-gap); }
73
+ :is(.mbe-root, .mbe-flow) > .mbe-list-item[data-list-start] { margin-top: var(--mbe-block-gap); }
74
74
  /* Attribute rules instead of an inline custom property, so list items share computed styles. */
75
- .mbe-root > .mbe-list-item[data-indent='1'] { padding-inline-start: var(--mbe-indent-size); }
76
- .mbe-root > .mbe-list-item[data-indent='2'] { padding-inline-start: calc(2 * var(--mbe-indent-size)); }
77
- .mbe-root > .mbe-list-item[data-indent='3'] { padding-inline-start: calc(3 * var(--mbe-indent-size)); }
78
- .mbe-root > .mbe-list-item[data-indent='4'] { padding-inline-start: calc(4 * var(--mbe-indent-size)); }
79
- .mbe-root > .mbe-list-item[data-indent='5'] { padding-inline-start: calc(5 * var(--mbe-indent-size)); }
75
+ :is(.mbe-root, .mbe-flow) > .mbe-list-item[data-indent='1'] { padding-inline-start: var(--mbe-indent-size); }
76
+ :is(.mbe-root, .mbe-flow) > .mbe-list-item[data-indent='2'] { padding-inline-start: calc(2 * var(--mbe-indent-size)); }
77
+ :is(.mbe-root, .mbe-flow) > .mbe-list-item[data-indent='3'] { padding-inline-start: calc(3 * var(--mbe-indent-size)); }
78
+ :is(.mbe-root, .mbe-flow) > .mbe-list-item[data-indent='4'] { padding-inline-start: calc(4 * var(--mbe-indent-size)); }
79
+ :is(.mbe-root, .mbe-flow) > .mbe-list-item[data-indent='5'] { padding-inline-start: calc(5 * var(--mbe-indent-size)); }
80
80
  .mbe-root .mbe-marker {
81
81
  flex: none;
82
82
  box-sizing: border-box;
@@ -156,27 +156,44 @@
156
156
  padding: 0;
157
157
  width: 12em;
158
158
  }
159
- .mbe-root > .mbe-code pre,
160
- .mbe-root > pre.mbe-raw { white-space: pre-wrap; tab-size: 2; }
161
- .mbe-root > .mbe-code pre { margin: 0; }
162
- .mbe-root > .mbe-code pre code { display: block; }
159
+ :is(.mbe-root, .mbe-flow) > .mbe-code pre,
160
+ :is(.mbe-root, .mbe-flow) > pre.mbe-raw { white-space: pre-wrap; tab-size: 2; }
161
+ :is(.mbe-root, .mbe-flow) > .mbe-code pre { margin: 0; }
162
+ :is(.mbe-root, .mbe-flow) > .mbe-code pre code { display: block; }
163
163
 
164
164
  /* Rendered views (see Extension.views): a token or raw block shown rendered,
165
165
  such as math. Clicking one, or moving the caret onto it, shows its source. */
166
- .mbe-root > .mbe-view-block { overflow-x: auto; overflow-y: hidden; }
166
+ :is(.mbe-root, .mbe-flow) > .mbe-view-block { overflow-x: auto; overflow-y: hidden; }
167
167
  /* A renderer's own block margins (KaTeX's display math has 1em) would add to the block gap. */
168
- .mbe-root > .mbe-view-block > * { margin-block: 0; }
168
+ :is(.mbe-root, .mbe-flow) > .mbe-view-block > * { margin-block: 0; }
169
+ /* Rendered HTML: line breaks between its tags are not text. The Markdown
170
+ between the tags of an HTML section (.mbe-flow) is blocks again, one
171
+ block gap from the HTML around it. */
172
+ .mbe-root .mbe-view-block.mbe-view-html { white-space: normal; }
173
+ .mbe-root .mbe-view-html .mbe-flow { white-space: pre-wrap; margin-block: var(--mbe-block-gap); }
174
+ .mbe-root .mbe-view-html :is(p, blockquote, pre, ul, ol, dl, table, details, figure, hr, h1, h2, h3, h4, h5, h6):not(.mbe-flow *) { margin-block: var(--mbe-block-gap); }
175
+ .mbe-root .mbe-view-html :is(ul, ol):not(.mbe-flow *) { padding-inline-start: var(--mbe-indent-size); }
176
+ .mbe-root .mbe-view-html ul:not(.mbe-flow *) { list-style: disc; }
177
+ .mbe-root .mbe-view-html ol:not(.mbe-flow *) { list-style: decimal; }
178
+ .mbe-root .mbe-view-html pre:not(.mbe-flow *) { white-space: pre; overflow-x: auto; }
179
+ .mbe-root .mbe-view-html summary:not(.mbe-flow *) { cursor: pointer; }
180
+ .mbe-root .mbe-view-html img:not(.mbe-flow *) { max-width: 100%; }
181
+ /* Nothing above the first element or below the last, as for blocks. */
182
+ .mbe-root .mbe-view-html :is(details, blockquote, li, td, th, div, section):not(.mbe-flow *) > :first-child,
183
+ .mbe-root .mbe-view-block.mbe-view-html > :first-child { margin-top: 0; }
184
+ .mbe-root .mbe-view-html :is(details, blockquote, li, td, th, div, section):not(.mbe-flow *) > :last-child,
185
+ .mbe-root .mbe-view-block.mbe-view-html > :last-child { margin-bottom: 0; }
169
186
 
170
187
  /* Divider: the padding gives the line a larger click target. */
171
- .mbe-root > .mbe-divider.mbe-block { margin: 16px 0; padding: 4px 0; cursor: default; }
172
- .mbe-root > .mbe-divider hr { margin: 0; }
188
+ :is(.mbe-root, .mbe-flow) > .mbe-divider.mbe-block { margin: 16px 0; padding: 4px 0; cursor: default; }
189
+ :is(.mbe-root, .mbe-flow) > .mbe-divider hr { margin: 0; }
173
190
 
174
191
  /* Table */
175
- .mbe-root > .mbe-table { overflow-x: auto; }
192
+ :is(.mbe-root, .mbe-flow) > .mbe-table { overflow-x: auto; }
176
193
  /* Columns fit their content and wrap at the editor's width; wider tables scroll. */
177
- .mbe-root > .mbe-table table { border-collapse: separate; border-spacing: 0; }
178
- .mbe-root > .mbe-table th,
179
- .mbe-root > .mbe-table td {
194
+ :is(.mbe-root, .mbe-flow) > .mbe-table table { border-collapse: separate; border-spacing: 0; }
195
+ :is(.mbe-root, .mbe-flow) > .mbe-table th,
196
+ :is(.mbe-root, .mbe-flow) > .mbe-table td {
180
197
  padding: 4px 8px;
181
198
  min-width: 4em;
182
199
  vertical-align: top;
@@ -185,23 +202,24 @@
185
202
  }
186
203
 
187
204
  /* Nothing above the first block or below the last, whatever their type. */
188
- .mbe-root > .mbe-block:first-child { margin-top: 0; }
189
- .mbe-root > .mbe-block:last-child { margin-bottom: 0; }
205
+ :is(.mbe-root, .mbe-flow) > .mbe-block:first-child { margin-top: 0; }
206
+ :is(.mbe-root, .mbe-flow) > .mbe-block:last-child { margin-bottom: 0; }
190
207
 
191
- /* Placeholders and hints. When both apply, the slash hint wins over the placeholder. */
208
+ /* Placeholders and hints. When both apply, the slash hint wins over the placeholder.
209
+ Hints for empty headings, list items and quotes are for writing, so read-only hides them. */
192
210
  .mbe-root .mbe-empty-content { position: relative; }
193
211
  .mbe-root > .mbe-paragraph.mbe-empty:only-child::before,
194
212
  .mbe-root > .mbe-paragraph.mbe-empty.mbe-show-hint::before,
195
- .mbe-root > .mbe-empty[data-hint]:is(h1, h2, h3, h4, h5, h6, blockquote)::before,
196
- .mbe-root > .mbe-list-item.mbe-empty[data-hint] > .mbe-content::before {
213
+ .mbe-root:not(.mbe-readonly) > .mbe-empty[data-hint]:is(h1, h2, h3, h4, h5, h6, blockquote)::before,
214
+ .mbe-root:not(.mbe-readonly) > .mbe-list-item.mbe-empty[data-hint] > .mbe-content::before {
197
215
  opacity: 0.5;
198
216
  pointer-events: none;
199
217
  position: absolute;
200
218
  }
201
219
  .mbe-root > .mbe-paragraph.mbe-empty:only-child::before { content: var(--mbe-placeholder, ''); }
202
220
  .mbe-root > .mbe-paragraph.mbe-empty.mbe-show-hint::before { content: var(--mbe-slash-hint, ''); }
203
- .mbe-root > .mbe-empty[data-hint]:is(h1, h2, h3, h4, h5, h6, blockquote)::before,
204
- .mbe-root > .mbe-list-item.mbe-empty[data-hint] > .mbe-content::before { content: attr(data-hint); }
221
+ .mbe-root:not(.mbe-readonly) > .mbe-empty[data-hint]:is(h1, h2, h3, h4, h5, h6, blockquote)::before,
222
+ .mbe-root:not(.mbe-readonly) > .mbe-list-item.mbe-empty[data-hint] > .mbe-content::before { content: attr(data-hint); }
205
223
 
206
224
  /* Widgets: app UI between blocks, such as an upload placeholder. Not editable text. */
207
225
  .mbe-root > .mbe-widget { cursor: default; user-select: none; -webkit-user-select: none; white-space: normal; }
package/dist/index.js CHANGED
@@ -2142,6 +2142,7 @@ var DEFAULT_LABELS = {
2142
2142
  list: "List",
2143
2143
  linkPlaceholder: "Paste or type a link\u2026",
2144
2144
  removeLink: "Remove link",
2145
+ /** Shown in the empty paragraph that has the caret, with the `slashHint` option. */
2145
2146
  slashHint: "Type '/' for commands",
2146
2147
  blockHandle: "Open block menu",
2147
2148
  listHint: "List",
@@ -2521,7 +2522,8 @@ function lowerQuote(children, out, o) {
2521
2522
  for (const child of children) {
2522
2523
  if (child.type !== "paragraph") continue;
2523
2524
  const content = lowerInlines(child.children, o);
2524
- if (content.length > 0) out.push(quote(content));
2525
+ if (isEmptyLine(content)) out.push(quote([]));
2526
+ else if (content.length > 0) out.push(quote(content));
2525
2527
  }
2526
2528
  }
2527
2529
 
@@ -4069,6 +4071,93 @@ function parseDelimiterRow(line) {
4069
4071
  return align;
4070
4072
  }
4071
4073
 
4074
+ // src/markdown/htmlSection.ts
4075
+ var VOID_ELEMENTS = /* @__PURE__ */ new Set([
4076
+ "area",
4077
+ "base",
4078
+ "br",
4079
+ "col",
4080
+ "embed",
4081
+ "hr",
4082
+ "img",
4083
+ "input",
4084
+ "keygen",
4085
+ "link",
4086
+ "meta",
4087
+ "param",
4088
+ "source",
4089
+ "track",
4090
+ "wbr"
4091
+ ]);
4092
+ var reOpaque = /<!--[\s\S]*?-->|<(script|style|textarea|pre)\b[^>]*>[\s\S]*?<\/\1\s*>/gi;
4093
+ var reTag = /<(\/?)([A-Za-z][A-Za-z0-9-]*)\b(?:[^>"']|"[^"]*"|'[^']*')*?(\/?)>/g;
4094
+ function applyTags(html, open) {
4095
+ const text = html.replace(reOpaque, "");
4096
+ reTag.lastIndex = 0;
4097
+ for (let m = reTag.exec(text); m; m = reTag.exec(text)) {
4098
+ const name = m[2].toLowerCase();
4099
+ if (!m[1]) {
4100
+ if (!m[3] && !VOID_ELEMENTS.has(name)) open.push(name);
4101
+ continue;
4102
+ }
4103
+ const at = open.lastIndexOf(name);
4104
+ if (at >= 0) open.length = at;
4105
+ }
4106
+ }
4107
+ function htmlSectionEnd(nodes, i) {
4108
+ const first = nodes[i];
4109
+ if (first.type !== "htmlBlock") return i;
4110
+ const open = [];
4111
+ applyTags(first.value, open);
4112
+ if (open.length === 0) return i;
4113
+ for (let j = i + 1; j < nodes.length; j++) {
4114
+ const node = nodes[j];
4115
+ if (node.type !== "htmlBlock") continue;
4116
+ applyTags(node.value, open);
4117
+ if (open.length === 0) return j;
4118
+ }
4119
+ return i;
4120
+ }
4121
+ var slotMarker = (index) => `\uE000${index}\uE001`;
4122
+ var SLOT_MARKER = /(\d+)/g;
4123
+ function splitHtmlSection(source, options = DEFAULT_MARKDOWN) {
4124
+ const root = parseAst(source, options);
4125
+ if (root.children.every((node) => node.type === "htmlBlock")) return null;
4126
+ const lineStarts = [0];
4127
+ const lineEnds = [];
4128
+ const re = /\r\n|\n|\r/g;
4129
+ for (let m = re.exec(source); m; m = re.exec(source)) {
4130
+ lineEnds.push(m.index);
4131
+ lineStarts.push(m.index + m[0].length);
4132
+ }
4133
+ lineEnds.push(source.length);
4134
+ const textOf = (i) => {
4135
+ const lines = root.lines[i];
4136
+ const from = lineStarts[lines.start - 1] ?? source.length;
4137
+ const to = lineEnds[Math.max(lines.start, lines.end) - 1] ?? source.length;
4138
+ return source.slice(from, Math.max(from, to));
4139
+ };
4140
+ const parts = [];
4141
+ const slots = [];
4142
+ let slot = null;
4143
+ root.children.forEach((node, i) => {
4144
+ if (node.type === "htmlBlock") {
4145
+ slot = null;
4146
+ parts.push(textOf(i));
4147
+ return;
4148
+ }
4149
+ if (!slot) {
4150
+ slot = [];
4151
+ slots.push(slot);
4152
+ parts.push(`
4153
+ ${slotMarker(slots.length - 1)}
4154
+ `);
4155
+ }
4156
+ slot.push(...lowerTopLevel(node, options, textOf(i)));
4157
+ });
4158
+ return { html: parts.join("\n"), slots };
4159
+ }
4160
+
4072
4161
  // src/markdown/serialize.ts
4073
4162
  function serializeMarkdown(doc, options = {}) {
4074
4163
  if (options.source) return saveWithSource(doc.blocks, options.source, options);
@@ -4083,7 +4172,6 @@ function serializeBlocks(blocks, options) {
4083
4172
  let prev = null;
4084
4173
  const lists = new ListState(ctx.paragraph);
4085
4174
  for (const block3 of blocks) {
4086
- if (block3.type === "quote" && block3.content.length === 0) continue;
4087
4175
  let text;
4088
4176
  let separator = "\n\n";
4089
4177
  switch (block3.type) {
@@ -4091,7 +4179,8 @@ function serializeBlocks(blocks, options) {
4091
4179
  text = block3.content.length === 0 ? "&nbsp;" : inlineToMarkdown(block3.content, ctx.paragraph);
4092
4180
  break;
4093
4181
  case "quote": {
4094
- text = prefixLines(inlineToMarkdown(block3.content, ctx.paragraph), "> ", ">");
4182
+ const body = block3.content.length === 0 ? "&nbsp;" : inlineToMarkdown(block3.content, ctx.paragraph);
4183
+ text = prefixLines(body, "> ", ">");
4095
4184
  if (prev?.type === "quote") separator = "\n>\n";
4096
4185
  break;
4097
4186
  }
@@ -4257,7 +4346,7 @@ function representableInline(inline3) {
4257
4346
  for (const span of spans) {
4258
4347
  if (span.marks.includes(mark)) {
4259
4348
  if (runStart < 0) runStart = pos;
4260
- runText += span.text;
4349
+ runText += span.marks.includes("code") ? "x".repeat(span.text.length) : span.text;
4261
4350
  } else {
4262
4351
  endRun();
4263
4352
  }
@@ -4633,7 +4722,7 @@ function loadMarkdown(text, options) {
4633
4722
  let lastEnd = 0;
4634
4723
  for (let i = 0; i < root.children.length; i++) {
4635
4724
  const node = root.children[i];
4636
- const last = detailsEnd(root.children, i);
4725
+ const last = htmlSectionEnd(root.children, i);
4637
4726
  const from = lineStarts[root.lines[i].start - 1] ?? text.length;
4638
4727
  const lines = root.lines[last];
4639
4728
  const to = lineEnds[Math.max(lines.start, lines.end) - 1] ?? text.length;
@@ -4650,17 +4739,6 @@ function loadMarkdown(text, options) {
4650
4739
  }
4651
4740
  return { doc: { blocks }, source: { text, groups, groupOf, definitions: root.definitions } };
4652
4741
  }
4653
- function detailsEnd(nodes, i) {
4654
- if (nodes[i].type !== "htmlBlock") return i;
4655
- let depth = 0;
4656
- for (let j = i; j < nodes.length; j++) {
4657
- const node = nodes[j];
4658
- if (node.type !== "htmlBlock") continue;
4659
- depth += (node.value.match(/<details\b/gi)?.length ?? 0) - (node.value.match(/<\/details\s*>/gi)?.length ?? 0);
4660
- if (depth <= 0) return j;
4661
- }
4662
- return i;
4663
- }
4664
4742
  function isOpen(source, options) {
4665
4743
  return parseAst(source + "\n\nx", options).children.length < 2;
4666
4744
  }
@@ -6381,11 +6459,15 @@ function renderBlock(block3, ctx, reveal) {
6381
6459
  break;
6382
6460
  }
6383
6461
  case "raw": {
6384
- const view = reveal?.has(REVEAL_BLOCK) ? null : ctx.view?.(block3.syntax, inlineText(block3.content), "block");
6462
+ const source = inlineText(block3.content);
6463
+ const section = block3.syntax === "html" && !reveal?.has(REVEAL_BLOCK) && ctx.views?.has("html") ? splitHtmlSection(source, ctx.markdown) : null;
6464
+ const view = reveal?.has(REVEAL_BLOCK) ? null : ctx.view?.(block3.syntax, section ? section.html : source, "block");
6385
6465
  if (view) {
6386
6466
  root = view;
6387
6467
  root.classList.add("mbe-view-block");
6468
+ if (block3.syntax === "html") root.classList.add("mbe-view-html");
6388
6469
  root.contentEditable = "false";
6470
+ if (section) fillSlots(root, section.slots.length, (i) => renderFlow(section.slots[i], ctx), ctx.doc);
6389
6471
  } else {
6390
6472
  root = el("pre");
6391
6473
  root.spellcheck = false;
@@ -6462,6 +6544,61 @@ function copyButton(ctx) {
6462
6544
  if (icon) button.appendChild(icon);
6463
6545
  return button;
6464
6546
  }
6547
+ function fillSlots(root, count, content, doc) {
6548
+ if (count === 0) return;
6549
+ const texts = [];
6550
+ const walker = doc.createTreeWalker(
6551
+ root,
6552
+ 4
6553
+ /* NodeFilter.SHOW_TEXT */
6554
+ );
6555
+ for (let node = walker.nextNode(); node; node = walker.nextNode()) if (node.nodeValue.includes("\uE000")) texts.push(node);
6556
+ const placed = /* @__PURE__ */ new Set();
6557
+ for (const text of texts) {
6558
+ const value = text.nodeValue;
6559
+ const fragment = doc.createDocumentFragment();
6560
+ let last = 0;
6561
+ for (const m of value.matchAll(SLOT_MARKER)) {
6562
+ if (m.index > last) fragment.append(value.slice(last, m.index));
6563
+ const index = Number(m[1]);
6564
+ if (index < count && !placed.has(index)) {
6565
+ placed.add(index);
6566
+ fragment.append(content(index));
6567
+ }
6568
+ last = m.index + m[0].length;
6569
+ }
6570
+ if (last < value.length) fragment.append(value.slice(last));
6571
+ const parent = text.parentElement;
6572
+ const alone = parent !== root && parent?.tagName === "P" && parent.childNodes.length === 1 && !value.replace(SLOT_MARKER, "").trim();
6573
+ (alone ? parent : text).replaceWith(fragment);
6574
+ }
6575
+ if (!root.hasChildNodes()) return;
6576
+ for (let i = 0; i < count; i++) if (!placed.has(i)) root.appendChild(content(i));
6577
+ }
6578
+ function renderFlow(blocks, ctx) {
6579
+ const flow = ctx.doc.createElement("div");
6580
+ flow.className = "mbe-flow";
6581
+ const elements = /* @__PURE__ */ new Map();
6582
+ for (const block3 of blocks) {
6583
+ const el = renderBlock(block3, ctx);
6584
+ elements.set(block3.id, el);
6585
+ flow.appendChild(el);
6586
+ }
6587
+ updateNeighborMarks({ blocks }, (id) => elements.get(id), /* @__PURE__ */ new WeakMap());
6588
+ for (const el of Array.from(flow.querySelectorAll(`[${BLOCK_ATTR}], [${CONTENT_ATTR}], [contenteditable], [data-row]`))) {
6589
+ el.removeAttribute(BLOCK_ATTR);
6590
+ el.removeAttribute(CONTENT_ATTR);
6591
+ el.removeAttribute("contenteditable");
6592
+ el.removeAttribute("data-row");
6593
+ el.removeAttribute("data-col");
6594
+ }
6595
+ for (const input of Array.from(flow.querySelectorAll("input"))) {
6596
+ if (input.type === "checkbox") input.disabled = true;
6597
+ else input.readOnly = true;
6598
+ input.tabIndex = -1;
6599
+ }
6600
+ return flow;
6601
+ }
6465
6602
  function fill(doc) {
6466
6603
  const br = doc.createElement("br");
6467
6604
  br.setAttribute(FILL_ATTR, "");
@@ -6511,9 +6648,10 @@ function renderInline(inline3, parent, ctx, decorations, reveal, cell) {
6511
6648
  const viewed = views ? (span, from) => !!span.token && views.has(span.token) && !reveal?.has(tokenKey(cell, from)) : void 0;
6512
6649
  const runs = views?.has("html") && inline3.some((s) => s.marks.includes("html")) ? htmlRuns(inline3).filter((run) => !reveal?.has(tokenKey(cell, run.from))) : [];
6513
6650
  const inRun = (from) => runs.some((run) => from >= run.from && from < run.to);
6514
- const atomic = viewed || runs.length > 0 ? (span, from) => !!viewed?.(span, from) || inRun(from) : void 0;
6651
+ const isHtml = (span) => span.marks.includes("html");
6652
+ const atomic = viewed || runs.length > 0 ? (span, from) => !!viewed?.(span, from) || isHtml(span) && inRun(from) : void 0;
6515
6653
  const pieces = splitByDecorations(inline3, decorations, atomic);
6516
- const render2 = (piece) => decorate(renderSpan(piece.span, ctx, piece.atomic && !inRun(offsets.get(piece) ?? -1)), piece.decorations, doc);
6654
+ const render2 = (piece) => decorate(renderSpan(piece.span, ctx, piece.atomic && !isHtml(piece.span)), piece.decorations, doc);
6517
6655
  const offsets = /* @__PURE__ */ new Map();
6518
6656
  let offset = 0;
6519
6657
  for (const piece of pieces) {
@@ -6524,12 +6662,37 @@ function renderInline(inline3, parent, ctx, decorations, reveal, cell) {
6524
6662
  while (i < pieces.length) {
6525
6663
  const span = pieces[i].span;
6526
6664
  const run = runs.find((r) => r.from === offsets.get(pieces[i]));
6527
- const view = run && ctx.view?.("html", serializeInline(sliceInline(inline3, run.from, run.to)), "inline");
6528
- if (run && view) {
6529
- const text = inlineText(sliceInline(inline3, run.from, run.to));
6530
- parent.appendChild(atom(view, classes("mbe-view", "mbe-view-html", ctx.classes.token?.html), text));
6531
- while (i < pieces.length && offsets.get(pieces[i]) < run.to) i++;
6532
- continue;
6665
+ if (run) {
6666
+ const html = [];
6667
+ const slots = [];
6668
+ let slot = null;
6669
+ let end = i;
6670
+ for (; end < pieces.length && offsets.get(pieces[end]) < run.to; end++) {
6671
+ const piece = pieces[end];
6672
+ if (isHtml(piece.span)) {
6673
+ html.push(piece.span.text);
6674
+ slot = null;
6675
+ continue;
6676
+ }
6677
+ if (!slot) {
6678
+ slot = [];
6679
+ slots.push(slot);
6680
+ html.push(slotMarker(slots.length - 1));
6681
+ }
6682
+ slot.push(render2(piece));
6683
+ }
6684
+ const view = ctx.view?.("html", html.join(""), "inline");
6685
+ if (view) {
6686
+ fillSlots(view, slots.length, (n) => {
6687
+ const fragment = doc.createDocumentFragment();
6688
+ fragment.append(...slots[n]);
6689
+ return fragment;
6690
+ }, doc);
6691
+ const text = inlineText(sliceInline(inline3, run.from, run.to));
6692
+ parent.appendChild(atom(view, classes("mbe-view", "mbe-view-html", ctx.classes.token?.html), text));
6693
+ i = end;
6694
+ continue;
6695
+ }
6533
6696
  }
6534
6697
  if (span.link) {
6535
6698
  const a = doc.createElement("a");
@@ -6662,6 +6825,8 @@ var BlockRenderer = class {
6662
6825
  widgetsAfter;
6663
6826
  /** Views of extension syntaxes, by syntax name. */
6664
6827
  views;
6828
+ /** The document's Markdown options, for the Markdown inside an HTML section. */
6829
+ markdown;
6665
6830
  /** Theme classes for rendered elements. Set before a full render. */
6666
6831
  classes = NO_CLASSES;
6667
6832
  /** The icon of a UI control. */
@@ -6695,6 +6860,7 @@ var BlockRenderer = class {
6695
6860
  decorateCode: this.decorateCode,
6696
6861
  views: this.views,
6697
6862
  view: this.views && ((syntax, source, kind) => this.renderView(syntax, source, kind)),
6863
+ markdown: this.markdown,
6698
6864
  classes: this.classes,
6699
6865
  icon: this.icon,
6700
6866
  codeCopy: this.codeCopy,
@@ -6774,7 +6940,12 @@ var BlockRenderer = class {
6774
6940
  this.renderingBlock = block3.id;
6775
6941
  const prev = this.rendered.get(block3.id);
6776
6942
  const reveal = this.revealed.get(block3.id);
6777
- next.set(block3.id, { block: block3, el: prev && kept.has(prev.el) ? prev.el : renderBlock(block3, ctx, reveal), reveal });
6943
+ let el = prev && kept.has(prev.el) ? prev.el : null;
6944
+ if (!el) {
6945
+ el = renderBlock(block3, ctx, reveal);
6946
+ if (prev && block3.type === "raw") keepDetailsOpen(prev.el, el);
6947
+ }
6948
+ next.set(block3.id, { block: block3, el, reveal });
6778
6949
  }
6779
6950
  this.loadedImages.clear();
6780
6951
  const nodes = [];
@@ -6855,6 +7026,15 @@ function updateNeighborMarks(doc, elementFor, written) {
6855
7026
  written.set(el, n);
6856
7027
  });
6857
7028
  }
7029
+ function keepDetailsOpen(from, to) {
7030
+ const before = from.querySelectorAll("details");
7031
+ if (before.length === 0) return;
7032
+ const after = to.querySelectorAll("details");
7033
+ if (after.length !== before.length) return;
7034
+ before.forEach((details, i) => {
7035
+ after[i].open = details.open;
7036
+ });
7037
+ }
6858
7038
  function setFlag(el, attr, on) {
6859
7039
  if (el && el.hasAttribute(attr) !== on) el.toggleAttribute(attr, on);
6860
7040
  }
@@ -7538,6 +7718,7 @@ var Editor = class {
7538
7718
  };
7539
7719
  const views = new Map(this.extensions.flatMap((e) => Object.entries(e.views ?? {})));
7540
7720
  if (views.size > 0) this.renderer.views = views;
7721
+ this.renderer.markdown = this.markdown;
7541
7722
  this.renderer.onViewSettled = (ids) => this.viewsSettled(ids);
7542
7723
  const codeDecorators = this.extensions.flatMap((e) => e.codeDecorations ? [e.codeDecorations] : []);
7543
7724
  if (codeDecorators.length > 0) {
@@ -9062,8 +9243,8 @@ var Editor = class {
9062
9243
  copyCode(button) {
9063
9244
  const blockEl = button.closest(`[${BLOCK_ATTR}]`);
9064
9245
  const block3 = blockEl && this.state.doc.blocks.find((b) => b.id === blockEl.getAttribute(BLOCK_ATTR));
9065
- if (!block3 || block3.type !== "code") return;
9066
- const text = inlineText(block3.content);
9246
+ const text = block3?.type === "code" ? inlineText(block3.content) : button.closest(".mbe-flow .mbe-code")?.querySelector("code")?.textContent;
9247
+ if (text == null) return;
9067
9248
  const doc = this.root.ownerDocument;
9068
9249
  const show = (name) => {
9069
9250
  const icon = iconNode(doc, this.icon(name));
@@ -9532,7 +9713,8 @@ var Editor = class {
9532
9713
  const pos = this.state.selection.focus;
9533
9714
  const el = focused && !this.blockSel && isCollapsed(this.state.selection) ? this.renderer.elementFor(pos.blockId) : void 0;
9534
9715
  const placeholderShown = !!this.options.placeholder && this.state.doc.blocks.length === 1;
9535
- const showHint = el && el.classList.contains("mbe-paragraph") && el.classList.contains("mbe-empty") && !this.readOnly && !placeholderShown;
9716
+ const slashHint = !!this.options.slashHint && this.options.slashMenu !== false;
9717
+ const showHint = slashHint && el && el.classList.contains("mbe-paragraph") && el.classList.contains("mbe-empty") && !this.readOnly && !placeholderShown;
9536
9718
  const nextHint = showHint ? el : null;
9537
9719
  if (this.hintEl !== nextHint) {
9538
9720
  this.hintEl?.classList.remove("mbe-show-hint");