Skip to content

Typography

Typography provides automatic typographic replacements as you type. It converts ASCII sequences like -- to em dashes, ... to ellipses, -> to arrows, 1/2 to fractions, (c) to copyright symbols, and straight quotes to curly smart quotes. All categories can be enabled or disabled individually.

Not included in StarterKit. Add it separately.

Type any of the sequences below and watch them convert automatically. Try -- for an em dash, ... for an ellipsis, -> for an arrow, 1/2 for a fraction, or (c) for a copyright symbol. Press Backspace right after to undo the replacement.

Click to try it out
import { Editor, Document, Paragraph, Text, Typography } from '@domternal/core';
import '@domternal/theme';
const editor = new Editor({
element: document.getElementById('editor')!,
extensions: [Document, Paragraph, Text, Typography],
content: '<p>Type -- for em dash, ... for ellipsis, or (c) for copyright.</p>',
});

All replacements happen as you type. Type the trigger sequence and it converts automatically. Press Backspace immediately after to undo the replacement.

All options are booleans that enable or disable a category of replacements. All default to true.

OptionTypeDefaultDescription
emDashbooleantrue-- to em dash
ellipsisbooleantrue... to ellipsis
arrowsbooleantrue<-, ->, => to arrows
fractionsbooleantrue1/2, 1/4, 3/4, 1/3, 2/3 to fraction characters
symbolsbooleantrue(c), (r), (tm), (sm) to symbols (case-insensitive)
mathbooleantrue+/-, !=, <=, >= to math operators
guillemetsbooleantrue<<, >> to guillemets
smartQuotesbooleantrueStraight quotes to curly quotes
openDoubleQuotestring\u201C (")Opening double quote character
closeDoubleQuotestring\u201D (")Closing double quote character
openSingleQuotestring\u2018 (')Opening single quote character
closeSingleQuotestring\u2019 (')Closing single quote character
Typography.configure({
emDash: true,
ellipsis: true,
arrows: false, // disable arrow replacements
fractions: true,
symbols: true,
math: false, // disable math replacements
guillemets: false, // disable guillemets
smartQuotes: true,
openDoubleQuote: '\u00AB', // use « as opening double quote
closeDoubleQuote: '\u00BB', // use » as closing double quote
})

Typography does not register any commands.

Typography does not register any keyboard shortcuts.

Typography registers up to 22 input rules depending on which categories are enabled. All replacements are undoable with Backspace immediately after triggering.

InputResultUnicode
--U+2014
InputResultUnicode
...U+2026
InputResultUnicode
<-U+2190
->U+2192
=>U+21D2
InputResultUnicode
1/2½U+00BD
1/4¼U+00BC
3/4¾U+00BE
1/3U+2153
2/3U+2154
InputResultUnicode
(c)©U+00A9
(r)®U+00AE
(tm)U+2122
(sm)U+2120

Symbol replacements are case-insensitive: (C), (R), (TM), and (SM) also work.

InputResultUnicode
+/-±U+00B1
!=U+2260
<=U+2264
>=U+2265
InputResultUnicode
<<«U+00AB
>>»U+00BB
InputResult
"text"”text” (curly double quotes)
'text'’text’ (curly single quotes)

Smart quotes replace the closing quote character, which triggers the conversion of the entire quoted text. The single quote rule uses a lookbehind to avoid converting apostrophes in contractions (e.g., don't stays as-is).

Typography does not register any toolbar items.

Typography uses ProseMirror input rules via the addInputRules() hook. Each rule consists of a regex pattern (with $ anchor to match at cursor) and a replacement.

Most rules use the textInputRule helper, which creates an InputRule that replaces the matched text with a Unicode character:

textInputRule({ find: /--$/, replace: '\u2014' }) // -- → —

The replacement happens in a single ProseMirror transaction: the matched text range is replaced with a text node containing the Unicode character. All simple replacements are undoable with Backspace.

Smart quotes use custom InputRule handlers (not textInputRule) because they need match groups to extract the quoted text and wrap it with the configured quote characters:

  • Double quotes: regex /"([^"]+)"$/ - matches "text" and replaces with "text" (using configured quote characters)
  • Single quotes: regex /(?:^|[\s([{])'([^']+)'$/ - matches 'text' only when the opening quote follows whitespace or brackets

The single quote pattern includes a prefix check to preserve apostrophes in contractions. If the character before the opening ' is a word character, the rule does not match.

import { Typography } from '@domternal/core';
import type { TypographyOptions } from '@domternal/core';
ExportTypeDescription
TypographyExtensionThe typography extension
TypographyOptionsTypeScript typeOptions for Typography.configure()

@domternal/core - Typography.ts