Skip to content

Commit

Permalink
Remove the trailing new line in .Code
Browse files Browse the repository at this point in the history
Fixes #9572
  • Loading branch information
bep committed Feb 26, 2022
1 parent afd63bf commit 10928a4
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 3 deletions.
16 changes: 16 additions & 0 deletions common/text/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package text

import (
"strings"
"sync"
"unicode"

Expand Down Expand Up @@ -45,3 +46,18 @@ func RemoveAccentsString(s string) string {
accentTransformerPool.Put(t)
return s
}

// Chomp removes trailing newline characters from s.
func Chomp(s string) string {
return strings.TrimRightFunc(s, func(r rune) bool {
return r == '\n' || r == '\r'
})
}

// Puts adds a trailing \n none found.
func Puts(s string) string {
if s == "" || s[len(s)-1] == '\n' {
return s
}
return s + "\n"
}
15 changes: 15 additions & 0 deletions common/text/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,18 @@ func TestRemoveAccents(t *testing.T) {
c.Assert(string(RemoveAccents([]byte("Hugo Rocks!"))), qt.Equals, "Hugo Rocks!")
c.Assert(string(RemoveAccentsString("Resumé")), qt.Equals, "Resume")
}

func TestChomp(t *testing.T) {
c := qt.New(t)

c.Assert(Chomp("\nA\n"), qt.Equals, "\nA")
c.Assert(Chomp("A\r\n"), qt.Equals, "A")
}

func TestPuts(t *testing.T) {
c := qt.New(t)

c.Assert(Puts("A"), qt.Equals, "A\n")
c.Assert(Puts("\nA\n"), qt.Equals, "\nA\n")
c.Assert(Puts(""), qt.Equals, "")
}
3 changes: 3 additions & 0 deletions hugolib/integrationtest_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import (
)

func NewIntegrationTestBuilder(conf IntegrationTestConfig) *IntegrationTestBuilder {
// Code fences.
conf.TxtarString = strings.ReplaceAll(conf.TxtarString, "§§§", "```")

data := txtar.Parse([]byte(conf.TxtarString))

c, ok := conf.T.(*qt.C)
Expand Down
31 changes: 31 additions & 0 deletions markup/goldmark/codeblocks/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,34 @@ Go Language: golang|
"<h2 id=\"bash-code\">Bash Code</h2>\n<div class=\"highlight blue\"><pre tabindex=\"0\" class=\"chroma\"><code class=\"language-bash\" data-lang=\"bash\"><span class=\"line\"><span class=\"ln\">32</span><span class=\"cl\"><span class=\"nb\">echo</span> <span class=\"s2\">&#34;l1&#34;</span><span class=\"p\">;</span>\n</span></span><span class=\"line hl\"><span class=\"ln\">33</span>",
)
}

func TestCodeChomp(t *testing.T) {
t.Parallel()

files := `
-- config.toml --
-- content/p1.md --
---
title: "p1"
---
§§§bash
echo "p1";
§§§
-- layouts/_default/single.html --
{{ .Content }}
-- layouts/_default/_markup/render-codeblock.html --
|{{ .Code | safeHTML }}|
`

b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: t,
TxtarString: files,
NeedsOsFS: false,
},
).Build()

b.AssertFileContent("public/p1/index.html", "|echo \"p1\";|")
}
4 changes: 3 additions & 1 deletion markup/goldmark/codeblocks/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"bytes"
"fmt"

htext "github.com/gohugoio/hugo/common/text"
"github.com/gohugoio/hugo/markup/converter/hooks"
"github.com/gohugoio/hugo/markup/goldmark/internal/render"
"github.com/gohugoio/hugo/markup/internal/attributes"
Expand Down Expand Up @@ -75,7 +76,8 @@ func (r *htmlRenderer) renderCodeBlock(w util.BufWriter, src []byte, node ast.No
line := n.b.Lines().At(i)
buff.Write(line.Value(src))
}
text := buff.String()

text := htext.Chomp(buff.String())

var info []byte
if n.b.Info != nil {
Expand Down
5 changes: 4 additions & 1 deletion markup/highlight/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
"github.com/gohugoio/hugo/common/hugio"
"github.com/gohugoio/hugo/common/text"
"github.com/gohugoio/hugo/identity"
"github.com/gohugoio/hugo/markup/converter/hooks"
"github.com/gohugoio/hugo/markup/internal/attributes"
Expand Down Expand Up @@ -123,7 +124,9 @@ func (h chromaHighlighter) RenderCodeblock(w hugio.FlexiWriter, ctx hooks.Codebl
return err
}

return highlight(w, ctx.Code(), ctx.Lang(), attributes, cfg)
code := text.Puts(ctx.Code())

return highlight(w, code, ctx.Lang(), attributes, cfg)
}

var id = identity.NewPathIdentity("chroma", "highlight")
Expand Down
3 changes: 2 additions & 1 deletion tpl/strings/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strings"
"unicode/utf8"

"github.com/gohugoio/hugo/common/text"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/helpers"

Expand Down Expand Up @@ -119,7 +120,7 @@ func (ns *Namespace) Chomp(s interface{}) (interface{}, error) {
return "", err
}

res := strings.TrimRight(ss, "\r\n")
res := text.Chomp(ss)
switch s.(type) {
case template.HTML:
return template.HTML(res), nil
Expand Down

0 comments on commit 10928a4

Please sign in to comment.