Skip to content

Commit 0e8e6fd

Browse files
committed
fix: renders <, >, and & characters from default values correctly (fixes #24)
1 parent 6e7dd6d commit 0e8e6fd

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: v2
2+
name: special-characters
3+
description: A chart demonstrating handling of special characters in values files
4+
version: "0.2.0"
5+
home: "https://github.com/norwoodj/helm-docs/tree/master/example-charts/special-characters"
6+
sources: ["https://github.com/norwoodj/helm-docs/tree/master/example-charts/special-characters"]
7+
engine: gotpl
8+
type: application
9+
maintainers:
10+
11+
name: John Norwood
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
special-characters
2+
==================
3+
A chart demonstrating handling of special characters in values files
4+
5+
Current chart version is `0.2.0`
6+
7+
Source code can be found [here](https://github.com/norwoodj/helm-docs/tree/master/example-charts/special-characters)
8+
9+
10+
11+
## Chart Values
12+
13+
| Key | Type | Default | Description |
14+
|-----|------|---------|-------------|
15+
| elasticsearch.clusterHealthCheckParams | string | `"wait_for_status=yellow&timeout=1s"` | The Elasticsearch cluster health status params that will be used by readinessProbe command |
16+
| elasticsearch.clusterHealthCheckParamsDescription | string | `""` | Now let's put some special characters in the description: wait_for_status=yellow&timeout=1s |
17+
| htmlSnippets.one | string | `"<html>\n <head></head>\n <body>\n <h1>Is this right, I don't know html</h1>\n </body>\n</html>\n"` | |
18+
| htmlSnippets.three | string | `"<html><head></head></html>"` | Another description |
19+
| htmlSnippets.two | string | `""` | Let's put it in the description <html></html> |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
elasticsearch:
2+
# elasticsearch.clusterHealthCheckParams -- The Elasticsearch cluster health status params that will be used by readinessProbe command
3+
clusterHealthCheckParams: "wait_for_status=yellow&timeout=1s"
4+
5+
# elasticsearch.clusterHealthCheckParamsDescription -- Now let's put some special characters in the description: wait_for_status=yellow&timeout=1s
6+
clusterHealthCheckParamsDescription: ""
7+
8+
htmlSnippets:
9+
one: |
10+
<html>
11+
<head></head>
12+
<body>
13+
<h1>Is this right, I don't know html</h1>
14+
</body>
15+
</html>
16+
17+
# htmlSnippets.two -- Let's put it in the description <html></html>
18+
two: ""
19+
20+
# htmlSnippets.three -- Another description
21+
# @default -- "<html><head></head></html>"
22+
three: ""

pkg/document/values.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package document
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
67
"regexp"
@@ -86,6 +87,19 @@ func parseNilValueType(key string, description helm.ChartValueDescription) value
8687
}
8788
}
8889

90+
func jsonMarshalNoEscape(key string, value interface{}) (string, error) {
91+
outputBuffer := &bytes.Buffer{}
92+
valueEncoder := json.NewEncoder(outputBuffer)
93+
valueEncoder.SetEscapeHTML(false)
94+
err := valueEncoder.Encode(value)
95+
96+
if err != nil {
97+
return "", fmt.Errorf("failed to marshal default value for %s to json: %s", key, err)
98+
}
99+
100+
return strings.TrimRight(outputBuffer.String(), "\n"), nil
101+
}
102+
89103
func createValueRow(
90104
key string,
91105
value interface{},
@@ -97,7 +111,7 @@ func createValueRow(
97111

98112
defaultValue := description.Default
99113
if defaultValue == "" {
100-
jsonEncodedValue, err := json.Marshal(value)
114+
jsonEncodedValue, err := jsonMarshalNoEscape(key, value)
101115
if err != nil {
102116
return valueRow{}, fmt.Errorf("failed to marshal default value for %s to json: %s", key, err)
103117
}

0 commit comments

Comments
 (0)