Skip to content

Commit e6a85c2

Browse files
committed
fix(hugo): reduce errors when using assigned variables in components
1 parent 420f853 commit e6a85c2

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2019 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 transform
15+
16+
import (
17+
"encoding/json"
18+
19+
"github.com/pkg/errors"
20+
)
21+
22+
// Unmarshal unmarshals the data given, which can be either a string, json.RawMessage
23+
// or a Resource. Supported formats are JSON, TOML, YAML, and CSV.
24+
// You can optionally provide an options map as the first argument.
25+
func (ns *Namespace) BookshopUnmarshal(args ...interface{}) (interface{}, error) {
26+
if len(args) != 1 {
27+
return nil, errors.New("Bookshop unmarshal takes 1 argument")
28+
}
29+
30+
var data interface{}
31+
32+
if r, ok := args[0].(string); ok {
33+
if err := json.Unmarshal([]byte(r), &data); err != nil {
34+
return nil, err
35+
}
36+
return data, nil
37+
}
38+
return nil, errors.New("Needed a string")
39+
}

javascript-modules/engines/hugo-engine/hugo-renderer/tpl/transform/init.go

+7
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ func init() {
9999

100100
// Bookshop: unmarshal has been removed (for now) due to tendrils
101101

102+
ns.AddMethodMapping(ctx.BookshopUnmarshal,
103+
[]string{"bookshopunmarshal"},
104+
[][2]string{
105+
{`{{ "{ hello = \"Hello World\" }" | transform.BookshopUnmarshal }}`, "map[hello:Hello World]"},
106+
},
107+
)
108+
102109
return ns
103110
}
104111

javascript-modules/engines/hugo-engine/lib/engine.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,16 @@ export class Engine {
162162
return `index (${obj}) ${index}`;
163163
})
164164

165-
const eval_str = `{{ jsonify (${str}) }}`;
165+
const assignments = Object.entries(props_obj).filter(([key]) => key.startsWith('$')).map(([key, value]) => {
166+
if (Array.isArray(value)) {
167+
return `{{ ${key} := index ( \`{"a": ${JSON.stringify(value)}}\` | transform.BookshopUnmarshal ) "a" }}`
168+
} else if (typeof value === 'object') {
169+
return `{{ ${key} := \`${JSON.stringify(value)}\` | transform.BookshopUnmarshal }}`
170+
} else {
171+
return `{{ ${key} := ${JSON.stringify(value)} }}`
172+
}
173+
}).join('');
174+
const eval_str = `${assignments}{{ jsonify (${str}) }}`;
166175
const output = window.renderHugo(eval_str, JSON.stringify(props_obj));
167176

168177
try {

0 commit comments

Comments
 (0)