     (.writeFileSync node-fs filepath buff)))
 
 (defn prepare! [parser src output-dir]
-  (let [m (parse parser src)]
+  (let [m (parse parser src)
+        all-chapters (chapters m)]
     (write! output-dir "styles.css" (style))
     (write! output-dir "7ce12f75-f371-4e85-a3e9-b7749a65f140.html" (toc m))
-    (doseq [ch (chapters m)]
-      (write! output-dir (rel-url ch) (chapter ch)))))+    (dorun
+      (map-indexed
+        #(write!
+          output-dir
+          (rel-url %2)
+          (chapter %2
+            (prev-chapter all-chapters %1)
+            (next-chapter all-chapters %1)))
+        all-chapters))))
    (rel-url book-id chap-num chap-cnt)))

(defn toc-book [b]
  [:p {:class "tocp"}
    [:span {:class "chapter"} (book-name (:id b))]
    " "
    (->> (:chapters b)
      (map (fn [ch]

              {:book-id (:id b)
               :book-num (:num b)
               :chap-cnt (count (:chapters b))}))
          (:chapters b))))
    (vec)))

(def ^:private beginbracket [:beginbracket "["])
(def ^:private endbracket [:endbracket "]"])
