refactor: markdown new properties syntax wip

pull/1819/head
Tienson Qin 2021-04-30 00:00:51 +08:00
parent d8d5a5d486
commit 3624ca290d
4 changed files with 59 additions and 55 deletions

View File

@ -145,7 +145,7 @@
:on-click (fn [_e]
(editor-handler/remove-block-property! block-id "background-color"))}
"Clear"]]
(let [empty-properties? (not (text/contains-properties? (:block/content block)))
(let [empty-properties? (empty? (:block/properties block))
all-hidden? (text/properties-hidden? (:block/properties block))]
(when (or empty-properties? all-hidden?)
(ui/menu-link

View File

@ -65,12 +65,16 @@
ref-tags (atom #{})
blocks (map (fn [block]
(let [block-ref-pages (seq (:block/refs block))
block-path-ref-pages (seq (:block/path-refs block))]
block-path-ref-pages (seq (:block/path-refs block))
content (get-block-content utf8-content block format)
content (if (= format :org)
content
(text/->new-properties content))]
(when block-ref-pages
(swap! ref-pages set/union (set block-ref-pages)))
(-> block
(dissoc :ref-pages)
(assoc :block/content (get-block-content utf8-content block format)
(assoc :block/content content
:block/file [:file/path file]
:block/format format
:block/page [:block/name (string/lower-case page)]

View File

@ -4,6 +4,9 @@
[clojure.string :as string]
[clojure.set :as set]))
(defonce properties-start ":PROPERTIES:")
(defonce properties-end ":END:")
(defn page-ref?
[s]
(and
@ -173,7 +176,9 @@
(remove #(let [s (string/lower-case (string/trim %))]
(and
(or (string/starts-with? s ":id:")
(string/starts-with? s ":custom_id:"))
(string/starts-with? s ":custom_id:")
(string/starts-with? s "id:: ")
(string/starts-with? s "custom_id:: "))
(let [id (and
(> (count s) 36)
(subs s (- (count s) 36)))]
@ -239,18 +244,6 @@
[k v])))))))
(into {}))))
(defn re-construct-block-properties
[format content properties block-with-title?]
(let [format (keyword format)
level-spaces (extract-level-spaces content format)
result (-> content
(remove-level-spaces format)
(remove-properties!)
(rejoin-properties properties {:block-with-title? block-with-title?}))]
(str (when level-spaces (string/trim-newline level-spaces))
(when (not block-with-title?) "\n")
(string/triml result))))
(defn insert-property
[content key value]
(when (and (not (string/blank? (name key)))
@ -319,3 +312,25 @@
(defn image-link?
[img-formats s]
(some (fn [fmt] (re-find (re-pattern (str "(?i)\\." fmt "(?:\\?([^#]*))?(?:#(.*))?$")) s)) img-formats))
(defn ->new-properties
"New syntax: key:: value"
[content]
(if (and (string/includes? content properties-start)
(string/includes? content properties-end))
(let [lines (string/split-lines content)
start-idx (.indexOf lines properties-start)
end-idx (.indexOf lines properties-end)]
(if (and (>= start-idx 0) (> end-idx 0) (> end-idx start-idx))
(let [before (subvec lines 0 start-idx)
middle (->> (subvec lines (inc start-idx) end-idx)
(map (fn [text]
(let [[k v] (util/split-first ":" (subs text 1))]
(if (and k v)
(str k ":: " (string/trim v))
text)))))
after (subvec lines (inc end-idx))
lines (concat before middle after)]
(string/join "\n" lines))
content))
content))

View File

@ -106,45 +106,6 @@
"hello\n:PROPERTIES:\n:id: f9873a81-07b9-4246-b910-53a6f5ec7e04\na: b\n:END:\n"
"hello\n:PROPERTIES:\na: b\n:END:"))
(deftest re-construct-block-properties
[]
(testing "block content without a title"
(are [x y] (= x y)
(text/re-construct-block-properties :org "** :PROPERTIES:\n:x: y\n:END:\n" {"x" "y"} false)
"** \n:PROPERTIES:\n:x: y\n:END:\n"
(text/re-construct-block-properties :markdown "## :PROPERTIES:\n:x: y\n:END:\n" {"x" "y"} false)
"## \n:PROPERTIES:\n:x: y\n:END:\n"))
(testing "query block without a title"
(are [x y] (= x y)
(text/re-construct-block-properties :org "** #+BEGIN_QUERY
test
#+END_QUERY" {"created_at" 1609332958103} false)
"** \n:PROPERTIES:\n:created_at: 1609332958103\n:END:\n#+BEGIN_QUERY\ntest\n#+END_QUERY"))
(testing "table without a title"
(are [x y] (= x y)
(text/re-construct-block-properties :org "** |x|y|
|1|2|" {"created_at" 1609332958103} false)
"** \n:PROPERTIES:\n:created_at: 1609332958103\n:END:\n|x|y|\n|1|2|"))
(testing "block content with a title"
(are [x y] (= x y)
(text/re-construct-block-properties :org "** hello\n:PROPERTIES:\n:x: y\n:END:\n" {"x" "y"} true)
"** hello\n:PROPERTIES:\n:x: y\n:END:\n"
(text/re-construct-block-properties :markdown "## hello\n:PROPERTIES:\n:x: y\n:END:\n" {"x" "y"} true)
"## hello\n:PROPERTIES:\n:x: y\n:END:\n"))
(testing "block content with custom properties"
(are [x y] (= x y)
(text/re-construct-block-properties :org "** hello\n:PROPERTIES:\n:x: y\n:END:\n" {"x" "z"} true)
"** hello\n:PROPERTIES:\n:x: z\n:END:\n"
(text/re-construct-block-properties :markdown "## hello\n:PROPERTIES:\n:x: y\n:END:\n" {"x" "y" "a" "b"} true)
"## hello\n:PROPERTIES:\n:x: y\n:a: b\n:END:\n")))
(defn test-remove-properties!
[]
(testing "properties with non-blank lines"
@ -177,4 +138,28 @@ test
(text/insert-property "hello\n:PROPERTIES:\n:a: b\n:END: world\n" "c" "d")
"hello\n:PROPERTIES:\n:c: d\n:END:\n:PROPERTIES:\n:a: b\n:END: world\n"))
(defn test->new-properties
[]
(are [x y] (= (text/->new-properties x) y)
":PROPERTIES:\n:foo: bar\n:END:"
"foo:: bar"
"hello\n:PROPERTIES:\n:foo: bar\n:END:"
"hello\nfoo:: bar"
"hello\n:PROPERTIES:\n:foo: bar\n:nice: bingo\n:END:"
"hello\nfoo:: bar\nnice:: bingo"
"hello\n:PROPERTIES:\n:foo: bar\n:nice: bingo\n:END:\n"
"hello\nfoo:: bar\nnice:: bingo"
"hello\n:PROPERTIES:\n:foo: bar\n:nice: bingo\n:END:\nnice"
"hello\nfoo:: bar\nnice:: bingo\nnice"
"hello\n:PROPERTIES:\n:foo: bar\n:nice:\n:END:\nnice"
"hello\nfoo:: bar\nnice:: \nnice"
"hello\n:PROPERTIES:\n:foo: bar\n:nice\n:END:\nnice"
"hello\nfoo:: bar\n:nice\nnice"))
#_(cljs.test/test-ns 'frontend.text-test)