mldoc.cljs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. (ns frontend.format.mldoc
  2. (:require [clojure.string :as string]
  3. [frontend.format.protocol :as protocol]
  4. [frontend.state :as state]
  5. [goog.object :as gobj]
  6. [lambdaisland.glogi :as log]
  7. ["mldoc" :as mldoc :refer [Mldoc]]
  8. [logseq.graph-parser.mldoc :as gp-mldoc]
  9. [logseq.graph-parser.util :as gp-util]))
  10. (defonce anchorLink (gobj/get Mldoc "anchorLink"))
  11. (defonce parseOPML (gobj/get Mldoc "parseOPML"))
  12. (defonce parseAndExportMarkdown (gobj/get Mldoc "parseAndExportMarkdown"))
  13. (defonce parseAndExportOPML (gobj/get Mldoc "parseAndExportOPML"))
  14. (defonce export (gobj/get Mldoc "export"))
  15. (defn parse-opml
  16. [content]
  17. (parseOPML content))
  18. (defn parse-export-markdown
  19. [content config references]
  20. (parseAndExportMarkdown content
  21. config
  22. (or references gp-mldoc/default-references)))
  23. (defn parse-export-opml
  24. [content config title references]
  25. (parseAndExportOPML content
  26. config
  27. title
  28. (or references gp-mldoc/default-references)))
  29. (defn block-with-title?
  30. [type]
  31. (contains? #{"Paragraph"
  32. "Raw_Html"
  33. "Hiccup"
  34. "Heading"} type))
  35. (defn opml->edn
  36. [content]
  37. (try
  38. (if (string/blank? content)
  39. {}
  40. (let [[headers blocks] (-> content (parse-opml) (gp-util/json->clj))]
  41. [headers (gp-mldoc/collect-page-properties blocks gp-mldoc/parse-property (state/get-config))]))
  42. (catch js/Error e
  43. (log/error :edn/convert-failed e)
  44. [])))
  45. (defn ->edn
  46. "Wrapper around gp-mldoc/->edn which provides config state"
  47. [content config]
  48. (gp-mldoc/->edn content config (state/get-config)))
  49. (defrecord MldocMode []
  50. protocol/Format
  51. (toEdn [_this content config]
  52. (->edn content config))
  53. (toHtml [_this content config references]
  54. (export "html" content config references))
  55. (loaded? [_this]
  56. true)
  57. (lazyLoad [_this _ok-handler]
  58. true)
  59. (exportMarkdown [_this content config references]
  60. (parse-export-markdown content config references))
  61. (exportOPML [_this content config title references]
  62. (parse-export-opml content config title references)))
  63. (defn plain->text
  64. [plains]
  65. (string/join (map last plains)))
  66. (defn properties?
  67. [ast]
  68. (contains? #{"Properties" "Property_Drawer"} (ffirst ast)))
  69. (defn typ-drawer?
  70. [ast typ]
  71. (and (contains? #{"Drawer"} (ffirst ast))
  72. (= typ (second (first ast)))))