Skip to content

Instantly share code, notes, and snippets.

@holyjak
Last active November 20, 2023 15:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holyjak/bbeb714ca25ec99b55933c40f2e75881 to your computer and use it in GitHub Desktop.
Save holyjak/bbeb714ca25ec99b55933c40f2e75881 to your computer and use it in GitHub Desktop.
Cryogen customization: autolink headings etc.
#post a.anchor, #custom-page a.anchor {
float: left;
padding-right: 4px;
margin-left: -20px;
}
;; http://cryogenweb.org/ customization that adds self-links to
;; all headings, GitHub-style
;; Version: cryogen-core "0.2.3"
(ns cryogen.core
(:require [cryogen-core.compiler :refer [compile-assets-timed]]
[cryogen-core.plugins :refer [load-plugins]]
[net.cgrand.enlive-html :as enlive])
(:import (java.io StringReader)))
;;------------------------------------------------------------ autolink-headings
(defn permalink-node [{{heading-id :id} :attrs :as heading} blog-prefix]
(first
(enlive/html
[:a {:href (str "#" heading-id)
:aria-label (str "Permalink to " (enlive/text heading))
:class "anchor"}
[:svg {:aria-hidden true :focusable false :width 16 :height 16}
[:use {:xlink:href (str blog-prefix "/img/icons.svg#icon-link")}]]])))
(defn autolink-content-headings [content-nodes blog-prefix]
(-> content-nodes
(enlive/transform
[#{:h1 :h2 :h3 :h4 :h5 :h6}]
(fn autolink-heading [heading]
(update heading
:content
#(apply vector (permalink-node heading blog-prefix) %))))))
(defn autolink-headings [article {:keys [blog-prefix]}]
(update article :content-dom autolink-content-headings blog-prefix))
;;---------------------------------------------------------------------
(defn compile-site []
(compile-assets-timed
{:update-article-fn ; NOTE You may want to have a look at :postprocess-article-html-fn instead
(fn update-article [article config]
;; Skip articles with `:ignore? true` in metadata
(when-not (:ignore? article)
(autolink-headings article config)))}))
(defn -main []
(load-plugins)
(compile-site)
(System/exit 0))
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@lacarmen
Copy link

There's a couple places in cryogen.server where compile-assets-timed gets called that should be replaced by compile-site as well if you actually want to see the customization while running your blog locally 😛

@JohnnyJayJay
Copy link

For users of newer cryogen versions: Replace :update-article-fn with :postprocess-article-html-fn, this way it keeps working.

@holyjak
Copy link
Author

holyjak commented Nov 18, 2023

Thank you both!

@holyjak
Copy link
Author

holyjak commented Nov 18, 2023

@JohnnyJayJay actually :update-article-fn still works, it only needed to be updated to (update article :content-dom ...) (instead of :cintent) and autolink-content-headings had to be changed to simply return the output of enlive/transform. Here is live, working code from my blog.

Notice that postprocess-article-html-fn receives the HTML, while update-article-fn can use the parsed DOM nodes in content-dom, which is better for Enlive - you do not need to re-parse the text.

@JohnnyJayJay
Copy link

Yeah, I did the same for my use of this code. The change I suggested in this comment is just the "minimal change" that was necessary to keep it working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment