extract_test.cljs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. (defn- extract-title [file text]
  15. (-> (extract/extract file text {}) :pages first :block/properties :title))
  16. (deftest extract-blocks-for-headings
  17. (is (= ["a" "b" "c"]
  18. (extract
  19. "- a
  20. - b
  21. - c")))
  22. (is (= ["## hello" "world" "nice" "nice" "bingo" "world"]
  23. (extract "## hello
  24. - world
  25. - nice
  26. - nice
  27. - bingo
  28. - world")))
  29. (is (= ["# a" "## b" "### c" "#### d" "### e" "f" "g" "h" "i" "j"]
  30. (extract "# a
  31. ## b
  32. ### c
  33. #### d
  34. ### e
  35. - f
  36. - g
  37. - h
  38. - i
  39. - j"))))
  40. (deftest parse-page-title
  41. (is (= nil
  42. (extract-title "foo.org" "")))
  43. (is (= "Howdy"
  44. (extract-title "foo.org" "#+title: Howdy")))
  45. (is (= "Howdy"
  46. (extract-title "foo.org" "#+TITLE: Howdy")))
  47. (is (= "Howdy"
  48. (extract-title "foo.org" "#+TiTlE: Howdy")))
  49. (is (= "diagram/abcdef"
  50. (extract-title "foo.org" ":PROPERTIES:
  51. :ID: 72289d9a-eb2f-427b-ad97-b605a4b8c59b
  52. :END:
  53. #+TITLE: diagram/abcdef")))
  54. (is (= "diagram/abcdef"
  55. (extract-title "foo.org" ":PROPERTIES:
  56. :ID: 72289d9a-eb2f-427b-ad97-b605a4b8c59b
  57. :END:
  58. #+title: diagram/abcdef")))
  59. )
  60. (deftest extract-blocks-with-property-pages-config
  61. (are [extract-args expected-refs]
  62. (= expected-refs
  63. (->> (apply extract/extract extract-args)
  64. :blocks
  65. (mapcat #(->> % :block/refs (map :block/name)))
  66. set))
  67. ["a.md" "foo:: #bar\nbaz:: #bing" {:block-pattern "-" :user-config {:property-pages/enabled? true}}]
  68. #{"bar" "bing" "foo" "baz"}
  69. ["a.md" "foo:: #bar\nbaz:: #bing" {:block-pattern "-" :user-config {:property-pages/enabled? false}}]
  70. #{"bar" "bing"}))
  71. (deftest test-regression-1902
  72. (is (= ["line1" "line2" "line3" "line4"]
  73. (extract
  74. "- line1
  75. - line2
  76. - line3
  77. - line4"))))
  78. (def foo-edn
  79. "Example exported whiteboard page as an edn exportable."
  80. '{:blocks
  81. ({:block/content "foo content a",
  82. :block/format :markdown},
  83. {:block/content "foo content b",
  84. :block/format :markdown}),
  85. :pages
  86. ({:block/format :markdown,
  87. :block/original-name "Foo"
  88. :block/properties {:title "my whiteboard foo"}})})
  89. (deftest test-extract-whiteboard-edn
  90. (let [{:keys [pages blocks]} (extract/extract-whiteboard-edn "/whiteboards/foo.edn" (pr-str foo-edn) {})
  91. page (first pages)]
  92. (is (= (get-in page [:block/file :file/path]) "/whiteboards/foo.edn"))
  93. (is (= (:block/name page) "foo"))
  94. (is (true? (contains? (:block/type page) "whiteboard")))
  95. (is (= (:block/original-name page) "Foo"))
  96. (is (every? #(= (:block/parent %) {:block/name "foo"}) blocks))))