Add Goldmark as the new default markdown handler

This commit adds the fast and CommonMark compliant Goldmark as the new default markdown handler in Hugo.

If you want to continue using BlackFriday as the default for md/markdown extensions, you can use this configuration:

```toml
[markup]
defaultMarkdownHandler="blackfriday"
```

Fixes #5963
Fixes #1778
Fixes #6355
This commit is contained in:
Bjørn Erik Pedersen
2019-11-06 20:10:47 +01:00
parent a3fe5e5e35
commit bfb9613a14
69 changed files with 3424 additions and 1668 deletions

View File

@@ -18,32 +18,41 @@ import (
// change without notice if it serves a purpose in the docs.
// Format is one of json, yaml or toml.
func (ns *Namespace) Remarshal(format string, data interface{}) (string, error) {
from, err := cast.ToStringE(data)
if err != nil {
return "", err
}
var meta map[string]interface{}
from = strings.TrimSpace(from)
format = strings.TrimSpace(strings.ToLower(format))
if from == "" {
return "", nil
}
mark, err := toFormatMark(format)
if err != nil {
return "", err
}
fromFormat := metadecoders.Default.FormatFromContentString(from)
if fromFormat == "" {
return "", errors.New("failed to detect format from content")
if m, ok := data.(map[string]interface{}); ok {
meta = m
} else {
from, err := cast.ToStringE(data)
if err != nil {
return "", err
}
from = strings.TrimSpace(from)
if from == "" {
return "", nil
}
fromFormat := metadecoders.Default.FormatFromContentString(from)
if fromFormat == "" {
return "", errors.New("failed to detect format from content")
}
meta, err = metadecoders.Default.UnmarshalToMap([]byte(from), fromFormat)
if err != nil {
return "", err
}
}
meta, err := metadecoders.Default.UnmarshalToMap([]byte(from), fromFormat)
if err != nil {
return "", err
}
// Make it so 1.0 float64 prints as 1 etc.
applyMarshalTypes(meta)
var result bytes.Buffer
if err := parser.InterfaceToConfig(meta, mark, &result); err != nil {
@@ -53,6 +62,23 @@ func (ns *Namespace) Remarshal(format string, data interface{}) (string, error)
return result.String(), nil
}
// The unmarshal/marshal dance is extremely type lossy, and we need
// to make sure that integer types prints as "43" and not "43.0" in
// all formats, hence this hack.
func applyMarshalTypes(m map[string]interface{}) {
for k, v := range m {
switch t := v.(type) {
case map[string]interface{}:
applyMarshalTypes(t)
case float64:
i := int64(t)
if t == float64(i) {
m[k] = i
}
}
}
}
func toFormatMark(format string) (metadecoders.Format, error) {
if f := metadecoders.FormatFromString(format); f != "" {
return f, nil