소스 검색

fix: fragment links especially the footnotes

Tienson Qin 4 년 전
부모
커밋
575674919c

+ 4 - 4
src/main/frontend/components/block.cljs

@@ -628,12 +628,12 @@
           (image-link config url s label metadata full_text)
 
           (= \# (first s))
-          (->elem :a {:href (str "#" (mldoc/anchorLink (subs s 1)))} (map-inline config label))
+          (->elem :a {:on-click #(route-handler/jump-to-anchor! (mldoc/anchorLink (subs s 1)))} (subs s 1))
 
           ;; FIXME: same headline, see more https://orgmode.org/manual/Internal-Links.html
           (and (= \* (first s))
                (not= \* (last s)))
-          (->elem :a {:href (str "#" (mldoc/anchorLink (subs s 1)))} (map-inline config label))
+          (->elem :a {:on-click #(route-handler/jump-to-anchor! (mldoc/anchorLink (subs s 1)))} (subs s 1))
 
           (re-find #"(?i)^http[s]?://" s)
           (->elem :a {:href s}
@@ -751,7 +751,7 @@
       [:sup.fn
        [:a {:id (str "fnr." encode-name)
             :class "footref"
-            :href (str "#fn." encode-name)}
+            :on-click #(route-handler/jump-to-anchor! (str "fn." encode-name))}
         name]])
 
     ["Macro" options]
@@ -1934,7 +1934,7 @@
            [:a.ml-1 {:id (str "fn." id)
                      :style {:font-size 14}
                      :class "footnum"
-                     :href (str "#fnr." id)}
+                     :on-click #(route-handler/jump-to-anchor! (str "fnr." id))}
             [:sup.fn (str name "↩︎")]])]])
 
       :else

+ 4 - 3
src/main/frontend/components/search.cljs

@@ -129,9 +129,10 @@
 
                         :block
                         (let [block-uuid (uuid (:block/uuid data))
-                              page (:page/name (:block/page (db/entity [:block/uuid block-uuid])))
-                              path (str "/page/" (util/encode-str page) "#ls-block-" (:block/uuid data))]
-                          (route/redirect-with-fragment! path))
+                              page (:page/name (:block/page (db/entity [:block/uuid block-uuid])))]
+                          (route/redirect! {:to :page
+                                            :path-params {:name page}
+                                            :query-params {:anchor (str "ls-block-" (:block/uuid data))}}))
                         nil))
          :on-shift-chosen (fn [{:keys [type data]}]
                             (case type

+ 0 - 4
src/main/frontend/config.cljs

@@ -26,10 +26,6 @@
 (def asset-domain (env/get-static :asset-domain))
 (def github-app-name (env/get-static :github-app-name))
 
-;; change if you want to use your own domain instead of default asset.logseq.com
-(def asset-domain (util/format "https://asset.%s.com"
-                               app-name))
-
 (defn asset-uri
   [path]
   (cond

+ 1 - 1
src/main/frontend/core.cljs

@@ -13,7 +13,7 @@
 (defn set-router!
   []
   (rfe/start!
-   (rf/router routes/routes {})
+   (rf/router routes/routes nil)
    route/set-route-match!
    ;; set to false to enable HistoryAPI
    {:use-fragment true}))

+ 10 - 11
src/main/frontend/handler/route.cljs

@@ -23,11 +23,6 @@
   []
   (redirect! {:to :home}))
 
-(defn redirect-with-fragment!
-  [path]
-  (.pushState js/window.history nil "" path)
-  (rfh/-on-navigate @rfe/history path))
-
 (defn get-title
   [name path-params]
   (case name
@@ -78,14 +73,18 @@
 
 (defn set-route-match!
   [route]
-  (swap! state/state assoc :route-match route)
-  (let [{:keys [data path-params]} route
-        title (get-title (:name data) path-params)]
-    (util/set-title! title)
-    (if-let [fragment (util/get-fragment)]
-      (ui-handler/highlight-element! fragment)
+  (let [route route]
+    (swap! state/state assoc :route-match route)
+    (let [{:keys [data path-params]} route
+          title (get-title (:name data) path-params)]
+      (util/set-title! title)
       (util/scroll-to-top))))
 
+(defn jump-to-anchor!
+  [anchor-text]
+  (when anchor-text
+    (ui-handler/highlight-element! anchor-text)))
+
 (defn go-to-search!
   []
   (when-let [element (gdom/getElement "search_field")]

+ 1 - 1
src/main/frontend/page.cljs

@@ -49,4 +49,4 @@
         ;;           :timeout {:enter 300
         ;;                     :exit 200}}
         ;;          (route-view view route-match)))))))
-         [:div "404 Page"])))))
+         )))))

+ 1 - 0
src/main/frontend/routes.cljs

@@ -10,6 +10,7 @@
             [frontend.components.external :as external]
             [frontend.components.publishing :as publishing]))
 
+;; http://localhost:3000/#?anchor=fn.1
 (def routes
   [["/"
     {:name :home

+ 6 - 0
src/main/frontend/util.cljc

@@ -341,6 +341,12 @@
               (string/split #"\?")
               (first))))))
 
+#?(:cljs
+   (defn fragment-with-anchor
+     [anchor]
+     (let [fragment (get-fragment)]
+       (str "#" fragment "?anchor=" anchor))))
+
 ;; (defn scroll-into-view
 ;;   [element]
 ;;   (let [scroll-top (gobj/get element "offsetTop")