Olddevs UI4.14.0
Recipes

Merge Tag: inserting fields into an email

An inline Atom with a field key and a Toolbar button — on send, the server puts the customer's name in its place.

A newsletter is written once, and the server fills in the customer's name and plan when it sends it. The author has to mark where each value goes: that is a Merge Tag, an inline Atom holding a field key. This Recipe builds it as a custom Tool (CustomTool): a Block, a Schema rule and a Toolbar button with a popover.

Try it

Press the “Merge tag” button (the tag icon) on the Toolbar or Ctrl+Alt+M (on Apple, ⌘+⌥+M) and pick a field — it lands at the caret.

How it works

Extensions

The Extensions (engine) hold a Tiptap Node: an Atom, its attribute and the Atom text (renderText) that getText(), maxLength and the word count read. The kit sets the node's group and its HTML parsing and rendering from the Schema rules, so you write no parseHTML or renderHTML. addNodeView renders the Atom as a chip in the Editor; it is only a view and never reaches the Document.

Schema rules

The Schema rules are plain data without Tiptap: kind: "inline" and the acme_key attribute with its list of allowed values. The kit uses them to write HTML and sanitize the Document; a key that is not on the list is stripped both in the browser and on the server.

<span data-block="acme_mergeTag" data-acme_key="first_name"></span>

Button

The button is a buttons entry: a label (a string or a function that receives { locale }), a required icon, can and a popover. The kit handles the popover frame, focus, Escape, the shortcut hint and aria-keyshortcuts.

Prefix in names

The name of everything custom starts with a Consumer prefix and an underscore: the acme_mergeTag Tool, the acme_key attribute. Kit names have no underscore, so custom names never collide with built-in ones, either in the tools array or in a saved Document.

Sanitization on the server

The server sanitizes the Document with the same tools array the Editor got. Otherwise your Block is stripped on save:

import { sanitizeRichText } from "@olddevs/ui/editor/schema";

const html = sanitizeRichText(dirty, DOMPurify, EDITOR_TOOLS);

To import the Tool description on the server without Tiptap and React, hand the Extensions over as a Loader — see the extension API and the “Lazy loading the engine” Recipe.

On this page