Skip to content

Commit 897ba4f

Browse files
committed
add an ending slash to the dir link, and don't encode the slashes.
1 parent c470f68 commit 897ba4f

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/babashka/http_server.clj

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,18 @@
131131
(let [mime-types (merge default-mime-types mime-types)]
132132
(mime-types (filename-ext filename)))))
133133

134+
(defn- file-link
135+
"Get HTML link for a file/directory in the given dir."
136+
[dir f]
137+
(let [rel-path (fs/relativize dir f)
138+
ending (if (fs/directory? f) "/" "")
139+
names (seq rel-path)
140+
enc-names (map #(URLEncoder/encode (str %)) names)]
141+
[:a {:href (str "/" (str/join "/" enc-names) ending)}
142+
(str rel-path ending)]))
143+
134144
(defn- index [dir f]
135-
(let [files (map #(str (.relativize dir %))
145+
(let [files (map #(file-link dir %)
136146
(fs/list-dir f))]
137147
{:body (-> [:html
138148
[:head
@@ -142,8 +152,7 @@
142152
[:h1 "Index of " [:code (str f)]]
143153
[:ul
144154
(for [child files]
145-
[:li [:a {:href (URLEncoder/encode (str child))} child
146-
(when (fs/directory? (fs/path dir child)) "/")]])]
155+
[:li child])]
147156
[:hr]
148157
[:footer {:style {"text-aling" "center"}} "Served by http-server.clj"]]]
149158
html/html

0 commit comments

Comments
 (0)