This commit is contained in:
Tienson Qin
2020-05-07 11:42:40 +08:00
parent 8366359c52
commit 85cc9e03d6
12 changed files with 180 additions and 78 deletions

View File

@@ -11,8 +11,14 @@
(when (loaded?)
(.makeHtml (js/window.showdown.Converter.) content)))
(defn ->edn
[content config]
nil)
(defrecord AdocMode []
protocol/Format
(toEdn [this content config]
(->edn content config))
(toHtml [this content config]
(when (loaded?)
(let [config {:attributes {:showTitle false

View File

@@ -1,7 +1,12 @@
(ns frontend.format.org.block
(ns frontend.format.block
(:require [frontend.util :as util]
[clojure.walk :as walk]
[clojure.string :as string]))
[clojure.string :as string]
[frontend.format :as format]
[frontend.utf8 :as utf8]
[medley.core :as medley]
[datascript.core :as d]
[clojure.set :as set]))
(defn heading-block?
[block]
@@ -66,14 +71,26 @@
[{:keys [title children] :as heading}]
(let [ref-pages (atom [])]
(walk/postwalk
(fn [form]
(when (page-reference-block? form)
(let [page (second (:url (second form)))]
(swap! ref-pages conj (string/capitalize page))))
form)
(concat title children))
(fn [form]
(when (page-reference-block? form)
(let [page (second (:url (second form)))]
(swap! ref-pages conj (string/capitalize page))))
form)
(concat title children))
(assoc heading :ref-pages (vec @ref-pages))))
(defn safe-headings
[headings]
(mapv (fn [heading]
(let [heading (util/remove-nils heading)
heading (if (:heading/uuid heading)
heading
(assoc heading :heading/uuid (d/squuid)))]
(medley/map-keys
(fn [k] (keyword "heading" k))
heading)))
headings))
;; TODO create a dummy heading if no headings exists
(defn extract-headings
[blocks last-pos]
@@ -130,3 +147,43 @@
:else
(> m1 m2))))
headings)))
(defn parse-heading
[{:heading/keys [uuid content meta file page] :as heading} format]
(let [ast (format/to-edn content format nil)
start-pos (:pos meta)
encoded-content (utf8/encode content)
content-length (utf8/length encoded-content)
headings (extract-headings ast content-length)
headings (safe-headings headings)
ref-pages-atom (atom [])
headings (doall
(map-indexed
(fn [idx {:heading/keys [ref-pages meta] :as heading}]
(let [heading (merge
heading
{:heading/file file
:heading/page page
:heading/content (utf8/substring encoded-content
(:pos meta)
(:end-pos meta))}
(when (zero? idx)
{:heading/uuid uuid})
(when (seq ref-pages)
{:heading/ref-pages
(mapv
(fn [page]
(let [page-name (string/capitalize page)
page {:page/name page-name}]
(swap! ref-pages-atom conj page)
page))
ref-pages)}))]
(-> heading
(assoc-in [:heading/meta :pos] (+ (:pos meta) start-pos))
(assoc-in [:heading/meta :end-pos] (+ (:end-pos meta) start-pos)))))
headings))
pages (vec (distinct @ref-pages-atom))]
{:headings headings
:pages pages
:start-pos start-pos
:end-pos (+ start-pos content-length)}))

View File

@@ -14,8 +14,14 @@
:strikethrough true
:simplifiedAutoLink true})
(defn ->edn
[content config]
nil)
(defrecord MdMode []
protocol/Format
(toEdn [this content config]
(->edn content config))
(toHtml [this content config]
(when (loaded?)
(.makeHtml (js/window.showdown.Converter. (bean/->js (or config default-config))) content)))

View File

@@ -27,9 +27,9 @@
(when (loaded?)
(.parseJson js/window.MldocOrg content config))))
(defn ->clj
(defn ->edn
([content]
(->clj content default-config))
(->edn content default-config))
([content config]
(if (string/blank? content)
{}
@@ -39,6 +39,8 @@
(defrecord OrgMode []
protocol/Format
(toEdn [this content config]
(->edn content config))
(toHtml [this content config]
(.parseHtml js/window.MldocOrg content config))
(loaded? [this]

View File

@@ -1,6 +1,7 @@
(ns frontend.format.protocol)
(defprotocol Format
(toEdn [this content config])
(toHtml [this content config])
(loaded? [this])
(lazyLoad [this ok-handler]))