EmDashのネイティブプラグインでPortable Text(/メニュー)に独自ブロックを追加する方法

EmDashのリッチテキストエディタ(/メニュー)に独自の入力フォーム付きブロックを追加するネイティブプラグインの作成手順を、書評用「書籍情報」ブロックの実装例とともに解説します。

EmDashのネイティブプラグインによるPortable Textカスタムブロック追加のイメージイラスト
目次

    EmDash で記事を書く際、標準の「見出し」「引用」「コードブロック」などに加えて、「書籍情報カード」のような独自ブロックを本文エディタの /(スラッシュ)メニューから挿入したい場面があります。

    管理画面の「コンテンツタイプ > ブロックタイプ」は別枠のブロックフィールド向けであり、本文(Portable Text)の / メニューへ独自ブロックを追加するには、ネイティブプラグイン(format: "native")として実装する必要があります。

    この記事では、書評用の「書籍情報ブロック(book)」を例に、EmDash へネイティブプラグインを追加する手順をまとめます。

    1. ネイティブプラグインの構成

    サイト側の Astro コンポーネントとエディタ側の入力フォームを結びつけるため、ローカルプラグイン一式を src/plugins/blocks/ に配置します。

    • src/components/BookBlock.astro: 公開ページでの表示用コンポーネント
    • src/plugins/blocks/registry.ts: 管理画面エディタ用のブロック・フォーム定義(admin.portableTextBlocks)
    • src/plugins/blocks/components.ts: _type と Astro コンポーネントの対応表(blockComponents)
    • src/plugins/blocks/index.ts: astro.config.mjs に渡すプラグイン記述子(PluginDescriptor)

    2. 管理画面の入力フォームを定義する(registry.ts)

    definePlugin() の admin.portableTextBlocks にブロックを定義します。fields に Block Kit 要素を並べることで、/ メニューから選んだときにモーダルフォームが開くようになります。

    TYPESCRIPT
    
    			
    			import { definePlugin } from "emdash";
    		
    			
    			 
    		
    			
    			export function createPlugin() {
    		
    			
    			  return definePlugin({
    		
    			
    			    id: "weblabo-blocks",
    		
    			
    			    version: "0.1.0",
    		
    			
    			    admin: {
    		
    			
    			      portableTextBlocks: [
    		
    			
    			        {
    		
    			
    			          type: "book",
    		
    			
    			          label: "書籍情報(技術書・書評用)",
    		
    			
    			          description: "技術書や関連書籍の書評末尾に、書籍情報と購入・公式リンクを表示します",
    		
    			
    			          icon: "link-external",
    		
    			
    			          fields: [
    		
    			
    			            { type: "text_input", action_id: "title", label: "書籍タイトル" },
    		
    			
    			            { type: "text_input", action_id: "author", label: "著者・訳者" },
    		
    			
    			            { type: "text_input", action_id: "publisher", label: "出版社・出版年" },
    		
    			
    			            { type: "media_picker", action_id: "cover", label: "表紙画像" },
    		
    			
    			            { type: "text_input", action_id: "summary", label: "対象読者・おすすめポイント", multiline: true },
    		
    			
    			            { type: "text_input", action_id: "amazon_url", label: "Amazon URL" },
    		
    			
    			            { type: "text_input", action_id: "official_url", label: "出版社・公式 / サポートページ URL" },
    		
    			
    			          ],
    		
    			
    			        },
    		
    			
    			      ],
    		
    			
    			    },
    		
    			
    			  });
    		
    			
    			}
    		
    			
    			 
    		
    			
    			export default createPlugin;
    		

    各フィールドの action_id がそのまま Portable Text ブロックのデータキーになり、公開側のコンポーネントへ Astro.props.node として渡されます。

    3. 表示用コンポーネントをエクスポートする(components.ts)

    公開ページ側の <PortableText> が自動で読み込めるよう、blockComponents という名前でコンポーネントのマップをエクスポートします。

    TYPESCRIPT
    
    			
    			import BookBlock from "../../components/BookBlock.astro";
    		
    			
    			 
    		
    			
    			export const blockComponents = {
    		
    			
    			  book: BookBlock,
    		
    			
    			};
    		

    BookBlock.astro 側では const { node } = Astro.props; で受け取り、表紙画像・タイトル・メタ情報・外部リンクボタン(target="_blank" rel="noopener noreferrer")を描画します。

    4. プラグイン記述子と astro.config.mjs への登録

    componentsEntry を使うプラグインは format: "native" が必須です。また、EmDash の仮想モジュール(virtual:emdash/block-components)から解決できるよう、パスを絶対パス(POSIX 形式)で渡します。

    TYPESCRIPT
    
    			
    			// src/plugins/blocks/index.ts
    		
    			
    			import { fileURLToPath } from "node:url";
    		
    			
    			import type { PluginDescriptor } from "emdash";
    		
    			
    			 
    		
    			
    			const toPosixPath = (url: URL) => fileURLToPath(url).replace(/\\/g, "/");
    		
    			
    			 
    		
    			
    			export function weblaboBlocksPlugin(): PluginDescriptor {
    		
    			
    			  return {
    		
    			
    			    id: "weblabo-blocks",
    		
    			
    			    version: "0.1.0",
    		
    			
    			    format: "native",
    		
    			
    			    entrypoint: toPosixPath(new URL("./registry.ts", import.meta.url)),
    		
    			
    			    componentsEntry: toPosixPath(new URL("./components.ts", import.meta.url)),
    		
    			
    			  };
    		
    			
    			}
    		

    あとは astro.config.mjs の emdash({ ... }) に plugins: [weblaboBlocksPlugin()] を追加してビルド・デプロイすれば完了です(sandboxed: [] ではなく plugins: [] に渡します)。

    まとめ

    ネイティブプラグインで admin.portableTextBlocks と componentsEntry を定義しておくと、WordPress のブロックエディターと同じ感覚で / メニューから独自ブロックを呼び出し、フォーム入力と記事内での再編集ができるようになります。

    この記事をシェア
    Xでシェア