Skip to content

Commit 2979733

Browse files
committed
Redirect to active doc or home from root
1 parent 75593df commit 2979733

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

src/nextjournal/clerk/webserver.clj

+20-19
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,12 @@
174174

175175
(declare present+reset!)
176176

177-
(def router
178-
{"" 'nextjournal.clerk.home})
179-
180177
(defn ->nav-path [file-or-ns]
181-
(or (get (set/map-invert router) file-or-ns)
182-
(cond (symbol? file-or-ns) (str "'" file-or-ns)
183-
(string? file-or-ns) (when (fs/exists? file-or-ns)
184-
(fs/unixify (cond->> file-or-ns
185-
(fs/absolute? file-or-ns)
186-
(fs/relativize (fs/cwd))))))))
178+
(cond (symbol? file-or-ns) (str "'" file-or-ns)
179+
(string? file-or-ns) (when (fs/exists? file-or-ns)
180+
(fs/unixify (cond->> file-or-ns
181+
(fs/absolute? file-or-ns)
182+
(fs/relativize (fs/cwd)))))))
187183

188184
#_(->nav-path 'nextjournal.clerk.home)
189185
#_(->nav-path 'nextjournal.clerk.tap)
@@ -196,15 +192,20 @@
196192
((resolve 'nextjournal.clerk/show!) opts file-or-ns))
197193

198194
(defn navigate! [{:as opts :keys [nav-path]}]
199-
(show! opts (router nav-path nav-path)))
200-
201-
(defn serve-notebook [uri]
202-
(try (show! {} (->file-or-ns (let [nav-path (subs uri 1)]
203-
(router nav-path nav-path))))
204-
(catch Exception _))
205-
{:status 200
206-
:headers {"Content-Type" "text/html" "Cache-Control" "no-store"}
207-
:body (view/doc->html {:doc @!doc})})
195+
(show! opts nav-path))
196+
197+
(defn serve-notebook [{:as req :keys [uri]}]
198+
(let [nav-path (subs uri 1)]
199+
(if (str/blank? nav-path)
200+
{:status 302
201+
:headers {"Location" (or (:nav-path @!doc)
202+
(->nav-path 'nextjournal.clerk.home))}}
203+
(do
204+
(try (show! {} (->file-or-ns nav-path))
205+
(catch Exception _))
206+
{:status 200
207+
:headers {"Content-Type" "text/html" "Cache-Control" "no-store"}
208+
:body (view/doc->html {:doc @!doc})}))))
208209

209210
(defn app [{:as req :keys [uri]}]
210211
(if (:websocket? req)
@@ -216,7 +217,7 @@
216217
("_fs") (serve-file uri (str/replace uri "/_fs/" ""))
217218
"_ws" {:status 200 :body "upgrading..."}
218219
"favicon.ico" {:status 404}
219-
(serve-notebook uri))
220+
(serve-notebook req))
220221
(catch Throwable e
221222
{:status 500
222223
:body (with-out-str (pprint/pprint (Throwable->map e)))}))))

0 commit comments

Comments
 (0)