Repository navigation
fix: standardize blog cover presentation #69
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
Merged
zhourrr
merged 3 commits into
zvec-ai:main
from
feihongxu0824:codex/fix-blog-cover-crop
Oct 21, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,35 @@ | ||
| # Blog contribution guide | ||
|
|
||
| Every post must provide a repository-hosted cover image. The blog index uses one predictable presentation rule for every cover, so article-specific fit overrides are not supported. | ||
|
|
||
| ## Cover requirements | ||
|
|
||
| - Store cover assets under `public/img/blog/<post-slug>/` and reference them with `/img/blog/<post-slug>/<file>`. | ||
| - Use a 16:9 landscape canvas. Recommended export size: `1600x900` or `1920x1080`; minimum accepted size: `800x450`. | ||
| - Keep titles, logos, diagrams, and other essential content inside a 7% safe margin. Covers are displayed with `object-fit: cover` and may lose a very small edge area because of responsive rounding. | ||
| - Prefer providing both `image` and `imageDark`, designed for light and dark page backgrounds respectively. Both covers must use the same dimensions, composition, and safe-area rules. | ||
| - Do not use remote image URLs or add per-post CSS/object-fit exceptions. | ||
|
|
||
| ## Frontmatter | ||
|
|
||
| ```yaml | ||
| --- | ||
| title: "Post title" | ||
| date: "YYYY-MM-DD" | ||
| image: /img/blog/YYYY-MM-DD-post-slug/header.webp | ||
| imageDark: /img/blog/YYYY-MM-DD-post-slug/header-dark.webp # optional | ||
| description: "Short summary" | ||
| --- | ||
| ``` | ||
|
|
||
| ## Before opening a PR | ||
|
|
||
| Run: | ||
|
|
||
| ```bash | ||
| npm run blog:covers:check | ||
| npm run types:check | ||
| npm run build | ||
| ``` | ||
|
|
||
| `npm run build` also runs the cover check. A blog PR with a missing, remote, undersized, or incorrectly proportioned cover will fail CI before deployment. | ||
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
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
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,196 @@ | ||
| import { readFile, readdir } from 'node:fs/promises'; | ||
| import path from 'node:path'; | ||
| import process from 'node:process'; | ||
| import { pathToFileURL } from 'node:url'; | ||
| import { frontmatter } from 'fumadocs-core/content/md/frontmatter'; | ||
|
|
||
| const ROOT = process.cwd(); | ||
| const BLOG_ROOT = path.join(ROOT, 'content', 'blog'); | ||
| const PUBLIC_ROOT = path.join(ROOT, 'public'); | ||
| const TARGET_RATIO = 16 / 9; | ||
| const MAX_RATIO_ERROR = 0.02; | ||
| const MIN_WIDTH = 800; | ||
| const MIN_HEIGHT = 450; | ||
|
|
||
| async function walk(directory) { | ||
| const entries = await readdir(directory, { withFileTypes: true }); | ||
| const files = await Promise.all(entries.map(async (entry) => { | ||
| const target = path.join(directory, entry.name); | ||
| return entry.isDirectory() ? walk(target) : [target]; | ||
| })); | ||
| return files.flat(); | ||
| } | ||
|
|
||
| export function readFrontmatter(source) { | ||
| const data = frontmatter(source).data; | ||
| return data && typeof data === 'object' && !Array.isArray(data) ? data : {}; | ||
| } | ||
|
|
||
| export function validateCoverFields(fields, owner) { | ||
| const covers = []; | ||
| const errors = []; | ||
| const image = typeof fields.image === 'string' ? fields.image.trim() : ''; | ||
|
|
||
| if (!image) { | ||
| errors.push(`${owner}: image is required and must be a local /img/blog/... path`); | ||
| } else { | ||
| covers.push({ key: 'image', image }); | ||
| } | ||
|
|
||
| if (fields.imageDark !== undefined && fields.imageDark !== null) { | ||
| if (typeof fields.imageDark !== 'string') { | ||
| errors.push(`${owner}: imageDark must be a local /img/blog/... path when provided`); | ||
| } else if (fields.imageDark.trim()) { | ||
| covers.push({ key: 'imageDark', image: fields.imageDark.trim() }); | ||
| } | ||
| } | ||
|
|
||
| return { covers, errors }; | ||
| } | ||
|
|
||
| function pngSize(buffer) { | ||
| if (buffer.subarray(1, 4).toString() !== 'PNG') return null; | ||
| return { width: buffer.readUInt32BE(16), height: buffer.readUInt32BE(20) }; | ||
| } | ||
|
|
||
| function jpegSize(buffer) { | ||
| if (buffer[0] !== 0xff || buffer[1] !== 0xd8) return null; | ||
| let offset = 2; | ||
|
|
||
| while (offset + 9 < buffer.length) { | ||
| if (buffer[offset] !== 0xff) { | ||
| offset += 1; | ||
| continue; | ||
| } | ||
|
|
||
| const marker = buffer[offset + 1]; | ||
| offset += 2; | ||
| if (marker === 0xd8 || marker === 0xd9) continue; | ||
| const length = buffer.readUInt16BE(offset); | ||
| if ([0xc0, 0xc1, 0xc2, 0xc3, 0xc5, 0xc6, 0xc7, 0xc9, 0xca, 0xcb, 0xcd, 0xce, 0xcf].includes(marker)) { | ||
| return { width: buffer.readUInt16BE(offset + 5), height: buffer.readUInt16BE(offset + 3) }; | ||
| } | ||
| offset += length; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| function webpSize(buffer) { | ||
| if (buffer.subarray(0, 4).toString() !== 'RIFF' || buffer.subarray(8, 12).toString() !== 'WEBP') return null; | ||
| let offset = 12; | ||
|
|
||
| while (offset + 8 <= buffer.length) { | ||
| const type = buffer.subarray(offset, offset + 4).toString(); | ||
| const length = buffer.readUInt32LE(offset + 4); | ||
| const data = offset + 8; | ||
|
|
||
| if (type === 'VP8X' && data + 10 <= buffer.length) { | ||
| return { | ||
| width: 1 + buffer.readUIntLE(data + 4, 3), | ||
| height: 1 + buffer.readUIntLE(data + 7, 3), | ||
| }; | ||
| } | ||
| if (type === 'VP8 ' && data + 10 <= buffer.length) { | ||
| return { | ||
| width: buffer.readUInt16LE(data + 6) & 0x3fff, | ||
| height: buffer.readUInt16LE(data + 8) & 0x3fff, | ||
| }; | ||
| } | ||
| if (type === 'VP8L' && data + 5 <= buffer.length && buffer[data] === 0x2f) { | ||
| const bits = buffer.readUInt32LE(data + 1); | ||
| return { | ||
| width: 1 + (bits & 0x3fff), | ||
| height: 1 + ((bits >> 14) & 0x3fff), | ||
| }; | ||
| } | ||
|
|
||
| offset = data + length + (length % 2); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| function svgSize(buffer) { | ||
| const source = buffer.toString('utf8', 0, Math.min(buffer.length, 4096)); | ||
| if (!/<svg\b/i.test(source)) return null; | ||
| const viewBox = source.match(/viewBox=["']\s*[-\d.]+\s+[-\d.]+\s+([\d.]+)\s+([\d.]+)\s*["']/i); | ||
| if (viewBox) return { width: Number(viewBox[1]), height: Number(viewBox[2]) }; | ||
| const width = source.match(/\bwidth=["']([\d.]+)/i); | ||
| const height = source.match(/\bheight=["']([\d.]+)/i); | ||
| return width && height ? { width: Number(width[1]), height: Number(height[1]) } : null; | ||
| } | ||
|
|
||
| function imageSize(buffer) { | ||
| return pngSize(buffer) || jpegSize(buffer) || webpSize(buffer) || svgSize(buffer); | ||
| } | ||
|
|
||
| export async function validateBlogCovers() { | ||
| const contentFiles = (await walk(BLOG_ROOT)).filter((file) => file.endsWith('.mdx')); | ||
| const references = new Map(); | ||
| const errors = []; | ||
|
|
||
| for (const contentFile of contentFiles) { | ||
| const owner = path.relative(ROOT, contentFile); | ||
| let fields; | ||
| try { | ||
| fields = readFrontmatter(await readFile(contentFile, 'utf8')); | ||
| } catch (error) { | ||
| errors.push(`${owner}: invalid YAML frontmatter (${error instanceof Error ? error.message : String(error)})`); | ||
| continue; | ||
| } | ||
|
|
||
| const result = validateCoverFields(fields, owner); | ||
| errors.push(...result.errors); | ||
| for (const { key, image } of result.covers) { | ||
| if (!image.startsWith('/img/blog/')) { | ||
| errors.push(`${owner}: ${key} must be a local /img/blog/... path`); | ||
| continue; | ||
| } | ||
| const owners = references.get(image) || []; | ||
| owners.push(`${owner} (${key})`); | ||
| references.set(image, owners); | ||
| } | ||
| } | ||
|
|
||
| for (const [image, owners] of references) { | ||
| const asset = path.resolve(PUBLIC_ROOT, `.${image}`); | ||
| if (!asset.startsWith(`${PUBLIC_ROOT}${path.sep}`)) { | ||
| errors.push(`${image}: path escapes public/`); | ||
| continue; | ||
| } | ||
|
|
||
| let buffer; | ||
| try { | ||
| buffer = await readFile(asset); | ||
| } catch { | ||
| errors.push(`${image}: file does not exist (used by ${owners.join(', ')})`); | ||
| continue; | ||
| } | ||
|
|
||
| const size = imageSize(buffer); | ||
| if (!size || !Number.isFinite(size.width) || !Number.isFinite(size.height)) { | ||
| errors.push(`${image}: unsupported image or unreadable dimensions`); | ||
| continue; | ||
| } | ||
|
|
||
| const ratioError = Math.abs(size.width / size.height - TARGET_RATIO) / TARGET_RATIO; | ||
| if (ratioError > MAX_RATIO_ERROR) { | ||
| errors.push(`${image}: ${size.width}x${size.height} is not 16:9 (maximum deviation: 2%)`); | ||
| } | ||
| if (size.width < MIN_WIDTH || size.height < MIN_HEIGHT) { | ||
| errors.push(`${image}: ${size.width}x${size.height} is smaller than ${MIN_WIDTH}x${MIN_HEIGHT}`); | ||
| } | ||
| } | ||
|
|
||
| return { references, errors }; | ||
| } | ||
|
|
||
| if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { | ||
| const { references, errors } = await validateBlogCovers(); | ||
| if (errors.length > 0) { | ||
| console.error('Blog cover validation failed:\n'); | ||
| for (const error of errors) console.error(`- ${error}`); | ||
| process.exitCode = 1; | ||
| } else { | ||
| console.log(`Validated ${references.size} blog cover assets (16:9, local, minimum ${MIN_WIDTH}x${MIN_HEIGHT}).`); | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.