Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…

Read the release notes

data-markdown-editable

Inline markdown editing on any text element: raw markdown while focused, sanitised formatting after blur. On a textarea or input it renders a preview instead.

Overview

data-markdown-editable turns a short piece of text into an inline markdown field. While the element has focus it shows the raw markdown. When focus leaves, the markdown is rendered as inline HTML (bold, italic, strikethrough, code, links), sanitised, and put back in place. Without JavaScript the element shows the raw markdown as plain text.

It works in two ways, depending on the element:

  • A text element (<p>, <span>, a table cell…) becomes contenteditable and switches between raw and rendered text on focus and blur.
  • A <textarea> or <input> keeps its raw markdown as its value, so it submits with a form, and renders a preview next to it on blur.

The script ships with the full bundle (vanilla-breeze.js) and the autoload bundle, which load it on demand when a [data-markdown-editable] element first appears. It is not part of vanilla-breeze-core.js.

Options

Attribute On Description
data-markdown-editable any text element, <textarea>, <input> Boolean. Turns on inline markdown editing. Read once, when the element is enhanced.
data-preview <textarea>, <input> The id of an element that receives the rendered preview. Without it, an <output class="md-editable-preview" aria-label="Markdown preview"> is inserted after the field.
data-markdown-value text elements (Set by the script.) The raw markdown, kept while the element shows rendered HTML. Read it to save the source.
data-md-editing text elements (Set by the script.) Present while the element has focus and shows raw markdown. Styling hook.
data-markdown-editable-init any (Set by the script.) Marks an enhanced element so it is not enhanced twice. Do not author it.

Events

Event Detail When
markdown-editable:change { value, html }: the raw markdown and the rendered, sanitised HTML On blur. Text elements fire only when the markdown changed; a <textarea> or <input> fires on every blur. Bubbles, so one listener on document covers every field.

Examples

Textarea with a preview target

On a <textarea> the value stays markdown, so the field works with <form-field>, validation and form submission like any other. Point data-preview at the element that should show the rendered text.

Inside lists and headings

The script gives the element role="textbox", which replaces its own role. Put the attribute on a <span> inside a list item or heading so the list and the heading keep their meaning.

Styling

  • [data-markdown-editable] gets a text cursor.
  • [data-md-editing] (focused, showing raw markdown) switches to the monospace font, keeps line breaks visible (white-space: pre-wrap) and draws a 2px --color-primary outline.
  • .md-editable-preview, the generated preview, is small muted text with a top border, and is hidden while empty. A data-preview target gets none of this: style it yourself.

The rules live in src/utils/markdown-editable.css. There is no resting style, so an editable element looks like the element it is. Give it a visible edge when people need to know it can be edited, as the demo does.

Accessibility

  • Give every editable element a name. The script sets role="textbox" and aria-multiline="false", but not a label. Add aria-label or aria-labelledby, as you would for any text field. A <textarea> takes a normal <label>.
  • The role replaces the element's role. A <li>, heading or cell with the attribute is announced as a text box, not as a list item, heading or cell. Use an inner <span>.
  • Editable elements are in the tab order (contenteditable makes them focusable), so keyboard users can reach and edit them.
  • Rendered text is real HTML: <strong>, <em>, <del>, <code> and <a> keep their semantics once the element loses focus.
  • IME composition is respected: focus and blur switching waits until a composition has ended.

Security

Markdown is parsed with the vendored marked (inline syntax only) and the result goes through VB's sanitiser (src/lib/sanitize-html.js) as nodes, never re-parsed from a string. Scripts, event-handler attributes, style, form-hijack and invoker attributes are removed, and dangerous URL schemes are dropped from links. Raw HTML typed into the field is treated the same way.

The sanitiser protects what the page renders. It does not make stored content safe for other places: sanitise again on the server, or wherever else you render the saved markdown.

Gotchas

  • Inline markdown only. Headings, lists, block quotes and code fences are not rendered. Use <markdown-editor> for whole documents.
  • Enter is not intercepted. aria-multiline="false" tells assistive technology the field is single-line, but the browser still inserts a line break on Enter. Cancel insertParagraph and insertLineBreak in a beforeinput listener if the text must stay on one line.
  • Formatting typed or pasted while editing is discarded. On blur the script reads the element's plain text and renders it as markdown, so rich text pasted into the raw view becomes text.
  • Links are not followed by a click. Clicking a rendered link focuses the element and switches it to raw markdown.
  • Read once. Removing the attribute later does not undo the enhancement, and the initial markdown is taken from the element's text when it is enhanced.
  • A contenteditable element is not a form control. It is not submitted and has no validation. Use a <textarea data-markdown-editable> when the value belongs in a form.

Related

Which to choose

  • <textarea> — plain text in a form. The default choice.
  • data-markdown-editable — short inline text with bold, italic, code and links, edited in place and stored as markdown (titles, captions, notes, list items).
  • <markdown-editor> — longer documents with block markdown, submitted with a form.
  • contenteditable — real rich text, when you are prepared to normalise and sanitise the HTML yourself.
↑