@usememos/editor 26.10.24 → 26.10.25

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
@@ -50,7 +50,7 @@ The same names work in `new Editor(options)` and as React props, except that Rea
50
50
  | `value` | `string` | React only. The editor updates only when `value` differs from the last Markdown it emitted. |
51
51
  | `defaultValue` | `string` | React only. Initial Markdown for uncontrolled use. |
52
52
  | `markdown` | `{ breaks?, setext?, autolinks?, callouts?, details? }` | Markdown dialect. `breaks: true` reads a single newline as a line break and saves line breaks as plain newlines (as GitHub comments do). `setext: false` turns off setext headings, so `Title`⏎`---` is a paragraph and a divider. `autolinks: false` turns off GFM autolink literals, so a bare `https://…`, `www.…` or e-mail address stays text. `callouts: false` reads a GitHub alert such as `> [!NOTE]` as a plain quote and drops Callout from the slash menu. `details: false` reads `<details>` as raw HTML and drops Collapsible section from the slash menu. See [docs/syntax.md](docs/syntax.md). |
53
- | `extensions` | `Extension[]` | Adds syntax and behavior. Built in: `math()` (`$…$` and `$$` blocks), `footnotes()` (`[^1]` and `[^1]: …`, shown as a raised label and a small note; `footnotes({ render: false })` keeps the source), `tags()` (`#tag`) and `mentions()` (`@name`). Extension syntax is saved verbatim, and shown as source unless the extension renders it (see `views`). |
53
+ | `extensions` | `Extension[]` | Adds syntax and behavior. Built in: `math()` (`$…$` and `$$` blocks), `footnotes()` (`[^1]` and `[^1]: …`, shown as a raised label and a small note whose text renders as Markdown, collected at the end as on GitHub when read-only; `footnotes({ render: false })` keeps the source), `tags()` (`#tag`) and `mentions()` (`@name`). Extension syntax is saved verbatim, and shown as source unless the extension renders it (see `views`). |
54
54
  | `placeholder` | `string` | Shown when the document is empty. |
55
55
  | `editable` | `boolean` | Default `true`. With `false`, no editing, and links open on click. |
56
56
  | `taskToggle` | `boolean` | Default `false`. While `editable` is `false`, lets the user tick and untick task items, for example for a memo's owner. Each tick emits `change` with Markdown in which only that item's `[ ]` or `[x]` changed. Ignored while `editable` is `true`. |
