html_export.cljs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. (ns frontend.tools.html-export
  2. (:require-macros [hiccups.core :as hiccups :refer [html]])
  3. (:require [clojure.set :as set]
  4. [clojure.walk :as walk]
  5. [frontend.components.block :as block]
  6. [frontend.db :as db]
  7. [medley.core :as medley]
  8. [frontend.format.block :as format-block]))
  9. ;; Consider generate a db index so that search can still works
  10. ;; Or maybe TiddlyWiki
  11. ;; It could be better that we can reuse some parts of this module in a nodejs tool,
  12. ;; so users don't have to use the web for exporting to htmls or publishing.
  13. (defn- build-block
  14. [config block]
  15. (let [block (format-block/parse-title-and-body block)
  16. body (:block/body block)
  17. block (block/build-block-title config block)]
  18. [:div.block
  19. block
  20. (when (seq body)
  21. (for [child body]
  22. (block/markup-element-cp config child)))]))
  23. (defn export-page
  24. [page-name blocks show-notification!]
  25. (let [{:keys [slide]} (db/get-page-properties page-name)
  26. slide? slide
  27. blocks (if (:block/pre-block? (first blocks))
  28. (rest blocks)
  29. blocks)]
  30. (if (seq blocks)
  31. (let [config {:html-export? true :slide? slide?}
  32. hiccup [:div.page
  33. (for [block blocks]
  34. (build-block config block))]
  35. remove-attrs #{:on-click :on-change}
  36. hiccup (walk/postwalk (fn [f]
  37. (if (and (map? f)
  38. (seq (set/intersection remove-attrs (set (keys f)))))
  39. (medley/remove-keys remove-attrs f)
  40. f))
  41. hiccup)]
  42. (html hiccup))
  43. (show-notification! "The published content can't be empty." :error))))