Skip to content

Commit

Permalink
Add canonical header to help search engines
Browse files Browse the repository at this point in the history
Fixes #472
  • Loading branch information
zedeus committed Dec 30, 2021
1 parent 173dd8f commit eed4d40
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/formatters.nim
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ proc getTwitterLink*(path: string; params: Table[string, string]): string =
path = "/search"

if "/search" notin path and query.fromUser.len < 2:
return $(twitter / path ? filterParams(params))
return $(twitter / path)

let p = {
"f": if query.kind == users: "user" else: "live",
Expand Down
6 changes: 4 additions & 2 deletions src/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ proc cleanFilename*(filename: string): string =
result &= ".png"

proc filterParams*(params: Table): seq[(string, string)] =
const filter = ["name", "id", "list", "referer", "scroll"]
toSeq(params.pairs()).filterIt(it[0] notin filter and it[1].len > 0)
const filter = ["name", "tab", "id", "list", "referer", "scroll"]
for p in params.pairs():
if p[1].len > 0 and p[0] notin filter:
result.add p

proc isTwitterUrl*(uri: Uri): bool =
uri.hostname in twitterDomains
Expand Down
17 changes: 11 additions & 6 deletions src/views/general.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ const
doctype = "<!DOCTYPE html>\n"
lp = readFile("public/lp.svg")

proc renderNavbar*(cfg: Config, rss: string; req: Request): VNode =
let twitterPath = getTwitterLink(req.path, req.params)
proc renderNavbar(cfg: Config; req: Request; rss, canonical: string): VNode =
var path = $(parseUri(req.path) ? filterParams(req.params))
if "/status" in path: path.add "#m"

Expand All @@ -27,14 +26,14 @@ proc renderNavbar*(cfg: Config, rss: string; req: Request): VNode =
icon "search", title="Search", href="/search"
if cfg.enableRss and rss.len > 0:
icon "rss-feed", title="RSS Feed", href=rss
icon "bird", title="Open in Twitter", href=twitterPath
icon "bird", title="Open in Twitter", href=canonical
a(href="https://liberapay.com/zedeus"): verbatim lp
icon "info", title="About", href="/about"
iconReferer "cog", "/settings", path, title="Preferences"

proc renderHead*(prefs: Prefs; cfg: Config; titleText=""; desc=""; video="";
images: seq[string] = @[]; banner=""; ogTitle=""; theme="";
rss=""): VNode =
rss=""; canonical=""): VNode =
let ogType =
if video.len > 0: "video"
elif rss.len > 0: "object"
Expand All @@ -58,6 +57,9 @@ proc renderHead*(prefs: Prefs; cfg: Config; titleText=""; desc=""; video="";
link(rel="search", type="application/opensearchdescription+xml", title=cfg.title,
href=opensearchUrl)

if canonical.len > 0:
link(rel="canonical", href=canonical)

if cfg.enableRss and rss.len > 0:
link(rel="alternate", type="application/rss+xml", href=rss, title="RSS feed")

Expand Down Expand Up @@ -117,11 +119,14 @@ proc renderMain*(body: VNode; req: Request; cfg: Config; prefs=defaultPrefs;
if "theme" in req.params:
theme = toLowerAscii(req.params["theme"]).replace(" ", "_")

let canonical = getTwitterLink(req.path, req.params)

let node = buildHtml(html(lang="en")):
renderHead(prefs, cfg, titleText, desc, video, images, banner, ogTitle, theme, rss)
renderHead(prefs, cfg, titleText, desc, video, images, banner, ogTitle,
theme, rss, canonical)

body:
renderNavbar(cfg, rss, req)
renderNavbar(cfg, req, rss, canonical)

tdiv(class="container"):
body
Expand Down

0 comments on commit eed4d40

Please sign in to comment.