Documents strings.TrimSpace

This commit is contained in:
Joe Mooring
2024-10-21 09:22:23 -07:00
committed by GitHub
parent 533dd3a7bc
commit 6103c1e841
11 changed files with 47 additions and 59 deletions
+1
View File
@@ -7,6 +7,7 @@ action:
aliases: [chomp]
related:
- functions/strings/Trim
- functions/strings/TrimSpace
- functions/strings/TrimLeft
- functions/strings/TrimPrefix
- functions/strings/TrimRight
@@ -1,6 +1,6 @@
---
title: strings.ContainsNonSpace
description: Reports whether the given string contains any non-space characters as defined by Unicode's White Space property.
description: Reports whether the given string contains any non-space characters as defined by Unicode.
categories: []
keywords: []
action:
@@ -18,18 +18,12 @@ aliases: [/functions/strings.containsnonspace]
{{< new-in 0.111.0 >}}
Whitespace characters include `\t`, `\n`, `\v`, `\f`, `\r`, and characters in the [Unicode Space Separator] category.
[Unicode Space Separator]: https://www.compart.com/en/unicode/category/Zs
```go-html-template
{{ strings.ContainsNonSpace "\n" }} → false
{{ strings.ContainsNonSpace " " }} → false
{{ strings.ContainsNonSpace "\n abc" }} → true
```
Common whitespace characters include:
```text
'\t', '\n', '\v', '\f', '\r', ' '
```
See the [Unicode Character Database] for a complete list.
[Unicode Character Database]: https://www.unicode.org/Public/UCD/latest/ucd/PropList.txt
+1 -38
View File
@@ -7,6 +7,7 @@ action:
aliases: [trim]
related:
- functions/strings/Chomp
- functions/strings/TrimSpace
- functions/strings/TrimLeft
- functions/strings/TrimPrefix
- functions/strings/TrimRight
@@ -19,41 +20,3 @@ aliases: [/functions/trim]
```go-html-template
{{ trim "++foo--" "+-" }} → foo
```
To remove leading and trailing newline characters and carriage returns:
```go-html-template
{{ trim "\nfoo\n" "\n\r" }} → foo
{{ trim "\n\nfoo\n\n" "\n\r" }} → foo
{{ trim "\r\nfoo\r\n" "\n\r" }} → foo
{{ trim "\r\n\r\nfoo\r\n\r\n" "\n\r" }} → foo
```
The `strings.Trim` function is commonly used in shortcodes to remove leading and trailing newlines characters and carriage returns from the content within the opening and closing shortcode tags.
For example, with this Markdown:
```text
{{</* my-shortcode */>}}
Able was I ere I saw Elba.
{{</* /my-shortcode */>}}
```
The value of `.Inner` in the shortcode template is:
```text
\nAble was I ere I saw Elba.\n
```
If authored on a Windows system the value of `.Inner` might, depending on the editor configuration, be:
```text
\r\nAble was I ere I saw Elba.\r\n
```
This construct is common in shortcode templates:
```go-html-template
{{ trim .Inner "\n\r" }}
```
+1
View File
@@ -8,6 +8,7 @@ action:
related:
- functions/strings/Chomp
- functions/strings/Trim
- functions/strings/TrimSpace
- functions/strings/TrimPrefix
- functions/strings/TrimRight
- functions/strings/TrimSuffix
@@ -8,6 +8,7 @@ action:
related:
- functions/strings/Chomp
- functions/strings/Trim
- functions/strings/TrimSpace
- functions/strings/TrimLeft
- functions/strings/TrimRight
- functions/strings/TrimSuffix
@@ -8,6 +8,7 @@ action:
related:
- functions/strings/Chomp
- functions/strings/Trim
- functions/strings/TrimSpace
- functions/strings/TrimLeft
- functions/strings/TrimPrefix
- functions/strings/TrimSuffix
+26
View File
@@ -0,0 +1,26 @@
---
title: strings.TrimSpace
description: Returns the given string, removing leading and trailing whitespace as defined by Unicode.
categories: []
keywords: []
action:
related:
- functions/strings/Chomp
- functions/strings/Trim
- functions/strings/TrimLeft
- functions/strings/TrimPrefix
- functions/strings/TrimRight
- functions/strings/TrimSuffix
returnType: string
signatures: [strings.TrimSpace INPUT]
---
{{< new-in 0.137.0 >}}
Whitespace characters include `\t`, `\n`, `\v`, `\f`, `\r`, and characters in the [Unicode Space Separator] category.
[Unicode Space Separator]: https://www.compart.com/en/unicode/category/Zs
```go-html-template
{{ strings.TrimSpace "\n\r\t foo \n\r\t" }} → foo
```
@@ -8,6 +8,7 @@ action:
related:
- functions/strings/Chomp
- functions/strings/Trim
- functions/strings/TrimSpace
- functions/strings/TrimLeft
- functions/strings/TrimPrefix
- functions/strings/TrimRight
+7 -7
View File
@@ -29,7 +29,7 @@ With this shortcode:
<div class="card-title">{{ . }}</div>
{{ end }}
<div class="card-content">
{{ trim .Inner "\r\n" }}
{{ .Inner | strings.TrimSpace }}
</div>
</div>
{{< /code >}}
@@ -46,9 +46,9 @@ Is rendered to:
```
{{% note %}}
Content between opening and closing shortcode tags may include leading and/or trailing newlines, depending on placement within the Markdown. Use the [`trim`] function as shown above to remove both carriage returns and newlines.
Content between opening and closing shortcode tags may include leading and/or trailing newlines, depending on placement within the Markdown. Use the [`strings.TrimSpace`] function as shown above to remove both carriage returns and newlines.
[`trim`]: /functions/strings/trim/
[`strings.TrimSpace`]: /functions/strings/trimspace/
{{% /note %}}
{{% note %}}
@@ -68,7 +68,7 @@ Let's modify the example above to pass the value returned by `Inner` through the
<div class="card-title">{{ . }}</div>
{{ end }}
<div class="card-content">
{{ trim .Inner "\r\n" | .Page.RenderString }}
{{ .Inner | strings.TrimSpace | .Page.RenderString }}
</div>
</div>
{{< /code >}}
@@ -119,7 +119,7 @@ Second, because we are rendering the entire shortcode as Markdown, we must adher
{{ end }}
<div class="card-content">
{{ trim .Inner "\r\n" }}
{{ .Inner | strings.TrimSpace }}
</div>
</div>
{{< /code >}}
@@ -136,9 +136,9 @@ The difference between this and the previous example is subtle but required. Not
+ <div class="card-title">{{ . }}</div>
{{ end }}
<div class="card-content">
- {{ trim .Inner "\r\n" | .Page.RenderString }}
- {{ .Inner | strings.TrimSpace | .Page.RenderString }}
+
+ {{ trim .Inner "\r\n" }}
+ {{ .Inner | strings.TrimSpace }}
</div>
</div>
```
@@ -38,7 +38,7 @@ With this shortcode, calling `Inner` instead of `InnerDeindent`:
{{< code file=layouts/shortcodes/gallery.html >}}
<div class="gallery">
{{ trim .Inner "\r\n" | .Page.RenderString }}
{{ .Inner | strings.TrimSpace | .Page.RenderString }}
</div>
{{< /code >}}
@@ -69,7 +69,7 @@ Although technically correct per the CommonMark specification, this is not what
{{< code file=layouts/shortcodes/gallery.html >}}
<div class="gallery">
{{ trim .InnerDeindent "\r\n" | .Page.RenderString }}
{{ .InnerDeindent | strings.TrimSpace | .Page.RenderString }}
</div>
{{< /code >}}
+1 -1
View File
@@ -21,7 +21,7 @@ Welcome. Today is {{</* now */>}}.
{{< code file=layouts/shortcodes/greeting.html >}}
<div class="greeting">
{{ trim .Inner "\r\n" | .Page.RenderString }}
{{ .Inner | strings.TrimSpace | .Page.RenderString }}
</div>
{{< /code >}}