-
Notifications
You must be signed in to change notification settings - Fork 3
/
link.clj
40 lines (36 loc) · 1.59 KB
/
link.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
(ns net.thegeez.w3a.link
(:require [clojure.string :as string]
[io.pedestal.http.route :as route]
[io.pedestal.http.route.definition :as definition]
[io.pedestal.interceptor :as interceptor]
[io.pedestal.impl.interceptor :as impl-interceptor]
[io.pedestal.log :as log]))
(defn link [context kw-name & options]
(let [linker (get-in context [:bindings #'route/*url-for*])
{:keys [absolute? query-params]
:or {absolute? true}} options
[scheme port] (if (get-in context [:request :headers "x-forwarded-proto"])
[:https 443]
[:http nil])]
(apply linker kw-name
:absolute? absolute?
:scheme scheme
:port port
:query-params (cond-> query-params
(= (get-in context [:request :headers "accept"]) "application/edn+html")
(assoc :format "dev"))
options)))
(defn self [context]
(when-let [kw-name (get-in context [:route :route-name])]
(link context kw-name)))
(defn add-breadcrumb [context title route-name]
(update-in context [:response ::breadcrumbs]
(fnil conj [])
{:title title
:route-name route-name
:link (link context route-name :path-params (get-in context [:request :path-params]))}))
(defn breadcrumb [title route-name]
(interceptor/interceptor
{:name ::breadcrumb
:enter (fn [context]
(add-breadcrumb context title route-name))}))