-
-
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)) |
| <svg style="position: absolute; width: 0; height: 0; overflow: hidden;" version="1.1" xmlns="http://www.w3.org/2000/svg"> | |
| <defs> | |
| <symbol id="icon-link" viewBox="0 0 16 16"> | |
| <path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"> | |
| </path> | |
| </symbol> | |
| </defs> | |
| </svg> |
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 😛