rewrite fix

This commit is contained in:
Ryan Stafford 2023-07-15 22:41:39 -04:00
parent 23d08a730c
commit 9782144dcb
2 changed files with 4 additions and 2 deletions

View file

@ -74,6 +74,8 @@ func test() {
[]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{"/u/dude@lemmy.world", "/lemmy.local/u/dude@lemmy.world", "/u/dude@lemmy.world"},
[]string{"/u/dude@lemmy.local", "/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"},

View file

@ -165,11 +165,11 @@ 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\.\-]+)[ $]?`, `<a href="/c/$1">!$1@$2</a> `)
body = RegReplace(body, `!([a-zA-Z0-9]+)@([a-zA-Z0-9\.\-]+)([ \n\r]+|<\/p>)`, `<a href="/c/$1@$2">!$1@$2</a> `)
// 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"`)
body = RegReplace(body, `href="(https:\/)?(\/[a-zA-Z0-9\.\-]+)?\/((c|u)\/[a-zA-Z0-9]+@[a-zA-Z0-9\.\-]+)@([a-zA-Z0-9\.\-]+)"`, `href="/$3"`)
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`)