mirror of
https://github.com/gohugoio/hugoDocs.git
synced 2025-09-13 07:37:31 -04:00
content: Adjust usage of whitespace removal with action delimiters
This commit is contained in:
parent
03315336df
commit
f41d28ee12
@ -145,7 +145,7 @@ We leave writing the template code as an exercise for you. Aim for a result simi
|
||||
To access output formats, each `Page` object provides two methods: [`OutputFormats`] (for all formats, including the current one) and [`AlternativeOutputFormats`]. Use `AlternativeOutputFormats` to create a link `rel` list within your site's `head` element, as shown below:
|
||||
|
||||
```go-html-template
|
||||
{{ range .AlternativeOutputFormats -}}
|
||||
{{ range .AlternativeOutputFormats }}
|
||||
<link rel="{{ .Rel }}" type="{{ .MediaType.Type }}" href="{{ .Permalink | safeURL }}">
|
||||
{{ end }}
|
||||
```
|
||||
|
@ -44,16 +44,16 @@ Hugo does not provide a built-in template for Mermaid diagrams. Create your own
|
||||
|
||||
[code block render hook]: /render-hooks/code-blocks/
|
||||
|
||||
```go-html-template {file="layouts/_default/_markup/render-codeblock-mermaid.html"}
|
||||
```go-html-template {file="layouts/_default/_markup/render-codeblock-mermaid.html" copy=true}
|
||||
<pre class="mermaid">
|
||||
{{- .Inner | htmlEscape | safeHTML }}
|
||||
{{ .Inner | htmlEscape | safeHTML }}
|
||||
</pre>
|
||||
{{ .Page.Store.Set "hasMermaid" true }}
|
||||
```
|
||||
|
||||
And then include this snippet at the _bottom_ of the content template, before the closing `body` tag:
|
||||
Then include this snippet at the _bottom_ of your base template, before the closing `body` tag:
|
||||
|
||||
```go-html-template
|
||||
```go-html-template {file="layouts/_default/baseof.html" copy=true}
|
||||
{{ if .Store.Get "hasMermaid" }}
|
||||
<script type="module">
|
||||
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs';
|
||||
@ -64,7 +64,7 @@ And then include this snippet at the _bottom_ of the content template, before th
|
||||
|
||||
With that you can use the `mermaid` language in Markdown code blocks:
|
||||
|
||||
````text
|
||||
````text {copy=true}
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant Alice
|
||||
|
@ -121,7 +121,7 @@ Create a [passthrough render hook] to capture and render the LaTeX markup.
|
||||
{{- $opts := dict "output" "htmlAndMathml" "displayMode" (eq .Type "block") }}
|
||||
{{- with try (transform.ToMath .Inner $opts) }}
|
||||
{{- with .Err }}
|
||||
{{ errorf "Unable to render mathematical markup to HTML using the transform.ToMath function. The KaTeX display engine threw the following error: %s: see %s." . $.Position }}
|
||||
{{- errorf "Unable to render mathematical markup to HTML using the transform.ToMath function. The KaTeX display engine threw the following error: %s: see %s." . $.Position }}
|
||||
{{- else }}
|
||||
{{- .Value }}
|
||||
{{- $.Page.Store.Set "hasMath" true }}
|
||||
|
@ -28,7 +28,7 @@ Generate a `link` element in the `<head>` of each page for each of the alternati
|
||||
{{ if .IsHome }}
|
||||
{{ $title = site.Title }}
|
||||
{{ end }}
|
||||
{{ range .AlternativeOutputFormats -}}
|
||||
{{ range .AlternativeOutputFormats }}
|
||||
{{ printf `<link rel=%q type=%q href=%q title=%q>` .Rel .MediaType.Type .Permalink $title | safeHTML }}
|
||||
{{ end }}
|
||||
...
|
||||
|
@ -22,7 +22,7 @@ The `OutputFormats` method on a `Page` object returns a slice of `OutputFormat`
|
||||
To link to the RSS feed for the current page:
|
||||
|
||||
```go-html-template
|
||||
{{ with .OutputFormats.Get "rss" -}}
|
||||
{{ with .OutputFormats.Get "rss" }}
|
||||
<a href="{{ .RelPermalink }}">RSS Feed</a>
|
||||
{{ end }}
|
||||
```
|
||||
|
@ -105,12 +105,12 @@ For example, to create a code block render hook to render [Mermaid] diagrams:
|
||||
|
||||
```go-html-template {file="layouts/_default/_markup/render-codeblock-mermaid.html" copy=true}
|
||||
<pre class="mermaid">
|
||||
{{- .Inner | htmlEscape | safeHTML }}
|
||||
{{ .Inner | htmlEscape | safeHTML }}
|
||||
</pre>
|
||||
{{ .Page.Store.Set "hasMermaid" true }}
|
||||
```
|
||||
|
||||
Then include this snippet at the bottom of the your base template:
|
||||
Then include this snippet at the _bottom_ of your base template, before the closing `body` tag:
|
||||
|
||||
```go-html-template {file="layouts/_default/baseof.html" copy=true}
|
||||
{{ if .Store.Get "hasMermaid" }}
|
||||
|
@ -90,7 +90,7 @@ Instead of client-side JavaScript rendering of mathematical markup using MathJax
|
||||
{{- $opts := dict "output" "htmlAndMathml" "displayMode" (eq .Type "block") }}
|
||||
{{- with try (transform.ToMath .Inner $opts) }}
|
||||
{{- with .Err }}
|
||||
{{ errorf "Unable to render mathematical markup to HTML using the transform.ToMath function. The KaTeX display engine threw the following error: %s: see %s." . $.Position }}
|
||||
{{- errorf "Unable to render mathematical markup to HTML using the transform.ToMath function. The KaTeX display engine threw the following error: %s: see %s." . $.Position }}
|
||||
{{- else }}
|
||||
{{- .Value }}
|
||||
{{- $.Page.Store.Set "hasMath" true }}
|
||||
|
@ -47,7 +47,7 @@ email = 'jdoe@example.org'
|
||||
To include a feed reference in the `head` element of your rendered pages, place this within the `head` element of your templates:
|
||||
|
||||
```go-html-template
|
||||
{{ with .OutputFormats.Get "rss" -}}
|
||||
{{ with .OutputFormats.Get "rss" }}
|
||||
{{ printf `<link rel=%q type=%q href=%q title=%q>` .Rel .MediaType.Type .Permalink site.Title | safeHTML }}
|
||||
{{ end }}
|
||||
```
|
||||
|
@ -116,7 +116,7 @@ Create a shortcode to capture an image as a page resource, resize it to the give
|
||||
|
||||
```go-html-template {file="layouts/shortcodes/image.html"}
|
||||
{{- with .Page.Resources.Get (.Get "path") }}
|
||||
{{- with .Process (printf "resize %dx wepb" ($.Get "width")) }}
|
||||
{{- with .Process (printf "resize %dx wepb" ($.Get "width")) -}}
|
||||
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="{{ $.Get "alt" }}">
|
||||
{{- end }}
|
||||
{{- end -}}
|
||||
@ -150,21 +150,21 @@ Read more about context in the [introduction to templating].
|
||||
The previous example, while functional, silently fails if the image is missing, and does not gracefully exit if a required argument is missing. We'll add error handling to address these issues:
|
||||
|
||||
```go-html-template {file="layouts/shortcodes/image.html"}
|
||||
{{ with .Get "path" }}
|
||||
{{- with .Get "path" }}
|
||||
{{- with $r := $.Page.Resources.Get ($.Get "path") }}
|
||||
{{- with $.Get "width" }}
|
||||
{{- with $r.Process (printf "resize %dx wepb" ($.Get "width" )) }}
|
||||
{{- $alt := or ($.Get "alt") "" }}
|
||||
{{- $alt := or ($.Get "alt") "" -}}
|
||||
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="{{ $alt }}">
|
||||
{{- end }}
|
||||
{{- else }}
|
||||
{{- errorf "The %q shortcode requires a 'width' argument: see %s" $.Name $.Position }}
|
||||
{{- end }}
|
||||
{{- else }}
|
||||
{{ warnf "The %q shortcode was unable to find %s: see %s" $.Name ($.Get "path") $.Position }}
|
||||
{{- warnf "The %q shortcode was unable to find %s: see %s" $.Name ($.Get "path") $.Position }}
|
||||
{{- end }}
|
||||
{{- else }}
|
||||
{{ errorf "The %q shortcode requires a 'path' argument: see %s" .Name .Position }}
|
||||
{{- errorf "The %q shortcode requires a 'path' argument: see %s" .Name .Position }}
|
||||
{{- end -}}
|
||||
```
|
||||
|
||||
@ -198,9 +198,9 @@ Here's how to call it with positional arguments:
|
||||
Using the `Get` method with zero-indexed keys, we'll initialize variables with descriptive names in our template:
|
||||
|
||||
```go-html-template {file="layouts/shortcodes/image.html"}
|
||||
{{- $path := .Get 0 }}
|
||||
{{- $width := .Get 1 }}
|
||||
{{- $alt := .Get 2 }}
|
||||
{{ $path := .Get 0 }}
|
||||
{{ $width := .Get 1 }}
|
||||
{{ $alt := .Get 2 }}
|
||||
```
|
||||
|
||||
{{< note >}}
|
||||
@ -212,9 +212,9 @@ Positional arguments work well for frequently used shortcodes with one or two ar
|
||||
You can create a shortcode that will accept both named and positional arguments, but not at the same time. Use the [`IsNamedParams`] method to determine whether the shortcode call used named or positional arguments:
|
||||
|
||||
```go-html-template {file="layouts/shortcodes/image.html"}
|
||||
{{- $path := cond (.IsNamedParams) (.Get "path") (.Get 0) }}
|
||||
{{- $width := cond (.IsNamedParams) (.Get "width") (.Get 1) }}
|
||||
{{- $alt := cond (.IsNamedParams) (.Get "alt") (.Get 2) }}
|
||||
{{ $path := cond (.IsNamedParams) (.Get "path") (.Get 0) }}
|
||||
{{ $width := cond (.IsNamedParams) (.Get "width") (.Get 1) }}
|
||||
{{ $alt := cond (.IsNamedParams) (.Get "alt") (.Get 2) }}
|
||||
```
|
||||
|
||||
This example uses the `cond` alias for the [`compare.Conditional`] function to get the argument by name if `IsNamedParams` returns `true`, otherwise get the argument by position.
|
||||
@ -235,9 +235,9 @@ When using named arguments, the `Params` method returns a map:
|
||||
```
|
||||
|
||||
```go-html-template {file="layouts/shortcodes/image.html"}
|
||||
{{- .Params.path }} → a.jpg
|
||||
{{- .Params.width }} → 300
|
||||
{{- .Params.alt }} → A white kitten
|
||||
{{ .Params.path }} → a.jpg
|
||||
{{ .Params.width }} → 300
|
||||
{{ .Params.alt }} → A white kitten
|
||||
```
|
||||
|
||||
When using positional arguments, the `Params` method returns a slice:
|
||||
@ -247,9 +247,9 @@ When using named arguments, the `Params` method returns a map:
|
||||
```
|
||||
|
||||
```go-html-template {file="layouts/shortcodes/image.html"}
|
||||
{{- index .Params 0 }} → a.jpg
|
||||
{{- index .Params 1 }} → 300
|
||||
{{- index .Params 1 }} → A white kitten
|
||||
{{ index .Params 0 }} → a.jpg
|
||||
{{ index .Params 1 }} → 300
|
||||
{{ index .Params 1 }} → A white kitten
|
||||
```
|
||||
|
||||
Combine the `Params` method with the [`collections.IsSet`] function to determine if a parameter is set, even if its value is falsy.
|
||||
|
Loading…
x
Reference in New Issue
Block a user