Communitygithub.com

ahmadasrizalmi/hermes-skills

使用 Astro 5 建立靜態或 SSR 網站。涵蓋專案設定、佈局、頁面、整合套件(@astrojs/sitemap、@astrojs/mdx)、SEO(meta、OG、JSON-LD、sitemap、robots.txt)、電子書/內容佈局(含側邊欄導航與閱讀進度條)、Cloudflare Pages 部署,以及常見陷阱(JSX 大括號解析、lightningcss、匯入路徑)。

hermes-skills 是什麼?

此技能專為 AI 代理(如 Claude Code)設計,用於快速建立基於 Astro 5 的生產級靜態或 SSR 網站。它提供從專案初始化到部署的完整指引,特別適合用於建置作品集、部落格、行銷網站、電子書平台或文件網站。技能詳細說明了專案結構、Astro 配置(已針對 Cloudflare Pages 優化)、全面的 SEO 模式(包含 Open Graph、Twitter Card 和 JSON-LD 結構化資料),以及一個功能豐富的 `EbookLayout`,該佈局具備固定側邊欄導航、閱讀進度條、程式碼區塊、提示框和多種回應式中斷點。此外,還涵蓋了 Cloudflare Pages 的部署設定(建置指令、安全標頭、重新導向)以及使用 Wrangler CLI 進行直接上傳。最後,此技能明確列出常見的陷阱與反模式,例如 JSX 中大括號的處理、Astro 作用域 CSS 的運作方式,以及獨立 HTML 檔案的整合要點,幫助開發者避免常見錯誤。

相容平台✓Claude Code~Codex CLI~Cursor
npx skills add https://github.com/ahmadasrizalmi/hermes-skills/tree/HEAD/astro-site

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

Astro 網站建置工具

使用 Astro 5 建立可投入生產的靜態網站。涵蓋作品集、電子書平台、部落格、行銷網站和文件網站。

使用時機

  • 使用者要求建立 Astro 網站、作品集或部落格
  • 使用者想要部署到 Cloudflare Pages
  • 使用者需要具備側邊欄導航的電子書/文件網站
  • 使用者想要 SEO 最佳化的靜態網站,包含 sitemap、meta 標籤、結構化資料
  • 使用者提到「Astro」、「Cloudflare Pages」、「JAMstack 靜態網站」

快速開始

# 建立專案
npm create astro@latest my-site -- --template minimal
cd my-site

# 核心相依套件
npm install @astrojs/sitemap

# 建置與預覽
npm run build    # 輸出至 dist/
npm run preview  # 本地預覽伺服器

專案結構

project/
├── astro.config.mjs
├── package.json
├── tsconfig.json
├── public/
│   ├── robots.txt
│   └── _headers          # Cloudflare 安全標頭
├── src/
│   ├── layouts/
│   │   ├── BaseLayout.astro    # 網站整體框架(頁首、頁尾、SEO)
│   │   └── EbookLayout.astro   # 內容佈局(側邊欄、進度條)
│   └── pages/
│       ├── index.astro         # 首頁
│       ├── blog.astro
│       └── ebook/
│           ├── index.astro     # 電子書列表
│           └── video-editing/
│               └── index.astro # 單一電子書頁面
└── dist/                       # 建置輸出(部署此資料夾)

Astro 配置(Cloudflare 最佳化)

// astro.config.mjs
import { defineConfig } from 'astro/config';
import sitemap from '@astrojs/sitemap';

export default defineConfig({
  site: 'https://yourdomain.com',
  integrations: [sitemap()],
  prefetch: true,
  build: {
    inlineStylesheets: 'auto',  // 內嵌關鍵 CSS 以加快 FCP
  },
});

SEO 模式(BaseLayout)

每個頁面透過 Props 獲得完整的 SEO:

---
export interface Props {
  title: string;
  description?: string;
  canonical?: string;
  image?: string;
  type?: string;  // 'website' | 'article'
}
---

在 <head> 中包含:

  • <title>(格式:頁面標題 | 網站名稱)
  • <meta name="description">
  • <link rel="canonical">
  • Open Graph 標籤(og:type, og:title, og:description, og:image, og:url)
  • Twitter Card meta 標籤
  • JSON-LD 結構化資料(視情況使用 Person, Organization, Article)
  • <meta name="robots" content="index, follow">

EbookLayout 模式

適用於帶有側邊欄導航的長篇內容。完整的 CSS 請參閱 references/ebook-layout-css.md。

