|
58 | 58 | (doseq [file (fs/glob dir "**/*.md")] |
59 | 59 | (process-md (str file) dry-run?))) |
60 | 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})) |
| 61 | +;; YAML processing for _data dir (which stores nav info): |
| 62 | + |
| 63 | +(defn- add-metabase-prefix? [node] |
| 64 | + (and (map? node) |
| 65 | + (contains? node :url) |
| 66 | + (not (str/starts-with? (:url node) "/docs/")) |
| 67 | + (not (str/starts-with? (:url node) "https://metabase.com")) |
| 68 | + (not (str/starts-with? (:url node) "https://www.metabase.com")) |
| 69 | + (not (str/starts-with? (:url node) "http://metabase.com")) |
| 70 | + (not (str/starts-with? (:url node) "http://www.metabase.com")))) |
| 71 | + |
| 72 | +(defn- remove-metabase-prefix? [node] |
| 73 | + (and (map? node) |
| 74 | + (contains? node :url) |
| 75 | + (or (str/starts-with? (:url node) "https://metabase.com/docs") |
| 76 | + (str/starts-with? (:url node) "https://www.metabase.com/docs") |
| 77 | + (str/starts-with? (:url node) "http://metabase.com/docs") |
| 78 | + (str/starts-with? (:url node) "http://www.metabase.com/docs")))) |
| 79 | + |
| 80 | +(defn- update-node [node] |
| 81 | + (let [add? (add-metabase-prefix? node) |
| 82 | + remove? (remove-metabase-prefix? node)] |
| 83 | + (when add? (u/log " 📝" (str "Adding prefix to: " (:url node)))) |
| 84 | + (when remove? (u/log " 📝" (str "Removing prefix from: " (:url node)))) |
| 85 | + (cond-> node |
| 86 | + add? (update-in [:url] #(str "https://metabase.com" %)) |
| 87 | + remove? (update-in [:url] |
| 88 | + (comp |
| 89 | + #(str/replace % #"^http://www.metabase.com" "") |
| 90 | + #(str/replace % #"^http://metabase.com" "") |
| 91 | + #(str/replace % #"^https://www.metabase.com" "") |
| 92 | + #(str/replace % #"^https://metabase.com" "")))))) |
| 93 | + |
| 94 | +(comment |
| 95 | + (mapv (comp :url update-node) |
| 96 | + [{:url "/learn/latest/cloud/start"} |
| 97 | + {:url "http://www.metabase.com/docs/latest/cloud/start"}]) |
| 98 | +;; => ["https://metabase.com/learn/latest/cloud/start" |
| 99 | +;; "/docs/latest/cloud/start"] |
| 100 | + ) |
| 101 | + |
| 102 | +(defn- process-yaml [path dry-run?] |
| 103 | + (let [parsed (yaml/parse-string (slurp path)) |
| 104 | + updated (walk/postwalk |
| 105 | + update-node |
| 106 | + parsed)] |
| 107 | + (cond |
| 108 | + dry-run? |
| 109 | + (u/log " 📝" (str "Dry run: Would update file: " path)) |
| 110 | + |
| 111 | + (= parsed updated) |
| 112 | + (u/log " ℹ️" (str "No changes for file: " path)) |
| 113 | + |
| 114 | + :else |
| 115 | + (do (spit path (u/generate-yaml updated)) |
77 | 116 | (u/log " ✅" (str "Updated file: " path)))))) |
78 | | - (process-yaml "_data/docs/nav/latest.yml" true)) |
79 | 117 |
|
| 118 | +(comment |
| 119 | + |
| 120 | + (process-yaml "_data/docs/nav/latest.yml" false) |
| 121 | + |
| 122 | + ) |
80 | 123 |
|
81 | 124 | (defn- crawl-data-directory |
82 | 125 | [dry-run?] |
83 | | - (doseq [file (concat (fs/glob "_data" "**/*.yaml") (fs/glob "_data" "**/*.yml"))] |
| 126 | + (doseq [file (concat (fs/glob "_data" "**/*.yaml") |
| 127 | + (fs/glob "_data" "**/*.yml"))] |
84 | 128 | (process-yaml (str file) dry-run?))) |
85 | 129 |
|
86 | 130 | (defn -main |
|
0 commit comments