Skip to content

Instantly share code, notes, and snippets.

@hl
Created January 15, 2024 10:51
Show Gist options
  • Save hl/0fe9a1026283c77d85d6fad44caf06be to your computer and use it in GitHub Desktop.
Save hl/0fe9a1026283c77d85d6fad44caf06be to your computer and use it in GitHub Desktop.
Blog with Janet
(use joy)
(import markable)
(import filesystem)
# Layout
(defn app-layout [{:body body :request request}]
(text/html
(doctype :html5)
[:html {:lang "en"}
[:head
[:title "blog"]
[:meta {:charset "utf-8"}]
[:meta {:name "viewport" :content "width=device-width, initial-scale=1"}]
[:meta {:name "csrf-token" :content (csrf-token-value request)}]
[:link {:href "/app.css" :rel "stylesheet"}]
[:script {:src "/app.js" :defer ""}]]
[:body
body]]))
# Routes
(route :get "/:slug" :show)
(defn show [request]
(let [slug (get-in request [:params :slug])
file_name (cond (= "" slug) "index" slug)
file_path (string/format "pages/%s.md" file_name)
page_contents (if (filesystem/exists? file_path)
(filesystem/read-file file_path)
(filesystem/read-file "pages/404.md"))]
[:div {:class "container"}
(raw (markable/markdown->html (string page_contents) [:full-info-string :github-pre-lang :smart]))]))
# Middleware
(def app (-> (handler)
(layout app-layout)
(with-csrf-token)
(with-session)
(extra-methods)
(query-string)
(body-parser)
(json-body-parser)
(server-error)
(x-headers)
(static-files)
(not-found)
(logger)))
# Server
(defn main [& args]
(let [port (get args 1 (os/getenv "PORT" "9001"))
host (get args 2 "localhost")]
(server app port host)))
(declare-project
:name "blog"
:description ""
:dependencies ["https://github.com/joy-framework/joy"
"https://github.com/pyrmont/markable"
"https://github.com/jeannekamikaze/janet-filesystem"]
:author ""
:license ""
:url ""
:repo "")
(phony "server" []
(os/shell "janet main.janet"))
(declare-executable
:name "app"
:entry "main.janet")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment