github.com/neohugo/neohugo@v0.123.8/hugolib/rendershortcodes_test.go (about)

     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 hugolib
    15  
    16  import (
    17  	"strings"
    18  	"testing"
    19  )
    20  
    21  func TestRenderShortcodesBasic(t *testing.T) {
    22  	t.Parallel()
    23  
    24  	files := `
    25  -- hugo.toml --
    26  disableKinds = ["home", "taxonomy", "term"]
    27  -- content/p1.md --
    28  ---
    29  title: "p1"
    30  ---
    31  ## p1-h1
    32  {{% include "p2" %}}
    33  -- content/p2.md --
    34  ---
    35  title: "p2"
    36  ---
    37  ### p2-h1
    38  {{< withhtml >}}
    39  ### p2-h2
    40  {{% withmarkdown %}}
    41  ### p2-h3
    42  {{% include "p3" %}}
    43  -- content/p3.md --
    44  ---
    45  title: "p3"
    46  ---
    47  ### p3-h1
    48  {{< withhtml >}}
    49  ### p3-h2
    50  {{% withmarkdown %}}
    51  {{< level3 >}}
    52  -- layouts/shortcodes/include.html --
    53  {{ $p := site.GetPage (.Get 0) }}
    54  {{ $p.RenderShortcodes }}
    55  -- layouts/shortcodes/withhtml.html --
    56  <div>{{ .Page.Title }} withhtml</div>
    57  -- layouts/shortcodes/withmarkdown.html --
    58  #### {{ .Page.Title }} withmarkdown
    59  -- layouts/shortcodes/level3.html --
    60  Level 3: {{ .Page.Title }}
    61  -- layouts/_default/single.html --
    62  Fragments: {{ .Fragments.Identifiers }}|
    63  HasShortcode Level 1: {{ .HasShortcode "include" }}|
    64  HasShortcode Level 2: {{ .HasShortcode "withmarkdown" }}|
    65  HasShortcode Level 3: {{ .HasShortcode "level3" }}|
    66  HasShortcode not found: {{ .HasShortcode "notfound" }}|
    67  Content: {{ .Content }}|
    68  `
    69  
    70  	b := Test(t, files)
    71  
    72  	b.AssertFileContent("public/p1/index.html",
    73  		"Fragments: [p1-h1 p2-h1 p2-h2 p2-h3 p2-withmarkdown p3-h1 p3-h2 p3-withmarkdown]|",
    74  		"HasShortcode Level 1: true|",
    75  		"HasShortcode Level 2: true|",
    76  		"HasShortcode Level 3: true|",
    77  		"HasShortcode not found: false|",
    78  	)
    79  }
    80  
    81  func TestRenderShortcodesNestedMultipleOutputFormatTemplates(t *testing.T) {
    82  	t.Parallel()
    83  
    84  	files := `
    85  -- hugo.toml --
    86  disableKinds = ["home", "taxonomy", "term", "section", "rss", "sitemap", "robotsTXT", "404"]
    87  [outputs]
    88  page = ["html", "json"]
    89  -- content/p1.md --
    90  ---
    91  title: "p1"
    92  ---
    93  ## p1-h1
    94  {{% include "p2" %}}
    95  -- content/p2.md --
    96  ---
    97  title: "p2"
    98  ---
    99  ### p2-h1
   100  {{% myshort %}}
   101  -- layouts/shortcodes/include.html --
   102  {{ $p := site.GetPage (.Get 0) }}
   103  {{ $p.RenderShortcodes }}
   104  -- layouts/shortcodes/myshort.html --
   105  Myshort HTML.
   106  -- layouts/shortcodes/myshort.json --
   107  Myshort JSON.
   108  -- layouts/_default/single.html --
   109  HTML: {{ .Content }}
   110  -- layouts/_default/single.json --
   111  JSON: {{ .Content }}
   112  
   113  
   114  `
   115  
   116  	b := Test(t, files)
   117  
   118  	b.AssertFileContent("public/p1/index.html", "Myshort HTML")
   119  	b.AssertFileContent("public/p1/index.json", "Myshort JSON")
   120  }
   121  
   122  func TestRenderShortcodesEditNested(t *testing.T) {
   123  	t.Parallel()
   124  
   125  	files := `
   126  -- hugo.toml --
   127  disableLiveReload = true
   128  disableKinds = ["home", "taxonomy", "term", "section", "rss", "sitemap", "robotsTXT", "404"]
   129  -- content/p1.md --
   130  ---
   131  title: "p1"
   132  ---
   133  ## p1-h1
   134  {{% include "p2" %}}
   135  -- content/p2.md --
   136  ---
   137  title: "p2"
   138  ---
   139  ### p2-h1
   140  {{% myshort %}}
   141  -- layouts/shortcodes/include.html --
   142  {{ $p := site.GetPage (.Get 0) }}
   143  {{ $p.RenderShortcodes }}
   144  -- layouts/shortcodes/myshort.html --
   145  Myshort Original.
   146  -- layouts/_default/single.html --
   147   {{ .Content }}
   148  `
   149  	b := TestRunning(t, files)
   150  	b.AssertFileContent("public/p1/index.html", "Myshort Original.")
   151  
   152  	b.EditFileReplaceAll("layouts/shortcodes/myshort.html", "Original", "Edited").Build()
   153  	b.AssertFileContent("public/p1/index.html", "Myshort Edited.")
   154  }
   155  
   156  func TestRenderShortcodesEditIncludedPage(t *testing.T) {
   157  	t.Parallel()
   158  
   159  	files := `
   160  -- hugo.toml --
   161  disableLiveReload = true
   162  disableKinds = ["home", "taxonomy", "term", "section", "rss", "sitemap", "robotsTXT", "404"]
   163  -- content/p1.md --
   164  ---
   165  title: "p1"
   166  ---
   167  ## p1-h1
   168  {{% include "p2" %}}
   169  -- content/p2.md --
   170  ---
   171  title: "p2"
   172  ---
   173  ### Original
   174  {{% myshort %}}
   175  -- layouts/shortcodes/include.html --
   176  {{ $p := site.GetPage (.Get 0) }}
   177  {{ $p.RenderShortcodes }}
   178  -- layouts/shortcodes/myshort.html --
   179  Myshort Original.
   180  -- layouts/_default/single.html --
   181   {{ .Content }}
   182  
   183  
   184  
   185  `
   186  
   187  	b := NewIntegrationTestBuilder(
   188  		IntegrationTestConfig{
   189  			T:           t,
   190  			TxtarString: files,
   191  			Running:     true,
   192  		},
   193  	).Build()
   194  
   195  	b.AssertFileContent("public/p1/index.html", "Original")
   196  
   197  	b.EditFileReplaceFunc("content/p2.md", func(s string) string {
   198  		return strings.Replace(s, "Original", "Edited", 1)
   199  	})
   200  	b.Build()
   201  	b.AssertFileContent("public/p1/index.html", "Edited")
   202  }