Last active
May 13, 2024 01:19
-
-
Save aburd/36a58e3c571c19639beeb40355965f7b to your computer and use it in GitHub Desktop.
babashka script to make a markdown index for any markdown file
This file contains hidden or 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
#!/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