mirror of https://github.com/logseq/logseq
fix: ci
parent
fc690ef31d
commit
4cae0f9ebd
|
@ -11,32 +11,24 @@
|
|||
|
||||
(defn insert-property
|
||||
[format content key value]
|
||||
(when (string? content)
|
||||
(let [ast (mldoc/->edn content (mldoc/default-config format))
|
||||
key-exists? (fn [k] (boolean (k (last (ffirst ast)))))
|
||||
key (if (string? key) (keyword key) key)
|
||||
old-value (key (last (ffirst ast)))
|
||||
new-value (case key
|
||||
:title value
|
||||
(-> (if (coll? old-value)
|
||||
(concat old-value [value])
|
||||
(conj [old-value] value))
|
||||
(distinct)))
|
||||
build-property-fn (fn [value]
|
||||
(util/format (case format
|
||||
:org "#+%s: %s"
|
||||
"%s:: %s")
|
||||
(name key)
|
||||
(if (coll? value)
|
||||
(->> (remove nil? value)
|
||||
(string/join ", "))
|
||||
value)))
|
||||
old-property-str (when old-value (build-property-fn old-value))
|
||||
new-property-str (build-property-fn new-value)]
|
||||
(if (key-exists? key)
|
||||
(string/replace content old-property-str new-property-str)
|
||||
(string/join "\n" (remove #(= "" %)
|
||||
[new-property-str content]))))))
|
||||
(when (and (string? content) (not (string/blank? (name key))))
|
||||
(let [key (if (string? key) (keyword key) key)
|
||||
key-part (util/format (case format
|
||||
:org "#+%s: "
|
||||
"%s:: ") (name key))
|
||||
new-property-line (str key-part value)
|
||||
lines (string/split-lines content)
|
||||
key-exists? (atom false)
|
||||
lines (doall
|
||||
(map (fn [line]
|
||||
(if (and (string/starts-with? line key-part) (not @key-exists?)) ; only replace the first match
|
||||
(do
|
||||
(reset! key-exists? true)
|
||||
new-property-line)
|
||||
line)) lines))
|
||||
lines (if (= lines [""]) nil lines)
|
||||
lines (if @key-exists? lines (cons new-property-line lines))]
|
||||
(string/join "\n" lines))))
|
||||
|
||||
(defn insert-properties
|
||||
[format content kvs]
|
||||
|
|
|
@ -15,19 +15,13 @@
|
|||
"#+title: new title\nhello"
|
||||
|
||||
(property/insert-property :org "#+title: title\nhello" :alias "alias1")
|
||||
"#+title: title\n#+alias: alias1\nhello"
|
||||
"#+alias: alias1\n#+title: title\nhello"
|
||||
|
||||
(property/insert-property :org "#+title: title\n#+alias: alias1\nhello" :alias "alias2")
|
||||
"#+title: title\n#+alias: alias1, alias2\nhello"
|
||||
"#+title: title\n#+alias: alias2\nhello"
|
||||
|
||||
(property/insert-property :org "#+title: title\n#+alias: alias1, alias2\nhello" :alias "alias3")
|
||||
"#+title: title\n#+alias: alias1, alias2, alias3\nhello"
|
||||
|
||||
(property/insert-property :org "#+title: title\n#+alias: alias1, alias2\nhello" :aliases "aliases1")
|
||||
"#+title: title\n#+alias: alias1, alias2\n#+aliases: aliases1\nhello"
|
||||
|
||||
(property/insert-property :org "#+title: title\n#+alias: alias1, alias2\n#+aliases: aliases1\nhello" :aliases "aliases2")
|
||||
"#+title: title\n#+alias: alias1, alias2\n#+aliases: aliases1, aliases2\nhello"))
|
||||
"#+title: title\n#+alias: alias3\nhello"))
|
||||
|
||||
(testing "add markdown page property"
|
||||
(are [x y] (= x y)
|
||||
|
@ -41,19 +35,19 @@
|
|||
"title:: new title\nhello"
|
||||
|
||||
(property/insert-property :markdown "title:: title\nhello" :alias "alias1")
|
||||
"title:: title\nalias:: alias1\nhello"
|
||||
"alias:: alias1\ntitle:: title\nhello"
|
||||
|
||||
(property/insert-property :markdown "title:: title\nalias:: alias1\nhello" :alias "alias2")
|
||||
"title:: title\nalias:: alias1, alias2\nhello"
|
||||
"title:: title\nalias:: alias2\nhello"
|
||||
|
||||
(property/insert-property :markdown "title:: title\nalias:: alias1, alias2\nhello" :alias "alias3")
|
||||
"title:: title\nalias:: alias1, alias2, alias3\nhello"
|
||||
"title:: title\nalias:: alias3\nhello"
|
||||
|
||||
(property/insert-property :markdown "title:: title\nalias:: alias1, alias2\nhello" :aliases "aliases1")
|
||||
"title:: title\nalias:: alias1, alias2\naliases:: aliases1\nhello"
|
||||
"aliases:: aliases1\ntitle:: title\nalias:: alias1, alias2\nhello"
|
||||
|
||||
(property/insert-property :markdown "title:: title\nalias:: alias1, alias2\naliases:: aliases1\nhello" :aliases "aliases2")
|
||||
"title:: title\nalias:: alias1, alias2\naliases:: aliases1, aliases2\nhello")))
|
||||
"title:: title\nalias:: alias1, alias2\naliases:: aliases2\nhello")))
|
||||
|
||||
(deftest test-insert-properties
|
||||
(testing "add org page properties"
|
||||
|
@ -68,19 +62,19 @@
|
|||
(property/insert-properties :org "#+title: title\nhello"
|
||||
{:title "new title"
|
||||
:alias "alias1"})
|
||||
"#+title: new title\n#+alias: alias1\nhello"
|
||||
"#+alias: alias1\n#+title: new title\nhello"
|
||||
|
||||
(property/insert-properties :org "#+title: title\n#+alias: alias1\nhello"
|
||||
{:title "new title"
|
||||
:alias "alias2"
|
||||
:aliases "aliases1"})
|
||||
"#+title: new title\n#+alias: alias1, alias2\n#+aliases: aliases1\nhello"
|
||||
"#+aliases: aliases1\n#+title: new title\n#+alias: alias2\nhello"
|
||||
|
||||
(property/insert-properties :org "#+title: title\n#+alias: alias1, alias2\n#+aliases: aliases1\nhello"
|
||||
{:title "new title"
|
||||
:alias "alias2"
|
||||
:aliases "aliases1"})
|
||||
"#+title: new title\n#+alias: alias1, alias2\n#+aliases: aliases1\nhello"))
|
||||
"#+title: new title\n#+alias: alias2\n#+aliases: aliases1\nhello"))
|
||||
|
||||
(testing "add markdown page properties"
|
||||
(are [x y] (= x y)
|
||||
|
@ -93,18 +87,18 @@
|
|||
(property/insert-properties :markdown "title:: title\nhello"
|
||||
{:title "new title"
|
||||
:alias "alias1"})
|
||||
"title:: new title\nalias:: alias1\nhello"
|
||||
"alias:: alias1\ntitle:: new title\nhello"
|
||||
|
||||
(property/insert-properties :markdown "title:: title\nalias:: alias1\nhello"
|
||||
{:title "new title"
|
||||
:alias "alias2"
|
||||
:aliases "aliases1"})
|
||||
"title:: new title\nalias:: alias1, alias2\naliases:: aliases1\nhello"
|
||||
"aliases:: aliases1\ntitle:: new title\nalias:: alias2\nhello"
|
||||
|
||||
(property/insert-properties :markdown "title:: title\nalias:: alias1, alias2\naliases:: aliases1\nhello"
|
||||
{:title "new title"
|
||||
:alias "alias2"
|
||||
:aliases "aliases1"})
|
||||
"title:: new title\nalias:: alias1, alias2\naliases:: aliases1\nhello")))
|
||||
"title:: new title\nalias:: alias2\naliases:: aliases1\nhello")))
|
||||
|
||||
#_(cljs.test/run-tests)
|
||||
|
|
Loading…
Reference in New Issue