API 參考
本文檔提供 Open Graph 中繼標籤生成器的技術細節與其底層功能說明。
工具概覽
Open Graph 中繼標籤生成器是一款在用戶端執行的工具,用於產生適用於 Open Graph 與 Twitter Card 協定的 HTML meta 標籤。它會處理使用者輸入,並輸出格式正確的 HTML meta 標籤。
輸入參數
一般資訊
| 參數 | 類型 | 必填 | 說明 | 範例 |
|---|---|---|---|---|
pageType | string | 是 | 內容類型 | "website", "article", "book" |
title | string | 否 | 頁面標題 | "My Awesome Website" |
description | string | 否 | 頁面描述 | "A great website for developers" |
url | string | 否 | 頁面 URL | "https://example.com/page" |
siteName | string | 否 | 網站名稱 | "My Company" |
圖片設定
| 參數 | 類型 | 必填 | 說明 | 範例 |
|---|---|---|---|---|
imageUrl | string | 否 | 圖片 URL | "https://example.com/image.jpg" |
imageAlt | string | 否 | 圖片 Alt 文字 | "Company logo" |
imageWidth | string | 否 | 圖片寬度(像素) | "1200" |
imageHeight | string | 否 | 圖片高度(像素) | "630" |
Twitter 設定
| 參數 | 類型 | 必填 | 說明 | 範例 |
|---|---|---|---|---|
twitterCardType | string | 是 | Twitter 卡片類型 | "summary_large_image" |
twitterSite | string | 否 | 網站 Twitter 帳號 | "@example" |
twitterCreator | string | 否 | 作者 Twitter 帳號 | "@author" |
輸出格式
此工具會產生如下格式的 HTML meta 標籤:
Open Graph 標籤
<!-- Open Graph Meta Tags -->
<meta property="og:type" content="website" />
<meta property="og:title" content="Page Title" />
<meta property="og:description" content="Page description" />
<meta property="og:url" content="https://example.com/page" />
<meta property="og:site_name" content="Site Name" />
<meta property="og:image" content="https://example.com/image.jpg" />
<meta property="og:image:alt" content="Image description" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
Twitter 標籤
<!-- Twitter Meta Tags -->
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" content="@example" />
<meta name="twitter:creator" content="@author" />
<meta name="twitter:title" content="Page Title" />
<meta name="twitter:description" content="Page description" />
<meta name="twitter:image" content="https://example.com/image.jpg" />
內容類型
支援的頁面類型
| 類型 | 說明 | 使用情境 |
|---|---|---|
website | 一般網站頁面 | 首頁、關於頁、聯絡頁 |
article | 部落格文章、新聞文章 | 部落格文章、新聞文章、教學 |
book | 書籍頁面、電子書 | 書籍列表頁、電子書頁面 |
profile | 個人或企業介紹頁 | 使用者個人頁、公司頁面 |
music | 音樂曲目、專輯 | 音樂串流、專輯頁面 |
video | 影片內容 | 影片頁面、電影列表頁 |
Twitter Card 類型
| 類型 | 說明 | 最適合 |
|---|---|---|
summary | 含縮圖的基本卡片 | 部落格文章、新聞文章 |
summary_large_image | 含大圖的卡片 | 視覺內容、產品 |
app | 行動 App 推廣 | App Store 頁面 |
player | 影片/音訊內容 | 影片頁面、Podcast |
資料處理
HTML 轉義
所有使用者輸入都會自動轉義,以防止 XSS 攻擊:
function escapeHtml(text) {
const div = document.createElement("div");
div.textContent = text;
return div.innerHTML;
}
URL 驗證
系統會驗證 URL,確保其格式正確:
function isValidUrl(string) {
try {
new URL(string);
return true;
} catch (_) {
return false;
}
}
內容長度限制
此工具會套用建議的長度限制:
- 標題:最多 60 個字元
- 描述:最多 160 個字元
- URL:必須為有效的 URL 格式
瀏覽器相容性
支援的瀏覽器
| 瀏覽器 | 版本 | 支援情況 |
|---|---|---|
| Chrome | 60+ | 完整支援 |
| Firefox | 55+ | 完整支援 |
| Safari | 12+ | 完整支援 |
| Edge | 79+ | 完整支援 |
必要功能
- ES6+ 支援
- Clipboard API 支援
- Local Storage 支援
- CSS Grid 支援
效能考量
用戶端處理
- 所有處理皆在瀏覽器中完成
- 不需伺服器請求
- 可即時產生並預覽
- 記憶體占用極低
快取
- 產生的 meta 標籤會快取於瀏覽器儲存空間中
- 歷史記錄會儲存在本機
- 無外部相依套件
錯誤處理
輸入驗證錯誤
// Example error handling
if (!title.trim()) {
showError("Title is required");
return;
}
if (url && !isValidUrl(url)) {
showError("Please enter a valid URL");
return;
}
圖片載入錯誤
// Handle image loading errors
function handleImageError(event) {
const img = event.target;
img.style.display = "none";
showWarning("Image failed to load");
}
整合範例
WordPress 整合
// Add to functions.php
function add_open_graph_tags() {
if (is_single()) {
$title = get_the_title();
$description = get_the_excerpt();
$image = get_the_post_thumbnail_url();
echo '<meta property="og:title" content="' . esc_attr($title) . '" />';
echo '<meta property="og:description" content="' . esc_attr($description) . '" />';
echo '<meta property="og:image" content="' . esc_url($image) . '" />';
}
}
add_action('wp_head', 'add_open_graph_tags');
React 整合
// React component example
function OpenGraphMeta({ title, description, image, url }) {
return (
<>
<meta property="og:title" content={title} />
<meta property="og:description" content={description} />
<meta property="og:image" content={image} />
<meta property="og:url" content={url} />
</>
);
}
Next.js 整合
// Next.js Head component
import Head from "next/head";
function MyPage() {
return (
<Head>
<meta property="og:title" content="My Page Title" />
<meta property="og:description" content="My page description" />
<meta property="og:image" content="https://example.com/image.jpg" />
</Head>
);
}
測試與驗證
社群媒體除錯工具
- Facebook: developers.facebook.com/tools/debug
- Twitter: cards-dev.twitter.com/validator
- LinkedIn: linkedin.com/post-inspector
自動化測試
// Example test for meta tag generation
describe("Open Graph Meta Generator", () => {
test("generates correct meta tags", () => {
const input = {
pageType: "website",
title: "Test Page",
description: "Test description",
};
const output = generateMetaTags(input);
expect(output).toContain('<meta property="og:type" content="website" />');
expect(output).toContain('<meta property="og:title" content="Test Page" />');
});
});
疑難排解
常見問題
- 圖片未顯示: 檢查 URL 格式與可存取性
- 標籤未生效: 使用社群媒體除錯工具驗證
- 內容被截斷: 檢查字元長度限制
- 快取問題: 使用除錯工具重新整理快取
除錯模式
啟用除錯模式以查看詳細資訊:
// Enable debug logging
localStorage.setItem("debug", "true");
後續增強
規劃中的功能
- 支援 Schema.org 結構化資料
- 支援多語系
- 支援批次產生
- 提供伺服器端產生的 API 端點
- 提供進階圖片最佳化
- 整合分析能力
貢獻方式
若你想為 Open Graph 中繼標籤生成器做出貢獻:
- Fork 儲存庫
- 建立功能分支
- 提交你的變更
- 加入測試
- 提交 pull request
支援
如需技術支援或有相關問題: