Skip to content

Commit

Permalink
markup/goldmark: Add config options for the typographer extension
Browse files Browse the repository at this point in the history
Note that the config per language part of this will be handled in gohugoio#10602.

Updates gohugoio#9772
  • Loading branch information
bep committed Apr 12, 2023
1 parent d01731d commit 4ad256c
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 12 deletions.
7 changes: 5 additions & 2 deletions markup/goldmark/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions markup/goldmark/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,3 +499,18 @@ LINE5
c.Assert(result, qt.Contains, "<span class=\"ln\">2</span><span class=\"cl\">LINE2\n</span></span>")
})
}

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 = "&laquo;"
mconf.Goldmark.Extensions.Typographer.RightDoubleQuote = "&raquo;"
b := convert(c, mconf, content)
got := string(b.Bytes())

c.Assert(got, qt.Contains, "<p>A &laquo;quote&raquo; and &lsquo;another quote&rsquo; and a &laquo;quote with a &rsquo;nested&rsquo; quote&raquo; and a &lsquo;quote with a &laquo;nested&raquo; quote&rsquo; and an ellipsis&hellip;</p>\n")
}
61 changes: 59 additions & 2 deletions markup/goldmark/goldmark_config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -23,7 +25,19 @@ const (
// DefaultConfig holds the default Goldmark configuration.
var Default = Config{
Extensions: Extensions{
Typographer: true,
Typographer: Typographer{
Disable: false,
LeftSingleQuote: "&lsquo;",
RightSingleQuote: "&rsquo;",
LeftDoubleQuote: "&ldquo;",
RightDoubleQuote: "&rdquo;",
EnDash: "&ndash;",
EmDash: "&mdash;",
Ellipsis: "&hellip;",
LeftAngleQuote: "&laquo;",
RightAngleQuote: "&raquo;",
Apostrophe: "&rsquo;",
},
Footnote: true,
DefinitionList: true,
Table: true,
Expand Down Expand Up @@ -54,7 +68,7 @@ type Config struct {
}

type Extensions struct {
Typographer bool
Typographer Typographer
Footnote bool
DefinitionList bool

Expand All @@ -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 '<br>'
HardWraps bool
Expand Down
33 changes: 25 additions & 8 deletions markup/markup_config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,32 @@ func Decode(cfg config.Provider) (conf Config, err error) {

func normalizeConfig(m map[string]any) {
v, err := maps.GetNestedParam("goldmark.parser", ".", m)
if err != nil {
return
if err == nil {
vm := maps.ToStringMap(v)
// Changed from a bool in 0.81.0
if vv, found := vm["attribute"]; found {
if vvb, ok := vv.(bool); ok {
vm["attribute"] = goldmark_config.ParserAttribute{
Title: vvb,
}
}
}
}
vm := maps.ToStringMap(v)
// Changed from a bool in 0.81.0
if vv, found := vm["attribute"]; found {
if vvb, ok := vv.(bool); ok {
vm["attribute"] = goldmark_config.ParserAttribute{
Title: vvb,

// Changed from a bool in 0.112.0.
v, err = maps.GetNestedParam("goldmark.extensions", ".", m)
if err == nil {
vm := maps.ToStringMap(v)
const typographerKey = "typographer"
if vv, found := vm[typographerKey]; found {
if vvb, ok := vv.(bool); ok {
if !vvb {
vm[typographerKey] = goldmark_config.Typographer{
Disable: true,
}
} else {
delete(vm, typographerKey)
}
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions markup/markup_config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,38 @@ func TestConfig(t *testing.T) {
c.Assert(conf.AsciidocExt.Extensions[0], qt.Equals, "asciidoctor-html5s")
})

c.Run("Decode legacy typographer", func(c *qt.C) {
c.Parallel()
v := config.New()

// typographer was changed from a bool to a struct in 0.112.0.
v.Set("markup", map[string]any{
"goldmark": map[string]any{
"extensions": map[string]any{
"typographer": false,
},
},
})

conf, err := Decode(v)

c.Assert(err, qt.IsNil)
c.Assert(conf.Goldmark.Extensions.Typographer.Disable, qt.Equals, true)

v.Set("markup", map[string]any{
"goldmark": map[string]any{
"extensions": map[string]any{
"typographer": true,
},
},
})

conf, err = Decode(v)

c.Assert(err, qt.IsNil)
c.Assert(conf.Goldmark.Extensions.Typographer.Disable, qt.Equals, false)
c.Assert(conf.Goldmark.Extensions.Typographer.Ellipsis, qt.Equals, "&hellip;")

})

}

0 comments on commit 4ad256c

Please sign in to comment.