@bookshop/hugo-engine 2.5.0 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.nyc_output/{3912b392-2912-48cc-9cca-bf322bf0d4e6.json → 898d2cdb-39d3-43f1-8e67-73ff2486a451.json} +0 -0
- package/.nyc_output/{73434d66-94ab-46e8-a516-e2fb6d961625.json → 98716901-9bd9-4e10-89c7-27bb55998257.json} +0 -0
- package/.nyc_output/{81517a6e-3da5-4c5a-85fb-c3782bc44a31.json → b9ec436e-de9f-4018-a0c6-8c889bc94c66.json} +0 -0
- package/.nyc_output/{90c0d905-7a53-48a0-9c22-a39b1b831857.json → f1d5464a-8b36-494a-b4b0-fdc2b034270e.json} +0 -0
- package/.nyc_output/{c8f28bb3-0364-45d6-858b-7fe760a39c33.json → f23634b1-d1b0-46bf-9098-6c7a68cbe98a.json} +0 -0
- package/.nyc_output/processinfo/{3912b392-2912-48cc-9cca-bf322bf0d4e6.json → 898d2cdb-39d3-43f1-8e67-73ff2486a451.json} +1 -1
- package/.nyc_output/processinfo/{73434d66-94ab-46e8-a516-e2fb6d961625.json → 98716901-9bd9-4e10-89c7-27bb55998257.json} +1 -1
- package/.nyc_output/processinfo/{81517a6e-3da5-4c5a-85fb-c3782bc44a31.json → b9ec436e-de9f-4018-a0c6-8c889bc94c66.json} +1 -1
- package/.nyc_output/processinfo/f1d5464a-8b36-494a-b4b0-fdc2b034270e.json +1 -0
- package/.nyc_output/processinfo/{c8f28bb3-0364-45d6-858b-7fe760a39c33.json → f23634b1-d1b0-46bf-9098-6c7a68cbe98a.json} +1 -1
- package/.nyc_output/processinfo/index.json +1 -1
- package/hugo-renderer/hugo_renderer.wasm +0 -0
- package/hugo-renderer/tpl/bookshop_library/bookshop_components.go +17 -2
- package/hugo-renderer/tpl/transform/bookshop_unmarshal.go +39 -0
- package/hugo-renderer/tpl/transform/init.go +7 -0
- package/lib/engine.js +11 -2
- package/lib/hugoIdentifierParser.js +1 -1
- package/lib/hugoIdentifierParser.test.js +19 -0
- package/lib/translateTextTemplate.js +52 -28
- package/lib/translateTextTemplate.test.js +57 -1
- package/package.json +2 -2
- package/.nyc_output/processinfo/90c0d905-7a53-48a0-9c22-a39b1b831857.json +0 -1
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"parent":"
|
|
1
|
+
{"parent":"f1d5464a-8b36-494a-b4b0-fdc2b034270e","pid":11288,"argv":["/Users/liambigelow/.nvm/versions/node/v14.16.0/bin/node","/Users/liambigelow/Projects/CC/bookshop/javascript-modules/node_modules/ava/lib/worker/subprocess.js"],"execArgv":[],"cwd":"/Users/liambigelow/Projects/CC/bookshop/javascript-modules/engines/hugo-engine","time":1645749730776,"ppid":11287,"coverageFilename":"/Users/liambigelow/Projects/CC/bookshop/javascript-modules/engines/hugo-engine/.nyc_output/898d2cdb-39d3-43f1-8e67-73ff2486a451.json","externalId":"","uuid":"898d2cdb-39d3-43f1-8e67-73ff2486a451","files":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"parent":"
|
|
1
|
+
{"parent":"f1d5464a-8b36-494a-b4b0-fdc2b034270e","pid":11291,"argv":["/Users/liambigelow/.nvm/versions/node/v14.16.0/bin/node","/Users/liambigelow/Projects/CC/bookshop/javascript-modules/node_modules/ava/lib/worker/subprocess.js"],"execArgv":[],"cwd":"/Users/liambigelow/Projects/CC/bookshop/javascript-modules/engines/hugo-engine","time":1645749730792,"ppid":11287,"coverageFilename":"/Users/liambigelow/Projects/CC/bookshop/javascript-modules/engines/hugo-engine/.nyc_output/98716901-9bd9-4e10-89c7-27bb55998257.json","externalId":"","uuid":"98716901-9bd9-4e10-89c7-27bb55998257","files":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"parent":"
|
|
1
|
+
{"parent":"f1d5464a-8b36-494a-b4b0-fdc2b034270e","pid":11290,"argv":["/Users/liambigelow/.nvm/versions/node/v14.16.0/bin/node","/Users/liambigelow/Projects/CC/bookshop/javascript-modules/node_modules/ava/lib/worker/subprocess.js"],"execArgv":[],"cwd":"/Users/liambigelow/Projects/CC/bookshop/javascript-modules/engines/hugo-engine","time":1645749730791,"ppid":11287,"coverageFilename":"/Users/liambigelow/Projects/CC/bookshop/javascript-modules/engines/hugo-engine/.nyc_output/b9ec436e-de9f-4018-a0c6-8c889bc94c66.json","externalId":"","uuid":"b9ec436e-de9f-4018-a0c6-8c889bc94c66","files":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"parent":null,"pid":11287,"argv":["/Users/liambigelow/.nvm/versions/node/v14.16.0/bin/node","/Users/liambigelow/Projects/CC/bookshop/javascript-modules/node_modules/ava/cli.js","-v"],"execArgv":[],"cwd":"/Users/liambigelow/Projects/CC/bookshop/javascript-modules/engines/hugo-engine","time":1645749730164,"ppid":11278,"coverageFilename":"/Users/liambigelow/Projects/CC/bookshop/javascript-modules/engines/hugo-engine/.nyc_output/f1d5464a-8b36-494a-b4b0-fdc2b034270e.json","externalId":"","uuid":"f1d5464a-8b36-494a-b4b0-fdc2b034270e","files":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"parent":"
|
|
1
|
+
{"parent":"f1d5464a-8b36-494a-b4b0-fdc2b034270e","pid":11289,"argv":["/Users/liambigelow/.nvm/versions/node/v14.16.0/bin/node","/Users/liambigelow/Projects/CC/bookshop/javascript-modules/node_modules/ava/lib/worker/subprocess.js"],"execArgv":[],"cwd":"/Users/liambigelow/Projects/CC/bookshop/javascript-modules/engines/hugo-engine","time":1645749730784,"ppid":11287,"coverageFilename":"/Users/liambigelow/Projects/CC/bookshop/javascript-modules/engines/hugo-engine/.nyc_output/f23634b1-d1b0-46bf-9098-6c7a68cbe98a.json","externalId":"","uuid":"f23634b1-d1b0-46bf-9098-6c7a68cbe98a","files":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"processes":{"
|
|
1
|
+
{"processes":{"898d2cdb-39d3-43f1-8e67-73ff2486a451":{"parent":"f1d5464a-8b36-494a-b4b0-fdc2b034270e","children":[]},"98716901-9bd9-4e10-89c7-27bb55998257":{"parent":"f1d5464a-8b36-494a-b4b0-fdc2b034270e","children":[]},"b9ec436e-de9f-4018-a0c6-8c889bc94c66":{"parent":"f1d5464a-8b36-494a-b4b0-fdc2b034270e","children":[]},"f1d5464a-8b36-494a-b4b0-fdc2b034270e":{"parent":null,"children":["898d2cdb-39d3-43f1-8e67-73ff2486a451","98716901-9bd9-4e10-89c7-27bb55998257","b9ec436e-de9f-4018-a0c6-8c889bc94c66","f23634b1-d1b0-46bf-9098-6c7a68cbe98a"]},"f23634b1-d1b0-46bf-9098-6c7a68cbe98a":{"parent":"f1d5464a-8b36-494a-b4b0-fdc2b034270e","children":[]}},"files":{},"externalIds":{}}
|
|
Binary file
|
|
@@ -86,14 +86,24 @@ func UnwrapBookshopComponent(context interface{}) (string, interface{}) {
|
|
|
86
86
|
key := fmt.Sprintf("components/%s/%s.hugo.html", component.(string), component.(string))
|
|
87
87
|
return key, context
|
|
88
88
|
}
|
|
89
|
+
return "err_no_bookshop_name_key", context
|
|
89
90
|
}
|
|
90
91
|
if componentData, ok := context.([]interface{}); ok {
|
|
91
92
|
if component, ok := componentData[0].(string); ok {
|
|
92
93
|
key := fmt.Sprintf("components/%s/%s.hugo.html", component, component)
|
|
93
94
|
return key, componentData[1]
|
|
94
95
|
}
|
|
96
|
+
return "err_slice_zero_not_string", context
|
|
95
97
|
}
|
|
96
|
-
|
|
98
|
+
if componentData, ok := context.([]string); ok {
|
|
99
|
+
key := fmt.Sprintf("components/%s/%s.hugo.html", componentData[0], componentData[0])
|
|
100
|
+
return key, componentData[1]
|
|
101
|
+
}
|
|
102
|
+
if componentName, ok := context.(string); ok {
|
|
103
|
+
key := fmt.Sprintf("components/%s/%s.hugo.html", componentName, componentName)
|
|
104
|
+
return key, nil
|
|
105
|
+
}
|
|
106
|
+
return "err_not_map_slice_or_string", context
|
|
97
107
|
}
|
|
98
108
|
|
|
99
109
|
func UnwrapBookshopPartial(context interface{}) (string, interface{}) {
|
|
@@ -102,6 +112,11 @@ func UnwrapBookshopPartial(context interface{}) (string, interface{}) {
|
|
|
102
112
|
key := fmt.Sprintf("shared/hugo/%s.hugo.html", partial)
|
|
103
113
|
return key, partialData[1]
|
|
104
114
|
}
|
|
115
|
+
return "err_slice_zero_not_string", context
|
|
116
|
+
}
|
|
117
|
+
if partialName, ok := context.(string); ok {
|
|
118
|
+
key := fmt.Sprintf("shared/hugo/%s.hugo.html", partialName)
|
|
119
|
+
return key, nil
|
|
105
120
|
}
|
|
106
|
-
return "
|
|
121
|
+
return "err_not_slice_or_string", context
|
|
107
122
|
}
|
|
@@ -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
|
+
}
|
|
@@ -99,6 +99,13 @@ func init() {
|
|
|
99
99
|
|
|
100
100
|
// Bookshop: unmarshal has been removed (for now) due to tendrils
|
|
101
101
|
|
|
102
|
+
ns.AddMethodMapping(ctx.BookshopUnmarshal,
|
|
103
|
+
[]string{"bookshopunmarshal"},
|
|
104
|
+
[][2]string{
|
|
105
|
+
{`{{ "{ hello = \"Hello World\" }" | transform.BookshopUnmarshal }}`, "map[hello:Hello World]"},
|
|
106
|
+
},
|
|
107
|
+
)
|
|
108
|
+
|
|
102
109
|
return ns
|
|
103
110
|
}
|
|
104
111
|
|
package/lib/engine.js
CHANGED
|
@@ -162,13 +162,22 @@ export class Engine {
|
|
|
162
162
|
return `index (${obj}) ${index}`;
|
|
163
163
|
})
|
|
164
164
|
|
|
165
|
-
const
|
|
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}) }}`;
|
|
166
175
|
const output = window.renderHugo(eval_str, JSON.stringify(props_obj));
|
|
167
176
|
|
|
168
177
|
try {
|
|
169
178
|
return JSON.parse(output);
|
|
170
179
|
} catch (e) {
|
|
171
|
-
console.
|
|
180
|
+
console.warn(`Error evaluating \`${str}\` in the Hugo engine`, output);
|
|
172
181
|
return null;
|
|
173
182
|
}
|
|
174
183
|
}
|
|
@@ -88,4 +88,23 @@ test(`Converts a dot index into a JS object property`, async t => {
|
|
|
88
88
|
ident = `(index . 1234)`;
|
|
89
89
|
output = (new IdentifierParser(ident)).build();
|
|
90
90
|
t.deepEqual(output, `1234`);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test(`Multiline dicts and slices`, async t => {
|
|
94
|
+
const ident = `(dict
|
|
95
|
+
"contents"
|
|
96
|
+
(slice 1 "2"
|
|
97
|
+
(dict
|
|
98
|
+
"a" .Params.b)
|
|
99
|
+
) "len" (
|
|
100
|
+
len
|
|
101
|
+
.some_array
|
|
102
|
+
))`;
|
|
103
|
+
const output = (new IdentifierParser(ident)).build();
|
|
104
|
+
t.deepEqual(output, {
|
|
105
|
+
"contents": ["1", `"2"`, {
|
|
106
|
+
"a": "Params.b"
|
|
107
|
+
}],
|
|
108
|
+
"len": `(\n len\n .some_array\n)`
|
|
109
|
+
});
|
|
91
110
|
});
|
|
@@ -1,17 +1,26 @@
|
|
|
1
1
|
import { Tokenizer } from 'liquidjs';
|
|
2
2
|
|
|
3
|
+
// Homebrewed pretty-regex writer
|
|
3
4
|
const tokens = {
|
|
4
|
-
END:
|
|
5
|
-
BEGIN:
|
|
6
|
-
BEGIN_SCOPED:
|
|
7
|
-
LOOP:
|
|
8
|
-
INDEX_LOOP:
|
|
9
|
-
ASSIGN:
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
END: `{{ end }}`,
|
|
6
|
+
BEGIN: `{{ (if)`,
|
|
7
|
+
BEGIN_SCOPED: `{{ (range|with|define|block|template)`,
|
|
8
|
+
LOOP: `{{ range () }}`,
|
|
9
|
+
INDEX_LOOP: `{{ range (\\$.+), \\$.+ := () }}`,
|
|
10
|
+
ASSIGN: `{{ (\\$\\S+) := () }}`,
|
|
11
|
+
REASSIGN: `{{ (\\$\\S+) = () }}`,
|
|
12
|
+
WITH: `{{ with () }}`,
|
|
13
|
+
BOOKSHOP: `{{ partial "bookshop" \\( slice "()" () \\) }}`,
|
|
14
|
+
BOOKSHOP_SCOPED: `{{ partial "bookshop" \\(? \\. \\)? }}`,
|
|
13
15
|
}
|
|
14
16
|
|
|
17
|
+
const TOKENS = {};
|
|
18
|
+
Object.entries(tokens).forEach(([name, r]) => {
|
|
19
|
+
TOKENS[name] = new RegExp(r
|
|
20
|
+
.replace(/\(\)/g, '([\\S\\s]+?)') // Empty capturing group defaults to lazy multiline capture
|
|
21
|
+
.replace(/ /g, '[\\n\\r\\s]+') // Two spaces actually means one or more blanks
|
|
22
|
+
.replace(/ /g, '[\\n\\r\\s]*')); // One space means zero or more blanks
|
|
23
|
+
});
|
|
15
24
|
/**
|
|
16
25
|
* Parse a go text/template using the liquidjs parser
|
|
17
26
|
* that we already have in the bundle.
|
|
@@ -25,44 +34,53 @@ const rewriteTag = function (token, src, endTags, liveMarkup) {
|
|
|
25
34
|
|
|
26
35
|
// Skip non-value tags
|
|
27
36
|
if (token.kind !== 8) return outputToken;
|
|
28
|
-
if (
|
|
37
|
+
if (TOKENS.END.test(raw)) {
|
|
29
38
|
endTags.push(outputToken);
|
|
30
39
|
return outputToken;
|
|
31
40
|
}
|
|
32
41
|
|
|
33
|
-
if (
|
|
42
|
+
if (TOKENS.BEGIN.test(raw)) {
|
|
34
43
|
endTags.pop();
|
|
35
44
|
}
|
|
36
45
|
|
|
37
|
-
if (
|
|
46
|
+
if (TOKENS.BEGIN_SCOPED.test(raw)) {
|
|
38
47
|
outputToken.text = `${outputToken.text}{{ \`<!--bookshop-live stack-->\` | safeHTML }}`;
|
|
39
48
|
|
|
40
49
|
let matchingEnd = endTags.pop();
|
|
41
50
|
matchingEnd.text = `{{ \`<!--bookshop-live unstack-->\` | safeHTML }}${matchingEnd.text}`;
|
|
42
51
|
}
|
|
43
52
|
|
|
44
|
-
if (liveMarkup &&
|
|
45
|
-
let [, index_variable, iterator] = raw.match(
|
|
53
|
+
if (liveMarkup && TOKENS.INDEX_LOOP.test(raw)) {
|
|
54
|
+
let [, index_variable, iterator] = raw.match(TOKENS.INDEX_LOOP);
|
|
55
|
+
const r = required_wrapper_hugo_func(iterator);
|
|
46
56
|
outputToken.text = [`${outputToken.text}`,
|
|
47
|
-
`{{ (printf \`<!--bookshop-live context(.: (index (${iterator}) %
|
|
57
|
+
`{{${r[0]} (printf \`<!--bookshop-live context(.: (index (${tidy(iterator)}) %v))-->\` (jsonify ${index_variable}))${r[1]} | safeHTML }}`
|
|
48
58
|
].join('')
|
|
49
|
-
} else if (liveMarkup &&
|
|
50
|
-
let [, iterator] = raw.match(
|
|
59
|
+
} else if (liveMarkup && TOKENS.LOOP.test(raw)) {
|
|
60
|
+
let [, iterator] = raw.match(TOKENS.LOOP);
|
|
61
|
+
const r = required_wrapper_hugo_func(iterator);
|
|
51
62
|
outputToken.text = [`{{ $bookshop__live__iterator := 0 }}`,
|
|
52
63
|
`${outputToken.text}`,
|
|
53
|
-
`{{ (printf \`<!--bookshop-live context(.: (index (${iterator}) %d))-->\` $bookshop__live__iterator) | safeHTML }}`,
|
|
64
|
+
`{{${r[0]} (printf \`<!--bookshop-live context(.: (index (${tidy(iterator)}) %d))-->\` $bookshop__live__iterator)${r[1]} | safeHTML }}`,
|
|
54
65
|
`{{ $bookshop__live__iterator = (add $bookshop__live__iterator 1) }}`
|
|
55
66
|
].join('')
|
|
56
|
-
} else if (liveMarkup &&
|
|
57
|
-
let [, identifier, value] = raw.match(
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
67
|
+
} else if (liveMarkup && TOKENS.ASSIGN.test(raw)) {
|
|
68
|
+
let [, identifier, value] = raw.match(TOKENS.ASSIGN);
|
|
69
|
+
const r = required_wrapper_hugo_func(value);
|
|
70
|
+
outputToken.text = `${outputToken.text}{{${r[0]} \`<!--bookshop-live context(${identifier}: (${tidy(value)}))-->\`${r[1]} | safeHTML }}`
|
|
71
|
+
} else if (liveMarkup && TOKENS.REASSIGN.test(raw)) {
|
|
72
|
+
let [, identifier, value] = raw.match(TOKENS.REASSIGN);
|
|
73
|
+
const r = required_wrapper_hugo_func(value);
|
|
74
|
+
outputToken.text = `${outputToken.text}{{${r[0]} \`<!--bookshop-live reassign(${identifier}: (${tidy(value)}))-->\`${r[1]} | safeHTML }}`
|
|
75
|
+
} else if (liveMarkup && TOKENS.WITH.test(raw)) {
|
|
76
|
+
let [, value] = raw.match(TOKENS.WITH);
|
|
77
|
+
const r = required_wrapper_hugo_func(value);
|
|
78
|
+
outputToken.text = `${outputToken.text}{{${r[0]} \`<!--bookshop-live context(.: (${tidy(value)}))-->\`${r[1]} | safeHTML }}`
|
|
79
|
+
} else if (liveMarkup && TOKENS.BOOKSHOP.test(raw)) {
|
|
80
|
+
let [, name, params] = raw.match(TOKENS.BOOKSHOP);
|
|
81
|
+
const r = required_wrapper_hugo_func(params);
|
|
82
|
+
outputToken.text = `{{${r[0]} \`<!--bookshop-live name(${name}) params(.: (${tidy(params)}))-->\`${r[1]} | safeHTML }}${outputToken.text}{{ \`<!--bookshop-live end-->\` | safeHTML }}`
|
|
83
|
+
} else if (liveMarkup && TOKENS.BOOKSHOP_SCOPED.test(raw)) {
|
|
66
84
|
outputToken.text = [`{{ if reflect.IsSlice . }}{{ (printf \`<!--bookshop-live name(%s) params(.: .)-->\` (index . 0)) | safeHTML }}`,
|
|
67
85
|
`{{- else if reflect.IsMap . -}}{{ (printf \`<!--bookshop-live name(%s) params(.: .)-->\` ._bookshop_name) | safeHTML }}{{ end }}`,
|
|
68
86
|
`${outputToken.text}`,
|
|
@@ -73,6 +91,12 @@ const rewriteTag = function (token, src, endTags, liveMarkup) {
|
|
|
73
91
|
return outputToken;
|
|
74
92
|
}
|
|
75
93
|
|
|
94
|
+
// limit comments to one line & escape backticks to something we undo later
|
|
95
|
+
const tidy = val => val.replace(/[\r\n]/g, ' ').replace(/`/g, 'BKSH_BACKTICK');
|
|
96
|
+
|
|
97
|
+
// The replace function we need to add to undo the backtick tidy above
|
|
98
|
+
const required_wrapper_hugo_func = val => /`/.test(val) ? [` replace`, ` "BKSH_BACKTICK" "\`"`] : [``, ``];
|
|
99
|
+
|
|
76
100
|
export default function (text, opts) {
|
|
77
101
|
opts = {
|
|
78
102
|
liveMarkup: true,
|
|
@@ -36,6 +36,10 @@ test("add live markup to assigns", t => {
|
|
|
36
36
|
input = `{{ $a := .b | chomp }}`;
|
|
37
37
|
expected = `{{ $a := .b | chomp }}{{ \`<!--bookshop-live context($a: (.b | chomp))-->\` | safeHTML }}`;
|
|
38
38
|
t.is(translateTextTemplate(input, {}), expected);
|
|
39
|
+
|
|
40
|
+
input = `{{ $a = .b }}`;
|
|
41
|
+
expected = `{{ $a = .b }}{{ \`<!--bookshop-live reassign($a: (.b))-->\` | safeHTML }}`;
|
|
42
|
+
t.is(translateTextTemplate(input, {}), expected);
|
|
39
43
|
});
|
|
40
44
|
|
|
41
45
|
test("add live markup to withs", t => {
|
|
@@ -68,7 +72,7 @@ test("add live markup to loops with iterators", t => {
|
|
|
68
72
|
const input = `{{range $loop_index, $element := .columns}}<p>{{$element}}</p>{{ end }}`;
|
|
69
73
|
const expected = [`{{range $loop_index, $element := .columns}}`,
|
|
70
74
|
`{{ \`<!--bookshop-live stack-->\` | safeHTML }}`,
|
|
71
|
-
`{{ (printf \`<!--bookshop-live context(.: (index (.columns) %
|
|
75
|
+
`{{ (printf \`<!--bookshop-live context(.: (index (.columns) %v))-->\` (jsonify $loop_index)) | safeHTML }}`,
|
|
72
76
|
`<p>{{$element}}</p>`,
|
|
73
77
|
`{{ \`<!--bookshop-live unstack-->\` | safeHTML }}`,
|
|
74
78
|
`{{ end }}`
|
|
@@ -76,6 +80,12 @@ test("add live markup to loops with iterators", t => {
|
|
|
76
80
|
t.is(translateTextTemplate(input, {}), expected);
|
|
77
81
|
});
|
|
78
82
|
|
|
83
|
+
test("escape backticks in values", t => {
|
|
84
|
+
let input = `{{ $a := "hi\`:)" }}`;
|
|
85
|
+
let expected = `{{ $a := "hi\`:)" }}{{ replace \`<!--bookshop-live context($a: ("hiBKSH_BACKTICK:)"))-->\` "BKSH_BACKTICK" "\`" | safeHTML }}`;
|
|
86
|
+
t.is(translateTextTemplate(input, {}), expected);
|
|
87
|
+
});
|
|
88
|
+
|
|
79
89
|
test("add live markup to complex end structures", t => {
|
|
80
90
|
const input = `
|
|
81
91
|
{{ range .items }}
|
|
@@ -108,6 +118,52 @@ test("add live markup to complex end structures", t => {
|
|
|
108
118
|
{{ \`<!--bookshop-live unstack-->\` | safeHTML }}{{end}}
|
|
109
119
|
{{ end }}
|
|
110
120
|
|
|
121
|
+
{{ \`<!--bookshop-live unstack-->\` | safeHTML }}{{ end }}`;
|
|
122
|
+
t.is(translateTextTemplate(input, {}), expected);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("add live markup to complex components", t => {
|
|
126
|
+
const input = `
|
|
127
|
+
{{ $level := default 2 .level }}
|
|
128
|
+
{{ $level = string
|
|
129
|
+
$level }}
|
|
130
|
+
{{ $level_classes := dict
|
|
131
|
+
"1" "border-b py-8 text-4xl"
|
|
132
|
+
"2" "border-b py-6 text-3xl"
|
|
133
|
+
"3" "border-b py-4 text-2xl font-bold"
|
|
134
|
+
"4" "border-b text-xl font-bold"
|
|
135
|
+
}}
|
|
136
|
+
{{ $level_class := "lg" }}
|
|
137
|
+
{{ with index $level_classes $level }}
|
|
138
|
+
{{ $level_class = . }}
|
|
139
|
+
{{ end }}
|
|
140
|
+
{{ $open := printf \`<h%s class="%s">\` $level $level_class }}
|
|
141
|
+
{{ $close := printf \`</h%s>\` $level }}
|
|
142
|
+
{{ with .copy }}
|
|
143
|
+
{{ safeHTML $open }}
|
|
144
|
+
{{ markdownify . }} | bookshop
|
|
145
|
+
{{ safeHTML $close }}
|
|
146
|
+
{{ end }}`;
|
|
147
|
+
const expected = `
|
|
148
|
+
{{ $level := default 2 .level }}{{ \`<!--bookshop-live context($level: (default 2 .level))-->\` | safeHTML }}
|
|
149
|
+
{{ $level = string
|
|
150
|
+
$level }}{{ \`<!--bookshop-live reassign($level: (string $level))-->\` | safeHTML }}
|
|
151
|
+
{{ $level_classes := dict
|
|
152
|
+
"1" "border-b py-8 text-4xl"
|
|
153
|
+
"2" "border-b py-6 text-3xl"
|
|
154
|
+
"3" "border-b py-4 text-2xl font-bold"
|
|
155
|
+
"4" "border-b text-xl font-bold"
|
|
156
|
+
}}{{ \`<!--bookshop-live context($level_classes: (dict "1" "border-b py-8 text-4xl" "2" "border-b py-6 text-3xl" "3" "border-b py-4 text-2xl font-bold" "4" "border-b text-xl font-bold"))-->\` | safeHTML }}
|
|
157
|
+
{{ $level_class := "lg" }}{{ \`<!--bookshop-live context($level_class: ("lg"))-->\` | safeHTML }}
|
|
158
|
+
{{ with index $level_classes $level }}{{ \`<!--bookshop-live stack-->\` | safeHTML }}{{ \`<!--bookshop-live context(.: (index $level_classes $level))-->\` | safeHTML }}
|
|
159
|
+
{{ $level_class = . }}{{ \`<!--bookshop-live reassign($level_class: (.))-->\` | safeHTML }}
|
|
160
|
+
{{ \`<!--bookshop-live unstack-->\` | safeHTML }}{{ end }}
|
|
161
|
+
{{ $open := printf \`<h%s class="%s">\` $level $level_class }}{{ replace \`<!--bookshop-live context($open: (printf BKSH_BACKTICK<h%s class="%s">BKSH_BACKTICK $level $level_class))-->\` "BKSH_BACKTICK" "\`" | safeHTML }}
|
|
162
|
+
{{ $close := printf \`</h%s>\` $level }}{{ replace \`<!--bookshop-live context($close: (printf BKSH_BACKTICK</h%s>BKSH_BACKTICK $level))-->\` "BKSH_BACKTICK" "\`" | safeHTML }}
|
|
163
|
+
{{ with .copy }}{{ \`<!--bookshop-live stack-->\` | safeHTML }}{{ \`<!--bookshop-live context(.: (.copy))-->\` | safeHTML }}
|
|
164
|
+
{{ safeHTML $open }}
|
|
165
|
+
{{ markdownify . }} | bookshop
|
|
166
|
+
{{ safeHTML $close }}
|
|
111
167
|
{{ \`<!--bookshop-live unstack-->\` | safeHTML }}{{ end }}`;
|
|
112
168
|
t.is(translateTextTemplate(input, {}), expected);
|
|
113
169
|
});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bookshop/hugo-engine",
|
|
3
3
|
"packageManager": "yarn@3.0.0",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.6.0",
|
|
5
5
|
"description": "Bookshop frontend Hugo renderer",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"sideEffects": false,
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"nyc": "^15.1.0"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@bookshop/helpers": "2.
|
|
32
|
+
"@bookshop/helpers": "2.6.0",
|
|
33
33
|
"esbuild": "^0.13.10",
|
|
34
34
|
"liquidjs": "9.28.0"
|
|
35
35
|
},
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"parent":null,"pid":36197,"argv":["/Users/liambigelow/.nvm/versions/node/v14.16.0/bin/node","/Users/liambigelow/Projects/CC/bookshop/javascript-modules/node_modules/ava/cli.js","-v"],"execArgv":[],"cwd":"/Users/liambigelow/Projects/CC/bookshop/javascript-modules/engines/hugo-engine","time":1645506759370,"ppid":36196,"coverageFilename":"/Users/liambigelow/Projects/CC/bookshop/javascript-modules/engines/hugo-engine/.nyc_output/90c0d905-7a53-48a0-9c22-a39b1b831857.json","externalId":"","uuid":"90c0d905-7a53-48a0-9c22-a39b1b831857","files":[]}
|