Fix issue of code highlight

pull/645/head
Tienson Qin 2020-04-08 13:57:53 +08:00
parent 160510a298
commit f6636b5805
13 changed files with 165 additions and 1873 deletions

1835
web/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -14,7 +14,7 @@
"dependencies": {
"browserfs": "^1.4.3",
"dev": "^0.1.3",
"isomorphic-git": "^1.1.2",
"isomorphic-git": "^1.3.1",
"mldoc_org": "^0.2.6",
"purgecss": "^2.1.0",
"react": "^16.12.0",

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,29 @@
(ns frontend.components.content
(:require [rum.core :as rum]
[frontend.format :as format]
[frontend.format.org-mode :as org]
[frontend.handler :as handler]
[frontend.util :as util]))
(defn- highlight!
[]
(doseq [block (-> (js/document.querySelectorAll "pre code")
(array-seq))]
(js/hljs.highlightBlock block)))
(rum/defc html <
{:did-mount (fn [state]
(highlight!)
(handler/render-local-images!)
state)
:did-update (fn [state]
(prn "did update")
(highlight!)
state)}
[content format]
(case format
(list :png :jpg :jpeg)
content
(util/raw-html (format/to-html content format
org/config-with-line-break))))

View File

@ -7,6 +7,7 @@
[frontend.components.sidebar :as sidebar]
[frontend.ui :as ui]
[frontend.format :as format]
[frontend.components.content :as content]
[goog.crypt.base64 :as b64]))
(defn- get-path
@ -17,29 +18,32 @@
[encoded-path decoded-path]))
(rum/defcs file <
{:did-mount (fn [state]
(doseq [block (-> (js/document.querySelectorAll "pre code")
(array-seq))]
(js/hljs.highlightBlock block))
state)}
[state content]
(let [content (db/get-file (last (get-path state)))
[encoded-path path] (get-path state)
suffix (last (string/split path #"\."))]
[state]
(let [[encoded-path path] (get-path state)
suffix (keyword (string/lower-case (last (string/split path #"\."))))]
(sidebar/sidebar
(if (and suffix (contains? #{"md" "markdown" "org"} suffix))
(cond
(and suffix (contains? #{:md :markdown :org} suffix))
[:div.content
[:a {:href (str "/file/" encoded-path "/edit")}
"edit"]
(cond
(string/blank? content)
[:span]
(let [content (db/get-file (last (get-path state)))]
(cond
(string/blank? content)
[:span]
content
(util/raw-html (format/to-html content suffix))
content
(content/html content suffix)
:else
"Loading ...")]
:else
"Loading ..."))]
;; image type
(and suffix (contains? #{:png :jpg :jpeg} suffix))
(content/html [:img.img-local {:id encoded-path}] suffix)
:else
[:div "File " suffix " is not supported."]))))
(defn- count-newlines

View File

@ -10,7 +10,8 @@
[frontend.state :as state]
[frontend.format.org-mode :as org]
[goog.object :as gobj]
[frontend.image :as image]))
[frontend.image :as image]
[frontend.components.content :as content]))
(def edit-content (atom ""))
(rum/defc editor-box <
@ -64,7 +65,10 @@
(defn- split-heading-body
[content]
(split-first #"\n" content))
(let [result (split-first #"\n" content)]
(if (= 1 (count result))
[result ""]
result)))
(rum/defc journal-cp < rum/reactive
[{:keys [uuid title content] :as journal}]
@ -81,8 +85,10 @@
(reset! edit-content content))
:style {:padding 8
:min-height 200}}
(util/raw-html (format/to-html content "org"
org/config-with-line-break))])]))
(if (or (not content)
(string/blank? content))
[:div]
(content/html content "org"))])]))
(rum/defcs journals < rum/reactive
{:will-mount (fn [state]

View File

@ -7,10 +7,10 @@
([content suffix]
(to-html content suffix nil))
([content suffix config]
(when-let [record (case suffix
"org"
(when-let [record (case (keyword suffix)
:org
(->OrgMode content)
(list "md" "markdown")
(list :md :markdown)
(->Markdown content)
nil)]
(if config

View File

@ -7,4 +7,7 @@
(defrecord Markdown [content]
protocol/Format
(toHtml [this]
(.makeHtml converter content))
(toHtml [this config]
;; TODO:
(.makeHtml converter content)))

View File

@ -15,6 +15,11 @@
(js/pfs.readFile (str dir "/" path)
(clj->js {:encoding "utf8"})))
(defn read-file-2
[dir path]
(js/pfs.readFile (str dir "/" path)
(clj->js {})))
(defn write-file
[dir path content]
(js/pfs.writeFile (str dir "/" path) content))
@ -33,3 +38,7 @@
(fn [error]
(write-file dir path initial-content)
false))))
(comment
(def dir "/notes")
)

View File

@ -10,6 +10,23 @@
{:username token
:password "x-oauth-basic"})
(defn set-username-email
[dir username email]
(prn {:dir dir
:username username
:email email})
(util/p-handle (js/git.config (clj->js
{:dir dir
:path "user.name"
:value username}))
(fn [result]
(js/git.config (clj->js
{:dir dir
:path "user.email"
:value email})))
(fn [error]
(prn "error:" error))))
(defn with-auth
[token m]
(clj->js

View File

@ -14,9 +14,11 @@
[reitit.frontend.easy :as rfe]
[goog.crypt.base64 :as b64]
[goog.object :as gobj]
[goog.dom :as gdom]
[rum.core :as rum]
[datascript.core :as d]
[frontend.utf8 :as utf8])
[frontend.utf8 :as utf8]
[frontend.image :as image])
(:import [goog.events EventHandler]))
;; We only support Github token now
@ -41,6 +43,7 @@
(fn [files]
(when (> (count files) 0)
(let [files (js->clj files)]
;; FIXME: don't load blobs
(if (contains? (set files) config/hidden-file)
(load-file repo-url config/hidden-file
(fn [patterns-content]
@ -75,6 +78,7 @@
(util/p-handle
(git/pull repo-url token)
(fn [result]
(prn "pull successfully!")
(get-latest-commit
(fn [commit]
(when (or (nil? @latest-commit)
@ -269,9 +273,22 @@
"DONE rollbacks to TODO."
content')))))))
(defn remove-non-text-files
[files]
(remove
(fn [file]
(not (contains?
#{"org"
"md"
"markdown"
"txt"}
(string/lower-case (last (string/split file #"\."))))))
files))
(defn load-all-contents!
[repo-url ok-handler]
(let [files (db/get-repo-files repo-url)]
(let [files (db/get-repo-files repo-url)
files (remove-non-text-files files)]
(-> (p/all (for [file files]
(load-file repo-url file
(fn [content]
@ -394,7 +411,8 @@
(defn periodically-pull-and-push
[repo-url]
(periodically-pull repo-url)
(periodically-push-tasks repo-url))
;; (periodically-push-tasks repo-url)
)
(defn set-state-kv!
[key value]
@ -436,6 +454,31 @@
(set-state-kv! :latest-journals (db/get-latest-journals {:content new-content}))
(alter-file path "Auto save" new-content false)))))
(defn render-local-images!
[]
(prn "re-render images"
(count (array-seq (gdom/getElementsByClass "img-local"))))
(doseq [img (array-seq (gdom/getElementsByClass "img-local"))]
(prn {:img img})
(let [id (gobj/get img "id")
path (b64/decodeString id)]
(util/p-handle
(fs/read-file-2 (git/get-repo-dir (db/get-current-repo))
path)
(fn [blob]
(let [blob (js/Blob. (array blob) (clj->js {:type "image/png"}))
img-url (image/create-object-url blob)
element (gdom/getElement id)]
(gobj/set element "src" img-url)))))))
;; FIXME:
(defn set-username-email
[]
(git/set-username-email
(git/get-repo-dir (db/get-current-repo))
"Tienson Qin"
"tiensonqin@gmail.com"))
(defn start!
[]
(db/restore!)
@ -445,5 +488,12 @@
(let [repos (db/get-repos)]
(doseq [repo repos]
(create-month-journal-if-not-exists repo)
;; (periodically-pull-and-push repo)
)))
(periodically-pull-and-push repo))))
(comment
(util/p-handle (fs/read-file (git/get-repo-dir (db/get-current-repo)) "test.org")
(fn [content]
(prn content)))
(pull (db/get-current-repo) (db/get-github-token))
)

View File

@ -65,6 +65,17 @@
(fn [orientation]
(fix-orientation img orientation cb max-width max-height))))
(defn create-object-url
[file]
(.createObjectURL (or (.-URL js/window)
(.-webkitURL js/window))
file))
;; (defn build-image
;; []
;; (let [img (js/Image.)]
;; ))
(defn upload
[files file-cb & {:keys [max-width max-height]
:or {max-width 1920
@ -87,6 +98,4 @@
max-width
max-height)))
(set! (.-src img)
(.createObjectURL (or (.-URL js/window)
(.-webkitURL js/window))
file)))))))
(create-object-url file)))))))

View File

@ -784,10 +784,10 @@ isexe@^2.0.0:
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
integrity sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=
isomorphic-git@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/isomorphic-git/-/isomorphic-git-1.1.2.tgz#1c0257f3b7e2371df8b34296035379b3ef549547"
integrity sha512-qXTN0E1S75ZojXwQlT6vQqRwxxyd2kXkQAvMklZYJJB5Ud9ZcN7IsXo9dazrI09f5MPSWKTPLYBahhUCyY0mkw==
isomorphic-git@^1.3.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/isomorphic-git/-/isomorphic-git-1.3.1.tgz#6f18f7cb86df6a968c3ce3b518b62aea39f678aa"
integrity sha512-VmWXdKNCJ/K+UH3S8OUqcJy6rsh8fWPvFhrf7/R45+PA7l8VbJoq4GC/CLxeogkoljhKkThwm0BpDnerSNc1tw==
dependencies:
async-lock "^1.1.0"
clean-git-ref "^2.0.1"