Last active
November 20, 2023 15:35
-
-
Save holyjak/bbeb714ca25ec99b55933c40f2e75881 to your computer and use it in GitHub Desktop.
Cryogen customization: autolink headings etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#post a.anchor, #custom-page a.anchor { | |
float: left; | |
padding-right: 4px; | |
margin-left: -20px; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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)) |
@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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you both!