Skip to content

Commit

Permalink
Add configuration option for :page-suffix (#79)
Browse files Browse the repository at this point in the history
Default setting is ".html" to keep current behavior. Use empty
string to omit extension.

There's still a few "index.html" links in the templates, but as
those are easily replaced there directly, I don't think we'll need
to add an option for that.

Signed-off-by: Anders Eknert <anders@eknert.com>
  • Loading branch information
anderseknert committed Nov 27, 2023
1 parent aaffad1 commit 9841b90
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 17 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ Instances of quickblog can be seen here:
- [Michiel Borkent's blog](https://blog.michielborkent.nl)
- [Josh Glover's blog](https://jmglov.net/blog)
- [Jeremy Taylor's blog](https://jdt.me/strange-reflections.html)
- [Luc Engelen's blog](https://blog.cofx.nl/) ([source](https://github.com/cofx22/blog))
- [JP Monetta's blog](https://jpmonettas.github.io/my-blog/public/)
- [Luc Engelen's blog](https://blog.cofx.nl/) - ([source](https://github.com/cofx22/blog))
- [Rattlin.blog](https://rattlin.blog/)
- [REP‘ti’L‘e’](https://kuna.us/)
- [Søren Sjørup's blog](https://zoren.dk)
- [Henry Widd's blog](https://widdindustries.com/blog)
- [Anders means different](https://www.eknert.com/blog) - ([source](https://github.com/anderseknert/blog))

## Unreleased

- [#78](https://github.com/borkdude/quickblog/issues/78): Allow configurable :page-suffix to omit `.html` from page links
- [#76](https://github.com/borkdude/quickblog/pull/76): Remove livejs script tag
on render. ([@jmglov](https://github.com/jmglov))
- [#75](https://github.com/borkdude/quickblog/pull/75): Omit preview posts from
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Instances of quickblog can be seen here:
- [REP‘ti’L‘e’](https://kuna.us/)
- [Søren Sjørup's blog](https://zoren.dk)
- [Henry Widd's blog](https://widdindustries.com/blog)
- [Anders means different](https://www.eknert.com/blog/bronchitis)
- [Anders means different](https://www.eknert.com/blog) - ([source](https://github.com/anderseknert/blog))

Feel free to PR yours.

Expand Down
4 changes: 2 additions & 2 deletions resources/quickblog/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<div class="site-header">
<div class="wrapper">
<div class="site-nav">
<a class="page-link" href="{{relative-path | safe}}archive.html">Archive</a>
<a class="page-link" href="{{relative-path | safe}}archive{{page-suffix}}">Archive</a>
<a class="page-link" href="{{relative-path | safe}}tags/index.html">Tags</a>
<a class="page-link" href="{{discuss-link}}">Discuss</a>
<a class="page-link" href="{{relative-path | safe}}atom.xml">
Expand Down Expand Up @@ -77,7 +77,7 @@ <h1 class="site-title">

{% if not skip-archive %}
<div style="margin-bottom: 20px; float: right;">
<a class="page-link" href="{{relative-path | safe}}archive.html">Archive</a>
<a class="page-link" href="{{relative-path | safe}}archive{{page-suffix}}">Archive</a>
</div>
{% endif %}
</div>
Expand Down
2 changes: 1 addition & 1 deletion resources/quickblog/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ <h1><a href="{{post.post-link}}">{{post.title}}</a></h1>
Tagged:
{% for tag in post.tags %}
<span class="tag">
<a href="tags/{{tag|escape-tag}}.html">{{tag}}</a>
<a href="tags/{{tag|escape-tag}}{{page-suffix}}">{{tag}}</a>
</span>
{% endfor %}
</i></p>
Expand Down
2 changes: 1 addition & 1 deletion resources/quickblog/templates/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ <h1>
Tagged:
{% for tag in tags %}
<span class="tag">
<a href="tags/{{tag|escape-tag}}.html">{{tag}}</a>
<a href="tags/{{tag|escape-tag}}{{page-suffix}}">{{tag}}</a>
</span>
{% endfor %}
</i>
Expand Down
16 changes: 11 additions & 5 deletions src/quickblog/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
:ref "<handle>"
:group :optional-metadata}

:page-suffix
{:desc "Suffix to use for page links (default .html)"
:ref "<suffix>"
:default ".html"
:group :optional-metadata}

;; Post config
:default-metadata
{:desc "Default metadata to add to posts"
Expand Down Expand Up @@ -289,15 +295,15 @@

;;;; Generate index page with last `num-index-posts` posts

(defn- index [{:keys [posts] :as opts}]
(defn- index [{:keys [posts page-suffix] :as opts}]
(let [posts (for [{:keys [file html] :as post} posts
:let [preview (first (str/split @html #"<!-- end-of-preview -->" 2))]]
(assoc post
:post-link (str/replace file ".md" ".html")
:post-link (str/replace file ".md" page-suffix)
:body preview
:truncated (not= preview @html)))
index-template (lib/ensure-template opts "index.html")]
(selmer/render (slurp index-template) {:posts posts})))
(selmer/render (slurp index-template) (merge opts {:posts posts}))))

(defn- spit-index
[{:keys [blog-title blog-description blog-image blog-image-alt twitter-handle
Expand Down Expand Up @@ -373,7 +379,7 @@

(defn- atom-feed
;; validate at https://validator.w3.org/feed/check.cgi
[{:keys [blog-title blog-author blog-root] :as opts} posts]
[{:keys [blog-title blog-author blog-root page-suffix] :as opts} posts]
(-> (xml/sexp-as-element
[::atom/feed
{:xmlns "http://www.w3.org/2005/Atom"}
Expand All @@ -386,7 +392,7 @@
[::atom/name blog-author]]
(for [{:keys [title date file preview html]} posts
:when (not preview)
:let [html-file (str/replace file ".md" ".html")
:let [html-file (str/replace file ".md" page-suffix)
link (lib/blog-link opts html-file)]]
[::atom/entry
[::atom/id link]
Expand Down
9 changes: 5 additions & 4 deletions src/quickblog/internal.clj
Original file line number Diff line number Diff line change
Expand Up @@ -363,19 +363,19 @@
slurp
(selmer/render opts))))

(defn post-links [title posts {:keys [relative-path] :as opts}]
(defn post-links [title posts {:keys [relative-path page-suffix] :as opts}]
(let [post-links-template (ensure-template opts "post-links.html")
post-links (for [{:keys [file title date preview]} posts
:when (not preview)]
{:url (str relative-path (str/replace file ".md" ".html"))
{:url (str relative-path (str/replace file ".md" page-suffix))
:title title
:date date})]
(selmer/render (slurp post-links-template) {:title title
:post-links post-links})))

(defn tag-links [title tags opts]
(let [tags-template (ensure-template opts "tags.html")
tags (map (fn [[tag posts]] {:url (str (escape-tag tag) ".html")
tags (map (fn [[tag posts]] {:url (str (escape-tag tag) (:page-suffix opts))
:tag tag
:count (count posts)}) tags)]
(selmer/render (slurp tags-template) {:title title
Expand All @@ -390,13 +390,14 @@
(defn write-post! [{:keys [twitter-handle
discuss-link
out-dir
page-suffix
page-template
post-template]
:as opts}
{:keys [file html description image image-alt]
:as post-metadata}]
(let [out-file (fs/file out-dir (html-file file))
post-metadata (merge {:discuss discuss-link} (assoc post-metadata :body @html))
post-metadata (merge {:discuss discuss-link :page-suffix page-suffix} (assoc post-metadata :body @html))
body (selmer/render post-template post-metadata)
author (-> (:twitter-handle post-metadata) (or twitter-handle))
image (when image (if (re-matches #"^https?://.+" image)
Expand Down
29 changes: 27 additions & 2 deletions test/quickblog/api_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,31 @@
(is (not (str/includes? (slurp (fs/file out-dir filename))
"preview.html"))))))

(testing "with blank page suffix"
(with-dirs [posts-dir
templates-dir
out-dir]
(write-test-post posts-dir {:tags #{"foobar" "tag with spaces"}})
(api/render {:page-suffix ""
:posts-dir posts-dir
:templates-dir templates-dir
:out-dir out-dir})
(doseq [filename ["test.html" "index.html" "archive.html"
(fs/file "tags" "index.html")
(fs/file "tags" "tag-with-spaces.html")
"atom.xml" "planetclojure.xml"]]
(is (fs/exists? (fs/file out-dir filename))))
(is (str/includes? (slurp (fs/file out-dir "test.html"))
"<a class=\"page-link\" href=\"archive\">Archive</a>"))
(is (str/includes? (slurp (fs/file out-dir "test.html"))
"<a href=\"tags/foobar\">foobar</a>"))
(is (str/includes? (slurp (fs/file out-dir "test.html"))
"<a href=\"tags/tag-with-spaces\">tag with spaces</a>"))
(is (str/includes? (slurp (fs/file out-dir "tags" "index.html"))
"<a href=\"foobar\">foobar</a>"))
(is (str/includes? (slurp (fs/file out-dir "tags" "index.html"))
"<a href=\"tag-with-spaces\">tag with spaces</a>"))))

(testing "with favicon"
(with-dirs [favicon-dir
posts-dir
Expand Down Expand Up @@ -152,7 +177,7 @@
(is (str/includes? (slurp (fs/file out-dir "preview.html")) "<p>only part of full post</p>"))
(is (str/includes? (slurp (fs/file out-dir "index.html")) "<p>always included</p>"))
(is (not (str/includes? (slurp (fs/file out-dir "index.html")) "<p>only part of full post</p>")))))

(testing "multiline links"
(with-dirs [posts-dir
templates-dir
Expand Down Expand Up @@ -181,7 +206,7 @@
:cache-dir cache-dir
:out-dir out-dir})
(is (str/includes? (slurp (fs/file out-dir "planetclojure.xml")) "Post about ClojureScript"))))

(testing "non-Clojure tag"
(with-dirs [assets-dir
posts-dir
Expand Down

0 comments on commit 9841b90

Please sign in to comment.