|
174 | 174 |
|
175 | 175 | (declare present+reset!) |
176 | 176 |
|
177 | | -(def router |
178 | | - {"" 'nextjournal.clerk.home}) |
179 | | - |
180 | 177 | (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))))))) |
187 | 183 |
|
188 | 184 | #_(->nav-path 'nextjournal.clerk.home) |
189 | 185 | #_(->nav-path 'nextjournal.clerk.tap) |
|
196 | 192 | ((resolve 'nextjournal.clerk/show!) opts file-or-ns)) |
197 | 193 |
|
198 | 194 | (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 (->file-or-ns 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})})))) |
208 | 209 |
|
209 | 210 | (defn app [{:as req :keys [uri]}] |
210 | 211 | (if (:websocket? req) |
|
216 | 217 | ("_fs") (serve-file uri (str/replace uri "/_fs/" "")) |
217 | 218 | "_ws" {:status 200 :body "upgrading..."} |
218 | 219 | "favicon.ico" {:status 404} |
219 | | - (serve-notebook uri)) |
| 220 | + (serve-notebook req)) |
220 | 221 | (catch Throwable e |
221 | 222 | {:status 500 |
222 | 223 | :body (with-out-str (pprint/pprint (Throwable->map e)))})))) |
|
0 commit comments