Astro 開発者向け EmDash

このページ

EmDash は Astro 専用に作られた CMS であり、汎用ヘッドレス CMS の Astro アダプターではありません。データベース-backed のコンテンツ、洗練された管理 UI、WordPress 風の機能(メニュー、ウィジェット、タクソノミー)で Astro サイトを拡張しつつ、期待どおりの開発体験を保ちます。

Astro について知っていることはそのまま通用します。EmDash はサイトを強化するもので、ワークフローを置き換えるものではありません。

EmDash が足すもの

ファイルベースの Astro サイトに欠けがちなコンテンツ管理機能を EmDash が提供します。

機能説明
Admin UI/_emdash/admin のフル WYSIWYG 編集 UI
Database storageSQLite、libSQL、または Cloudflare D1 にコンテンツを保存
Media library画像とファイルのアップロード・整理・配信
Navigation menusドラッグ&ドロップとネストのあるメニュー管理
Widget areas動的サイドバーとフッター領域
Site settingsグローバル設定(タイトル、ロゴ、SNS リンク)
Taxonomiesカテゴリ、タグ、カスタムタクソノミー
Preview system下書き用の署名付きプレビュー URL
Revisionsコンテンツのバージョン履歴

Astro Collections と EmDash

Astro の astro:content コレクションはファイルベースでビルド時に解決されます。EmDash コレクションはデータベース-backed でランタイムに解決されます。

Astro CollectionsEmDash Collections
Storagesrc/content/ の Markdown/MDX ファイルSQLite/D1 データベース
EditingコードエディタAdmin UI
Content formatフロントマター付き MarkdownPortable Text(構造化 JSON)
Updates再ビルドが必要即時(SSR)
Schemacontent.config.ts の Zod管理画面で定義、DB に保存
Best for開発者が管理するコンテンツ編集者が管理するコンテンツ

両方を一緒に使う

Astro コレクションと EmDash は共存できます。開発者向けコンテンツ(ドキュメント、チェンジログ)は Astro、編集者向け(ブログ記事、ページ)は EmDash にします。

---
import { getCollection } from "astro:content";
import { getEmDashCollection } from "emdash";

// Developer-managed docs from files
const docs = await getCollection("docs");

// Editor-managed posts from database
const { entries: posts } = await getEmDashCollection("posts", {
  status: "published",
  limit: 5,
});
---

設定

EmDash には設定ファイルが 2 つ必要です。

Astro インテグレーション

import { defineConfig } from "astro/config";
import emdash, { local } from "emdash/astro";
import { sqlite } from "emdash/db";

export default defineConfig({
	output: "server", // Required for EmDash
	integrations: [
		emdash({
			database: sqlite({ url: "file:./data.db" }),
			storage: local({
				directory: "./uploads",
				baseUrl: "/_emdash/api/media/file",
			}),
		}),
	],
});

Live Collections Loader

import { defineLiveCollection } from "astro:content";
import { emdashLoader } from "emdash/runtime";

export const collections = {
	_emdash: defineLiveCollection({
		loader: emdashLoader(),
	}),
};

これで EmDash がライブコンテンツソースとして登録されます。_emdash コレクションは内部的にコンテンツタイプ(posts、pages、products など)へルーティングします。

コンテンツのクエリ

EmDash は Astro の live content collections パターンに沿ったクエリ関数を提供し、{ entries, error } または { entry, error } を返します。

EmDash

import { getEmDashCollection, getEmDashEntry } from "emdash";

// Get all published posts - returns { entries, error }
const { entries: posts } = await getEmDashCollection("posts", {
status: "published",
});

// Get a single post by slug - returns { entry, error, isPreview }
const { entry: post } = await getEmDashEntry("posts", "my-post");

Astro

import { getCollection, getEntry } from "astro:content";

// Get all blog entries
const posts = await getCollection("blog");

// Get a single entry by slug
const post = await getEntry("blog", "my-post");

フィルタオプション

getEmDashCollection は Astro の getCollection にないフィルタをサポートします。

const { entries: posts } = await getEmDashCollection("posts", {
	status: "published", // draft | published | archived
	limit: 10, // max results
	where: { category: "news" }, // taxonomy filter
});