功能

  • 側邊欄導航 — 固定左側側邊欄,包含章節連結,透過滾動位置追踪啟用狀態
  • 閱讀進度條 — 固定在頂部的進度條,顯示閱讀進度(0-100%),漸層填色(藍綠色到靛藍色)
  • 回到頂端按鈕 — 固定在右下角,滾動超過 400px 後顯示
  • 作者資訊 — 內容底部的頭像圓圈 + 姓名/角色
  • 程式碼區塊 — 深色背景(#1e293b)、漸層頂部強調條、等寬字型、水平滾動
  • 表格 — 漸層標題列、交替行顏色、懸停效果、圓角邊框
  • 提示框 — 4 種類型:資訊(藍色)、警告(琥珀色)、提示(綠色)、危險(紅色),每種都附有 SVG 圖示
  • 列印樣式 — 隱藏側邊欄/進度條/導航,展開內容,分頁規則
  • 響應式 — 3 個斷點(960px 隱藏側邊欄,600px 緊湊模式,手機垂直堆疊)

CSS 策略:在 <style slot="head"> 上使用 is:global

EbookLayout CSS 必須使用 is:global,以便樣式能套用至頁面元件中的插槽內容:

<style slot="head" is:global>
  /* 此處的所有 class 都會套用至插槽內容 */
  .ebook-content h1 { ... }
  .callout { ... }
  .callout-info { ... }
</style>

如果不使用 is:global,則在 EbookLayout 中定義的 .callout-info 等 class 將無法套用至使用 <EbookLayout> 的頁面元件中的元素。

EbookLayout Props 介面

export interface Props {
  title: string;
  description?: string;
  chapters: { slug: string; title: string; number: number }[];
  currentSlug: string;
  ebookTitle: string;
  ebookDescription?: string;
  author?: string;  // 預設值為 'Ahmad Asri Zalmi'
}

內容頁面模式

每個電子書頁面定義 chapters 陣列並傳遞給 EbookLayout:

---
import EbookLayout from '../../../layouts/EbookLayout.astro';
const chapters = [
  { slug: 'index', title: '前言', number: 1 },
  { slug: 'topic-a', title: '主題A', number: 2 },
];
---
<EbookLayout title="頁面標題" description="SEO 描述" chapters={chapters}
  currentSlug="ebook-slug" ebookTitle="電子書標題" ebookDescription="副標題" author="作者姓名">
  <h1 id="index">頁面標題</h1>
  <p class="ebook-subtitle">副標題文字</p>
  <div class="ebook-meta">
    <!-- SVG 使用者圖示 + 作者名稱 + SVG 日曆圖示 + 年份 -->
  </div>
  <!-- 章節,h2 id 需與 slug 匹配 -->
</EbookLayout>

標題/頁首區塊(必填)

<h1 id="index">標題</h1>
<p class="ebook-subtitle">副標題</p>
<div class="ebook-meta">
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></svg>
  <span>作者姓名</span>
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="margin-left:0.5rem"><rect x="3" y="4" width="18" height="18" rx="2" ry="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/></svg>
  <span>2026</span>
</div>

提示框 HTML(4 種類型)

<!-- 資訊(藍色 #3b82f6) -->
<div class="callout callout-info">
  <svg class="callout-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
  <div class="callout-content"><strong>標題</strong><p>內容。</p></div>
</div>

<!-- 警告(橙色 #f59e0b) -->
<div class="callout callout-warning">
  <svg class="callout-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg>
  <div class="callout-content"><strong>標題</strong><p>內容。</p></div>
</div>

<!-- 提示(綠色 #22c55e) -->
<div class="callout callout-tip">
  <svg class="callout-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg>
  <div class="callout-content"><strong>標題</strong><p>內容。</p></div>
</div>

<!-- 危險(紅色 #ef4444) -->
<div class="callout callout-danger">
  <svg class="callout-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="15" y1="9" x2="9" y2="15"/><line x1="9" y1="9" x2="15" y2="15"/></svg>
  <div class="callout-content"><strong>標題</strong><p>內容。</p></div>
</div>

資源連結區塊(電子書結尾)

<div class="resource-links">
  <h3>資源與連結</h3>
  <ul>
    <li>
      <a href="https://..." target="_blank" rel="noopener">連結文字</a>
      <span class="desc">簡短描述</span>
    </li>
  </ul>
</div>

使用 set:html 的程式碼區塊

在 frontmatter 中定義程式碼字串,使用 set:html 渲染:

---
const code_example = `# shell 指令
npm run build`;
---
<pre><code set:html={code_example}></code></pre>

側邊欄啟用狀態(JS)

const navLinks = document.querySelectorAll('[data-nav-link]');
const headings = [];
navLinks.forEach(link => {
  const id = link.getAttribute('href').slice(1);
  const el = document.getElementById(id);
  if (el) headings.push({ el, link });
});
const updateActive = () => {
  let active = headings[0];
  for (const h of headings) {
    if (h.el.getBoundingClientRect().top <= 100) active = h;
  }
  navLinks.forEach(l => l.classList.remove('active'));
  if (active) active.link.classList.add('active');
};
window.addEventListener('scroll', updateActive, { passive: true });

提示框 HTML

<div class="callout callout-info">
  <svg class="callout-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
    <circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/>
  </svg>
  <div class="callout-content">
    <strong>標題</strong>
    <p>這裡是內容文字。</p>
  </div>
</div>

匯入路徑規則

Astro 使用 Vite 的檔案式路由。匯入路徑相對於匯入檔案:

檔案位置從此處匯入佈局
src/pages/index.astro../layouts/BaseLayout.astro
src/pages/blog.astro../layouts/BaseLayout.astro
src/pages/ebook/index.astro../../layouts/BaseLayout.astro
src/pages/ebook/video-editing/index.astro../../../layouts/BaseLayout.astro

計算從 src/ 開始的目錄深度,以決定 ../ 的數量。

Cloudflare Pages 部署

建置設定

設定值
框架預設Astro
建置指令npm run build
建置輸出目錄dist
Node.js 版本18+(透過環境變數 NODE_VERSION 設定)

安全標頭(public/_headers)

/*
  X-Frame-Options: DENY
  X-Content-Type-Options: nosniff
  Referrer-Policy: strict-origin-when-cross-origin
  Permissions-Policy: camera=(), microphone=(), geolocation=()

/assets/*
  Cache-Control: public, max-age=31536000, immutable

重新導向(public/_redirects)

/old-path  /new-path  301

Wrangler CLI 部署(直接上傳)

# 先建置
npm run build

# 部署至 Cloudflare Pages
CLOUDFLARE_API_TOKEN=cfut_xxx CLOUDFLARE_ACCOUNT_ID=xxx \
  npx wrangler pages deploy dist --project-name=my-site --branch=main

# 預覽部署(非 main 分支)
npx wrangler pages deploy dist --project-name=my-site --branch=staging

獨立 HTML 一致性

當獨立的 HTML 電子書檔案位於 public/ebook/(繞過 Astro 佈局)時,需要手動注入共用的頁首、側邊欄、色彩配置和作者描述。請參閱 references/standalone-html-consistency.md 獲取完整模式,包括:

  • 注入的固定頁首(含標誌 + 導航,使用內聯 CSS 以避免衝突)
  • 當在固定側邊欄上方新增頁首時,調整側邊欄 top/height
  • 色彩配置統一(CSS 變數、rgba、漸層 — 全部必須更新)
  • 回到頂端按鈕注入(CSS + HTML + JS)
  • 修補工具 JS 巢狀結構陷阱(修補後務必驗證閉合大括號)
  • 作者描述一致性檢查清單

陷阱

詳細問題請參閱 references/astro5-pitfalls.md。摘要:

  1. HTML 中的大括號 — { 和 } 是 JSX 表達式。程式碼區塊請使用 set:html。
  2. lightningcss 未打包 — 請單獨安裝,或不要使用 cssMinify: 'lightningcss'。
  3. 匯入路徑深度 — 必須與目錄結構完全匹配。
  4. 不使用 emoji — 使用內聯 SVG 圖示(來自 ebook-generator 技能的使用者偏好)。
  5. set:html 僅限於自閉合標籤或與 slot 搭配使用 — 使用 set:html 時不能有子元素。
  6. Astro 作用域 CSS — <style> 中的 class 是針對每個元件作用域的。對於共用 class,請使用 <style is:global> 或 :global(.class)。EbookLayout 必須使用 <style slot="head" is:global>,樣式才能套用至插槽內容。
  7. data-astro-cid 作用域不匹配 — BaseLayout 中的 .container class 不會套用至頁面元件中的元素。需要全域 class:.container、.btn、.section、.grid-* 等。
  8. JS 切換 class + 作用域 CSS = 無聲失敗 — 動態新增/移除 class 的 JavaScript(例如 IntersectionObserver 在滾動時新增 .fade-in)將無法匹配 Astro 的作用域選擇器。Astro 將 .fade-in 限定為 .fade-in[data-astro-cid-xxxx],但 JS 新增的是單純的 .fade-in class。症狀: 區塊永遠保持隱藏(opacity:0)。修正方法: 要麼 (a) 對動畫 class 使用 <style is:global>,要麼 (b) 在作用域樣式中使用 :global(.fade-in),要麼 (c) 完全移除 CSS 動畫 class 並讓內容立即渲染。對於動畫非關鍵的 SSR 網站,選項 (c) 最簡單。完整細節請參閱 references/astro5-pitfalls.md 第 9 點。
  9. 獨立 HTML 側邊欄偏移 — 將固定頁首注入獨立電子書 HTML 檔案時,務必將固定側邊欄的 top 從 0 調整為頁首高度(約 56px),並設定 height: calc(100vh - 56px)。忘記此步驟將導致側邊欄內容隱藏在頁首後方。

反模式

  • 不要在未執行 npm install lightningcss 的情況下使用 cssMinify: 'lightningcss'
  • 不要將 {variable} 放在 <pre><code> 區塊內 — Astro 會將其解析為 JSX
  • 不要使用 emoji 作為圖示 — 請使用內聯 SVG
  • 不要為了 SEO 而省略 robots.txt 和 sitemap
  • 不要忘記為行動裝置加入 <meta name="viewport">
  • 不要硬編碼網站 URL — 請使用設定檔中的 Astro.site

相關技能