From 4801ab79176647b5ac49021ed84a515ace43a5a2 Mon Sep 17 00:00:00 2001 From: Philip Kaludercic Date: Fri, 6 Feb 2026 20:23:18 +0100 Subject: [PATCH] Add Typographer to replace punctuation with Unicode --- extension/typographer.go | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/extension/typographer.go b/extension/typographer.go index 44c15ebf..969ff998 100644 --- a/extension/typographer.go +++ b/extension/typographer.go @@ -82,6 +82,22 @@ func newDefaultSubstitutions() [][]byte { return replacements } +func newUnicodeSubstitutions() [][]byte { + replacements := make([][]byte, typographicPunctuationMax) + replacements[LeftSingleQuote] = []byte("‘") + replacements[RightSingleQuote] = []byte("’") + replacements[LeftDoubleQuote] = []byte("“") + replacements[RightDoubleQuote] = []byte("”") + replacements[EnDash] = []byte("–") + replacements[EmDash] = []byte("—") + replacements[Ellipsis] = []byte("…") + replacements[LeftAngleQuote] = []byte("«") + replacements[RightAngleQuote] = []byte("»") + replacements[Apostrophe] = []byte("’") + + return replacements +} + // SetOption implements SetOptioner. func (b *TypographerConfig) SetOption(name parser.OptionName, value interface{}) { switch name { @@ -113,8 +129,8 @@ func (o *withTypographicSubstitutions) SetTypographerOption(p *TypographerConfig p.Substitutions = o.value } -// WithTypographicSubstitutions is a functional otpion that specify replacement text -// for punctuations. +// WithTypographicSubstitutions is a functional option that specifies replacement text +// for punctuations using SGML text entities. func WithTypographicSubstitutions[T []byte | string](values map[TypographicPunctuation]T) TypographerOption { replacements := newDefaultSubstitutions() for k, v := range values { @@ -124,6 +140,17 @@ func WithTypographicSubstitutions[T []byte | string](values map[TypographicPunct return &withTypographicSubstitutions{replacements} } +// WithTypographicSubstitutions is a functional option that specify replacement text +// for punctuations with Unicode. +func WithTypographicSubstitutionsUnicode[T []byte | string](values map[TypographicPunctuation]T) TypographerOption { + replacements := newUnicodeSubstitutions() + for k, v := range values { + replacements[k] = []byte(v) + } + + return &withTypographicSubstitutions{replacements} +} + type typographerDelimiterProcessor struct { }