コンテンツのレンダリング

EmDash はリッチテキストを構造化 JSON 形式の Portable Text として保存します。PortableText コンポーネントで描画します。

EmDash

---
import { getEmDashEntry } from "emdash";
import { PortableText } from "emdash/ui";

const { slug } = Astro.params;
const { entry: post } = await getEmDashEntry("posts", slug);

if (!post) {
return Astro.redirect("/404");
}

---

<article>
  <h1>{post.data.title}</h1>
  <PortableText value={post.data.content} />
</article>

Astro

---
import { getEntry, render } from "astro:content";

const { slug } = Astro.params;
const post = await getEntry("blog", slug);
const { Content } = await render(post);

---

<article>
  <h1>{post.data.title}</h1>
  <Content />
</article>

動的機能

EmDash は Astro のコンテンツ層にない WordPress 風機能の API を提供します。

ナビゲーションメニュー

---
import { getMenu } from "emdash";

const primaryMenu = await getMenu("primary");
---

{primaryMenu && (
  <nav>
    <ul>
      {primaryMenu.items.map(item => (
        <li>
          <a href={item.url}>{item.label}</a>
          {item.children.length > 0 && (
            <ul>
              {item.children.map(child => (
                <li><a href={child.url}>{child.label}</a></li>
              ))}
            </ul>
          )}
        </li>
      ))}
    </ul>
  </nav>
)}

ウィジェットエリア

---
import { getWidgetArea } from "emdash";
import { PortableText } from "emdash/ui";

const sidebar = await getWidgetArea("sidebar");
---

{sidebar && sidebar.widgets.length > 0 && (
  <aside>
    {sidebar.widgets.map(widget => (
      <div class="widget">
        {widget.title && <h3>{widget.title}</h3>}
        {widget.type === "content" && widget.content && (
          <PortableText value={widget.content} />
        )}
      </div>
    ))}
  </aside>
)}

サイト設定

---
import { getSiteSettings, getSiteSetting } from "emdash";

const settings = await getSiteSettings();
// Or fetch individual values:
const title = await getSiteSetting("title");
---

<header>
  {settings.logo ? (
    <img src={settings.logo.url} alt={settings.title} />
  ) : (
    <span>{settings.title}</span>
  )}
  {settings.tagline && <p>{settings.tagline}</p>}
</header>

プラグイン

フック、ストレージ、設定、管理 UI を追加するプラグインで EmDash を拡張します。

import emdash from "emdash/astro";
import seoPlugin from "@emdash-cms/plugin-seo";

export default defineConfig({
	integrations: [
		emdash({
			// ...
			plugins: [seoPlugin({ generateSitemap: true })],
		}),
	],
});

definePlugin でカスタムプラグインを作成します。

import { definePlugin } from "emdash";

export default definePlugin({
	id: "analytics",
	version: "1.0.0",
	capabilities: ["read:content"],

	hooks: {
		"content:afterSave": async (event, ctx) => {
			ctx.log.info("Content saved", { id: event.content.id });
		},
	},

	admin: {
		settingsSchema: {
			trackingId: { type: "string", label: "Tracking ID" },
		},
	},
});

サーバーレンダリング

EmDash サイトは SSR モードで動きます。コンテンツ変更はリビルドなしですぐ反映されます。

getStaticPaths 付きの静的ページでは、ビルド時にコンテンツを取得します。

---
import { getEmDashCollection, getEmDashEntry } from "emdash";

export async function getStaticPaths() {
  const { entries: posts } = await getEmDashCollection("posts", {
    status: "published",
  });

  return posts.map((post) => ({
    params: { slug: post.data.slug },
  }));
}

const { slug } = Astro.params;
const { entry: post } = await getEmDashEntry("posts", slug);
---

動的ページでは prerender = false にしてリクエストごとに取得します。

---
export const prerender = false;

import { getEmDashEntry } from "emdash";

const { slug } = Astro.params;
const { entry: post, error } = await getEmDashEntry("posts", slug);

if (error) {
  return new Response("Server error", { status: 500 });
}

if (!post) {
  return new Response(null, { status: 404 });
}
---

次のステップ