diff --git a/src/cryogen_core/toc.clj b/src/cryogen_core/toc.clj index a1021d5..871dba4 100644 --- a/src/cryogen_core/toc.clj +++ b/src/cryogen_core/toc.clj @@ -18,16 +18,26 @@ headings))) [] content)) -(defn make-links [headings] +(defn make-links + "Create a table of contents from the given headings. This function will look + for either: + (1) headings with a child anchor with a non-nil name attribute, e.g. +

Reference Title

+ or + (2) headings with an id attribute, e.g.

Reference Title

+ In both cases above, the anchor reference becomes \"#reference\" and the + anchor text is \"Reference Title\"." + [headings] (loop [items headings acc nil _last nil] - (if-let [{tag :tag [{{name :name} :attrs} title] :content} (first items)] - (if (nil? name) (recur (rest items) acc nil) - (let [entry [:li [:a {:href (str "#" name)} title]] - jump (compare_index _last tag)] - (cond (> jump 0) (recur (rest items) (str acc "
    " (hiccup/html entry)) tag) - (= jump 0) (recur (rest items) (str acc (hiccup/html entry)) tag) - (< jump 0) (recur (rest items) (str acc (apply str (repeat (* -1 jump) "
")) - (hiccup/html entry)) tag)))) + (if-let [{tag :tag {id :id} :attrs [{{name :name} :attrs} title :as htext] :content} (first items)] + (let [anchor (or id name)] + (if (nil? anchor) (recur (rest items) acc nil) + (let [entry [:li [:a {:href (str "#" anchor)} (or title (first htext))]] + jump (compare_index _last tag)] + (cond (> jump 0) (recur (rest items) (str acc "
    " (hiccup/html entry)) tag) + (= jump 0) (recur (rest items) (str acc (hiccup/html entry)) tag) + (< jump 0) (recur (rest items) (str acc (apply str (repeat (* -1 jump) "
")) + (hiccup/html entry)) tag))))) (str acc "")))) (defn generate-toc [html]