| 1 |
// Copyright 2017 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package transform_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"html/template" |
| 19 |
"strings" |
| 20 |
"testing" |
| 21 |
|
| 22 |
"github.com/gohugoio/hugo/hugolib" |
| 23 |
"github.com/gohugoio/hugo/tpl/transform" |
| 24 |
|
| 25 |
qt "github.com/frankban/quicktest" |
| 26 |
) |
| 27 |
|
| 28 |
type tstNoStringer struct{} |
| 29 |
|
| 30 |
func TestEmojify(t *testing.T) { |
| 31 |
t.Parallel() |
| 32 |
b := hugolib.Test(t, "") |
| 33 |
|
| 34 |
ns := transform.New(b.H.Deps) |
| 35 |
|
| 36 |
for _, test := range []struct { |
| 37 |
s any |
| 38 |
expect any |
| 39 |
}{ |
| 40 |
{":notamoji:", template.HTML(":notamoji:")}, |
| 41 |
{"I :heart: Hugo", template.HTML("I ❤️ Hugo")}, |
| 42 |
// errors |
| 43 |
{tstNoStringer{}, false}, |
| 44 |
} { |
| 45 |
|
| 46 |
result, err := ns.Emojify(test.s) |
| 47 |
|
| 48 |
if bb, ok := test.expect.(bool); ok && !bb { |
| 49 |
b.Assert(err, qt.Not(qt.IsNil)) |
| 50 |
continue |
| 51 |
} |
| 52 |
|
| 53 |
b.Assert(err, qt.IsNil) |
| 54 |
b.Assert(result, qt.Equals, test.expect) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
func TestHighlight(t *testing.T) { |
| 59 |
t.Parallel() |
| 60 |
b := hugolib.Test(t, "") |
| 61 |
|
| 62 |
ns := transform.New(b.H.Deps) |
| 63 |
|
| 64 |
for _, test := range []struct { |
| 65 |
s any |
| 66 |
lang string |
| 67 |
opts any |
| 68 |
expect any |
| 69 |
}{ |
| 70 |
{"func boo() {}", "go", "", "boo"}, |
| 71 |
{"func boo() {}", "go", nil, "boo"}, |
| 72 |
// Issue #4179 |
| 73 |
{`<Foo attr=" < "></Foo>`, "xml", "", `&lt;`}, |
| 74 |
{tstNoStringer{}, "go", "", false}, |
| 75 |
// Issue #9591 |
| 76 |
{strings.Repeat("AAA \n", 10), "bash", template.HTML("linenos=true,noClasses=false"), "line"}, |
| 77 |
} { |
| 78 |
|
| 79 |
result, err := ns.Highlight(test.s, test.lang, test.opts) |
| 80 |
|
| 81 |
if bb, ok := test.expect.(bool); ok && !bb { |
| 82 |
b.Assert(err, qt.Not(qt.IsNil)) |
| 83 |
continue |
| 84 |
} |
| 85 |
|
| 86 |
b.Assert(err, qt.IsNil) |
| 87 |
b.Assert(string(result), qt.Contains, test.expect.(string)) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
func TestCanHighlight(t *testing.T) { |
| 92 |
t.Parallel() |
| 93 |
|
| 94 |
c := qt.New(t) |
| 95 |
ns := &transform.Namespace{} |
| 96 |
|
| 97 |
c.Assert(ns.CanHighlight("go"), qt.Equals, true) |
| 98 |
c.Assert(ns.CanHighlight("foo"), qt.Equals, false) |
| 99 |
} |
| 100 |
|
| 101 |
func TestHTMLEscape(t *testing.T) { |
| 102 |
t.Parallel() |
| 103 |
b := hugolib.Test(t, "") |
| 104 |
|
| 105 |
ns := transform.New(b.H.Deps) |
| 106 |
|
| 107 |
for _, test := range []struct { |
| 108 |
s any |
| 109 |
expect any |
| 110 |
}{ |
| 111 |
{`"Foo & Bar's Diner" <y@z>`, `"Foo & Bar's Diner" <y@z>`}, |
| 112 |
{"Hugo & Caddy > Wordpress & Apache", "Hugo & Caddy > Wordpress & Apache"}, |
| 113 |
// errors |
| 114 |
{tstNoStringer{}, false}, |
| 115 |
} { |
| 116 |
|
| 117 |
result, err := ns.HTMLEscape(test.s) |
| 118 |
|
| 119 |
if bb, ok := test.expect.(bool); ok && !bb { |
| 120 |
b.Assert(err, qt.Not(qt.IsNil)) |
| 121 |
continue |
| 122 |
} |
| 123 |
|
| 124 |
b.Assert(err, qt.IsNil) |
| 125 |
b.Assert(result, qt.Equals, test.expect) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func TestHTMLUnescape(t *testing.T) { |
| 130 |
t.Parallel() |
| 131 |
b := hugolib.Test(t, "") |
| 132 |
|
| 133 |
ns := transform.New(b.H.Deps) |
| 134 |
|
| 135 |
for _, test := range []struct { |
| 136 |
s any |
| 137 |
expect any |
| 138 |
}{ |
| 139 |
{`"Foo & Bar's Diner" <y@z>`, `"Foo & Bar's Diner" <y@z>`}, |
| 140 |
{"Hugo & Caddy > Wordpress & Apache", "Hugo & Caddy > Wordpress & Apache"}, |
| 141 |
// errors |
| 142 |
{tstNoStringer{}, false}, |
| 143 |
} { |
| 144 |
|
| 145 |
result, err := ns.HTMLUnescape(test.s) |
| 146 |
|
| 147 |
if bb, ok := test.expect.(bool); ok && !bb { |
| 148 |
b.Assert(err, qt.Not(qt.IsNil)) |
| 149 |
continue |
| 150 |
} |
| 151 |
|
| 152 |
b.Assert(err, qt.IsNil) |
| 153 |
b.Assert(result, qt.Equals, test.expect) |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
func TestMarkdownify(t *testing.T) { |
| 158 |
t.Parallel() |
| 159 |
b := hugolib.Test(t, "") |
| 160 |
|
| 161 |
ns := transform.New(b.H.Deps) |
| 162 |
|
| 163 |
for _, test := range []struct { |
| 164 |
s any |
| 165 |
expect any |
| 166 |
}{ |
| 167 |
{"Hello **World!**", template.HTML("Hello <strong>World!</strong>")}, |
| 168 |
{[]byte("Hello Bytes **World!**"), template.HTML("Hello Bytes <strong>World!</strong>")}, |
| 169 |
{tstNoStringer{}, false}, |
| 170 |
} { |
| 171 |
|
| 172 |
result, err := ns.Markdownify(context.Background(), test.s) |
| 173 |
|
| 174 |
if bb, ok := test.expect.(bool); ok && !bb { |
| 175 |
b.Assert(err, qt.Not(qt.IsNil)) |
| 176 |
continue |
| 177 |
} |
| 178 |
|
| 179 |
b.Assert(err, qt.IsNil) |
| 180 |
b.Assert(result, qt.Equals, test.expect) |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
// Issue #3040 |
| 185 |
func TestMarkdownifyBlocksOfText(t *testing.T) { |
| 186 |
t.Parallel() |
| 187 |
b := hugolib.Test(t, "") |
| 188 |
|
| 189 |
ns := transform.New(b.H.Deps) |
| 190 |
|
| 191 |
text := ` |
| 192 |
#First |
| 193 |
|
| 194 |
This is some *bold* text. |
| 195 |
|
| 196 |
## Second |
| 197 |
|
| 198 |
This is some more text. |
| 199 |
|
| 200 |
And then some. |
| 201 |
` |
| 202 |
|
| 203 |
result, err := ns.Markdownify(context.Background(), text) |
| 204 |
b.Assert(err, qt.IsNil) |
| 205 |
b.Assert(result, qt.Equals, template.HTML( |
| 206 |
"<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")) |
| 207 |
} |
| 208 |
|
| 209 |
func TestPlainify(t *testing.T) { |
| 210 |
t.Parallel() |
| 211 |
b := hugolib.Test(t, "") |
| 212 |
|
| 213 |
ns := transform.New(b.H.Deps) |
| 214 |
|
| 215 |
for _, test := range []struct { |
| 216 |
s any |
| 217 |
expect any |
| 218 |
}{ |
| 219 |
{"<em>Note:</em> blah <b>blah</b>", "Note: blah blah"}, |
| 220 |
{"<div data-action='click->my-controller#doThing'>qwe</div>", "qwe"}, |
| 221 |
// errors |
| 222 |
{tstNoStringer{}, false}, |
| 223 |
} { |
| 224 |
|
| 225 |
result, err := ns.Plainify(test.s) |
| 226 |
|
| 227 |
if bb, ok := test.expect.(bool); ok && !bb { |
| 228 |
b.Assert(err, qt.Not(qt.IsNil)) |
| 229 |
continue |
| 230 |
} |
| 231 |
|
| 232 |
b.Assert(err, qt.IsNil) |
| 233 |
b.Assert(result, qt.Equals, template.HTML(test.expect.(string))) |
| 234 |
} |
| 235 |
} |