Skip to content

Commit

Permalink
Clean URLs with and without trailing slash
Browse files Browse the repository at this point in the history
  • Loading branch information
niamu committed May 21, 2019
1 parent c05bbcf commit a8530c4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
25 changes: 16 additions & 9 deletions src/cryogen_core/compiler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,12 @@
"Creates a URI from file name. `uri-type` is any of the uri types specified in config, e.g., `:post-root-uri`."
([file-name params]
(page-uri file-name nil params))
([file-name uri-type {:keys [blog-prefix clean-urls?] :as params}]
([file-name uri-type {:keys [blog-prefix clean-urls] :as params}]
(let [page-uri (get params uri-type)
uri-end (if clean-urls? (string/replace file-name #"(index)?\.html" "") file-name)]
uri-end (condp = clean-urls
:trailing-slash (string/replace file-name #"(index)?\.html" "/")
:no-trailing-slash (string/replace file-name #"(index)?\.html" "")
:dirty file-name)]
(cryogen-io/path "/" blog-prefix page-uri uri-end))))

(defn read-page-meta
Expand Down Expand Up @@ -228,13 +231,17 @@
(map (partial sort-by :page-index) [navbar-pages sidebar-pages])))

(defn write-html
"When `clean-urls?` is set, appends `.html` before spit; otherwise just spits."
[file-uri {:keys [clean-urls?]} data]
(if clean-urls?
(cryogen-io/create-file
(if (= "/" file-uri) "index.html" (str file-uri ".html"))
data)
(cryogen-io/create-file file-uri data)))
"When `clean-urls` is set to:
- `:trailing-slash` appends `/index.html`.
- `:no-trailing-slash` appends `.html`.
- `:dirty` just spits."
[file-uri {:keys [clean-urls]} data]
(condp = clean-urls
:trailing-slash (cryogen-io/create-file-recursive
(cryogen-io/path file-uri "index.html") data)
:no-trailing-slash (cryogen-io/create-file
(cryogen-io/path file-uri "index.html") data)
:dirty (cryogen-io/create-file file-uri data)))

(defn- print-debug-info [data]
(println "DEBUG:")
Expand Down
4 changes: 3 additions & 1 deletion src/cryogen_core/schemas.clj
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
:previews? s/Bool
(s/optional-key :posts-per-page) s/Int
(s/optional-key :blocks-per-preview) s/Int
:clean-urls? s/Bool
:clean-urls (s/enum :trailing-slash
:no-trailing-slash
:dirty)
(s/optional-key :collapse-subdirs?) s/Bool
:hide-future-posts? s/Bool
(s/optional-key :klipse) Klipse
Expand Down
2 changes: 1 addition & 1 deletion test/cryogen_core/compiler_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ and more content.
:posts-per-page 5
:blocks-per-preview 2
:previews? false
:clean-urls? true
:clean-urls :trailing-slash
:collapse-subdirs? false
:hide-future-posts? true
:klipse {}
Expand Down

0 comments on commit a8530c4

Please sign in to comment.