html_export.cljs 2.4 KB

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