-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.go
118 lines (97 loc) 路 2.83 KB
/
page.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package builder
import (
"bytes"
"context"
_ "embed"
"encoding/json"
"fmt"
"path/filepath"
)
type ssrData struct {
Head string
Body string
//this is created during bundling
BundledCSS string
//these pare provided by the user
Lang string
}
func (v *ViewManager) Render(
_ context.Context,
viewPath string,
props interface{},
) (string, error) {
view := v.ViewByRelPath(viewPath)
if view == nil {
return "", fmt.Errorf("view does not exist in path %s", viewPath)
}
//TODO: Create a sanitized copy of the props object where
// string objects are escaped to avoid script injections on the front end
// Should users be able to bypass escaping using tags?
jsonValue := "{}"
if props != nil {
jsonProps, err := json.Marshal(props)
if err != nil {
return "", fmt.Errorf("failed to json serialize props %w", err)
}
jsonValue = string(jsonProps)
}
expr := fmt.Sprintf(
"; __aviator__.render(%q, %s, {})",
view.WrappedUniqueName,
jsonValue,
)
renderOutputStr, err := v.vm.Eval("runtime_renderer", expr)
if err != nil {
return renderOutputStr, err
}
ssrOutputData := &ssrData{}
err = json.Unmarshal([]byte(renderOutputStr), ssrOutputData)
if err != nil {
return "", err
}
ssrOutputData.Head = ssrOutputData.Head + "\n" +
v.createJSImportTags(view.JSImports)
_, baseStyleFound := v.staticContent[baseCSSStyleName]
if baseStyleFound {
ssrOutputData.Head += v.createCSSImportTag(baseCSSStyleName)
}
ssrOutputData.Head +=
v.createCSSImportTags(view.CSSImports) +
v.createPropsScriptElem(jsonValue)
ssrOutputData.Lang = v.htmlLang
//cssPath := path.Join(a.assetListenPath, a._compiledCSSFileName)
//ssrOutputData.BundledCSS = "<link href=\"" + cssPath + "\" rel=\"stylesheet\">"
buf := new(bytes.Buffer)
err = v.htmlGenerator.Execute(buf, ssrOutputData)
if err != nil {
return "", err
}
return buf.String(), nil
}
func (v *ViewManager) GetStaticAsset(name string) (StaticAsset, bool) {
staticAsset, ok := v.staticContent[name]
return staticAsset, ok
}
func (v *ViewManager) createPropsScriptElem(props string) string {
format := "<script id=\"__aviator_props\" type=\"text/template\" defer>%s</script>\n"
return fmt.Sprintf(format, props)
}
func (v *ViewManager) createJSImportTags(assetImports []string) string {
output := ""
format := "<script type=\"module\" src=\"%s\" defer></script>\n"
for _, rawPath := range assetImports {
output += fmt.Sprintf(format, filepath.Join(v.staticAssetsRoute, rawPath))
}
return output
}
func (v *ViewManager) createCSSImportTags(assetImports []string) string {
output := ""
for _, rawPath := range assetImports {
output += v.createCSSImportTag(rawPath)
}
return output
}
func (v *ViewManager) createCSSImportTag(path string) string {
format := "<link href=\"%s\" rel=\"stylesheet\">\n"
return fmt.Sprintf(format, filepath.Join(v.staticAssetsRoute, path))
}