feat(website): 文章发布时间精确到秒
This commit is contained in:
@@ -53,9 +53,31 @@ export function categoryError(name, categories, id) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const pad = (value) => String(value).padStart(2, '0');
|
||||
|
||||
// Publish time is stored as local "YYYY-MM-DD HH:mm:ss"; legacy "YYYY-MM-DD" stays valid.
|
||||
export function isValidPublishDate(value) {
|
||||
const match = /^(\d{4})-(\d{2})-(\d{2})(?: (\d{2}):(\d{2}):(\d{2}))?$/.exec(
|
||||
value,
|
||||
);
|
||||
if (!match) return false;
|
||||
const [year, month, day, hour = 0, minute = 0, second = 0] = match
|
||||
.slice(1)
|
||||
.map((part) => (part === undefined ? undefined : Number(part)));
|
||||
const date = new Date(year, month - 1, day, hour, minute, second);
|
||||
return (
|
||||
date.getFullYear() === year &&
|
||||
date.getMonth() === month - 1 &&
|
||||
date.getDate() === day &&
|
||||
date.getHours() === hour &&
|
||||
date.getMinutes() === minute &&
|
||||
date.getSeconds() === second
|
||||
);
|
||||
}
|
||||
|
||||
export function newArticle(category = 'all') {
|
||||
const now = new Date();
|
||||
const date = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`;
|
||||
const date = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`;
|
||||
return {
|
||||
id: '',
|
||||
title: '',
|
||||
@@ -89,11 +111,7 @@ export function articleErrors(article, insights) {
|
||||
)
|
||||
errors.category = '请选择所属分类';
|
||||
const date = String(article.date ?? '');
|
||||
if (
|
||||
!/^\d{4}-\d{2}-\d{2}$/.test(date) ||
|
||||
!Number.isFinite(Date.parse(date)) ||
|
||||
new Date(date).toISOString().slice(0, 10) !== date
|
||||
)
|
||||
if (!isValidPublishDate(date))
|
||||
errors.date = date ? '请选择有效的发布日期' : '请选择发布日期';
|
||||
const body = String(article.body ?? '');
|
||||
if (
|
||||
|
||||
Reference in New Issue
Block a user