-
-
Save holyjak/bbeb714ca25ec99b55933c40f2e75881 to your computer and use it in GitHub Desktop.
| #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)) |
For users of newer cryogen versions: Replace :update-article-fn with :postprocess-article-html-fn, this way it keeps working.
Thank you both!
@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.
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
There's a couple places in
cryogen.serverwherecompile-assets-timedgets called that should be replaced bycompile-siteas well if you actually want to see the customization while running your blog locally 😛