Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

Markdown escapes: backslash keeps literals, cmd-Z reverts a conversion

nyblnet0a30db50f4c2
diff --git a/CLAUDE.md b/CLAUDE.md
index 72808802..1a16a062 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -56,6 +56,11 @@ One HTML file = the document + viewer + editor. See `README.md` for the vision.
autoformat (editor/markdown.ts: **bold** *italic* `code` ~~strike~~ and "- "
bullets collapse as typed — mind contentEditable NBSPs and stale
Selection offsets after DOM surgery; pasted plain text converts too).
+ Escapes: backslash before a marker keeps it literal (stripped at commit);
+ ⌘Z immediately after a conversion restores the typed markers (one-shot,
+ cleared by the next input). Note markdown.ts carries LITERAL invisible
+ chars (NBSP, ZWSP, U+E000) — they don't survive retyping; edit that file
+ with line-targeted scripts, not copy-typed strings.
Sidebar has hover insert-gaps between thumbnails (never between a parent and
its states). Selecto's continue-select list is synced in syncTargets — stale
cross-slide shift-click selections were a real bug.
diff --git a/slides/src/editor/canvas.ts b/slides/src/editor/canvas.ts
index 44ec123c..884f1c93 100644
--- a/slides/src/editor/canvas.ts
+++ b/slides/src/editor/canvas.ts
@@ -7,7 +7,7 @@ import Selecto from 'selecto'
import type { Store } from '../store'
import type { SlideElement } from '../model'
import { renderSlide, sanitizeHtml } from '../render'
-import { autoformatAtCaret, markdownToHtml } from './markdown'
+import { autoformatAtCaret, clearAutoformat, markdownToHtml, undoAutoformat } from './markdown'
import { PathEditor } from './patheditor'
export class SlideCanvas {
@@ -434,6 +434,12 @@ export class SlideCanvas {
// inline markup: ⌘/Ctrl+B/I/U toggle bold/italic/underline on the
// selection (sanitize keeps b/i/u/strong/em, so it round-trips)
if (ev.metaKey || ev.ctrlKey) {
+ // ⌘Z right after a markdown conversion restores the literal markers;
+ // otherwise the browser's native contentEditable undo runs
+ if (ev.key.toLowerCase() === 'z' && !ev.shiftKey) {
+ if (undoAutoformat()) ev.preventDefault()
+ return
+ }
const cmd = { b: 'bold', i: 'italic', u: 'underline' }[ev.key.toLowerCase()]
if (cmd) {
ev.preventDefault()
@@ -442,8 +448,11 @@ export class SlideCanvas {
}
})
// markdown affordances: **bold** / *italic* / `code` / ~~strike~~ / "- "
- // collapse as you type; pasted plain text converts the same patterns
- inner.addEventListener('input', () => autoformatAtCaret())
+ // collapse as you type (⌘Z reverts, backslash escapes); pasted plain
+ // text converts the same patterns
+ inner.addEventListener('input', () => {
+ if (!autoformatAtCaret()) clearAutoformat()
+ })
inner.addEventListener('paste', (ev) => {
const text = ev.clipboardData?.getData('text/plain')
if (!text) return
@@ -463,7 +472,7 @@ export class SlideCanvas {
if (!inner || !id) return
inner.contentEditable = 'false'
// drop the zero-width caret spacers autoformat leaves behind
- const html = sanitizeHtml(inner.innerHTML.replace(/​/g, ''))
+ const html = sanitizeHtml(inner.innerHTML.replace(/\u200B/g, '').replace(/\\([*_~`-])/g, '$1'))
const grownH = Math.max(parseFloat(node.style.height) || 0, inner.scrollHeight)
const el = this.store.element(id)
if (el && el.type === 'text' && (el.html !== html || grownH > el.h)) {
diff --git a/slides/src/editor/markdown.ts b/slides/src/editor/markdown.ts
index fb493dcb..e0c0d5d3 100644
--- a/slides/src/editor/markdown.ts
+++ b/slides/src/editor/markdown.ts
@@ -18,6 +18,37 @@ const INLINE: Array<{ re: RegExp; tag: string }> = [
{ re: /`([^`\n]+)`$/, tag: 'code' },
]
+/** The most recent conversion, revertible with ⌘Z until the next input. */
+type LastFormat =
+ | { kind: 'inline'; el: HTMLElement; source: string; tail: Text }
+ | { kind: 'bullet'; node: Text; offset: number; source: string }
+let lastFormat: LastFormat | null = null
+
+/** Forget the revertible conversion (any input that isn't a fresh conversion). */
+export function clearAutoformat() {
+ lastFormat = null
+}
+
+/** ⌘Z immediately after a conversion: restore the literal typed markers.
+ * Returns true when it reverted (caller should preventDefault). */
+export function undoAutoformat(): boolean {
+ const last = lastFormat
+ lastFormat = null
+ if (!last) return false
+ if (last.kind === 'bullet') {
+ if (!last.node.isConnected) return false
+ last.node.replaceData(last.offset, 2, last.source)
+ placeCaret(last.node, last.offset + last.source.length)
+ return true
+ }
+ if (!last.el.isConnected) return false
+ const literal = document.createTextNode(last.source)
+ last.el.replaceWith(literal)
+ last.tail.remove()
+ placeCaret(literal, literal.data.length)
+ return true
+}
+
/** Collapse a just-completed markdown marker before the caret. Returns true if it fired. */
export function autoformatAtCaret(): boolean {
const sel = document.getSelection()
@@ -29,8 +60,10 @@ export function autoformatAtCaret(): boolean {
// "- " at line start → bullet glyph (contentEditable renders the trailing
// space as NBSP, so match both; keep NBSP so the glyph's gap can't collapse)
if (/(?:^|\n)-[  ]$/.test(upto) && off >= 2 && isLineStart(node, off - 2)) {
+ const source = node.data.slice(off - 2, off)
node.replaceData(off - 2, 2, '• ')
placeCaret(node, off)
+ lastFormat = { kind: 'bullet', node, offset: off - 2, source }
return true
}
@@ -38,6 +71,8 @@ export function autoformatAtCaret(): boolean {
const m = upto.match(re)
if (!m || !m[1]) continue
const start = off - m[0].length
+ // backslash escape: \*literal\* stays literal (stripped on commit)
+ if (start > 0 && node.data[start - 1] === '\\') continue
const range = document.createRange()
range.setStart(node, start)
range.setEnd(node, off)
@@ -49,6 +84,7 @@ export function autoformatAtCaret(): boolean {
const tail = document.createTextNode('​')
el.after(tail)
placeCaret(tail, 1)
+ lastFormat = { kind: 'inline', el, source: m[0], tail }
return true
}
return false
@@ -78,9 +114,17 @@ function placeCaret(node: Node, offset: number) {
const escapeHtml = (s: string) =>
s.replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;')
-/** Pasted plain text → the inline-HTML subset (bold/italic/strike/code/bullets/<br>). */
+/** Pasted plain text → the inline-HTML subset (bold/italic/strike/code/bullets/<br>).
+ * Backslash escapes markers: \*x\* pastes as literal *x*. */
export function markdownToHtml(text: string): string {
- return text
+ // park escaped markers in the private-use area so patterns can't see them
+ const PARK = ''
+ const parked: string[] = []
+ const withParked = text.replace(/\\([*_~`-])/g, (_, c: string) => {
+ parked.push(c)
+ return PARK + (parked.length - 1) + PARK
+ })
+ const out = withParked
.split('\n')
.map((line) => {
let s = escapeHtml(line).replace(/^(\s*)- /, '$1• ')
@@ -93,4 +137,5 @@ export function markdownToHtml(text: string): string {
return s
})
.join('<br>')
+ return out.replace(/(\d+)/g, (_, i) => parked[+i])
}
diff --git a/slides/src/editor/panels.ts b/slides/src/editor/panels.ts
index 9740cc83..0dc880d2 100644
--- a/slides/src/editor/panels.ts
+++ b/slides/src/editor/panels.ts
@@ -414,7 +414,7 @@ export class PropsPanel {
this.section('Typography')
const hint = document.createElement('p')
hint.className = 'ed-hint'
- hint.innerHTML = 'While editing: <b>⌘B</b>/<b>⌘I</b>/<b>⌘U</b> · markdown auto-converts — **bold*&#8203;* *italic*&#8203; `code` ~~strike~~ and "- " bullets; pasting markdown converts too.'
+ hint.innerHTML = 'While editing: <b>⌘B</b>/<b>⌘I</b>/<b>⌘U</b> · markdown auto-converts — **bold*&#8203;* *italic*&#8203; `code` ~~strike~~ and "- " bullets; pasting markdown converts too. Escape with \\ or press ⌘Z right after to keep the literal characters.'
this.host.appendChild(hint)
this.row('Font', this.fontSelect(el))
this.row('Size', this.number(el.fontSize, 1, (v, fin) =>