diff --git a/denops/bufpreview/@renderer/markdown/main.html b/denops/bufpreview/@renderers/markdown/main.html similarity index 100% rename from denops/bufpreview/@renderer/markdown/main.html rename to denops/bufpreview/@renderers/markdown/main.html diff --git a/denops/bufpreview/@renderer/markdown/main.ts b/denops/bufpreview/@renderers/markdown/main.ts similarity index 100% rename from denops/bufpreview/@renderer/markdown/main.ts rename to denops/bufpreview/@renderers/markdown/main.ts diff --git a/denops/bufpreview/lib/deps.ts b/denops/bufpreview/lib/deps.ts index 1aa4e85..cb2d414 100644 --- a/denops/bufpreview/lib/deps.ts +++ b/denops/bufpreview/lib/deps.ts @@ -8,5 +8,6 @@ export * as anonymous from "https://deno.land/x/denops_std@v2.0.0/anonymous/mod. export { open } from "https://deno.land/x/open@v0.0.2/index.ts"; export { v4 } from "https://deno.land/std@0.109.0/uuid/mod.ts"; export { EventEmitter } from "https://deno.land/x/eventemitter@1.2.1/mod.ts"; +export { basename, join } from "https://deno.land/std@0.115.1/path/mod.ts"; export { Renderer } from "./types.ts"; diff --git a/denops/bufpreview/main.ts b/denops/bufpreview/main.ts index 13d5765..5917c9a 100644 --- a/denops/bufpreview/main.ts +++ b/denops/bufpreview/main.ts @@ -6,6 +6,8 @@ import { open, Renderer, vars, + basename, + join } from "./lib/deps.ts"; import Server from "./lib/server.ts"; @@ -13,13 +15,43 @@ import Server from "./lib/server.ts"; // 一度に開けるサーバーは一つ let server: Server | undefined; -const Markdown = - (await import( - (new URL("./@renderer/markdown/main.ts", import.meta.url)).href - )).default; +// レンダラー +interface Renderers { + [key: string]: Renderer; +} +let renderers: Renderers +// renderers.push( +// (await import( +// (new URL("./@renderers/markdown/main.ts", import.meta.url)).href +// )).default, +// ); + +export async function main(denops: Denops) { + // 全てのレンダラーを探索 + async function findRenderer(): Promise { + const runtimepath = (await op.runtimepath.getGlobal(denops)).split(","); + const paths: string[] = [] + const ret: Renderers = {} + // さがす + for (const path of runtimepath) { + const i = await fn.globpath(denops, path, "denops/bufpreview/@renderers/*", true, true) as string[] + paths.push(...i) + } + // よみこむ + // TODO: ここ非同期で読み込む + for (const path of paths) { + const name = basename(path) + const codePath = join(path, "main.ts") + ret[name] = (await import(codePath)).default + } + return ret + } + + // test + renderers = await findRenderer() -export function main(denops: Denops) { denops.dispatcher = { + // Vimから呼ばれる async md(arg: unknown): Promise { ensureString(arg); @@ -40,8 +72,8 @@ export function main(denops: Denops) { // サーバーを開く const openServer = async () => { - // レンダラー - const renderer = new Markdown(denops); + // @ts-ignore: これ型エラーにならない方法知っている方教えて + const renderer = new renderers["markdown"](denops) // サーバーが既に開かれているなら if (server != undefined) { server.close();