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
Open
feihongxu0824
wants to merge
1
commit into
zvec-ai:main
Choose a base branch
from
feihongxu0824:codex/fix-blog-cover-crop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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,166 @@ | ||
| import { readFile, readdir } from 'node:fs/promises'; | ||
| import path from 'node:path'; | ||
| import process from 'node:process'; | ||
|
|
||
| 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(); | ||
| } | ||
|
|
||
| function readFrontmatter(source) { | ||
| if (!source.startsWith('---')) return {}; | ||
| const end = source.indexOf('\n---', 3); | ||
| if (end === -1) return {}; | ||
|
|
||
| const fields = {}; | ||
| for (const line of source.slice(3, end).split('\n')) { | ||
| const match = line.match(/^([A-Za-z][A-Za-z0-9]*):\s*(.*?)\s*$/); | ||
| if (!match) continue; | ||
| fields[match[1]] = match[2].replace(/^(['"])(.*)\1$/, '$2'); | ||
| } | ||
| return fields; | ||
| } | ||
|
|
||
| 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); | ||
| } | ||
|
|
||
| const contentFiles = (await walk(BLOG_ROOT)).filter((file) => file.endsWith('.mdx')); | ||
| const references = new Map(); | ||
| const errors = []; | ||
|
|
||
| for (const contentFile of contentFiles) { | ||
| const fields = readFrontmatter(await readFile(contentFile, 'utf8')); | ||
| for (const key of ['image', 'imageDark']) { | ||
| const image = fields[key]; | ||
| if (!image) continue; | ||
| if (!image.startsWith('/img/blog/')) { | ||
| errors.push(`${path.relative(ROOT, contentFile)}: ${key} must be a local /img/blog/... path`); | ||
| continue; | ||
| } | ||
| const owners = references.get(image) || []; | ||
| owners.push(`${path.relative(ROOT, contentFile)} (${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}`); | ||
| } | ||
| } | ||
|
|
||
| if (errors.length > 0) { | ||
| console.error('Blog cover validation failed:\n'); | ||
| for (const error of errors) console.error(`- ${error}`); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| console.log(`Validated ${references.size} blog cover assets (16:9, local, minimum ${MIN_WIDTH}x${MIN_HEIGHT}).`); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个文件要不移动到 content/blog 下面,作为编写 blog 的指导说明
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
维护一个contributing.md就可以了吧,放在外面醒目且常用一些,标题暂时是blog contributing guide,后面有其他guide可以再修改标题