站点设置

本页内容

站点设置是网站的全局配置值:标题、标语、logo、社交链接和显示偏好。管理员通过管理界面管理这些设置,您可以在模板中访问它们。

查询设置

使用 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/..." }

当您只需要一两个值并希望避免获取所有内容时,这很有用。

在组件中使用设置

站点头部

以下组件渲染一个包含站点 logo 和主菜单的头部:

---
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 元标签

以下组件从站点设置构建文档和 Open Graph 元标签:

---
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";

// 使用 Intl.DateTimeFormat 或 date-fns 等库进行格式化
const formatted = new Intl.DateTimeFormat("en-US", {
  timeZone: timezone,
  dateStyle: "long",
}).format(new Date(date));
---

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

管理 API

通过 GET 请求以编程方式获取设置:

GET /_emdash/api/settings

响应是设置的 JSON 对象:

{
	"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>

类型安全:返回类型与您请求的键匹配。