網站設定

本頁內容

網站設定是網站的全域設定值:標題、標語、標誌、社群連結與顯示偏好。管理員透過管理介面進行維護,你在範本中存取這些設定。

查詢設定

使用 getSiteSettings() 取得所有網站設定:

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

const settings = await getSiteSettings();
---

<html lang="en">
  <head>
    <title>{settings.title}</title>
    {settings.favicon && (
      <link rel="icon" href={settings.favicon.url} />
    )}
  </head>
  <body>
    <header>
      {settings.logo ? (
        <img src={settings.logo.url} alt={settings.logo.alt || settings.title} />
      ) : (
        <span class="site-title">{settings.title}</span>
      )}
      {settings.tagline && <p class="tagline">{settings.tagline}</p>}
    </header>
    <slot />
  </body>
</html>

可用設定

EmDash 提供以下核心設定:

interface SiteSettings {
	// 識別
	title: string;
	tagline?: string;
	logo?: MediaReference;
	favicon?: MediaReference;

	// URL
	url?: string;

	// 顯示
	postsPerPage: number;
	dateFormat: string;
	timezone: string;

	// 社群
	social?: {
		twitter?: string;
		github?: string;
		facebook?: string;
		instagram?: string;
		linkedin?: string;
		youtube?: string;
	};
}

interface MediaReference {
	mediaId: string;
	alt?: string;
	url?: string; // 解析後的 URL(唯讀)
}

取得單一設定

使用 getSiteSetting() 依鍵值取得單一設定:

import { getSiteSetting } from "emdash";

const title = await getSiteSetting("title");
// 回傳:"My Site" 或 undefined

const logo = await getSiteSetting("logo");
// 回傳:{ mediaId: "...", url: "/_emdash/api/media/file/..." }

當你只需要一兩個值,不想取得全部設定時,這很實用。

在元件中使用設定

網站頁首

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

const settings = await getSiteSettings();
const menu = await getMenu("primary");
---

<header class="header">
  <a href="/" class="logo">
    {settings.logo ? (
      <img
        src={settings.logo.url}
        alt={settings.logo.alt || settings.title}
        width="150"
        height="50"
      />
    ) : (
      <span class="site-name">{settings.title}</span>
    )}
  </a>

  {menu && (
    <nav>
      {menu.items.map(item => (
        <a href={item.url}>{item.label}</a>
      ))}
    </nav>
  )}
</header>

社群連結

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

const social = await getSiteSetting("social");

const platforms = [
  { key: "twitter", label: "Twitter", baseUrl: "https://twitter.com/" },
  { key: "github", label: "GitHub", baseUrl: "https://github.com/" },
  { key: "facebook", label: "Facebook", baseUrl: "https://facebook.com/" },
  { key: "instagram", label: "Instagram", baseUrl: "https://instagram.com/" },
  { key: "linkedin", label: "LinkedIn", baseUrl: "https://linkedin.com/in/" },
  { key: "youtube", label: "YouTube", baseUrl: "https://youtube.com/@" },
] as const;
---

{social && (
  <div class="social-links">
    {platforms.map(({ key, label, baseUrl }) => (
      social[key] && (
        <a
          href={baseUrl + social[key]}
          rel="noopener noreferrer"
          target="_blank"
          aria-label={label}
        >
          {label}
        </a>
      )
    ))}
  </div>
)}

SEO 中繼標籤

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

interface Props {
  title?: string;
  description?: string;
  image?: string;
}

const settings = await getSiteSettings();
const {
  title,
  description = settings.tagline,
  image,
} = Astro.props;

const documentTitle = title
  ? `${title} | ${settings.title}`
  : settings.title;
const ogTitle = title ?? settings.title;
---

<title>{documentTitle}</title>
{description && <meta name="description" content={description} />}

<!-- Open Graph -->
<meta property="og:title" content={ogTitle} />
{description && <meta property="og:description" content={description} />}
{image && <meta property="og:image" content={image} />}
{settings.url && <meta property="og:url" content={settings.url + Astro.url.pathname} />}

<!-- Twitter -->
{settings.social?.twitter && (
  <meta name="twitter:site" content={settings.social.twitter} />
)}
<meta name="twitter:card" content={image ? "summary_large_image" : "summary"} />

日期格式

使用 dateFormattimezone 設定來統一日期顯示:

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

interface Props {
  date: string;
}

const { date } = Astro.props;
const dateFormat = await getSiteSetting("dateFormat") || "MMMM d, yyyy";
const timezone = await getSiteSetting("timezone") || "UTC";

const formatted = new Intl.DateTimeFormat("en-US", {
  timeZone: timezone,
  dateStyle: "long",
}).format(new Date(date));
---

<time datetime={date}>{formatted}</time>

管理 API

以程式方式取得設定:

GET /_emdash/api/settings

回應:

{
	"title": "My EmDash Site",
	"tagline": "A modern CMS",
	"logo": {
		"mediaId": "med_123",
		"url": "/_emdash/api/media/file/abc123"
	},
	"postsPerPage": 10,
	"dateFormat": "MMMM d, yyyy",
	"timezone": "America/New_York",
	"social": {
		"twitter": "@handle",
		"github": "username"
	}
}

更新設定(支援部分更新):

POST /_emdash/api/settings
Content-Type: application/json

{
  "title": "New Site Title",
  "tagline": "Updated tagline"
}

只有提供的欄位會被變更。省略的欄位保留目前的值。

媒體參考

logofavicon 設定儲存媒體參考。讀取時,EmDash 會自動解析 url 屬性:

const logo = await getSiteSetting("logo");
// {
//   mediaId: "med_123",
//   alt: "Site logo",
//   url: "/_emdash/api/media/file/abc123"
// }

透過 API 更新時,只需提供 mediaId

{
	"logo": {
		"mediaId": "med_456",
		"alt": "New logo"
	}
}

API 參考

getSiteSettings()

取得所有網站設定,包含已解析的媒體 URL。

回傳: Promise<Partial<SiteSettings>>

回傳部分物件。未設定的值為 undefined

getSiteSetting(key)

依鍵值取得單一設定。

參數:

  • key — 設定鍵值(例如 "title""logo""social"

回傳: Promise<SiteSettings[K] | undefined>

型別安全:回傳型別與你請求的鍵值相符。