Skip to content

Commit

Permalink
Open links in new window (#84)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
docd and Nicolas Lanaro authored Sep 14, 2023
1 parent 8ccb9d9 commit 54bc355
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ docker run -it -e LEMMY_DOMAIN='lemmydomain.com' -p "8080:8080" ghcr.io/rystaf/m
| LISTING | All |
| SORT | Hot |
| COMMENT_SORT | Hot |
| LINKS_IN_NEW_WINDOW | false |

12 changes: 12 additions & 0 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ func Initialize(Host string, r *http.Request) (State, error) {
if state.Listing == "" || state.Session == nil && state.Listing == "Subscribed" {
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
}
func GetTemplate(name string) (*template.Template, error) {
Expand Down Expand Up @@ -765,6 +770,13 @@ func Settings(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
setCookie(w, "", "HideThumbnails", "0")
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.Sort = r.FormValue("DefaultSortType")
state.CommentSort = r.FormValue("DefaultCommentSortType")
Expand Down
1 change: 1 addition & 0 deletions state.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ type State struct {
ShowNSFW bool
HideInstanceNames bool
HideThumbnails bool
LinksInNewWindow bool
SubmitURL string
SubmitTitle string
SubmitBody string
Expand Down
10 changes: 8 additions & 2 deletions templates/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@
</div>
{{ if not .State.HideThumbnails }}
<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>
</a>
</div>
{{ end }}
<div class="entry">
<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 . }})
</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>
Expand Down
6 changes: 6 additions & 0 deletions templates/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@
</label>
<input type="checkbox" name="hideThumbnails" {{ if .HideThumbnails }}checked{{end}}>
</div>
<div>
<label>
open links in new window
</label>
<input type="checkbox" name="linksInNewWindow" {{ if .LinksInNewWindow }}checked{{end}}>
</div>
<div>
<label>lemmy: {{ .Site.Version }}<br><a href="https://github.com/rystaf/mlmym">mlmym</a>: {{ .Version }}</label>
<input type="submit" value="save">
Expand Down

0 comments on commit 54bc355

Please sign in to comment.