Skip to content

Commit cf7be43

Browse files
静的ページにもOGPを設定 (#245)
* _ * update * chore: update OGP images --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent afb9112 commit cf7be43

File tree

6 files changed

+72
-11
lines changed

6 files changed

+72
-11
lines changed

.github/workflows/generate-ogp.yaml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
id: generation-check
3434
run: |
3535
# 生成後の差分をチェック
36-
if ! git diff --exit-code content/posts/**/cover.png; then
36+
if ! git diff --exit-code content/**/cover.png static/cover.png; then
3737
echo "has_changes=true" >> $GITHUB_OUTPUT
3838
else
3939
echo "has_changes=false" >> $GITHUB_OUTPUT
@@ -44,7 +44,7 @@ jobs:
4444
run: |
4545
git config --local user.email "github-actions[bot]@users.noreply.github.com"
4646
git config --local user.name "github-actions[bot]"
47-
git add content/posts/**/cover.png
47+
git add content/**/cover.png static/cover.png
4848
git commit -m "chore: update OGP images"
4949
git push origin HEAD:${{ github.head_ref }}
5050
@@ -85,7 +85,13 @@ jobs:
8585
IFS=',' read -ra FILES <<< "${{ steps.pr-check.outputs.files }}"
8686
for file in "${FILES[@]}"; do
8787
# ファイルパスから記事名を抽出
88-
POST_NAME=$(echo "$file" | sed 's|content/posts/||' | sed 's|/cover.png||')
88+
if [[ "$file" == "static/cover.png" ]]; then
89+
POST_NAME="トップページ (Top Page)"
90+
elif [[ "$file" == "content/about/cover.png" ]]; then
91+
POST_NAME="aboutページ (About Page)"
92+
else
93+
POST_NAME=$(echo "$file" | sed 's|content/posts/||' | sed 's|/cover.png||')
94+
fi
8995
IMAGE_URL="https://github.com/${{ github.repository }}/raw/${{ github.head_ref }}/${file}"
9096
COMMENT="${COMMENT}### ${POST_NAME}\n![${POST_NAME}](${IMAGE_URL})\n\n"
9197
done

content/about/cover.png

62.3 KB
Loading

content/about/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: "About Me"
33
date: "2023-06-06T01:15:48+09:00"
44
author: "kyu08"
55
authorTwitter: "kyu08_" #do not include @
6-
cover: "https://blog.kyu08.com/cover.png"
6+
cover: "cover.png"
77
tags: []
88
keywords: ["", ""]
99
description: ""

scripts/generate-ogp.mjs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,40 @@ async function generateOgpImage(metadata, fonts) {
157157
}
158158
}
159159

160+
/**
161+
* 静的ページ用のOGP画像を生成
162+
*/
163+
async function generateStaticOgpImage({ title, outputPath, author = 'kyu08', showDate = false, showTags = false }, fonts) {
164+
try {
165+
// Satoriでレンダリング
166+
const svg = await satori(
167+
generateOgpTemplate({
168+
title,
169+
date: '',
170+
tags: [],
171+
author,
172+
showDate,
173+
showTags,
174+
}),
175+
{
176+
width: 1200,
177+
height: 630,
178+
fonts,
179+
}
180+
);
181+
182+
// SVGをPNGに変換
183+
const png = svgToPng(svg);
184+
185+
// 指定されたパスに保存
186+
await fs.writeFile(outputPath, png);
187+
188+
console.log(`✅ Generated: ${path.relative(__dirname, outputPath)}`);
189+
} catch (error) {
190+
console.error(`❌ Failed to generate OGP for ${outputPath}:`, error);
191+
}
192+
}
193+
160194
/**
161195
* メイン処理
162196
*/
@@ -179,6 +213,27 @@ async function main() {
179213
await generateOgpImage(metadata, fonts);
180214
}
181215

216+
// 静的ページのOGP画像を生成
217+
console.log('\n📄 Generating static page OGP images...\n');
218+
219+
// aboutページ用
220+
await generateStaticOgpImage({
221+
title: 'blog.kyu08.com',
222+
outputPath: path.join(__dirname, '../content/about/cover.png'),
223+
author: 'kyu08',
224+
showDate: false,
225+
showTags: false,
226+
}, fonts);
227+
228+
// トップページ用
229+
await generateStaticOgpImage({
230+
title: 'blog.kyu08.com',
231+
outputPath: path.join(__dirname, '../static/cover.png'),
232+
author: 'kyu08',
233+
showDate: false,
234+
showTags: false,
235+
}, fonts);
236+
182237
console.log('\n✨ OGP image generation completed!');
183238
}
184239

scripts/ogp-template.mjs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Satoriを使ってReact風のJSXでデザインを記述
44
*/
55

6-
export function generateOgpTemplate({ title, date, tags, author = 'kyu08' }) {
6+
export function generateOgpTemplate({ title, date, tags, author = 'kyu08', showDate = true, showTags = true }) {
77
// タイトルの長さに応じてフォントサイズを調整
88
const getTitleFontSize = (titleLength) => {
99
if (titleLength > 50) return '48px';
@@ -110,8 +110,8 @@ export function generateOgpTemplate({ title, date, tags, author = 'kyu08' }) {
110110
children: `@${author}`,
111111
},
112112
},
113-
// Date
114-
{
113+
// Date (conditional)
114+
...(showDate && date ? [{
115115
type: 'div',
116116
props: {
117117
style: {
@@ -120,9 +120,9 @@ export function generateOgpTemplate({ title, date, tags, author = 'kyu08' }) {
120120
},
121121
children: date,
122122
},
123-
},
124-
// Tags
125-
...tags.map((tag) => ({
123+
}] : []),
124+
// Tags (conditional)
125+
...(showTags ? tags.map((tag) => ({
126126
type: 'div',
127127
props: {
128128
style: {
@@ -136,7 +136,7 @@ export function generateOgpTemplate({ title, date, tags, author = 'kyu08' }) {
136136
},
137137
children: `#${tag}`,
138138
},
139-
})),
139+
})) : []),
140140
],
141141
},
142142
},

static/cover.png

-38.1 KB
Loading

0 commit comments

Comments
 (0)