enhance: add image metadata support

Resolved #1112
pull/1117/head
Tienson Qin 2021-01-14 21:53:32 +08:00
parent 3238001ef1
commit 4ea98fe42c
3 changed files with 43 additions and 33 deletions

View File

@ -55,7 +55,7 @@
"gulp-cached": "^1.1.1",
"ignore": "^5.1.8",
"jszip": "^3.5.0",
"mldoc": "^0.3.0",
"mldoc": "^0.3.3",
"mousetrap": "^1.6.5",
"react": "^17.0.1",
"react-dom": "^17.0.1",

View File

@ -50,14 +50,17 @@
[frontend.context.i18n :as i18n]))
(defn safe-read-string
[s]
(try
(reader/read-string s)
(catch js/Error e
(println "read-string error:")
(js/console.error e)
[:div.warning {:title "read-string failed"}
s])))
([s]
(safe-read-string s true))
([s warn?]
(try
(reader/read-string s)
(catch js/Error e
(println "read-string error:")
(js/console.error e)
(when warn?
[:div.warning {:title "read-string failed"}
s])))))
;; local state
(defonce *block-children
@ -158,7 +161,7 @@
(rum/defcs asset-link < rum/reactive
(rum/local nil ::src)
[state href label]
[state href label metadata]
(let [title (second (first label))
src (::src state)
granted? (state/sub [:nfs/user-granted? (state/get-current-repo)])]
@ -168,24 +171,31 @@
(when @src
[:img
{:loading "lazy"
:src @src
:title title}])))
(merge
{:loading "lazy"
:src @src
:title title}
metadata)])))
;; TODO: safe encoding asciis
;; TODO: image link to another link
(defn image-link [config url href label]
(if (or (util/starts-with? href "/assets")
(util/starts-with? href "../assets"))
(asset-link href label)
(let [href (if (util/starts-with? href "http")
href
(get-file-absolute-path config href))]
[:img.rounded-sm.shadow-xl
{:loading "lazy"
;; :on-error (fn [])
:src href
:title (second (first label))}])))
(defn image-link [config url href label metadata full_text]
(let [metadata (if (string/blank? metadata)
nil
(safe-read-string metadata))]
(if (or (util/starts-with? href "/assets")
(util/starts-with? href "../assets"))
(asset-link href label metadata)
(let [href (if (util/starts-with? href "http")
href
(get-file-absolute-path config href))]
[:img.rounded-sm.shadow-xl
(merge
{:loading "lazy"
;; :on-error (fn [])
:src href
:title (second (first label))}
metadata)]))))
(defn repetition-to-string
[[[kind] [duration] n]]
@ -550,14 +560,14 @@
(nested-link config html-export? link)
["Link" link]
(let [{:keys [url label title]} link
(let [{:keys [url label title metadata full_text]} link
img-formats (set (map name (config/img-formats)))]
(match url
["Search" s]
(cond
;; image
(some (fn [fmt] (re-find (re-pattern (str "(?i)\\." fmt)) s)) img-formats)
(image-link config url s label)
(image-link config url s label metadata full_text)
(= \# (first s))
(->elem :a {:href (str "#" (mldoc/anchorLink (subs s 1)))} (map-inline config label))
@ -589,7 +599,7 @@
(= protocol "file")
(if (some (fn [fmt] (re-find (re-pattern (str "(?i)\\." fmt)) href)) img-formats)
(image-link config url href label)
(image-link config url href label metadata full_text)
(let [label-text (get-label-text label)
page (if (string/blank? label-text)
{:page/name (db/get-file-page (string/replace href "file:" ""))}
@ -612,7 +622,7 @@
;; image
(some (fn [fmt] (re-find (re-pattern (str "(?i)\\." fmt)) href)) img-formats)
(image-link config url href label)
(image-link config url href label metadata full_text)
:else
(->elem

View File

@ -3609,10 +3609,10 @@ mkdirp@~0.5.1:
dependencies:
minimist "^1.2.5"
mldoc@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/mldoc/-/mldoc-0.3.0.tgz#4bf24cc6e64c44a2de7b6d9e315a8a606347b517"
integrity sha512-+HGWz74rbD0QbZ43lwv5/oX6a1NtMP0q52z753Be3OskMgZR2Iv4kbI123qVYHRAdesG6U/UQRFD/UHyoVrm8w==
mldoc@^0.3.3:
version "0.3.3"
resolved "https://registry.yarnpkg.com/mldoc/-/mldoc-0.3.3.tgz#e02b97d82f0907432d8db10e958d7f02a9a806ff"
integrity sha512-NdksPLmtW8fe6fG/54Wy7WCaDWEpGrxgi6jsAci/CdnuTvWZKwmSYajoTVBBzyN9RmvY+6Sib3mbxorfxEvhSQ==
dependencies:
yargs "^12.0.2"