extract_test.cljs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. (ns logseq.graph-parser.extract-test
  2. (:require [cljs.test :refer [deftest is are]]
  3. [logseq.graph-parser.extract :as extract]
  4. [clojure.pprint :as pprint]))
  5. (defn- extract
  6. [text]
  7. (let [{:keys [blocks]} (extract/extract "a.md" text {:block-pattern "-"})
  8. lefts (map (juxt :block/parent :block/left) blocks)]
  9. (if (not= (count lefts) (count (distinct lefts)))
  10. (do
  11. (pprint/pprint (map (fn [x] (select-keys x [:block/uuid :block/level :block/content :block/left])) blocks))
  12. (throw (js/Error. ":block/parent && :block/left conflicts")))
  13. (mapv :block/content blocks))))
  14. (deftest extract-blocks-for-headings
  15. []
  16. (is (= ["a" "b" "c"]
  17. (extract
  18. "- a
  19. - b
  20. - c")))
  21. (is (= ["## hello" "world" "nice" "nice" "bingo" "world"]
  22. (extract "## hello
  23. - world
  24. - nice
  25. - nice
  26. - bingo
  27. - world")))
  28. (is (= ["# a" "## b" "### c" "#### d" "### e" "f" "g" "h" "i" "j"]
  29. (extract "# a
  30. ## b
  31. ### c
  32. #### d
  33. ### e
  34. - f
  35. - g
  36. - h
  37. - i
  38. - j"))))
  39. (deftest extract-blocks-with-property-pages-config
  40. []
  41. (are [extract-args expected-refs]
  42. (= expected-refs
  43. (->> (apply extract/extract extract-args)
  44. :blocks
  45. (mapcat #(->> % :block/refs (map :block/name)))
  46. set))
  47. ["a.md" "foo:: #bar\nbaz:: #bing" {:block-pattern "-" :user-config {:property-pages/enabled? true}}]
  48. #{"bar" "bing" "foo" "baz"}
  49. ["a.md" "foo:: #bar\nbaz:: #bing" {:block-pattern "-" :user-config {:property-pages/enabled? false}}]
  50. #{"bar" "bing"}))
  51. (deftest test-regression-1902
  52. []
  53. (is (= ["line1" "line2" "line3" "line4"]
  54. (extract
  55. "- line1
  56. - line2
  57. - line3
  58. - line4"))))
  59. (def foo-edn
  60. "Example exported whiteboard page as an edn exportable."
  61. '{:blocks
  62. ({:block/content "foo content a",
  63. :block/format :markdown},
  64. {:block/content "foo content b",
  65. :block/format :markdown}),
  66. :pages
  67. ({:block/format :markdown,
  68. :block/original-name "Foo"
  69. :block/properties {:title "my whiteboard foo"}})})
  70. (deftest test-extract-whiteboard-edn
  71. []
  72. (let [{:keys [pages blocks]} (extract/extract-whiteboard-edn "/whiteboards/foo.edn" (pr-str foo-edn) {})
  73. page (first pages)]
  74. (is (= (get-in page [:block/file :file/path]) "/whiteboards/foo.edn"))
  75. (is (= (:block/name page) "foo"))
  76. (is (= (:block/type page) "whiteboard"))
  77. (is (= (:block/original-name page) "Foo"))
  78. (is (every? #(= (:block/parent %) {:block/name "foo"}) blocks))))