From 2d8a3d23156883ff2e0c50d26bbb6d5a1a321e2f Mon Sep 17 00:00:00 2001 From: Ryan Stafford Date: Sat, 15 Jul 2023 20:59:32 -0400 Subject: [PATCH] localize community and user links, regex cleanup --- Makefile | 2 +- main.go | 35 ++++++++++++++++++++++++ routes.go | 59 ++++++++++++++++++++++++++++------------ templates/community.html | 4 +-- 4 files changed, 79 insertions(+), 21 deletions(-) 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
") - return template.HTML(converted) + body = buf.String() + body = strings.Replace(body, `$1$2") + return template.HTML(body) }, "rmmarkdown": func(body string) string { var buf bytes.Buffer @@ -173,6 +162,40 @@ var funcMap = template.FuncMap{ }, } +func LemmyLinkRewrite(input string, host string, lemmy_domain string) (body string) { + body = input + // community bangs + body = RegReplace(body, `!([a-zA-Z0-9]+)@([a-zA-Z0-9\.\-]+)[ $]?`, `!$1@$2 `) + // localize community and user links + body = RegReplace(body, `href="https:\/\/([a-zA-Z0-9\.\-]+)\/((c|u)\/.*?)"`, `href="/$2@$1"`) + // remove extra instance tag + body = RegReplace(body, `href="\/((c|u)\/.*@.*?)@(.*?)"`, `href="/$1"`) + if lemmy_domain == "" { + // add domain to relative links + body = RegReplace(body, `href="\/(c\/[a-zA-Z0-9\-]+"|(post|comment)\/\d+"|(c|u)\/(.*?)")`, `href="/`+host+`/$1`) + // convert links to relative + body = RegReplace(body, `href="https:\/\/([a-zA-Z0-9\.\-]+\/(c\/[a-zA-Z0-9]+"|(post|comment)\/\d+"|u\/(.*?)"))`, `href="/$1`) + } else { + // convert local links to relative + body = RegReplace(body, `href="https:\/\/`+lemmy_domain+`\/(c\/[a-zA-Z0-9]+"|(post|comment)\/\d+"|u\/(.*?)")`, `href="/$1`) + body = RegReplace(body, `href="(.*)@`+lemmy_domain+`"`, `href="$1"`) + } + // remove redundant instance tag + re := regexp.MustCompile(`href="\/([a-zA-Z0-9\.\-]+)\/(c|u)\/(.*?)@(.*?)"`) + matches := re.FindAllStringSubmatch(body, -1) + for _, match := range matches { + if match[1] == match[4] { + body = strings.Replace(body, match[0], `href="/`+strings.Join(match[1:4], "/")+`"`, -1) + } + } + return body +} + +func RegReplace(input string, match string, replace string) string { + re := regexp.MustCompile(match) + return re.ReplaceAllString(input, replace) +} + func Initialize(Host string, r *http.Request) (State, error) { state := State{ Host: Host, diff --git a/templates/community.html b/templates/community.html index f1af3c6..42d3f26 100644 --- a/templates/community.html +++ b/templates/community.html @@ -3,11 +3,11 @@ - c/{{fullcname .Community}}: {{.Community.Title}} + c/{{fullcname .Community}}: {{.Community.Title}}
{{ if .Community.Description.IsValid }}
- {{markdown "poop" .Community.Description.String}} + {{markdown "" .Community.Description.String}}
{{ end }}