Files
arcticnewone/scripts/generate-og.mjs
T
virtheart 5c3f3bca63 feat: 全量落地后端与功能闭环
- 真实后端(server/):SQLite 存储 + scrypt 会话鉴权 + SMTP 邮件
  - auth 注册/登录/登出/me/资料更新
  - contact 联系表单(落库+邮件)、newsletter 订阅(去重)
  - favorites 作品收藏、comments 新闻评论、account 管理收件箱
- 用户系统 UI:个人中心 /account(资料/收藏/评论/管理员面板)、
  收藏按钮、评论组件、登录/注册弹窗接真实 API(useApiError 错误映射)
- 内容单点事实源:app/data/news|work 消除三处重复,页面全部引用
- 修复断链:首页动态/作品卡可点击、分享按钮真实动作、移除 # 死链、
  语言切换器、社交链接统一 siteConfig
- SEO:动态 sitemap/robots、JSON-LD、脚本生成真实 og-image.png
- 列表增强:搜索 + 加载更多
- 部署自动化:build-and-push.sh 支持 git tag 版本化镜像;
  deploy.sh 一条命令构建推送+注入 Secret/ConfigMap+kubectl 滚动升级;
  deployment 挂 PVC 持久化;Dockerfile 支持 better-sqlite3 原生编译
- 组件注册去目录前缀,修复 AppLoader/AnimatedButton 等未渲染问题
2026-08-06 15:08:34 +08:00

140 lines
4.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 生成站点 Open Graph 分享图 public/og-image.png(1200x630)。
*
* 手写 PNG 编码器(node:zlib deflateSync),零第三方依赖、确定性可复现:
* - 深蓝渐变背景 + 中央白色菱形 logo 母题 + 星点(固定种子伪随机)
*
* 用法: node scripts/generate-og.mjs
*/
import { deflateSync } from 'node:zlib'
import { writeFileSync } from 'node:fs'
const W = 1200
const H = 630
// ---------- PNG 编码工具 ----------
let crcTable = null
function crc32(buf) {
if (!crcTable) {
crcTable = new Int32Array(256)
for (let n = 0; n < 256; n++) {
let c = n
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1
crcTable[n] = c
}
}
let c = 0xffffffff
for (let i = 0; i < buf.length; i++) c = crcTable[(c ^ buf[i]) & 0xff] ^ (c >>> 8)
return (c ^ 0xffffffff) >>> 0
}
function chunk(type, data) {
const len = Buffer.alloc(4)
len.writeUInt32BE(data.length)
const typeBuf = Buffer.from(type, 'ascii')
const crcBuf = Buffer.alloc(4)
crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data])))
return Buffer.concat([len, typeBuf, data, crcBuf])
}
// ---------- 确定性伪随机(mulberry32)----------
let seed = 20260806
function rand() {
seed = (seed + 0x6d2b79f5) | 0
let t = seed
t = Math.imul(t ^ (t >>> 15), t | 1)
t ^= t + Math.imul(t ^ (t >>> 7), t | 61)
return ((t ^ (t >>> 14)) >>> 0) / 4294967296
}
// ---------- 像素绘制 ----------
const raw = Buffer.alloc(H * (1 + W * 4))
const lerp = (a, b, t) => Math.round(a + (b - a) * t)
const clamp255 = (v) => Math.max(0, Math.min(255, v))
const cTop = [15, 23, 42] // #0f172a
const cBottom = [30, 41, 59] // #1e293b
const cAccent = [99, 102, 241] // indigo
const cx = W / 2
const cy = H / 2
const diamondHalf = 132 // 菱形对角线半长(L1 度量)
const diamondEdge = 16 // 菱形边缘柔和过渡像素
// 基础渐变 + 中央径向光晕 + 菱形
for (let y = 0; y < H; y++) {
const rowStart = y * (1 + W * 4)
raw[rowStart] = 0 // filter: none
const t = y / (H - 1)
let r = lerp(cTop[0], cBottom[0], t)
let g = lerp(cTop[1], cBottom[1], t)
let b = lerp(cTop[2], cBottom[2], t)
for (let x = 0; x < W; x++) {
const dx = x - cx
const dy = y - cy
const dist = Math.sqrt(dx * dx + dy * dy)
const glow = Math.max(0, 1 - dist / 520)
let pr = lerp(r, cAccent[0], glow * 0.45)
let pg = lerp(g, cAccent[1], glow * 0.45)
let pb = lerp(b, cAccent[2], glow * 0.45)
const l1 = Math.abs(dx) + Math.abs(dy)
if (l1 <= diamondHalf) {
const edge = clamp255(Math.max(0, diamondHalf - l1) / diamondEdge)
pr = lerp(pr, 255, edge * 0.94)
pg = lerp(pg, 255, edge * 0.94)
pb = lerp(pb, 255, edge * 0.94)
}
const off = rowStart + 1 + x * 4
raw[off] = clamp255(pr)
raw[off + 1] = clamp255(pg)
raw[off + 2] = clamp255(pb)
raw[off + 3] = 255
}
}
// 星点(后画,覆盖在基色上)
const stars = []
for (let i = 0; i < 180; i++) {
stars.push({ x: rand() * W, y: rand() * H, r: 0.5 + rand() * 2.2, a: 0.12 + rand() * 0.35 })
}
for (const s of stars) {
const r = s.r + 1
const x0 = Math.max(0, Math.floor(s.x - r))
const x1 = Math.min(W - 1, Math.ceil(s.x + r))
const y0 = Math.max(0, Math.floor(s.y - r))
const y1 = Math.min(H - 1, Math.ceil(s.y + r))
for (let y = y0; y <= y1; y++) {
const rowStart = y * (1 + W * 4)
for (let x = x0; x <= x1; x++) {
if (Math.hypot(s.x - x, s.y - y) > s.r) continue
const off = rowStart + 1 + x * 4
raw[off] = clamp255(lerp(raw[off], 255, s.a))
raw[off + 1] = clamp255(lerp(raw[off + 1], 255, s.a))
raw[off + 2] = clamp255(lerp(raw[off + 2], 255, s.a))
}
}
}
// ---------- 组装 PNG ----------
const ihdr = Buffer.alloc(13)
ihdr.writeUInt32BE(W, 0)
ihdr.writeUInt32BE(H, 4)
ihdr[8] = 8 // bit depth
ihdr[9] = 6 // color type: RGBA
ihdr[10] = 0 // compression
ihdr[11] = 0 // filter
ihdr[12] = 0 // interlace
const png = Buffer.concat([
Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
chunk('IHDR', ihdr),
chunk('IDAT', deflateSync(raw, { level: 9 })),
chunk('IEND', Buffer.alloc(0)),
])
writeFileSync('public/og-image.png', png)
console.log(`og-image.png generated: ${W}x${H}, ${png.length} bytes`)