Skip to content

Commit

Permalink
fix: normalize the case of property keys (logseq#8412)
Browse files Browse the repository at this point in the history
* fix: normalize the case of property keys

* chore: remove debugging print statement

* chore: remove unneeded file from diff

* Added tests for Org title parsing

---------

Co-authored-by: Bad3r <[email protected]>
Co-authored-by: Gabriel Horner <[email protected]>
  • Loading branch information
3 people authored Jan 30, 2023
1 parent a0226d4 commit e7b511d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
3 changes: 2 additions & 1 deletion deps/graph-parser/src/logseq/graph_parser/extract.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
(let [first-block (last (first (filter gp-block/heading-block? ast)))
property-name (when (contains? #{"Properties" "Property_Drawer"} (ffirst ast))
(let [properties-ast (second (first ast))
properties (zipmap (map (comp keyword first) properties-ast) (map second properties-ast))]
properties (zipmap (map (comp keyword string/lower-case first) properties-ast)
(map second properties-ast))]
(:title properties)))
first-block-name (let [title (last (first (:title first-block)))]
(and first-block
Expand Down
7 changes: 2 additions & 5 deletions deps/graph-parser/src/logseq/graph_parser/mldoc.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,8 @@
(map first))
(get grouped-ast false)]
properties (map (fn [[_directive k v]]
(let [kname (string/lower-case k)
k (keyword kname)
mldoc-ast (get-references v config)]
[k v mldoc-ast]))
properties-ast)]
[k v (get-references v config)])
properties-ast)]
(if (seq properties)
(cons [["Properties" properties] nil] other-ast)
original-ast))))
Expand Down
25 changes: 25 additions & 0 deletions deps/graph-parser/test/logseq/graph_parser/extract_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
(throw (js/Error. ":block/parent && :block/left conflicts")))
(mapv :block/content blocks))))

(defn- extract-title [file text]
(-> (extract/extract file text {}) :pages first :block/properties :title))

(deftest extract-blocks-for-headings
[]
(is (= ["a" "b" "c"]
Expand Down Expand Up @@ -41,6 +44,28 @@
- i
- j"))))

(deftest parse-page-title
[]
(is (= nil
(extract-title "foo.org" "")))
(is (= "Howdy"
(extract-title "foo.org" "#+title: Howdy")))
(is (= "Howdy"
(extract-title "foo.org" "#+TITLE: Howdy")))
(is (= "Howdy"
(extract-title "foo.org" "#+TiTlE: Howdy")))
(is (= "diagram/abcdef"
(extract-title "foo.org" ":PROPERTIES:
:ID: 72289d9a-eb2f-427b-ad97-b605a4b8c59b
:END:
#+TITLE: diagram/abcdef")))
(is (= "diagram/abcdef"
(extract-title "foo.org" ":PROPERTIES:
:ID: 72289d9a-eb2f-427b-ad97-b605a4b8c59b
:END:
#+title: diagram/abcdef")))
)

(deftest extract-blocks-with-property-pages-config
[]
(are [extract-args expected-refs]
Expand Down
19 changes: 19 additions & 0 deletions deps/graph-parser/test/logseq/graph_parser_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,25 @@
(remove built-in-pages)
set)))))

(testing "from cased org title"
(let [conn (ldb/start-conn)
built-in-pages (set default-db/built-in-pages-names)]
(graph-parser/parse-file conn
"foo.org"
":PROPERTIES:
:ID: 72289d9a-eb2f-427b-ad97-b605a4b8c59b
:END:
#+tItLe: Well parsed!"
{})
(is (= #{"Well parsed!"}
(->> (d/q '[:find (pull ?b [*])
:in $
:where [?b :block/name]]
@conn)
(map (comp :block/original-name first))
(remove built-in-pages)
set)))))

(testing "for file and web uris"
(let [conn (ldb/start-conn)
built-in-pages (set (map string/lower-case default-db/built-in-pages-names))]
Expand Down

0 comments on commit e7b511d

Please sign in to comment.