Skip to content

Commit

Permalink
Make relative paths work
Browse files Browse the repository at this point in the history
  • Loading branch information
zampino committed Dec 4, 2023
1 parent 92e9cdd commit a0e365b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
15 changes: 9 additions & 6 deletions src/nextjournal/clerk/builder.clj
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,15 @@
:paths ["notebooks/hello.clj"
"notebooks/markdown.md"]})

(build-static-app! {:xhr? true
:paths ["notebooks/meta_toc.clj"
"notebooks/rule_30.clj"
"notebooks/visibility.clj"
"notebooks/viewer_api.clj"
"notebooks/viewer_classes.clj"]})
(let [paths ["notebooks/meta_toc.clj"
"notebooks/rule_30.clj"
"notebooks/nested/cherry.clj"
"notebooks/viewer_api.clj"]]
(reset! viewer/!viewers {})
(viewer/reset-viewers! :default (viewer/add-viewers [(meta-toc/notebook-viewer {:paths paths})]))
(build-static-app! {:xhr? true
:resource->url {"/js/viewer.js" "http://localhost:7777/js/viewer.js"}
:paths paths}))

(build-static-app! {;; test against cljs release `bb build:js`
:resource->url {"/js/viewer.js" "/viewer.js"}
Expand Down
12 changes: 9 additions & 3 deletions src/nextjournal/clerk/render.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -749,12 +749,18 @@
(when-some [url (some-> e .-target closest-anchor-parent .-href ->URL)]
(when-not (ignore-anchor-click? e url)
(.preventDefault e)
(let [edn-path (str "/" (or (not-empty (subs (.-pathname url) 1)) "index") ".edn")]
(js/console.log :path-to-edn edn-path )
(let [path (.-pathname url)
edn-path (str path
(when (str/ends-with? path "/") "index")
".edn")]
(js/console.log "pathname" path "path to EDN" edn-path)
(-> (js/fetch edn-path)
(.then (fn [r]
(if (.-ok r)
(.then (.text r) (fn [edn] (set-state! {:doc (read-string edn)})))
(.then (.text r)
(fn [edn]
(set-state! {:doc (read-string edn)})
(.pushState js/history #js {} "" (str path "/")))) ;; 👈 trailing slash is needed to make relative paths work
(js/console.error "EDN not found" r))))
(.catch (fn [e] (js/console.log "Fetch failed" e ))))))))

Expand Down

0 comments on commit a0e365b

Please sign in to comment.