Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/tpl
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-11-06 22:10:47 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-11-23 16:12:24 +0300
commitbfb9613a14ab2d93a4474e5486d22e52a9d5e2b3 (patch)
tree81c4dbd10505e952489e1dbcf1d7bafc88b57c28 /tpl
parenta3fe5e5e35f311f22b6b4fc38abfcf64cd2c7d6f (diff)
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
Diffstat (limited to 'tpl')
-rw-r--r--tpl/transform/remarshal.go58
-rw-r--r--tpl/transform/remarshal_test.go18
-rw-r--r--tpl/transform/transform.go2
-rw-r--r--tpl/transform/transform_test.go2
4 files changed, 61 insertions, 19 deletions
diff --git a/tpl/transform/remarshal.go b/tpl/transform/remarshal.go
index 182bd21d6..d9b6829a0 100644
--- a/tpl/transform/remarshal.go
+++ b/tpl/transform/remarshal.go
@@ -18,33 +18,42 @@ 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
+ }
- meta, err := metadecoders.Default.UnmarshalToMap([]byte(from), fromFormat)
- 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
+ }
}
+ // 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 {
return "", err
@@ -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
diff --git a/tpl/transform/remarshal_test.go b/tpl/transform/remarshal_test.go
index 06bae42d4..12d8aafb5 100644
--- a/tpl/transform/remarshal_test.go
+++ b/tpl/transform/remarshal_test.go
@@ -156,11 +156,11 @@ Hugo = "Rules"
func TestTestRemarshalError(t *testing.T) {
t.Parallel()
+ c := qt.New(t)
v := viper.New()
v.Set("contentDir", "content")
ns := New(newDeps(v))
- c := qt.New(t)
_, err := ns.Remarshal("asdf", "asdf")
c.Assert(err, qt.Not(qt.IsNil))
@@ -169,3 +169,19 @@ func TestTestRemarshalError(t *testing.T) {
c.Assert(err, qt.Not(qt.IsNil))
}
+
+func TestTestRemarshalMapInput(t *testing.T) {
+ t.Parallel()
+ c := qt.New(t)
+ v := viper.New()
+ v.Set("contentDir", "content")
+ ns := New(newDeps(v))
+
+ input := map[string]interface{}{
+ "hello": "world",
+ }
+
+ output, err := ns.Remarshal("toml", input)
+ c.Assert(err, qt.IsNil)
+ c.Assert(output, qt.Equals, "hello = \"world\"\n")
+}
diff --git a/tpl/transform/transform.go b/tpl/transform/transform.go
index 24eedc24f..b168d2a50 100644
--- a/tpl/transform/transform.go
+++ b/tpl/transform/transform.go
@@ -65,7 +65,7 @@ func (ns *Namespace) Highlight(s interface{}, lang, opts string) (template.HTML,
return "", err
}
- highlighted, _ := ns.deps.ContentSpec.Highlight(ss, lang, opts)
+ highlighted, _ := ns.deps.ContentSpec.Converters.Highlight(ss, lang, opts)
return template.HTML(highlighted), nil
}
diff --git a/tpl/transform/transform_test.go b/tpl/transform/transform_test.go
index a6a3b793e..3794c6f2e 100644
--- a/tpl/transform/transform_test.go
+++ b/tpl/transform/transform_test.go
@@ -204,7 +204,7 @@ And then some.
result, err := ns.Markdownify(text)
c.Assert(err, qt.IsNil)
c.Assert(result, qt.Equals, template.HTML(
- "<p>#First</p>\n\n<p>This is some <em>bold</em> text.</p>\n\n<h2 id=\"second\">Second</h2>\n\n<p>This is some more text.</p>\n\n<p>And then some.</p>\n"))
+ "<p>#First</p>\n<p>This is some <em>bold</em> text.</p>\n<h2 id=\"second\">Second</h2>\n<p>This is some more text.</p>\n<p>And then some.</p>\n"))
}