| 1 |
// Copyright 2024 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 blockquotes_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"path/filepath" |
| 18 |
"strings" |
| 19 |
"testing" |
| 20 |
|
| 21 |
"github.com/gohugoio/hugo/hugolib" |
| 22 |
) |
| 23 |
|
| 24 |
func TestBlockquoteHook(t *testing.T) { |
| 25 |
t.Parallel() |
| 26 |
|
| 27 |
files := ` |
| 28 |
-- hugo.toml -- |
| 29 |
[markup] |
| 30 |
[markup.goldmark] |
| 31 |
[markup.goldmark.parser] |
| 32 |
[markup.goldmark.parser.attribute] |
| 33 |
block = true |
| 34 |
title = true |
| 35 |
-- layouts/_markup/render-blockquote.html -- |
| 36 |
Blockquote: |{{ .Text }}|{{ .Type }}| |
| 37 |
-- layouts/_markup/render-blockquote-alert.html -- |
| 38 |
{{ $text := .Text }} |
| 39 |
Blockquote Alert: |{{ $text }}|{{ .Type }}| |
| 40 |
Blockquote Alert Attributes: |{{ $text }}|{{ .Attributes }}| |
| 41 |
Blockquote Alert Page: |{{ $text }}|{{ .Page.Title }}|{{ .PageInner.Title }}| |
| 42 |
{{ if .Attributes.showpos }} |
| 43 |
Blockquote Alert Position: |{{ $text }}|{{ .Position | safeHTML }}| |
| 44 |
{{ end }} |
| 45 |
-- layouts/single.html -- |
| 46 |
Content: {{ .Content }} |
| 47 |
-- content/p1.md -- |
| 48 |
--- |
| 49 |
title: "p1" |
| 50 |
--- |
| 51 |
|
| 52 |
> [!NOTE] |
| 53 |
> This is a note with some whitespace after the alert type. |
| 54 |
|
| 55 |
> [!TIP] |
| 56 |
> This is a tip. |
| 57 |
|
| 58 |
> [!CAUTION] |
| 59 |
> This is a caution with some whitespace before the alert type. |
| 60 |
|
| 61 |
> A regular blockquote. |
| 62 |
|
| 63 |
> [!TIP] |
| 64 |
> This is a tip with attributes. |
| 65 |
{class="foo bar" id="baz"} |
| 66 |
|
| 67 |
> [!NOTE] |
| 68 |
> Note triggering showing the position. |
| 69 |
{showpos="true"} |
| 70 |
|
| 71 |
> [!nOtE] |
| 72 |
> Mixed case alert type. |
| 73 |
` |
| 74 |
|
| 75 |
b := hugolib.Test(t, files) |
| 76 |
b.AssertFileContentExact("public/p1/index.html", |
| 77 |
"Blockquote Alert: |<p>This is a note with some whitespace after the alert type.</p>|alert|", |
| 78 |
"Blockquote Alert: |<p>This is a tip.</p>", |
| 79 |
"Blockquote Alert: |<p>This is a caution with some whitespace before the alert type.</p>|alert|", |
| 80 |
"Blockquote: |<p>A regular blockquote.</p>\n|regular|", |
| 81 |
"Blockquote Alert Attributes: |<p>This is a tip with attributes.</p>|map[class:foo bar id:baz]|", |
| 82 |
filepath.FromSlash("/content/p1.md:20:1"), |
| 83 |
"Blockquote Alert Page: |<p>This is a tip with attributes.</p>|p1|p1|", |
| 84 |
|
| 85 |
// Issue 12767. |
| 86 |
"Blockquote Alert: |<p>Mixed case alert type.</p>|alert", |
| 87 |
) |
| 88 |
} |
| 89 |
|
| 90 |
func TestBlockquoteEmptyIssue12756(t *testing.T) { |
| 91 |
t.Parallel() |
| 92 |
|
| 93 |
files := ` |
| 94 |
-- hugo.toml -- |
| 95 |
-- content/p1.md -- |
| 96 |
--- |
| 97 |
title: "p1" |
| 98 |
--- |
| 99 |
|
| 100 |
> |
| 101 |
-- layouts/single.html -- |
| 102 |
Content: {{ .Content }} |
| 103 |
|
| 104 |
` |
| 105 |
|
| 106 |
b := hugolib.Test(t, files) |
| 107 |
b.AssertFileContentExact("public/p1/index.html", "Content: <blockquote>\n</blockquote>\n") |
| 108 |
} |
| 109 |
|
| 110 |
func TestBlockquObsidianWithTitleAndSign(t *testing.T) { |
| 111 |
t.Parallel() |
| 112 |
|
| 113 |
files := ` |
| 114 |
-- hugo.toml -- |
| 115 |
-- content/_index.md -- |
| 116 |
--- |
| 117 |
title: "Home" |
| 118 |
--- |
| 119 |
|
| 120 |
> [!danger] |
| 121 |
> Do not approach or handle without protective gear. |
| 122 |
|
| 123 |
> [!tip] Callouts can have custom titles |
| 124 |
> Like this one. |
| 125 |
|
| 126 |
> [!tip] Title-only callout |
| 127 |
|
| 128 |
> [!faq]- Foldable negated callout |
| 129 |
> Yes! In a foldable callout, the contents are hidden when the callout is collapsed |
| 130 |
|
| 131 |
> [!faq]+ Foldable callout |
| 132 |
> Yes! In a foldable callout, the contents are hidden when the callout is collapsed |
| 133 |
|
| 134 |
> [!info] Can callouts be nested? |
| 135 |
> > [!important] Yes!, they can. |
| 136 |
> > > [!tip] You can even use multiple layers of nesting. |
| 137 |
|
| 138 |
-- layouts/home.html -- |
| 139 |
{{ .Content }} |
| 140 |
-- layouts/_markup/render-blockquote.html -- |
| 141 |
AlertType: {{ .AlertType }}|AlertTitle: {{ .AlertTitle }}|AlertSign: {{ .AlertSign | safeHTML }}|Text: {{ .Text }}| |
| 142 |
|
| 143 |
` |
| 144 |
|
| 145 |
b := hugolib.Test(t, files) |
| 146 |
b.AssertFileContentExact("public/index.html", |
| 147 |
"AlertType: tip|AlertTitle: Callouts can have custom titles|AlertSign: |", |
| 148 |
"AlertType: tip|AlertTitle: Title-only callout|AlertSign: |", |
| 149 |
"AlertType: faq|AlertTitle: Foldable negated callout|AlertSign: -|Text: <p>Yes! In a foldable callout, the contents are hidden when the callout is collapsed</p>|", |
| 150 |
"AlertType: faq|AlertTitle: Foldable callout|AlertSign: +|Text: <p>Yes! In a foldable callout, the contents are hidden when the callout is collapsed</p>|", |
| 151 |
"AlertType: danger|AlertTitle: |AlertSign: |Text: <p>Do not approach or handle without protective gear.</p>|", |
| 152 |
"AlertTitle: Can callouts be nested?|", |
| 153 |
"AlertTitle: You can even use multiple layers of nesting.|", |
| 154 |
) |
| 155 |
} |
| 156 |
|
| 157 |
// Issue 12913 |
| 158 |
// Issue 13119 |
| 159 |
// Issue 13302 |
| 160 |
func TestBlockquoteRenderHookTextParsing(t *testing.T) { |
| 161 |
t.Parallel() |
| 162 |
|
| 163 |
files := ` |
| 164 |
-- hugo.toml -- |
| 165 |
disableKinds = ['page','rss','section','sitemap','taxonomy','term'] |
| 166 |
-- layouts/home.html -- |
| 167 |
{{ .Content }} |
| 168 |
-- layouts/_markup/render-blockquote.html -- |
| 169 |
AlertType: {{ .AlertType }}|AlertTitle: {{ .AlertTitle }}|Text: {{ .Text }}| |
| 170 |
-- content/_index.md -- |
| 171 |
--- |
| 172 |
title: home |
| 173 |
--- |
| 174 |
|
| 175 |
> [!one] |
| 176 |
|
| 177 |
> [!two] title |
| 178 |
|
| 179 |
> [!three] |
| 180 |
> line 1 |
| 181 |
|
| 182 |
> [!four] title |
| 183 |
> line 1 |
| 184 |
|
| 185 |
> [!five] |
| 186 |
> line 1 |
| 187 |
> line 2 |
| 188 |
|
| 189 |
> [!six] title |
| 190 |
> line 1 |
| 191 |
> line 2 |
| 192 |
|
| 193 |
> [!seven] |
| 194 |
> - list item |
| 195 |
|
| 196 |
> [!eight] title |
| 197 |
> - list item |
| 198 |
|
| 199 |
> [!nine] |
| 200 |
> line 1 |
| 201 |
> - list item |
| 202 |
|
| 203 |
> [!ten] title |
| 204 |
> line 1 |
| 205 |
> - list item |
| 206 |
|
| 207 |
> [!eleven] |
| 208 |
> line 1 |
| 209 |
> - list item |
| 210 |
> |
| 211 |
> line 2 |
| 212 |
|
| 213 |
> [!twelve] title |
| 214 |
> line 1 |
| 215 |
> - list item |
| 216 |
> |
| 217 |
> line 2 |
| 218 |
|
| 219 |
> [!thirteen] |
| 220 |
>  |
| 221 |
|
| 222 |
> [!fourteen] title |
| 223 |
>  |
| 224 |
|
| 225 |
> [!fifteen] _title_ |
| 226 |
|
| 227 |
> [!sixteen] _title_ |
| 228 |
> line one |
| 229 |
|
| 230 |
> seventeen |
| 231 |
` |
| 232 |
|
| 233 |
b := hugolib.Test(t, files) |
| 234 |
|
| 235 |
b.AssertFileContent("public/index.html", |
| 236 |
"AlertType: one|AlertTitle: |Text: |", |
| 237 |
"AlertType: two|AlertTitle: title|Text: |", |
| 238 |
"AlertType: three|AlertTitle: |Text: <p>line 1</p>|", |
| 239 |
"AlertType: four|AlertTitle: title|Text: <p>line 1</p>|", |
| 240 |
"AlertType: five|AlertTitle: |Text: <p>line 1\nline 2</p>|", |
| 241 |
"AlertType: six|AlertTitle: title|Text: <p>line 1\nline 2</p>|", |
| 242 |
"AlertType: seven|AlertTitle: |Text: <ul>\n<li>list item</li>\n</ul>|", |
| 243 |
"AlertType: eight|AlertTitle: title|Text: <ul>\n<li>list item</li>\n</ul>|", |
| 244 |
"AlertType: nine|AlertTitle: |Text: <p>line 1</p>\n<ul>\n<li>list item</li>\n</ul>|", |
| 245 |
"AlertType: ten|AlertTitle: title|Text: <p>line 1</p>\n<ul>\n<li>list item</li>\n</ul>|", |
| 246 |
"AlertType: eleven|AlertTitle: |Text: <p>line 1</p>\n<ul>\n<li>list item</li>\n</ul>\n<p>line 2</p>|", |
| 247 |
"AlertType: twelve|AlertTitle: title|Text: <p>line 1</p>\n<ul>\n<li>list item</li>\n</ul>\n<p>line 2</p>|", |
| 248 |
"AlertType: thirteen|AlertTitle: |Text: <p><img src=\"a.jpg\" alt=\"alt\"></p>|", |
| 249 |
"AlertType: fourteen|AlertTitle: title|Text: <p><img src=\"a.jpg\" alt=\"alt\"></p>|", |
| 250 |
"AlertType: fifteen|AlertTitle: <em>title</em>|Text: |", |
| 251 |
"AlertType: sixteen|AlertTitle: <em>title</em>|Text: <p>line one</p>|", |
| 252 |
"AlertType: |AlertTitle: |Text: <p>seventeen</p>\n|", |
| 253 |
) |
| 254 |
} |
| 255 |
|
| 256 |
// Issue14046 |
| 257 |
func TestBlockquoteDefaultOutput(t *testing.T) { |
| 258 |
t.Parallel() |
| 259 |
|
| 260 |
files := ` |
| 261 |
-- hugo.toml -- |
| 262 |
disableKinds = ['home','rss','section','sitemap','taxonomy','term'] |
| 263 |
-- content/p1.md -- |
| 264 |
--- |
| 265 |
title: p1 |
| 266 |
--- |
| 267 |
> foo |
| 268 |
-- layouts/page.html -- |
| 269 |
|{{ .Content }}| |
| 270 |
-- xxx -- |
| 271 |
<blockquote> |
| 272 |
{{ .Text }} |
| 273 |
</blockquote> |
| 274 |
` |
| 275 |
|
| 276 |
want := "|<blockquote>\n<p>foo</p>\n</blockquote>\n|" |
| 277 |
|
| 278 |
b := hugolib.Test(t, files) |
| 279 |
b.AssertFileContent("public/p1/index.html", want) // fail |
| 280 |
|
| 281 |
files = strings.ReplaceAll(files, "xxx", "layouts/_markup/render-blockquote.html") |
| 282 |
|
| 283 |
b = hugolib.Test(t, files) |
| 284 |
b.AssertFileContent("public/p1/index.html", want) |
| 285 |
} |