Files
logseq/src/main/frontend/util/priority.cljs
Gabriel Horner 1f0e22275d Add ns docstrings for most ns in src/main
- Added to CI now that it passes
- Added no-doc for docstrings that don't add any more than what's in the
ns name or for ones where I didn't know the ns that well
2022-09-27 13:55:16 +08:00

45 lines
1.5 KiB
Clojure

(ns frontend.util.priority
"Util fns for task priorities e.g. A, B, C"
(:require [clojure.string :as string]
[frontend.util :as util]
[frontend.util.marker :as marker]))
(defn cycle-priority-state
[content]
(let [priority-reg #"\[#([ABC]{1})\]\s{1}"
priority (last (util/safe-re-find priority-reg content))
next-priority (case priority
"A" "B"
"B" "C"
"C" nil
"A")]
(and next-priority (util/format "[#%s]" next-priority))))
(defn add-or-update-priority
[content format priority]
(let [priority-pattern #"(\[#[ABC]\])?\s?"
[re-pattern new-line-re-pattern]
(if (= :org format)
[#"\*+\s" #"\n\*+\s"]
[#"#+\s" #"\n#+\s"])
skip-hash-pos
(if-let [matches (seq (util/re-pos new-line-re-pattern content))]
(let [[start-pos content] (last matches)]
(+ start-pos (count content)))
(count (util/safe-re-find re-pattern content)))
skip-marker-pos
(if-let [matches (seq (util/re-pos marker/bare-marker-pattern (subs content skip-hash-pos)))]
(let [[start-pos content] (last matches)]
(+ start-pos (count content)))
0)
pos (+ skip-hash-pos skip-marker-pos)
new-content
(str (subs content 0 pos)
(string/replace-first (subs content pos)
priority-pattern
(str priority " ")))]
new-content))