Skip to content

Commit 5fab912

Browse files
committed
fix(hugo): correctly handle nested component paths in live editing
Components can now correctly reference other components that have a nested path, such as `generic/button`
1 parent 98610b4 commit 5fab912

File tree

4 files changed

+88
-7
lines changed

4 files changed

+88
-7
lines changed

javascript-modules/engines/hugo-engine/hugo-renderer/tpl/bookshop_library/bookshop_components.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"html/template"
8+
"strings"
89

910
"hugo-renderer/deps"
1011
"hugo-renderer/tpl/internal"
@@ -76,31 +77,37 @@ func RetrieveBookshopPartial(bookshop_key string) (*template.Template, bool) {
7677
return nil, false
7778
}
7879

80+
func bookshopKey(full_key string) string {
81+
split := strings.Split(full_key, "/")
82+
base := split[len(split)-1]
83+
return fmt.Sprintf("components/%s/%s.hugo.html", full_key, base)
84+
}
85+
7986
// UnwrapBookshopPartial takes the dict or slice that the {{ partial "bookshop" ... }}
8087
// was given, and pulls out the component / partial name that should be rendered.
8188
// TODO(bookshop): Ideally our templating support gets better,
8289
// and we can call the production bookshop module partials.
8390
func UnwrapBookshopComponent(context interface{}) (string, interface{}) {
8491
if componentData, ok := context.(map[string]interface{}); ok {
8592
if component, ok := componentData["_bookshop_name"]; ok {
86-
key := fmt.Sprintf("components/%s/%s.hugo.html", component.(string), component.(string))
93+
key := bookshopKey(component.(string))
8794
return key, context
8895
}
8996
return "err_no_bookshop_name_key", context
9097
}
9198
if componentData, ok := context.([]interface{}); ok {
9299
if component, ok := componentData[0].(string); ok {
93-
key := fmt.Sprintf("components/%s/%s.hugo.html", component, component)
100+
key := bookshopKey(component)
94101
return key, componentData[1]
95102
}
96103
return "err_slice_zero_not_string", context
97104
}
98105
if componentData, ok := context.([]string); ok {
99-
key := fmt.Sprintf("components/%s/%s.hugo.html", componentData[0], componentData[0])
106+
key := bookshopKey(componentData[0])
100107
return key, componentData[1]
101108
}
102109
if componentName, ok := context.(string); ok {
103-
key := fmt.Sprintf("components/%s/%s.hugo.html", componentName, componentName)
110+
key := bookshopKey(componentName)
104111
return key, nil
105112
}
106113
return "err_not_map_slice_or_string", context

javascript-modules/integration-tests/features/eleventy/live_editing/eleventy_bookshop_live_complex.feature

+23-1
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,26 @@ Feature: Eleventy Bookshop CloudCannon Live Editing Selective Re-rendering
285285
And 🌐 I have loaded my site in CloudCannon
286286
Then 🌐 There should be no errors
287287
* 🌐 There should be no logs
288-
* 🌐 The selector h1 should contain "Hello :)"
288+
* 🌐 The selector h1 should contain "Hello :)"
289+
290+
Scenario: Bookshop live renders a nested component path
291+
Given a component-lib/components/outer/outer.eleventy.liquid file containing:
292+
"""
293+
<div> {% bookshop "generic/button" %} </div>
294+
"""
295+
Given a component-lib/components/generic/button/button.eleventy.liquid file containing:
296+
"""
297+
<button>Button!</button>
298+
"""
299+
Given [front_matter]: "layout: layouts/default.liquid"
300+
And a site/index.html file containing:
301+
"""
302+
---
303+
[front_matter]
304+
---
305+
{% bookshop "outer" %}
306+
"""
307+
And 🌐 I have loaded my site in CloudCannon
308+
Then 🌐 There should be no errors
309+
* 🌐 There should be no logs
310+
* 🌐 The selector button should contain "Button!"

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

+31-1
Original file line numberDiff line numberDiff line change
@@ -402,4 +402,34 @@ Feature: Hugo Bookshop CloudCannon Live Editing Selective Re-rendering
402402
And 🌐 I have loaded my site in CloudCannon
403403
Then 🌐 There should be no errors
404404
* 🌐 There should be no logs
405-
* 🌐 The selector h1 should contain "Hello :)"
405+
* 🌐 The selector h1 should contain "Hello :)"
406+
407+
Scenario: Bookshop live renders a nested component path
408+
Given a component-lib/components/outer/outer.hugo.html file containing:
409+
"""
410+
<div> {{ partial "bookshop" "generic/button" }} </div>
411+
"""
412+
Given a component-lib/components/generic/button/button.hugo.html file containing:
413+
"""
414+
<button>Button!</button>
415+
"""
416+
Given [front_matter]: "empty: true"
417+
And a site/content/_index.md file containing:
418+
"""
419+
---
420+
---
421+
"""
422+
And a site/layouts/index.html file containing:
423+
"""
424+
<html>
425+
<body>
426+
{{ partial "bookshop_bindings" "" }}
427+
{{ partial "bookshop" "outer" }}
428+
</body>
429+
</html>
430+
"""
431+
And 🌐 I have loaded my site in CloudCannon
432+
Then 🌐 There should be no errors
433+
* 🌐 There should be no logs
434+
* 🌐 The selector button should contain "Button!"
435+

javascript-modules/integration-tests/features/jekyll/live_editing/jekyll_bookshop_live_complex.feature

+23-1
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,26 @@ Feature: Jekyll Bookshop CloudCannon Live Editing Selective Re-rendering
281281
And 🌐 I have loaded my site in CloudCannon
282282
Then 🌐 There should be no errors
283283
* 🌐 There should be no logs
284-
* 🌐 The selector h1 should contain "Hello :)"
284+
* 🌐 The selector h1 should contain "Hello :)"
285+
286+
Scenario: Bookshop live renders a nested component path
287+
Given a component-lib/components/outer/outer.jekyll.html file containing:
288+
"""
289+
<div> {% bookshop generic/button %} </div>
290+
"""
291+
Given a component-lib/components/generic/button/button.jekyll.html file containing:
292+
"""
293+
<button>Button!</button>
294+
"""
295+
Given [front_matter]: "layout: default"
296+
And a site/index.html file containing:
297+
"""
298+
---
299+
[front_matter]
300+
---
301+
{% bookshop outer %}
302+
"""
303+
And 🌐 I have loaded my site in CloudCannon
304+
Then 🌐 There should be no errors
305+
* 🌐 There should be no logs
306+
* 🌐 The selector button should contain "Button!"

0 commit comments

Comments
 (0)