Skip to content

Commit 408573f

Browse files
committed
fix(hugo): iterating over a map now works correctly when live editing
1 parent 5706627 commit 408573f

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const rewriteTag = function (token, src, endTags, liveMarkup) {
5454
let [, index_variable, iterator] = raw.match(TOKENS.INDEX_LOOP);
5555
const r = required_wrapper_hugo_func(iterator);
5656
outputToken.text = [`${outputToken.text}`,
57-
`{{${r[0]} (printf \`<!--bookshop-live context(.: (index (${tidy(iterator)}) %d))-->\` ${index_variable})${r[1]} | safeHTML }}`
57+
`{{${r[0]} (printf \`<!--bookshop-live context(.: (index (${tidy(iterator)}) %v))-->\` (jsonify ${index_variable}))${r[1]} | safeHTML }}`
5858
].join('')
5959
} else if (liveMarkup && TOKENS.LOOP.test(raw)) {
6060
let [, iterator] = raw.match(TOKENS.LOOP);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ test("add live markup to loops with iterators", t => {
7272
const input = `{{range $loop_index, $element := .columns}}<p>{{$element}}</p>{{ end }}`;
7373
const expected = [`{{range $loop_index, $element := .columns}}`,
7474
`{{ \`<!--bookshop-live stack-->\` | safeHTML }}`,
75-
`{{ (printf \`<!--bookshop-live context(.: (index (.columns) %d))-->\` $loop_index) | safeHTML }}`,
75+
`{{ (printf \`<!--bookshop-live context(.: (index (.columns) %v))-->\` (jsonify $loop_index)) | safeHTML }}`,
7676
`<p>{{$element}}</p>`,
7777
`{{ \`<!--bookshop-live unstack-->\` | safeHTML }}`,
7878
`{{ end }}`

javascript-modules/integration-tests/features/hugo/live_editing/hugo_bookshop_live_edge_cases.feature

+54
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,58 @@ Feature: Hugo Bookshop CloudCannon Live Editing Edge Cases
7575
Then 🌐 There should be no errors
7676
* 🌐 There should be no logs
7777
* 🌐 The selector h2 should match '<h2 class="class-two" data-cms-bind="#heading"><!--bookshop-live name(text) params(.: (.))--><span data-cms-bind="#heading.copy">New heading.</span><!--bookshop-live end--></h2>'
78+
79+
Scenario: Hugo Bookshop live renders a map iterator
80+
Given a component-lib/components/page/page.hugo.html file containing:
81+
"""
82+
{{ range . }}
83+
<div>
84+
{{ partial "bookshop" . }}
85+
{{ range $key, $value := . }}
86+
<span>{{ $key }}: {{ . }}</span>
87+
{{ end }}
88+
</div>
89+
{{ end }}
90+
"""
91+
Given a component-lib/components/text/text.hugo.html file containing:
92+
"""
93+
<p>{{ markdownify .text }}</p>
94+
"""
95+
Given [front_matter]:
96+
"""
97+
components:
98+
- _bookshop_name: text
99+
text: Hello
100+
"""
101+
And a site/content/_index.md file containing:
102+
"""
103+
---
104+
[front_matter]
105+
---
106+
"""
107+
And a site/layouts/index.html file containing:
108+
"""
109+
<html>
110+
<body>
111+
{{ partial "bookshop_bindings" `.Params.components` }}
112+
{{ partial "bookshop" (slice "page" .Params.components) }}
113+
</body>
114+
</html>
115+
"""
116+
And 🌐 I have loaded my site in CloudCannon
117+
When 🌐 CloudCannon pushes new yaml:
118+
"""
119+
components:
120+
- _bookshop_name: text
121+
text: Hello
122+
- _bookshop_name: text
123+
text: Hooray
124+
"""
125+
And 🌐 "window.bookshopLive?.renderCount === 2" evaluates
126+
Then 🌐 There should be no errors
127+
* 🌐 There should be no logs
128+
* 🌐 The selector div:nth-of-type(1)>span:nth-of-type(1) should contain '_bookshop_name: text'
129+
* 🌐 The selector div:nth-of-type(1)>span:nth-of-type(2) should contain 'text: Hello'
130+
* 🌐 The selector div:nth-of-type(2)>span:nth-of-type(1) should contain '_bookshop_name: text'
131+
* 🌐 The selector div:nth-of-type(2)>span:nth-of-type(2) should contain 'text: Hooray'
78132

0 commit comments

Comments
 (0)