diff --git a/markup/goldmark/convert.go b/markup/goldmark/convert.go index 3c8dbb299c9..3fe19442f5d 100644 --- a/markup/goldmark/convert.go +++ b/markup/goldmark/convert.go @@ -120,8 +120,11 @@ func newMarkdown(pcfg converter.ProviderConfig) goldmark.Markdown { extensions = append(extensions, extension.TaskList) } - if cfg.Extensions.Typographer { - extensions = append(extensions, extension.Typographer) + if !cfg.Extensions.Typographer.Disable { + t := extension.NewTypographer( + extension.WithTypographicSubstitutions(cfg.Extensions.Typographer.ToTypographicPunctuationMap()), + ) + extensions = append(extensions, t) } if cfg.Extensions.DefinitionList { diff --git a/markup/goldmark/convert_test.go b/markup/goldmark/convert_test.go index 647ffce583c..e92c651fc23 100644 --- a/markup/goldmark/convert_test.go +++ b/markup/goldmark/convert_test.go @@ -499,3 +499,18 @@ LINE5 c.Assert(result, qt.Contains, "2LINE2\n") }) } + +func TestTypographerConfig(t *testing.T) { + c := qt.New(t) + + content := ` +A "quote" and 'another quote' and a "quote with a 'nested' quote" and a 'quote with a "nested" quote' and an ellipsis... +` + mconf := markup_config.Default + mconf.Goldmark.Extensions.Typographer.LeftDoubleQuote = "«" + mconf.Goldmark.Extensions.Typographer.RightDoubleQuote = "»" + b := convert(c, mconf, content) + got := string(b.Bytes()) + + c.Assert(got, qt.Contains, "
A «quote» and ‘another quote’ and a «quote with a ’nested’ quote» and a ‘quote with a «nested» quote’ and an ellipsis…
\n") +} diff --git a/markup/goldmark/goldmark_config/config.go b/markup/goldmark/goldmark_config/config.go index ff0b6bbef92..a2ff50e3cb8 100644 --- a/markup/goldmark/goldmark_config/config.go +++ b/markup/goldmark/goldmark_config/config.go @@ -14,6 +14,8 @@ // Package goldmark_config holds Goldmark related configuration. package goldmark_config +import "github.com/yuin/goldmark/extension" + const ( AutoHeadingIDTypeGitHub = "github" AutoHeadingIDTypeGitHubAscii = "github-ascii" @@ -23,7 +25,19 @@ const ( // DefaultConfig holds the default Goldmark configuration. var Default = Config{ Extensions: Extensions{ - Typographer: true, + Typographer: Typographer{ + Disable: false, + LeftSingleQuote: "‘", + RightSingleQuote: "’", + LeftDoubleQuote: "“", + RightDoubleQuote: "”", + EnDash: "–", + EmDash: "—", + Ellipsis: "…", + LeftAngleQuote: "«", + RightAngleQuote: "»", + Apostrophe: "’", + }, Footnote: true, DefinitionList: true, Table: true, @@ -54,7 +68,7 @@ type Config struct { } type Extensions struct { - Typographer bool + Typographer Typographer Footnote bool DefinitionList bool @@ -66,6 +80,49 @@ type Extensions struct { TaskList bool } +// Typographer holds typographer configuration. +type Typographer struct { + // Whether to disable typographer. + Disable bool + + // Value used for left single quote. + LeftSingleQuote string + // Value used for right single quote. + RightSingleQuote string + // Value used for left double quote. + LeftDoubleQuote string + // Value used for right double quote. + RightDoubleQuote string + // Value used for en dash. + EnDash string + // Value used for em dash. + EmDash string + // Value used for ellipsis. + Ellipsis string + // Value used for left angle quote. + LeftAngleQuote string + // Value used for right angle quote. + RightAngleQuote string + // Value used for apostrophe. + Apostrophe string +} + +func (t Typographer) ToTypographicPunctuationMap() map[extension.TypographicPunctuation][]byte { + return map[extension.TypographicPunctuation][]byte{ + extension.LeftSingleQuote: []byte(t.LeftSingleQuote), + extension.RightSingleQuote: []byte(t.RightSingleQuote), + extension.LeftDoubleQuote: []byte(t.LeftDoubleQuote), + extension.RightDoubleQuote: []byte(t.RightDoubleQuote), + extension.EnDash: []byte(t.EnDash), + extension.EmDash: []byte(t.EmDash), + extension.Ellipsis: []byte(t.Ellipsis), + extension.LeftAngleQuote: []byte(t.LeftAngleQuote), + extension.RightAngleQuote: []byte(t.RightAngleQuote), + extension.Apostrophe: []byte(t.Apostrophe), + } + +} + type Renderer struct { // Whether softline breaks should be rendered as '