diff --git a/linker.go b/linker.go index ac6c9bc..0c91eaf 100644 --- a/linker.go +++ b/linker.go @@ -8,14 +8,17 @@ import ( // A Linker is used to correct the links found in the response body // It will add the prefix when a regex match is found type Linker struct { - quoted_reg *regexp.Regexp + quoted_reg *regexp.Regexp + hue_base_url *regexp.Regexp } func new_linker(paths string) Linker { quoted := `([='"])(` + paths + ")" + hue_base := `window.HUE_BASE_URL\s*[+]\s*'/hue'\s*[+]` linker := Linker{ - quoted_reg: regexp.MustCompile(quoted), + quoted_reg: regexp.MustCompile(quoted), + hue_base_url: regexp.MustCompile(hue_base), } return linker @@ -25,5 +28,7 @@ func (l *Linker) replace(file []byte, service_prefix string) []byte { prefix := "$1" + strings.TrimSuffix(service_prefix, "/") + "$2" replaced := l.quoted_reg.ReplaceAll(file, []byte(prefix)) + replaced = l.hue_base_url.ReplaceAll(replaced, []byte("window.HUE_BASE_URL +")) + return replaced } diff --git a/linker_test.go b/linker_test.go index ca824bc..0d42e29 100644 --- a/linker_test.go +++ b/linker_test.go @@ -26,6 +26,14 @@ func TestLinker(t *testing.T) { ` `, }, + { + `link = window.HUE_BASE_URL + '/hue' + link;`, + `link = window.HUE_BASE_URL + link;`, + }, + { + `link = window.HUE_BASE_URL+'/hue'+link;`, + `link = window.HUE_BASE_URL +link;`, + }, } for _, tc := range cases {