Skip to content

Commit 8843f78

Browse files
committed
update script to stitch links in _data/**.y{a,}ml
1 parent e3a019a commit 8843f78

File tree

3 files changed

+67
-25
lines changed

3 files changed

+67
-25
lines changed
Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
(ns tasks.stitch-outbound-links
22
(:require [babashka.fs :as fs]
3-
[clojure.string :as str]))
3+
[clj-yaml.core :as yaml]
4+
[clojure.string :as str]
5+
[clojure.walk :as walk]
6+
[tasks.util :as u]))
47

58
(def outbound-link-roots
69
#{"blog-authors" "case-studies" "cloud" "community-posts" "dashboards" "data"
@@ -10,40 +13,80 @@
1013
"sales" "sass" "site" "start" "startup-guide" "talk-to-a-person"
1114
"troubleshooting" "upgrade"})
1215

13-
(defn- log [level message] (println (str level " " message)))
16+
(def outbound-link-roots-re
17+
(str "(" (str/join "|" outbound-link-roots) ")"))
18+
19+
(def inline-pattern (re-pattern (str "(\\(\\s*)/" outbound-link-roots-re)))
20+
(def reference-pattern (re-pattern (str "(\\]\\:\\s*)/" outbound-link-roots-re)))
21+
22+
(comment
23+
;; How It Works:
24+
(update-links "[a](/gallery/x)")
25+
;; => "[a](https://metabase.com/gallery/x)"
26+
(update-links "[a]: /gallery/x")
27+
;; => "[a]: https://metabase.com/gallery/x"
28+
(update-links "[a]: /gallery/x")
29+
;; => "[a]: https://metabase.com/gallery/x"
30+
)
1431

1532
(defn- update-links
1633
"Replaces occurrences of links starting with '/learn' with 'https://metabase.com/learn'.
1734
It handles both inline links (e.g., [c](/learn/y)) and reference-style links (e.g., [b]: /learn/x)."
1835
[content]
1936
(-> content
20-
(str/replace #"(\(\s*)/learn" "$1https://metabase.com/learn")
21-
(str/replace #"(\]\:\s*)/learn" "$1https://metabase.com/learn")))
37+
(str/replace inline-pattern "$1https://metabase.com/$2")
38+
(str/replace reference-pattern "$1https://metabase.com/$2")))
2239

23-
(defn- process-file
40+
(defn- process-md
2441
"Reads a markdown file, updates its content if necessary, and writes the updated content back.
2542
When dry-run is enabled, logs the intended action instead of modifying the file."
26-
[file dry-run?]
27-
(log "πŸ”" (str "Processing file: " file))
28-
(let [content (slurp file)
43+
[path dry-run?]
44+
(u/log "πŸ”" (str "Processing file: " path))
45+
(let [content (slurp path)
2946
new-content (update-links content)]
3047
(if (= content new-content)
31-
(log "ℹ️" (str "No changes for file: " file))
32-
(do (if dry-run?
33-
(log "πŸ“" (str "Dry run: Would update file: " file))
34-
(do
35-
(spit file new-content)
36-
(log "βœ…" (str "Updated file: " file))))))))
37-
38-
(defn- crawl-directory
48+
(u/log " ℹ️" (str "No changes for file: " path))
49+
(if dry-run?
50+
(u/log " πŸ“" (str "Dry run: Would update file: " path))
51+
(do
52+
(spit path new-content)
53+
(u/log " βœ…" (str "Updated file: " path)))))))
54+
55+
(defn- crawl-md-directory
3956
"Recursively finds all Markdown files in the given directory and processes each one."
4057
[dir dry-run?]
4158
(doseq [file (fs/glob dir "**/*.md")]
42-
(prn (str file))
43-
(process-file (str file) dry-run?)))
59+
(process-md (str file) dry-run?)))
60+
61+
(do
62+
(defn process-yaml [path dry-run?]
63+
(let [parsed (yaml/parse-string (slurp path))
64+
updated (walk/postwalk
65+
(fn [node]
66+
(if (and (map? node)
67+
(contains? node :url)
68+
(some #(str/starts-with? (:url node) %) (map #(str "/" % "/") outbound-link-roots)))
69+
(update-in node [:url] #(str "https://metabase.com" %))
70+
node))
71+
parsed)]
72+
(if dry-run?
73+
(do (u/log " πŸ“" (str "Dry run: Would update file: " path))
74+
updated)
75+
(do
76+
(spit path (yaml/generate-string updated :dumper-options {:flow-style :block}))
77+
(u/log " βœ…" (str "Updated file: " path))))))
78+
(process-yaml "_data/docs/nav/latest.yml" true))
79+
80+
81+
(defn- crawl-data-directory
82+
[dry-run?]
83+
(doseq [file (concat (fs/glob "_data" "**/*.yaml") (fs/glob "_data" "**/*.yml"))]
84+
(process-yaml (str file) dry-run?)))
4485

4586
(defn -main
4687
"Entry point. Args are validated in bb.edn"
47-
[{:keys [dry-run? dir]}]
48-
(log "πŸš€" (str "Crawling directory: " dir (if dry-run? " (dry run mode)" "")))
49-
(crawl-directory dir dry-run?))
88+
[{:keys [dry-run?]}]
89+
(u/log "πŸš€" (str "Crawling _docs directory: updating markdown links" (when dry-run? " (dry run mode)")))
90+
(crawl-md-directory "_docs" dry-run?)
91+
(u/log "πŸš€" (str "Crawling _data directory: updating yaml links" (when dry-run? " (dry run mode)")))
92+
(crawl-data-directory dry-run?))

β€Ž__tasks/tasks/util.cljβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
(ns tasks.util
22
(:require [clj-yaml.core :as yaml]))
33

4+
(defn log [level message] (println (str level " " message)))
5+
46
(defn update-frontmatter!
57
"Reads a file, updates its YAML frontmatter by applying f to the value of the
68
YAML frontmatter, as a clojure map and writes the updated content back.

β€Žbb.ednβ€Ž

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,4 @@
1414

1515
stitch-outbound-links
1616
{:requires [[tasks.stitch-outbound-links :refer [-main]]]
17-
:task (let [{:keys [dir dry-run?] :as parsed} (parse-args *command-line-args*)]
18-
(if (and dir (fs/directory? dir))
19-
(-main parsed)
20-
(println "Usage: bb script.clj [--dry-run] <directory>")))}}}
17+
:task (-main (parse-args *command-line-args*))}}}

0 commit comments

Comments
Β (0)