mldoc.cljs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. (ns frontend.worker.mldoc
  2. "Mldoc related fns"
  3. (:require [logseq.graph-parser.mldoc :as gp-mldoc]
  4. [cljs-bean.core :as bean]
  5. [logseq.db.sqlite.util :as sqlite-util]
  6. [clojure.string :as string]))
  7. (defn get-default-config
  8. "Gets a mldoc default config for the given format. Works for DB and file graphs"
  9. [repo format]
  10. (let [db-based? (sqlite-util/db-based-graph? repo)]
  11. (->>
  12. (cond-> (gp-mldoc/default-config-map format)
  13. db-based?
  14. (assoc :enable_drawers false))
  15. bean/->js
  16. js/JSON.stringify)))
  17. (defn ->edn
  18. "Wrapper around gp-mldoc/->edn that builds mldoc config given a format"
  19. [repo content format]
  20. (gp-mldoc/->edn content (get-default-config repo format)))
  21. (defn properties?
  22. [ast]
  23. (contains? #{"Properties" "Property_Drawer"} (ffirst ast)))
  24. (defn block-with-title?
  25. [type]
  26. (contains? #{"Paragraph"
  27. "Raw_Html"
  28. "Hiccup"
  29. "Heading"} type))
  30. (defn- has-title?
  31. [repo content format]
  32. (let [ast (->edn repo content format)]
  33. (block-with-title? (ffirst (map first ast)))))
  34. (defn get-title&body
  35. "parses content and returns [title body]
  36. returns nil if no title"
  37. [repo content format]
  38. (let [lines (string/split-lines content)]
  39. (if (has-title? repo content format)
  40. [(first lines) (string/join "\n" (rest lines))]
  41. [nil (string/join "\n" lines)])))