diff --git a/CHANGELOG.md b/CHANGELOG.md index 841414b..11e4db2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,12 @@ Instances of quickblog can be seen here: - [Henry Widd's blog](https://widdindustries.com/blog) - [Anders means different](https://www.eknert.com/blog) - ([source](https://github.com/anderseknert/blog)) +## Unreleased + +- Add `--date` to api/new. ([@jmglov](https://github.com/jmglov)) +- Support Selmer template for new posts in api/new; see [Templates > New + posts](README.md#new-posts) in README. ([@jmglov](https://github.com/jmglov)) + ## 0.3.6 (2031-12-31) - Fix caching (this is hard) diff --git a/README.md b/README.md index 94a72e3..bddb575 100644 --- a/README.md +++ b/README.md @@ -242,6 +242,47 @@ this happens, you won't be able to use the new feature without making the same modifications to your local templates. The easiest way to do this is to run `bb quickblog refresh-templates`. +### New posts + +In addition to the HTML templates above, you can also use a template for +generating new posts. Assuming you have a template `new-post.md` that looks like +this: + +``` markdown +Title: {{title}} +Date: {{date}} +Tags: {{tags|join:\",\"}} +Image: {% if image %}{{image}}{% else %}{{assets-dir}}/{{file|replace:.md:}}-preview.png{% endif %} +Image-Alt: {{image-alt|default:FIXME}} +Discuss: {{discuss|default:FIXME}} +{% if preview %}Preview: true\n{% endif %} +Write a blog post here! +``` + +you can generate a new post like this: + +``` text +$ bb quickblog new --file "test.md" --title "Test" --preview --template-file new-post.md +``` + +And the resulting `posts/test.md` will look like this: + +``` markdown +Title: Test +Date: 2024-01-19 +Tags: clojure +Image: assets/test-preview.png +Image-Alt: FIXME +Discuss: FIXME +Preview: true + +Write a blog post here! +``` + +**It is not recommended to keep your new post template in your templates-dir, as +any changes to the new post template will cause all of your existing posts to be +re-rendered, which is probably not what you want!** + ## Breaking changes ### posts.edn removed diff --git a/src/quickblog/api.clj b/src/quickblog/api.clj index 6556e6a..6cf563e 100644 --- a/src/quickblog/api.clj +++ b/src/quickblog/api.clj @@ -169,7 +169,7 @@ [clojure.edn :as edn] [clojure.set :as set] [clojure.string :as str] - [quickblog.internal :as lib] + [quickblog.internal :as lib :refer [->map]] [selmer.parser :as selmer] [selmer.filters :as filters])) @@ -519,7 +519,11 @@ "Creates new `file` in posts dir." {:org.babashka/cli {:spec - {:file + {:date + {:desc "Date of post (default: today; example: --date 1970-01-01)" + :ref ""} + + :file {:desc "Filename of post (relative to posts-dir)" :ref "" :require true} @@ -536,10 +540,15 @@ :tags {:desc "List of tags (default: 'clojure'; example: --tags tag1 tag2 \"tag3 has spaces\")" :ref "" - :coerce []}}}} + :coerce []} + + :template-file + {:desc "Filename of Selmer template to use for the new post (see Templates > New posts in README)" + :ref ""}}}} [opts] - (let [{:keys [file preview title posts-dir tags default-metadata] + (let [{:keys [date file tags template-file default-metadata posts-dir] :as opts} (apply-default-opts opts) + date (or date (now)) tags (cond (empty? tags) (:tags default-metadata) (= tags [true]) [] ;; `--tags` without arguments :else tags)] @@ -549,12 +558,18 @@ file (str file ".md")) post-file (fs/file posts-dir file) - preview-str (if preview "Preview: true\n" "")] + template (if template-file + (slurp (fs/file template-file)) + (->> ["Title: {{title}}" + "Date: {{date}}" + "Tags: {{tags|join:\",\"}}" + "{% if preview %}Preview: true\n{% endif %}" + "Write a blog post here!"] + (str/join "\n")))] (when-not (fs/exists? post-file) (fs/create-dirs posts-dir) (spit (fs/file posts-dir file) - (format "Title: %s\nDate: %s\nTags: %s\n%s\nWrite a blog post here!" - title (now) (str/join "," tags) preview-str)))))) + (selmer/render template (merge opts (->map file date tags)))))))) (defn clean "Removes cache and output directories" diff --git a/test/quickblog/api_test.clj b/test/quickblog/api_test.clj index ae83b20..344bc9e 100644 --- a/test/quickblog/api_test.clj +++ b/test/quickblog/api_test.clj @@ -48,15 +48,59 @@ title date (str/join "," tags) preview-str content))))) (deftest new-test - (with-dirs [posts-dir] - (with-redefs [api/now (constantly "2022-01-02")] + (testing "happy path" + (with-dirs [posts-dir] (api/new {:posts-dir posts-dir + :date "1970-01-01" :file "test.md" :title "Test post" :tags ["clojure" "some other tag"]}) (let [post-file (fs/file posts-dir "test.md")] (is (fs/exists? post-file)) - (is (= "Title: Test post\nDate: 2022-01-02\nTags: clojure,some other tag\n\nWrite a blog post here!" + (is (= "Title: Test post\nDate: 1970-01-01\nTags: clojure,some other tag\n\nWrite a blog post here!" + (slurp post-file)))))) + + (testing "defaults" + (with-dirs [posts-dir] + (with-redefs [api/now (constantly "2022-01-02")] + (api/new {:posts-dir posts-dir + :file "test.md" + :title "Test post"}) + (let [post-file (fs/file posts-dir "test.md")] + (is (fs/exists? post-file)) + (is (= "Title: Test post\nDate: 2022-01-02\nTags: clojure\n\nWrite a blog post here!" + (slurp post-file))))))) + + (testing "template" + (with-dirs [assets-dir posts-dir tmp-dir] + (write-test-file tmp-dir "new-post.md" + (str/join "\n" + ["Title: {{title}}" + "Date: {{date}}" + "Tags: {{tags|join:\",\"}}" + "Image: {% if image %}{{image}}{% else %}{{assets-dir}}/{{file|replace:.md:.png}}{% endif %}" + "Image-Alt: {{image-alt|default:FIXME}}" + "Discuss: {{discuss|default:FIXME}}" + "{% if preview %}Preview: true\n{% endif %}" + "Write a blog post here!"])) + (api/new {:assets-dir assets-dir + :posts-dir posts-dir + :date "1970-01-01" + :file "test.md" + :title "Test post" + :tags ["clojure" "some other tag"] + :template-file (fs/file tmp-dir "new-post.md")}) + (let [post-file (fs/file posts-dir "test.md")] + (is (fs/exists? post-file)) + (is (= (str/join "\n" + ["Title: Test post" + "Date: 1970-01-01" + "Tags: clojure,some other tag" + (format "Image: %s/test.png" assets-dir) + "Image-Alt: FIXME" + "Discuss: FIXME" + "" + "Write a blog post here!"]) (slurp post-file))))))) (deftest migrate