Commit 8a0f98ab authored by AI Bot's avatar AI Bot

时间线数据接入CMS:迁入 content/timeline.md

- 新增 content/timeline.md(frontmatter 配置 cn/en 时间线数据,结构与 frontPage.md 等一致,由"自动文章合并"流程统一维护)
- pages/index.js 删除静态 timeline-data.js 引用,componentDidMount 注入 window.TIMELINE_DATA
- addThreeD3.js 数据访问改为对象结构({year, events:[{date,text}]})
- build-timeline-font.js 数据源改为 timeline.md(js-yaml 解析);pyftsubset 缺失或字体包下载失败时沿用仓库已有 woff2 继续构建,不阻塞发布
Co-Authored-By: 's avatarClaude Code <noreply@anthropic.com>
parent 77630fec
This diff is collapsed.
......@@ -10,6 +10,7 @@ import IndexPortfolioCycle from '../components/IndexPortfolioCycle'
import Video from '../components/Video'
/* eslint-enable no-unused-vars */
import frontPage from '../content/frontPage.md'
import timelinePage from '../content/timeline.md'
import { ThemeDecorator } from './_app.js'
import './index.css'
......@@ -43,6 +44,10 @@ class FrontPage extends React.Component {
})
}
componentDidMount() {
// 时间线数据(CMS: content/timeline.md)必须在 addThreeD 初始化前就位
if (timelinePage && timelinePage.attributes && timelinePage.attributes.timeline) {
window.TIMELINE_DATA = timelinePage.attributes.timeline
}
window.addEventListener('resize', this.handleWindowResize)
this.handleWindowResize()
this.threeDTimer = setTimeout(this.handleAddThree, 500)
......@@ -115,11 +120,6 @@ class FrontPage extends React.Component {
defer="defer"
src="/static/three.min.js"
></script>
<script
key="timelineData"
defer="defer"
src="/static/timeline-data.js"
></script>
<script
key="addThreejs"
defer="defer"
......
// 时间线中文字体子集生成:从 static/timeline-data.js 提取所有用到字符,
// 时间线中文字体子集生成:从 content/timeline.md(CMS 数据源)提取所有用到字符,
// 用 pyftsubset 从 MiSans-Light.ttf 裁出 woff2 子集,输出到
// static/fonts/woff2/MiSans-Timeline-Light.woff2(base.css 中 @font-face 引用)。
//
// 用法: npm run timeline-font (或 node scripts/build-timeline-font.js)
// 用法: npm run build 时自动执行(或 node scripts/build-timeline-font.js)
// 依赖: python3 + fonttools + brotli (pip install fonttools brotli)
//
// timeline-data.js 新增文案后必须重跑本脚本,否则新字符会回退到系统字体。
// timeline.md 新增文案后构建时会自动重新子集化;若构建机缺少 pyftsubset,
// 则沿用仓库里已生成的 woff2 继续构建(新字符会回退系统字体,不阻塞发布)。
// MiSans 由小米以免费商用许可发布: https://hyperos.mi.com/font
const fs = require('fs')
const path = require('path')
const { execFileSync } = require('child_process')
const yaml = require('js-yaml')
const root = path.join(__dirname, '..')
const dataFile = path.join(root, 'static/timeline-data.js')
const dataFile = path.join(root, 'content/timeline.md')
const cacheDir = path.join(__dirname, '.font-cache')
const srcTtf = path.join(cacheDir, 'MiSans-Light.ttf')
const outFile = path.join(root, 'static/fonts/woff2/MiSans-Timeline-Light.woff2')
......@@ -21,14 +23,25 @@ const outFile = path.join(root, 'static/fonts/woff2/MiSans-Timeline-Light.woff2'
const TTF_URL = 'https://cdn.cnbj1.fds.api.mi-img.com/vipmlmodel/font/MiSans/MiSans.zip'
function extractChars() {
const src = fs.readFileSync(dataFile, 'utf8')
const raw = fs.readFileSync(dataFile, 'utf8')
const m = raw.match(/^---\r?\n([\s\S]*?)\r?\n---/)
if (!m) throw new Error('timeline.md 缺少 frontmatter')
const doc = yaml.load(m[1])
// 收集数据里所有字符串字符(year/date/text)
const chars = new Set()
for (const m of src.match(/'((?:[^'\\]|\\.)*)'/g) || []) {
for (const ch of m.slice(1, -1)) chars.add(ch)
const walk = (v) => {
if (typeof v === 'string') {
for (const ch of v) chars.add(ch)
} else if (Array.isArray(v)) {
v.forEach(walk)
} else if (v && typeof v === 'object') {
Object.values(v).forEach(walk)
}
// 兜底:ASCII 数字/字母/常用标点(年份、日期、证券代码用 IBM Plex Sans,
// 但子集里保留可避免数据改动时意外缺字)
for (const ch of '0123456789.:;,%()-·') chars.add(ch)
}
walk(doc.timeline)
// 兜底:ASCII 数字/常用标点(年份、日期、证券代码由 IBM Plex Sans 渲染,
// 子集里保留可避免数据改动时意外缺字)
for (const ch of '0123456789.:;,%()-·+') chars.add(ch)
return [...chars].sort().join('')
}
......@@ -49,7 +62,16 @@ function ensureSourceTtf() {
function main() {
const text = extractChars()
console.log('时间线数据共', text.length, '个唯一字符')
let ttfReady = true
try {
ensureSourceTtf()
} catch (e) {
// 构建机无法下载/解压字体源:只要仓库里有现成 woff2 就继续构建
ttfReady = false
console.warn('[timeline-font] 无法获取 MiSans-Light.ttf:', e.message)
}
if (ttfReady) {
try {
const charFile = path.join(cacheDir, 'chars.txt')
fs.writeFileSync(charFile, text)
execFileSync('pyftsubset', [
......@@ -61,6 +83,18 @@ function main() {
'--no-hinting',
'--desubroutinize'
], { stdio: 'inherit' })
} catch (e) {
ttfReady = false
console.warn('[timeline-font] pyftsubset 执行失败(需要 pip install fonttools brotli):', e.message)
}
}
if (!ttfReady) {
if (!fs.existsSync(outFile)) {
throw new Error('字体子集生成失败且 static/fonts/woff2/MiSans-Timeline-Light.woff2 不存在,请在本机构建一次并提交产物')
}
console.warn('[timeline-font] 沿用仓库中已有的子集文件,timeline.md 新增的字符将回退系统字体')
return
}
const size = (fs.statSync(outFile).size / 1024).toFixed(1)
console.log('已生成', outFile, '(' + size + ' KB)')
}
......
......@@ -177,11 +177,12 @@ function drawTimelineStrip(ctx, layout, data, sliceIndex, spriteW) {
}
// 2. 每年:年份数字 + 条目列表(块宽与换行宽按块取自原 PNG 实测值)
// 数据来自 content/timeline.md 的 frontmatter(CMS 可配),结构: [{year, events:[{date,text}]}]
var yearFontPx = layout.yearFontH / 0.72
var blockX = layout.firstBlockX
for (var y = 0; y < data.length; y++) {
var yearLabel = data[y][0]
var entries = data[y][1]
var yearLabel = data[y].year
var entries = data[y].events
var blockW = layout.blockWidths[y]
ctx.save()
......@@ -198,11 +199,11 @@ function drawTimelineStrip(ctx, layout, data, sliceIndex, spriteW) {
ctx.textBaseline = 'top'
var cursorY = layout.firstEntryTop
for (var e = 0; e < entries.length; e++) {
var dateStr = entries[e][0]
var dateStr = entries[e].date
if (layout.dropYearSuffix && /^\d{4}年$/.test(dateStr)) {
dateStr = dateStr.slice(0, 4)
}
var text = entries[e][1]
var text = entries[e].text
if (dateStr) {
ctx.fillText(dateStr, localX(blockX + layout.dateShift), cursorY)
}
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment