diff --git a/Makefile b/Makefile
index ca918f2..0ac9db1 100644
--- a/Makefile
+++ b/Makefile
@@ -11,7 +11,7 @@ reload:
serve:
#python -m http.server --directory ./public 8081 &>/dev/null
- watchexec -e go -r "go run . --addr 0.0.0.0:8008 -w"
+ DEBUG=true watchexec -e go -r "go run . --addr 0.0.0.0:8008 -w"
style:
npm run watchcss > /dev/null 2>&1
diff --git a/main.go b/main.go
index ca577c2..b00f6cc 100644
--- a/main.go
+++ b/main.go
@@ -7,6 +7,7 @@ import (
"log"
"net"
"net/http"
+ "os"
"github.com/julienschmidt/httprouter"
"github.com/yuin/goldmark"
@@ -64,6 +65,40 @@ func init() {
templates[name] = glob
}
}
+ if os.Getenv("DEBUG") != "" {
+ test()
+ }
+}
+func test() {
+ links := [][]string{
+ []string{"https://lemmy.local/u/dude", "/lemmy.local/u/dude", "/u/dude"},
+ []string{"https://lemmy.local/u/dude@lemmy.local", "/lemmy.local/u/dude", "/u/dude"},
+ []string{"/u/dude", "/lemmy.local/u/dude", "/u/dude"},
+ []string{"https://lemmy.world/c/dude", "/lemmy.local/c/dude@lemmy.world", "/c/dude@lemmy.world"},
+ []string{"https://lemmy.world/u/dude", "/lemmy.local/u/dude@lemmy.world", "/u/dude@lemmy.world"},
+ []string{"https://lemmy.world/u/dude@lemmy.world", "/lemmy.local/u/dude@lemmy.world", "/u/dude@lemmy.world"},
+ []string{"https://lemmy.world/post/123", "/lemmy.world/post/123", "https://lemmy.world/post/123"},
+ []string{"/post/123", "/lemmy.local/post/123", "/post/123"},
+ []string{"/comment/123", "/lemmy.local/comment/123", "/comment/123"},
+ []string{"https://lemmy.local/comment/123", "/lemmy.local/comment/123", "/comment/123"},
+ }
+ for _, url := range links {
+ output := LemmyLinkRewrite(`href="`+url[0]+`"`, "lemmy.local", "")
+ success := (output == (`href="` + url[1] + `"`))
+ if !success {
+ fmt.Println("\n!!!! Link rewrite failure !!!!")
+ fmt.Println(url)
+ fmt.Println(output)
+ fmt.Println("")
+ }
+ output = LemmyLinkRewrite(`href="`+url[0]+`"`, ".", "lemmy.local")
+ success = (output == (`href="` + url[2] + `"`))
+ if !success {
+ fmt.Println(success, url)
+ fmt.Println(output)
+ fmt.Println("")
+ }
+ }
}
func middleware(n httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
diff --git a/routes.go b/routes.go
index 17c8355..a933b77 100644
--- a/routes.go
+++ b/routes.go
@@ -32,12 +32,12 @@ var funcMap = template.FuncMap{
}
return host
},
- "proxy": func(s string) string {
+ "localize": func(s string) string {
u, err := url.Parse(s)
if err != nil {
return s
}
- return "/" + u.Host + u.Path
+ return "." + u.Path + "@" + u.Host
},
"printer": func(n any) string {
p := message.NewPrinter(language.English)
@@ -137,22 +137,11 @@ var funcMap = template.FuncMap{
fmt.Println(err)
return template.HTML(body)
}
- converted := buf.String()
- converted = strings.Replace(converted, `!$1@$2 `)
- if os.Getenv("LEMMY_DOMAIN") == "" {
- re = regexp.MustCompile(`href="\/(c\/[a-zA-Z0-9\-]+|(post|comment)\/\d+)`)
- converted = re.ReplaceAllString(converted, `href="https://`+host+`/$1`)
- re := regexp.MustCompile(`href="https:\/\/([a-zA-Z0-9\.\-]+\/(c\/[a-zA-Z0-9]+|(post|comment)\/\d+))`)
- converted = re.ReplaceAllString(converted, `href="/$1`)
- } else {
- re := regexp.MustCompile(`href="https:\/\/` + os.Getenv("LEMMY_DOMAIN") + `\/(c\/[a-zA-Z0-9]+|(post|comment)\/\d+)`)
- converted = re.ReplaceAllString(converted, `href="/$1`)
- }
- re = regexp.MustCompile(`::: spoiler (.*?)\n([\S\s]*?):::`)
- converted = re.ReplaceAllString(converted, "
$1
$2