Open links in new window (#84)

* Adding boolean setting to open links in new window and the
implementation on posts and thumbnails

* Fixing template syntax

* Adding entry on settings form for links in new window
Adding environment variable LINKS_IN_NEW_WINDOW for default setting

resolves #52 

---------

Co-authored-by: Nicolas Lanaro <nicolas.lanaro@thirdpartytrust.com>
This commit is contained in:
docd 2023-09-14 21:40:32 +02:00 committed by GitHub
parent 8ccb9d95b9
commit 54bc355017
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 2 deletions

View file

@ -22,4 +22,5 @@ docker run -it -e LEMMY_DOMAIN='lemmydomain.com' -p "8080:8080" ghcr.io/rystaf/m
| LISTING | All | | LISTING | All |
| SORT | Hot | | SORT | Hot |
| COMMENT_SORT | Hot | | COMMENT_SORT | Hot |
| LINKS_IN_NEW_WINDOW | false |

View file

@ -282,6 +282,11 @@ func Initialize(Host string, r *http.Request) (State, error) {
if state.Listing == "" || state.Session == nil && state.Listing == "Subscribed" { if state.Listing == "" || state.Session == nil && state.Listing == "Subscribed" {
state.Listing = getenv("LISTING", "All") state.Listing = getenv("LISTING", "All")
} }
if linksInNewWindow := getCookie(r, "LinksInNewWindow"); linksInNewWindow != "" {
state.LinksInNewWindow = linksInNewWindow != "0"
} else {
state.LinksInNewWindow = os.Getenv("LINKS_IN_NEW_WINDOW") != ""
}
return state, nil return state, nil
} }
func GetTemplate(name string) (*template.Template, error) { func GetTemplate(name string) (*template.Template, error) {
@ -765,6 +770,13 @@ func Settings(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
setCookie(w, "", "HideThumbnails", "0") setCookie(w, "", "HideThumbnails", "0")
state.HideInstanceNames = false state.HideInstanceNames = false
} }
if r.FormValue("linksInNewWindow") != "" {
setCookie(w, "", "LinksInNewWindow", "1")
state.LinksInNewWindow = true
} else {
setCookie(w, "", "LinksInNewWindow", "0")
state.LinksInNewWindow = false
}
state.Listing = r.FormValue("DefaultListingType") state.Listing = r.FormValue("DefaultListingType")
state.Sort = r.FormValue("DefaultSortType") state.Sort = r.FormValue("DefaultSortType")
state.CommentSort = r.FormValue("DefaultCommentSortType") state.CommentSort = r.FormValue("DefaultCommentSortType")

View file

@ -105,6 +105,7 @@ type State struct {
ShowNSFW bool ShowNSFW bool
HideInstanceNames bool HideInstanceNames bool
HideThumbnails bool HideThumbnails bool
LinksInNewWindow bool
SubmitURL string SubmitURL string
SubmitTitle string SubmitTitle string
SubmitBody string SubmitBody string

View file

@ -25,14 +25,20 @@
</div> </div>
{{ if not .State.HideThumbnails }} {{ if not .State.HideThumbnails }}
<div class="thumb"> <div class="thumb">
<a class="url" href="{{ if .Post.URL.IsValid }}{{ .Post.URL }}{{ else }}/{{ .State.Host }}/post/{{ .Post.ID }}{{ end }}"> <a class="url"
href="{{ if .Post.URL.IsValid }}{{ .Post.URL }}{{ else }}/{{ .State.Host }}/post/{{ .Post.ID }}{{ end }}"
{{ if .State.LinksInNewWindow }} target="_blank" {{ end }}>
<div {{ if and .Post.NSFW (not (and .State.Community .State.Community.CommunityView.Community.NSFW))}}class="img-blur"{{end}} style="background-image: url({{thumbnail .Post}})"></div> <div {{ if and .Post.NSFW (not (and .State.Community .State.Community.CommunityView.Community.NSFW))}}class="img-blur"{{end}} style="background-image: url({{thumbnail .Post}})"></div>
</a> </a>
</div> </div>
{{ end }} {{ end }}
<div class="entry"> <div class="entry">
<div class="title"> <div class="title">
<a class="url" href="{{ if .Post.URL.IsValid }}{{ .Post.URL }}{{ else }}/{{ .State.Host }}/post/{{ .Post.ID }}{{ end }}">{{ rmmarkdown .Post.Name }}</a> <a class="url"
href="{{ if .Post.URL.IsValid }}{{ .Post.URL }}{{ else }}/{{ .State.Host }}/post/{{ .Post.ID }}{{ end }}"
{{ if .State.LinksInNewWindow }} target="_blank" {{ end }}>
{{ rmmarkdown .Post.Name }}
</a>
({{ domain . }}) ({{ domain . }})
</div> </div>
<div class="expando-button{{ if and (not (and .Post.Body.IsValid .Post.Body.String )) (not (isImage .Post.URL.String)) }} hidden{{else if eq .Rank 0}} open{{ end }}"></div> <div class="expando-button{{ if and (not (and .Post.Body.IsValid .Post.Body.String )) (not (isImage .Post.URL.String)) }} hidden{{else if eq .Rank 0}} open{{ end }}"></div>

View file

@ -130,6 +130,12 @@
</label> </label>
<input type="checkbox" name="hideThumbnails" {{ if .HideThumbnails }}checked{{end}}> <input type="checkbox" name="hideThumbnails" {{ if .HideThumbnails }}checked{{end}}>
</div> </div>
<div>
<label>
open links in new window
</label>
<input type="checkbox" name="linksInNewWindow" {{ if .LinksInNewWindow }}checked{{end}}>
</div>
<div> <div>
<label>lemmy: {{ .Site.Version }}<br><a href="https://github.com/rystaf/mlmym">mlmym</a>: {{ .Version }}</label> <label>lemmy: {{ .Site.Version }}<br><a href="https://github.com/rystaf/mlmym">mlmym</a>: {{ .Version }}</label>
<input type="submit" value="save"> <input type="submit" value="save">