@@ -286,7 +286,7 @@ const linkCardsBelow: Extension = {
286
286
 
287
287
  ### Theming
288
288
 
289
- `style.css` is two stylesheets: `base.css` holds the layout the editor needs and all spacing between blocks, and `theme.css` the default look. The default look is quiet and compact, like a rendered Markdown view: a 16px body with 24px lines, 8px between blocks, headings close to the body size, muted quotes and list markers, code blocks on a light panel with no border whose long lines scroll sideways rather than wrap, and tables as a light grid with a shaded header row and columns sized to their content. Editing and read-only look the same; only the cursor on links and checkboxes changes. It has no border or background of its own, so it fits inside your own card, comment box or chat input.
289
+ `style.css` is two stylesheets: `base.css` holds the layout the editor needs and all spacing between blocks, and `theme.css` the default look. The default look is quiet and compact, like a rendered Markdown view: a 16px body with 24px lines, 8px between blocks, headings close to the body size, muted quotes and list markers, code blocks on a light panel with no border whose long lines scroll sideways rather than wrap, and tables that fill the width inside a round frame, with a tinted header row, a line between rows and no vertical lines, as claude.ai renders them. Editing and read-only look the same; only the cursor on links and checkboxes changes. It has no border or background of its own, so it fits inside your own card, comment box or chat input.
290
290
 
291
291
  Every color, font and size in `theme.css` is a CSS custom property (`--mbe-text`, `--mbe-accent`, `--mbe-font`, …). Set them with the `theme` option's tokens, with a color set for light and one for dark; colors you leave out keep the defaults:
292
292
 
package/dist/base.css CHANGED
@@ -255,6 +255,13 @@
255
255
  .mbe-root > .mbe-block-view { overflow: visible; }
256
256
  /* A block view placed after its block, which stays editable text: not text itself, like a widget. */
257
257
  .mbe-root > .mbe-block-after { cursor: default; white-space: normal; }
258
+ /* Footnotes, read-only: the definitions hide where they are written and the
259
+ referenced ones are collected in a section at the end (see footnotes()). */
260
+ .mbe-root > .mbe-footnote-collected { display: none; }
261
+ .mbe-root > .mbe-footnotes { margin-top: 16px; cursor: default; }
262
+ .mbe-root > .mbe-footnotes > .mbe-footnote + .mbe-footnote { margin-top: 4px; }
263
+ .mbe-root .mbe-footnote-backref { display: inline-flex; align-items: center; vertical-align: -0.125em; }
264
+ .mbe-root .mbe-footnote-backref .mbe-icon { width: 0.875em; height: 0.875em; }
258
265
  /* Tags and mentions with an onClick, read-only: clickable, as links are. */
259
266
  .mbe-root.mbe-readonly :is([data-mbe-tag], [data-mbe-mention]) { cursor: pointer; }
260
267
  /* Rendered HTML: line breaks between its tags are not text. The Markdown
@@ -281,15 +288,19 @@
281
288
 
282
289
  /* Table */
283
290
  :is(.mbe-root, .mbe-flow) > .mbe-table { overflow-x: auto; }
284
- /* Columns fit their content and wrap at the editor's width, as GitHub lays
285
- tables out; wider tables scroll. No column has a minimum width, so a long
286
- column is not squeezed into wrapping by short ones. */
287
- :is(.mbe-root, .mbe-flow) > .mbe-table table { border-collapse: separate; border-spacing: 0; }
291
+ /* The table fills the editor's width and its columns share it by their
292
+ content, as claude.ai lays tables out; one whose longest words do not
293
+ fit scrolls. Cells wrap at words: the root's overflow-wrap: anywhere
294
+ would let the layout shrink any column to one character and break words
295
+ to make the table fit, break-word keeps every column as wide as its
296
+ longest word. */
297
+ :is(.mbe-root, .mbe-flow) > .mbe-table table { width: 100%; border-collapse: separate; border-spacing: 0; }
288
298
  :is(.mbe-root, .mbe-flow) > .mbe-table th,
289
299
  :is(.mbe-root, .mbe-flow) > .mbe-table td {
290
- padding: 4px 8px;
300
+ padding: 0.4375em 0.75em;
291
301
  vertical-align: top;
292
302
  text-align: start;
303
+ overflow-wrap: break-word;
293
304
  outline: none;
294
305
  }
295
306
  /* An empty cell keeps room to click into while editing. */
package/dist/index.js CHANGED
@@ -2424,6 +2424,9 @@ var DEFAULT_LABELS = {
2424
2424
  todoList: "To-do list",
2425
2425
  /** Accessible name of a task checkbox whose item has no text. */
2426
2426
  task: "Task",
2427
+ /** The footnotes collected at the end of a read-only document, and a note's backlink to its reference. */
2428
+ footnotes: "Footnotes",
2429
+ backToReference: "Back to reference",
2427
2430
  quote: "Quote",
2428
2431
  /** The slash menu item, and the title of a callout's block menu section. */
2429
2432
  callout: "Callout",
@@ -2563,6 +2566,8 @@ var ICONS = {
2563
2566
  delete: TRASH,
2564
2567
  /** `arrow-up` */
2565
2568
  addRowAbove: ["m5 12 7-7 7 7", "M12 19V5"],
2569
+ /** `arrow-up`: a footnote's backlink to its reference. */
2570
+ backToReference: ["m5 12 7-7 7 7", "M12 19V5"],
2566
2571
  /** `arrow-down` */
2567
2572
  addRowBelow: ["M12 5v14", "m19 12-7 7-7-7"],
2568
2573
  /** `arrow-left` */
@@ -7342,9 +7347,11 @@ function renderBlock(block3, ctx, reveal) {
7342
7347
  }
7343
7348
  case "raw": {
7344
7349
  const source = inlineText(block3.content);
7345
- const section = block3.syntax === "html" && !reveal?.has(REVEAL_BLOCK) && ctx.views?.has("html") ? splitHtmlSection(source, ctx.markdown) : null;
7346
- const nested = block3.syntax === "markdown" && !reveal?.has(REVEAL_BLOCK) && ctx.views?.get("markdown") === MARKDOWN_VIEW;
7347
- const view = reveal?.has(REVEAL_BLOCK) ? null : nested ? renderMarkdown(source, ctx) : ctx.view?.(block3.syntax, section ? section.html : source, "block");
7350
+ const revealed = reveal?.has(REVEAL_BLOCK) === true;
7351
+ const section = block3.syntax === "html" && !revealed && ctx.views?.has("html") ? splitHtmlSection(source, ctx.markdown) : null;
7352
+ const footnote = block3.syntax === "footnote" && !revealed && ctx.views?.has("footnote") ? splitFootnote(source) : null;
7353
+ const nested = block3.syntax === "markdown" && !revealed && ctx.views?.get("markdown") === MARKDOWN_VIEW;
7354
+ const view = revealed ? null : nested ? renderMarkdown(source, ctx) : footnote ? renderFootnote(footnote, ctx) : ctx.view?.(block3.syntax, section ? section.html : source, "block");
7348
7355
  if (view) {
7349
7356
  root = view;
7350
7357
  root.classList.add("mbe-view-block");
@@ -7496,6 +7503,30 @@ function appendBlocks(flow, blocks, ctx) {
7496
7503
  }
7497
7504
  updateNeighborMarks({ blocks }, (id) => elements.get(id), /* @__PURE__ */ new WeakMap(), /* @__PURE__ */ new Set(), ctx.labels);
7498
7505
  }
7506
+ var FOOTNOTE_DEFINITION = /^(\[\^((?:[^\]\s\\]|\\.)+)\]:)[ \t]*([\s\S]*)$/;
7507
+ var FOOTNOTE_REFERENCE = /\[\^((?:[^\]\s\\]|\\.)+)\]/g;
7508
+ var unescapeLabel = (label) => label.replace(/\\(.)/g, "$1");
7509
+ function splitFootnote(source) {
7510
+ const m = FOOTNOTE_DEFINITION.exec(source);
7511
+ return m ? { label: unescapeLabel(m[2]), head: `${m[1]} `, body: m[3] } : null;
7512
+ }
7513
+ function renderFootnote(definition, ctx) {
7514
+ const el = ctx.view?.("footnote", definition.head + slotMarker(0), "block") ?? null;
7515
+ if (el) fillSlots(el, 1, () => renderInlineMarkdown(definition.body, ctx), ctx.doc);
7516
+ return el;
7517
+ }
7518
+ function renderInlineMarkdown(source, ctx) {
7519
+ const flow = renderNodes(parseAst(source, ctx.markdown).children, ctx);
7520
+ const only = flow.childNodes.length === 1 ? flow.firstElementChild : null;
7521
+ if (flow.childNodes.length === 0 || only && only.tagName === "P") {
7522
+ const span = ctx.doc.createElement("span");
7523
+ if (only) span.append(...Array.from(only.childNodes));
7524
+ makeInert(span);
7525
+ return span;
7526
+ }
7527
+ makeInert(flow);
7528
+ return flow;
7529
+ }
7499
7530
  var MARKDOWN_VIEW = () => false;
7500
7531
  function renderMarkdown(source, ctx) {
7501
7532
  const root = ctx.doc.createElement("div");
@@ -7850,6 +7881,14 @@ var BlockRenderer = class {
7850
7881
  widgetsAfter;
7851
7882
  /** Views of extension syntaxes, by syntax name. */
7852
7883
  views;
7884
+ /**
7885
+ * Read-only: footnote definitions hide where they are written and the
7886
+ * referenced ones show again, in order of first reference, in a section
7887
+ * at the end of the document (see `footnoteSection`).
7888
+ */
7889
+ collectFootnotes = false;
7890
+ /** The last footnote section, kept while what it shows is the same. */
7891
+ footnotes;
7853
7892
  /** What a view gets besides its source: the editor. Set with `views`. */
7854
7893
  viewContext;
7855
7894
  /** Block views of extensions, with the extension's name, in extension order. */
@@ -8101,6 +8140,12 @@ var BlockRenderer = class {
8101
8140
  }
8102
8141
  this.loadedImages.clear();
8103
8142
  for (const [id, entry] of [...this.hosted]) if (next.get(id)?.block !== entry.block) this.disposeHosted(id);
8143
+ const collecting = this.collectFootnotes && this.views?.has("footnote") === true;
8144
+ for (const { block: block3, el } of next.values()) {
8145
+ if (block3.type === "raw" && block3.syntax === "footnote") el.classList.toggle("mbe-footnote-collected", collecting);
8146
+ }
8147
+ const footnotes2 = collecting ? this.footnoteSection(doc, next, ctx, force) : null;
8148
+ if (!collecting) this.footnotes = void 0;
8104
8149
  const nodes = [];
8105
8150
  for (const [id, { block: block3, el, reveal }] of next) {
8106
8151
  nodes.push(el);
@@ -8109,6 +8154,7 @@ var BlockRenderer = class {
8109
8154
  const widgets = this.widgetsAfter?.(id);
8110
8155
  if (widgets) nodes.push(...widgets);
8111
8156
  }
8157
+ if (footnotes2) nodes.push(footnotes2);
8112
8158
  if (force || this.rendered.size === 0) {
8113
8159
  const fragment = this.root.ownerDocument.createDocumentFragment();
8114
8160
  for (const el of nodes) {
@@ -8137,6 +8183,68 @@ var BlockRenderer = class {
8137
8183
  this.rendered = next;
8138
8184
  updateNeighborMarks(doc, (id) => next.get(id)?.el, this.numbers, this.collapsed, this.labels);
8139
8185
  }
8186
+ /**
8187
+ * The footnotes section, as GitHub renders one: every definition that is
8188
+ * referenced, in order of first reference, each rendered as its block is
8189
+ * (see `renderFootnote`) with a backlink per reference. Definitions nothing
8190
+ * references are left out. Null when there is nothing to show. The element
8191
+ * is kept while the definitions and references are the same.
8192
+ */
8193
+ footnoteSection(doc, next, ctx, force) {
8194
+ const definitions = /* @__PURE__ */ new Map();
8195
+ for (const block3 of doc.blocks) {
8196
+ if (block3.type !== "raw" || block3.syntax !== "footnote") continue;
8197
+ const definition = splitFootnote(inlineText(block3.content));
8198
+ if (definition && !definitions.has(definition.label)) definitions.set(definition.label, definition);
8199
+ }
8200
+ if (definitions.size === 0) return null;
8201
+ const order = [];
8202
+ const counts = /* @__PURE__ */ new Map();
8203
+ for (const block3 of doc.blocks) {
8204
+ const el = next.get(block3.id)?.el;
8205
+ if (!el) continue;
8206
+ for (const ref of Array.from(el.querySelectorAll(".mbe-view-footnote"))) {
8207
+ for (const m of (ref.dataset.source ?? "").matchAll(FOOTNOTE_REFERENCE)) {
8208
+ const label = unescapeLabel(m[1]);
8209
+ if (!definitions.has(label)) continue;
8210
+ const count = (counts.get(label) ?? 0) + 1;
8211
+ counts.set(label, count);
8212
+ if (count === 1) order.push(label);
8213
+ }
8214
+ }
8215
+ }
8216
+ if (order.length === 0) return null;
8217
+ const key = JSON.stringify(order.map((label) => [label, definitions.get(label).body, counts.get(label)]));
8218
+ if (!force && this.footnotes?.key === key) return this.footnotes.el;
8219
+ const section = ctx.doc.createElement("section");
8220
+ section.className = "mbe-footnotes";
8221
+ section.contentEditable = "false";
8222
+ section.setAttribute("aria-label", this.labels.footnotes);
8223
+ for (const label of order) {
8224
+ const item = renderFootnote(definitions.get(label), ctx);
8225
+ if (!item) continue;
8226
+ for (let i = 1; i <= counts.get(label); i++) {
8227
+ const back = ctx.doc.createElement("button");
8228
+ back.type = "button";
8229
+ back.className = "mbe-footnote-backref";
8230
+ back.dataset.label = label;
8231
+ back.dataset.ref = String(i);
8232
+ back.setAttribute("aria-label", `${this.labels.backToReference} ${label}${i > 1 ? ` ${i}` : ""}`);
8233
+ const icon = ctx.icon?.("backToReference");
8234
+ if (icon) back.appendChild(icon);
8235
+ else back.textContent = "\u2191";
8236
+ if (i > 1) {
8237
+ const sup = ctx.doc.createElement("sup");
8238
+ sup.textContent = String(i);
8239
+ back.appendChild(sup);
8240
+ }
8241
+ item.appendChild(back);
8242
+ }
8243
+ section.appendChild(item);
8244
+ }
8245
+ this.footnotes = { key, el: section };
8246
+ return section;
8247
+ }
8140
8248
  /** Marks blocks again after `collapsed` changed, without rendering any. */
8141
8249
  updateSections(doc) {
8142
8250
  updateNeighborMarks(doc, (id) => this.rendered.get(id)?.el, this.numbers, this.collapsed, this.labels);
@@ -9722,7 +9830,8 @@ var Editor = class {
9722
9830
  if (toggle) this.taskToggle = options.taskToggle === true;
9723
9831
  if (options.editable !== void 0 && options.editable === this.readOnly) {
9724
9832
  this.readOnly = !options.editable;
9725
- this.refreshRevealed();
9833
+ if (this.renderer.views?.has("footnote")) this.paint();
9834
+ else this.refreshRevealed();
9726
9835
  this.applyReadOnly();
9727
9836
  this.hideUi();
9728
9837
  } else if (toggle) {
@@ -10361,6 +10470,7 @@ var Editor = class {
10361
10470
  paint(force = false, lazy = false, hadFocus = this.hasFocus()) {
10362
10471
  const views = this.renderer.views;
10363
10472
  if (views && !this.pointerDown && !this.composing) this.renderer.revealed = revealedViews(this.state, views, !this.readOnly && hadFocus, this.hosted);
10473
+ this.renderer.collectFootnotes = this.readOnly;
10364
10474
  this.renderer.render(this.state.doc, force, lazy);
10365
10475
  this.applyHeadingIds();
10366
10476
  this.applyReadOnly();
@@ -12218,12 +12328,23 @@ function footnotes(options = {}) {
12218
12328
  views: { footnote: (source, element, kind, ctx) => (kind === "inline" ? renderReference : renderDefinition)(source, element, kind, ctx) },
12219
12329
  onCreate: ({ editor }) => {
12220
12330
  const onClick = (e) => {
12221
- const ref = e.target.closest?.(".mbe-footnote-ref");
12222
- if (!ref || editor.isEditable) return;
12223
- const definition = [...editor.root.querySelectorAll(".mbe-footnote")].find((d) => d.dataset.label === ref.dataset.label);
12224
- if (!definition) return;
12331
+ if (editor.isEditable) return;
12332
+ const target = e.target;
12333
+ const ref = target.closest?.(".mbe-footnote-ref");
12334
+ const back = ref ? null : target.closest?.(".mbe-footnote-backref");
12335
+ if (!ref && !back) return;
12336
+ const inSection = (el) => el.closest(".mbe-footnotes") !== null;
12337
+ let to;
12338
+ if (ref) {
12339
+ const notes = [...editor.root.querySelectorAll(".mbe-footnote")].filter((d) => d.dataset.label === ref.dataset.label);
12340
+ to = notes.find(inSection) ?? notes[0];
12341
+ } else if (back) {
12342
+ const refs = [...editor.root.querySelectorAll(".mbe-footnote-ref")].filter((r) => r.dataset.label === back.dataset.label && !inSection(r));
12343
+ to = refs[Number(back.dataset.ref) - 1];
12344
+ }
12345
+ if (!to) return;
12225
12346
  e.preventDefault();
12226
- definition.scrollIntoView({ block: "center", behavior: "smooth" });
12347
+ to.scrollIntoView({ block: "center", behavior: "smooth" });
12227
12348
  };
12228
12349
  editor.root.addEventListener("click", onClick);
12229
12350
  return () => editor.root.removeEventListener("click", onClick);