浏览代码

improve(plugin): add katex hooks

charlie 3 年之前
父节点
当前提交
a0e494f7c7

+ 9 - 1
libs/src/LSPlugin.ts

@@ -278,7 +278,7 @@ export type UserProxyTags = 'app' | 'editor' | 'db' | 'git' | 'ui' | 'assets'
  */
 export interface IAppProxy {
   /**
-   * @add 0.0.4
+   * @added 0.0.4
    * @param key
    */
   getInfo: (key?: keyof AppInfo) => Promise<AppInfo | any>
@@ -653,6 +653,8 @@ export interface IDBProxy {
 
   /**
    * Hook all transaction data of DB
+   *
+   * @added 0.0.2
    */
   onChanged: IUserHook<{
     blocks: Array<BlockEntity>
@@ -662,6 +664,8 @@ export interface IDBProxy {
 
   /**
    * Subscribe a specific block changed event
+   *
+   * @added 0.0.2
    */
   onBlockChanged(
     uuid: BlockUUID,
@@ -711,6 +715,10 @@ export interface IUIProxy {
  * Assets related APIs
  */
 export interface IAssetsProxy {
+  /**
+   * @added 0.0.2
+   * @param exts
+   */
   listFilesOfCurrentGraph(
     exts: string | string[]
   ): Promise<{

+ 13 - 1
libs/src/modules/LSPlugin.Experiments.ts

@@ -3,7 +3,9 @@ import { PluginLocal } from '../LSPlugin.core'
 import { safeSnakeCase } from '../helpers'
 
 /**
- * Some experiment features
+ * WARN: These are some experience features and may be adjusted at any time.
+ * These unofficial plugins that use these APIs are temporarily
+ * not supported on the Marketplace.
  */
 export class LSPluginExperiments {
   constructor(private ctx: LSPluginUser) {}
@@ -57,6 +59,16 @@ export class LSPluginExperiments {
     )
   }
 
+  registerExtensionsEnhancer<T = any>(
+    type: 'katex',
+    enhancer: (v: T) => Promise<any>
+  ) {
+    return this.ensureHostScope().logseq.api.exper_register_extensions_enhancer(
+      this.ctx.baseInfo.id,
+      type, enhancer
+    )
+  }
+
   ensureHostScope(): any {
     if (window === top) {
       throw new Error('Can not access host scope!')

+ 10 - 3
src/main/frontend/extensions/latex.cljs

@@ -4,6 +4,8 @@
             [frontend.ui :as ui]
             [frontend.config :as config]
             [frontend.util :as util]
+            [frontend.handler.plugin :refer [lsp-enabled? hook-extensions-enhancer-by-type] :as plugin-handler]
+            [promesa.core :as p]
             [goog.dom :as gdom]))
 
 ;; TODO: extracted to a rum mixin
@@ -37,9 +39,14 @@
          (loader/load
           (config/asset-uri "/static/js/mhchem.min.js")
           (fn []
-            (reset! *loading? false)
-            (render! state)))))))
-  state)
+            (p/finally
+              (p/all (when-let [enhancers (and lsp-enabled? (seq (hook-extensions-enhancer-by-type :katex)))]
+                       (for [f enhancers]
+                         (when (fn? f) (f js/window.katex)))))
+              (fn []
+                (reset! *loading? false)
+                (render! state))))))
+       state))))
 
 (rum/defc latex < rum/reactive
   {:did-mount (fn [state]

+ 17 - 1
src/main/frontend/handler/plugin.cljs

@@ -330,7 +330,7 @@
 
 (def *fenced-code-providers (atom #{}))
 
-(defn register_fenced_code_renderer
+(defn register-fenced-code-renderer
   [pid type {:keys [before subs render edit] :as _opts}]
   (when-let [key (and type (keyword type))]
     (register-plugin-resources pid :fenced-code-renderers
@@ -344,6 +344,22 @@
     (first (map #(state/get-plugin-resource % :fenced-code-renderers key)
                 @*fenced-code-providers))))
 
+(def *extensions-enhancer-providers (atom #{}))
+
+(defn register-extensions-enhancer
+  [pid type {:keys [enhancer] :as _opts}]
+  (when-let [key (and type (keyword type))]
+    (register-plugin-resources pid :extensions-enhancers
+       {:key key :enhancer enhancer})
+    (swap! *extensions-enhancer-providers conj pid)
+    #(swap! *extensions-enhancer-providers disj pid)))
+
+(defn hook-extensions-enhancer-by-type
+  [type]
+  (when-let [key (and type (keyword type))]
+    (map #(state/get-plugin-resource % :extensions-enhancers key)
+         @*extensions-enhancer-providers)))
+
 (defn select-a-plugin-theme
   [pid]
   (when-let [themes (get (group-by :pid (:plugin/installed-themes @state/state)) pid)]

+ 6 - 0
src/main/logseq/api.cljs

@@ -787,6 +787,12 @@
       (keyword pid) type (reduce #(assoc %1 %2 (aget opts (name %2))) {}
                                  [:edit :before :subs :render]))))
 
+(defn ^:export exper_register_extensions_enhancer
+  [pid type enhancer]
+  (when-let [^js _pl (and (fn? enhancer) (plugin-handler/get-plugin-inst pid))]
+    (plugin-handler/register-extensions-enhancer
+      (keyword pid) type {:enhancer enhancer})))
+
 ;; helpers
 (defn ^:export query_element_by_id
   [id]