Skip to content
Browse files

fix how paths are made relative for offline docs

the previous version of the code would strip the common start string of


and return just welcome.html which is wrong
  • Loading branch information
martinklepsch committed Sep 11, 2018
1 parent 2391ebb commit 125f4f6c6ccd0e93e3c89bd44834e16248f2d55d
Showing with 20 additions and 10 deletions.
  1. +15 −7 modules/shared-utils/src/cljdoc/util.clj
  2. +1 −1 src/cljdoc/util/fixref.clj
  3. +4 −2 test/cljdoc/util_test.clj
@@ -156,14 +156,22 @@
:userguide/articles (str (github-url :userguide/authors) "#articles")
:userguide/offline-docs (str (github-url :userguide/users) "#offline-docs"))))

(defn strip-common-start-string
"Remove the common substring from `s2` that both, `s1`
and `s2` start with."
(defn relativize-path
"Remove the segments at the beginning of a path `s2` that are identical
to the beginning segments of `s1`. This is useful when wanting to render
relative links instead of absolute ones.
(relativize-path \"doc/common-abc.html\" \"doc/common-xyz.html\")
;; => \"common-xyz.html\"
[s1 s2]
(->> (map vector s1 s2)
(take-while #(= (first %) (second %)))
(subs s2)))
(->> (reduce #(drop-while (partial = %2) %1)
(string/split s2 #"/")
(string/split s1 #"/"))
(string/join "/")))

(defn uri-path
"Return path part of a URL, this is probably part of pedestal in
@@ -56,7 +56,7 @@
(if-let [from-uri-map (get uri-map w-o-anchor)]
(-> (get uri-map file-path)
;; TODO check if relative links will work consistently
(util/strip-common-start-string from-uri-map)
(util/relativize-path from-uri-map)
(str anchor))
(str scm-base root-relative))))

@@ -36,8 +36,10 @@
(t/is (= (util/normalize-git-url "")

(t/deftest strip-common-start-string-test
(t/is (= "xyz.html" (util/strip-common-start-string "doc/abc.html" "doc/xyz.html"))))
(t/deftest relativize-path-test
(t/is (= "xyz.html" (util/relativize-path "doc/abc.html" "doc/xyz.html")))
(t/is (= "common-xyz.html" (util/relativize-path "doc/common-abc.html" "doc/common-xyz.html")))
(t/is (= "common-xyz/test.html" (util/relativize-path "doc/common-xyz.html" "doc/common-xyz/test.html"))))

(t/deftest replant-ns-test
(t/is (= "" (util/replant-ns "" "routes")))

0 comments on commit 125f4f6

Please sign in to comment.
You can’t perform that action at this time.