Skip to content

Instantly share code, notes, and snippets.

@aburd
Last active May 13, 2024 01:19
Show Gist options
  • Save aburd/36a58e3c571c19639beeb40355965f7b to your computer and use it in GitHub Desktop.
Save aburd/36a58e3c571c19639beeb40355965f7b to your computer and use it in GitHub Desktop.
babashka script to make a markdown index for any markdown file
#!/usr/bin/env bb
(require '[clojure.string :as s])
(defn usage []
(println "md-index.clj [target-markdown-file]"))
(let [[p] *command-line-args*]
(when (or (nil? p) (not (s/ends-with? p ".md")))
(usage)
(System/exit 1))
(let [content (slurp p)
headings (->> content
(s/split-lines)
(filter #(s/starts-with? % "#"))
(map (fn [line]
(let [[hashes title] (s/split line #"\s")
anchor-link (format "#%s" (s/replace (s/lower-case title) #"\s" "-"))
tabs (apply str (repeat (dec (count hashes)) " "))]
(format "%s- [%s](%s)" tabs title anchor-link)))))]
(println (s/join "\n" headings))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment