Skip to content
gpdoc
Playground

Typography

Turn typed punctuation and symbols into typographic characters.

Typography converts patterns such as three periods, straight quotes, and (c) as the user types. Its input rules skip inline code and code blocks.

Run this from a Quickstart project.

Terminal window
cargo add gpdoc-plugin-typography

With the paragraph plugin installed, type three periods in an empty paragraph to insert an ellipsis.

use gpdoc::{Editor, EditorConfig, Node};
use gpdoc_plugin_paragraph::paragraph;
use gpdoc_plugin_typography::{typography, TypographyOptions};
let config = EditorConfig::builder()
.plugin(paragraph())
.plugin(typography(Default::default()))
.build()
.expect("configure editor");
let mut editor = Editor::from_document(&config, vec![Node::textblock("paragraph", "")])?;
for _ in 0..3 {
editor.insert_text(".")?;
}
assert_eq!(editor.document().blocks(), [Node::textblock("paragraph", "…")]);

Set TypographyOptions::rtl_quotes to reverse the opening and closing quote characters. The default is false.

let quotes = typography(TypographyOptions { rtl_quotes: true, ..Default::default() });

The default replacements map is empty. Insert Some(String) to override a rule or None to disable it. Missing keys use their built-in replacements.

let mut options = TypographyOptions::default();
options.replacements.insert("ellipsis".into(), None);
options.replacements.insert("rightArrow".into(), Some("⇒".into()));
let punctuation = typography(options);

The typography input rule checks the text leading up to the caret as you type. Use the following keys in the replacement map.

Rule keys Built-in replacements in the same order
emDash, ellipsis —, …
openDoubleQuote, closeDoubleQuote, openSingleQuote, closeSingleQuote “, ”, ‘, ’
leftArrow, rightArrow, laquo, raquo ←, →, «, »
copyright, trademark, servicemark, registeredTrademark ©, ™, ℠, ®
oneHalf, oneQuarter, threeQuarters ½, ¼, ¾
plusMinus, notEqual, multiplication ±, ≠, ×
superscriptTwo, superscriptThree ², ³

typography/src/lib.rs