-
Couldn't load subscription status.
- Fork 2k
CI: add og image generation #2078
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3ef8077
og: test
ShubhamOulkar 6f65d1c
update ci
ShubhamOulkar d302023
add og generation script
ShubhamOulkar d36c303
test
ShubhamOulkar 0d35e49
add script to local setup
ShubhamOulkar 415e8cc
test
ShubhamOulkar 24c79e3
add netlify.toml
ShubhamOulkar 6d1bdff
remove toml file
ShubhamOulkar 2982231
test
ShubhamOulkar 6c25f98
generate og after jekyll build
ShubhamOulkar ee83677
test
ShubhamOulkar aac0537
test
ShubhamOulkar ad8869f
test
ShubhamOulkar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import fs from "fs/promises"; | ||
| import path from "path"; | ||
| import matter from "gray-matter"; | ||
| import satori from "satori"; | ||
| import sharp from "sharp"; | ||
| import { fileURLToPath } from "url"; | ||
|
|
||
| const __filename = fileURLToPath(import.meta.url); | ||
| const __dirname = path.dirname(__filename); | ||
|
|
||
| // Path posts folder | ||
| const postsDir = path.join(__dirname, '../../_posts'); | ||
|
|
||
| // Output folder for OG images | ||
| const outputDir = path.join(__dirname, '../../../_site/images/og-images'); | ||
| console.log(outputDir) | ||
|
|
||
| // font | ||
| const FONT_FILE_PATH = path.join(__dirname, '../../fonts/noto-sans-latin-700-normal.woff'); | ||
|
|
||
| let font = await fs.readFile(FONT_FILE_PATH); | ||
|
|
||
| async function generateOgImage(title, date, outFile) { | ||
| const eleObj = { | ||
| type: 'div', | ||
| props: { | ||
| style: { | ||
| display: "flex", | ||
| flexDirection: "column", | ||
| alignItems: "center", | ||
| justifyContent: "center", | ||
| width: 1200, | ||
| height: 630, | ||
| background: "radial-gradient(circle at center, #222222 0%, #000000 100%)", | ||
| color: "white", | ||
| fontFamily: "Noto Sans", | ||
| padding: "50px", | ||
| textAlign: "center", | ||
| }, | ||
| children: [ | ||
| { | ||
| type: 'h1', | ||
| props: { | ||
| style: { fontSize: 64, margin: 0 }, | ||
| children: title, | ||
| }, | ||
| }, | ||
| { | ||
| type: 'p', | ||
| props: { | ||
| style: { fontSize: 32, opacity: 0.7 }, | ||
| children: date, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| const svg = await satori(eleObj, { width: 1200, height: 630, fonts: [{ name: 'Noto Sans', data: font }] }); | ||
|
|
||
| const png = await sharp(Buffer.from(svg)).png({ quality: 100 }).toBuffer(); | ||
|
|
||
| await fs.mkdir(path.dirname(outFile), { recursive: true }); | ||
| await fs.writeFile(outFile, png); | ||
| console.log(`✅ Generated OG image for: ${title}`); | ||
| } | ||
|
|
||
| async function run() { | ||
| const files = await fs.readdir(postsDir); | ||
| for (const file of files) { | ||
| if (!file.endsWith(".md")) continue; | ||
|
|
||
| const content = await fs.readFile(path.join(postsDir, file), "utf-8"); | ||
| const { data } = matter(content); | ||
|
|
||
| const title = data.title || file.replace(/^\d{4}-\d{2}-\d{2}-/, "").replace(".md", "").replace(/-/g, " "); | ||
| const date = data.date || file.slice(0, 10); | ||
|
|
||
| const outFile = path.join(outputDir, file.replace(".md", ".webp")); | ||
| console.log(outFile) | ||
| await generateOgImage(title, date, outFile); | ||
| } | ||
| } | ||
|
|
||
| run().catch(console.error); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,3 +10,6 @@ _site | |
| .jekyll-metadata | ||
| vendor | ||
| .bundle | ||
|
|
||
| # og images | ||
| images/og-images | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.