fix(排序): 同日文章 date 补全时分秒 + EdgeOne 部署重试增强
现象:小乖同一天发的第二篇排到了第一篇后面。
根因:front matter 的 date 只写到年月日(写入侧只收得到日期),
同一天多篇的时间戳完全并列 → 排序退化成比标题,前台/后台都错。
改动:
- 数据(6 篇):用目录名/slug 里的时间戳把 date 补全为
'YYYY-MM-DDTHH:MM:SS+08:00',slug 即写作时刻。每篇仅 1 行 diff。
2026-10-05 两篇(必须)、2026-06-02 / 06-03 四篇(同一 bug 的历史遗留)。
- 写入侧根治
- editor-api/src/posts.mjs:新建时「只到天」补北京时间此刻;
保存时「同日保真」——没换日子则原有时分秒一字符不动。
- write-server/src/lib/posts.ts:parseDate 按 Asia/Shanghai 归日,
列表同天排序不再受 UTC 时区漂移影响。
- 回归测试:新增 editor-api/test/date-time.mjs(8 项,自带一次性临时仓库)。
- .cnb.yml:EdgeOne stage 重试 3 次/20s → 5 次/30s。
构建节点到 api.edgeone.ai 偶发 15s 超时会被 CLI 误报
「Invalid EDGEONE_PAGES_API_TOKEN」,纯网络抖动,令牌没坏。
- .gitignore:忽略 *.zip 与 .edgeone/(误留的 blog.zip 达 361MB)。
This commit is contained in:
1 parent
5d16c8c8d6
commit
5e9d328841
12 files changed
+234
-15
No files matched your search
@@ -39,6 +39,43 @@ function parseDate(dateStr: string): Date {
|
||||
return isNaN(d.getTime()) ? new Date(0) : d;
|
||||
}
|
||||
|
||||
/**
|
||||
* date 补时分秒 —— 防止「同一天发的两篇文章排序颠倒」。
|
||||
*
|
||||
* 起因:PostEditor 的日期框是 <input type="date">,只拿得到 YYYY-MM-DD,提交时又
|
||||
* 被转成 `2026-10-05T00:00:00.000Z`。于是同一天发的两篇 date 完全一样,前台
|
||||
* (Hugo 按 Date 排)和后台(listPosts 按 date 排)都只剩标题能当兜底比较 ——
|
||||
* 结果 21:30 发的新文章排在了 11:34 发的旧文章**后面**(2026-10-05 实测)。
|
||||
*
|
||||
* 规则:date 只到「天」(或时刻正好是 00:00:00)→ 补上北京时间此刻的时分秒,
|
||||
* 写成带 +08:00 偏移的完整时间戳;已经带非零时刻的一律原样保留(不去动老文章)。
|
||||
*/
|
||||
const CN_OFFSET_MS = 8 * 60 * 60 * 1000;
|
||||
|
||||
/** 北京时间的 YYYY-MM-DDTHH:mm:ss(不带偏移) */
|
||||
function cnStamp(): string {
|
||||
return new Date(Date.now() + CN_OFFSET_MS).toISOString().slice(0, 19);
|
||||
}
|
||||
|
||||
/** 'YYYY-MM-DD' / 'YYYY-MM-DDTHH:mm:ss.000Z'(时刻为 0)→ 补当前时刻;其它原样返回 */
|
||||
function withTime(dateStr: unknown): string {
|
||||
const s = String(dateStr ?? "").trim();
|
||||
if (!s) return cnStamp() + "+08:00";
|
||||
const m = /^(\d{4}-\d{2}-\d{2})(?:[T ]00:00:00(?:\.000)?Z?)?$/.exec(s);
|
||||
if (!m) return s;
|
||||
return `${m[1]}T${cnStamp().slice(11)}+08:00`;
|
||||
}
|
||||
|
||||
/** 只取「哪一天」(北京时间的年月日),用来判断用户到底有没有改日子 */
|
||||
function dayKey(value: unknown): string {
|
||||
const d = value instanceof Date ? value : new Date(String(value ?? "").trim());
|
||||
if (!isNaN(d.getTime())) {
|
||||
return new Date(d.getTime() + CN_OFFSET_MS).toISOString().slice(0, 10);
|
||||
}
|
||||
const hit = /^(\d{4}-\d{2}-\d{2})/.exec(String(value ?? ""));
|
||||
return hit ? hit[1] : "";
|
||||
}
|
||||
|
||||
function findPostDirectories(): string[] {
|
||||
const result: string[] = [];
|
||||
if (!fs.existsSync(CONTENT_DIR)) return result;
|
||||
@@ -176,7 +213,7 @@ export function createPost(frontMatter: PostFrontMatter, content: string): strin
|
||||
|
||||
const fm: Record<string, unknown> = {
|
||||
title: frontMatter.title,
|
||||
date: frontMatter.date,
|
||||
date: withTime(frontMatter.date),
|
||||
slug: frontMatter.slug,
|
||||
draft: frontMatter.draft ?? false,
|
||||
categories: frontMatter.categories || [],
|
||||
@@ -202,6 +239,16 @@ export function updatePost(
|
||||
|
||||
const merged = { ...post.frontMatter, ...frontMatter };
|
||||
|
||||
// date 保真:日期框只有「年月日」,保存时会把 date 回写成当天 00:00,抹掉原有的
|
||||
// 时分秒 —— 那样同一天的文章又会撞成一片。只要「哪一天」没变,就沿用原来那串
|
||||
// (原值可能是 Date 对象,也可能是带偏移的字符串,都别动);换了日子才补新时刻。
|
||||
if (frontMatter.date !== undefined && post.frontMatter.date !== undefined) {
|
||||
merged.date =
|
||||
dayKey(frontMatter.date) === dayKey(post.frontMatter.date)
|
||||
? post.frontMatter.date
|
||||
: withTime(frontMatter.date);
|
||||
}
|
||||
|
||||
// If slug changed, we need to rename the directory
|
||||
if (frontMatter.slug && frontMatter.slug !== post.frontMatter.slug) {
|
||||
const newDate = new Date(merged.date || post.frontMatter.date);
|
||||
|
||||
Reference in new issue
Block a user