Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean URLs with and without trailing slash #119

Merged
merged 1 commit into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 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,20 @@
(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 [blog-prefix 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
(if (or (= blog-prefix file-uri) (= "/" file-uri))
(cryogen-io/path file-uri "index.html")
(str file-uri ".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