diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-05-28 13:18:50 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-05-30 11:32:55 +0200 |
commit | d2cfaede5be420c7d8b701d97b98bc61b87e46d5 (patch) | |
tree | db0926a42c3b53df10b87671d1733391b831b517 /hugolib | |
parent | 322d19a81fedbf423a047bdf286499d2e25d14be (diff) |
Improve shortcode indentation handling
* Record the leading whitespace (tabs, spaces) before the shortcode when parsing the page.
* Apply that indentation to the rendered result of shortcodes without inner content (where the user will apply indentation).
Fixes #9946
Diffstat (limited to 'hugolib')
-rw-r--r-- | hugolib/shortcode.go | 27 | ||||
-rw-r--r-- | hugolib/shortcode_test.go | 73 |
2 files changed, 100 insertions, 0 deletions
diff --git a/hugolib/shortcode.go b/hugolib/shortcode.go index e718d861a..b822ecfe3 100644 --- a/hugolib/shortcode.go +++ b/hugolib/shortcode.go @@ -170,6 +170,8 @@ type shortcode struct { ordinal int err error + indentation string // indentation from source. + info tpl.Info // One of the output formats (arbitrary) templs []tpl.Template // All output formats @@ -398,6 +400,22 @@ func renderShortcode( return "", false, fe } + if len(sc.inner) == 0 && len(sc.indentation) > 0 { + b := bp.GetBuffer() + i := 0 + text.VisitLinesAfter(result, func(line string) { + // The first line is correctly indented. + if i > 0 { + b.WriteString(sc.indentation) + } + i++ + b.WriteString(line) + }) + + result = b.String() + bp.PutBuffer(b) + } + return result, hasVariants, err } @@ -447,6 +465,15 @@ func (s *shortcodeHandler) extractShortcode(ordinal, level int, pt *pageparser.I } sc := &shortcode{ordinal: ordinal} + // Back up one to identify any indentation. + if pt.Pos() > 0 { + pt.Backup() + item := pt.Next() + if item.IsIndentation() { + sc.indentation = string(item.Val) + } + } + cnt := 0 nestedOrdinal := 0 nextLevel := level + 1 diff --git a/hugolib/shortcode_test.go b/hugolib/shortcode_test.go index 372e1be9b..cafe76703 100644 --- a/hugolib/shortcode_test.go +++ b/hugolib/shortcode_test.go @@ -942,3 +942,76 @@ title: "p1" `) } + +func TestShortcodePreserveIndentation(t *testing.T) { + t.Parallel() + + files := ` +-- config.toml -- +-- content/p1.md -- +--- +title: "p1" +--- + +## List With Indented Shortcodes + +1. List 1 + {{% mark1 %}} + 1. Item Mark1 1 + 1. Item Mark1 2 + {{% mark2 %}} + {{% /mark1 %}} +-- layouts/shortcodes/mark1.md -- +{{ .Inner }} +-- layouts/shortcodes/mark2.md -- +1. Item Mark2 1 +1. Item Mark2 2 + 1. Item Mark2 2-1 +1. Item Mark2 3 +-- layouts/_default/single.html -- +{{ .Content }} +` + + b := NewIntegrationTestBuilder( + IntegrationTestConfig{ + T: t, + TxtarString: files, + Running: true, + }, + ).Build() + + b.AssertFileContent("public/p1/index.html", "<ol>\n<li>\n<p>List 1</p>\n<ol>\n<li>Item Mark1 1</li>\n<li>Item Mark1 2</li>\n<li>Item Mark2 1</li>\n<li>Item Mark2 2\n<ol>\n<li>Item Mark2 2-1</li>\n</ol>\n</li>\n<li>Item Mark2 3</li>\n</ol>\n</li>\n</ol>") + +} + +func TestShortcodeCodeblockIndent(t *testing.T) { + t.Parallel() + + files := ` +-- config.toml -- +-- content/p1.md -- +--- +title: "p1" +--- + +## Code block + + {{% code %}} + +-- layouts/shortcodes/code.md -- +echo "foo"; +-- layouts/_default/single.html -- +{{ .Content }} +` + + b := NewIntegrationTestBuilder( + IntegrationTestConfig{ + T: t, + TxtarString: files, + Running: true, + }, + ).Build() + + b.AssertFileContent("public/p1/index.html", "<pre><code>echo "foo";\n</code></pre>") + +} |