feat(website): 对齐官网文章配置与展示顺序
This commit is contained in:
@@ -53,41 +53,35 @@ export function categoryError(name, categories, id) {
|
||||
return '';
|
||||
}
|
||||
|
||||
export const RELATED_LIMIT = 3;
|
||||
|
||||
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.
|
||||
// Publish date is a local calendar day "YYYY-MM-DD" shown on the website list cards.
|
||||
export function isValidPublishDate(value) {
|
||||
const match = /^(\d{4})-(\d{2})-(\d{2})(?: (\d{2}):(\d{2}):(\d{2}))?$/.exec(
|
||||
value,
|
||||
);
|
||||
const match = /^(\d{4})-(\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);
|
||||
const [year, month, day] = match.slice(1).map(Number);
|
||||
const date = new Date(year, month - 1, day);
|
||||
return (
|
||||
date.getFullYear() === year &&
|
||||
date.getMonth() === month - 1 &&
|
||||
date.getDate() === day &&
|
||||
date.getHours() === hour &&
|
||||
date.getMinutes() === minute &&
|
||||
date.getSeconds() === second
|
||||
date.getDate() === day
|
||||
);
|
||||
}
|
||||
|
||||
export function newArticle(category = 'all') {
|
||||
// Website list card: category / date / tag / title / summary.
|
||||
// Website detail page: category / title / summary / rich body (H2 table of contents) / related.
|
||||
export function newArticle(category = '') {
|
||||
const now = new Date();
|
||||
const date = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`;
|
||||
return {
|
||||
id: '',
|
||||
title: '',
|
||||
category,
|
||||
date,
|
||||
subtitle: '',
|
||||
category: category === 'all' ? '' : category,
|
||||
date: `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`,
|
||||
summary: '',
|
||||
tag: '',
|
||||
body: '',
|
||||
toc: true,
|
||||
related: [],
|
||||
};
|
||||
}
|
||||
@@ -96,8 +90,7 @@ export function articleErrors(article, insights) {
|
||||
const errors = {};
|
||||
for (const [key, label, limit, required] of [
|
||||
['title', '文章标题', 80, true],
|
||||
['summary', '卡片摘要', 200, true],
|
||||
['subtitle', '详情副标题', 160, false],
|
||||
['summary', '文章描述', 200, true],
|
||||
['tag', '角标标签', 12, false],
|
||||
]) {
|
||||
const text = String(article[key] ?? '').trim();
|
||||
@@ -105,10 +98,8 @@ export function articleErrors(article, insights) {
|
||||
else if (text.length > limit)
|
||||
errors[key] = `${label}不能超过 ${limit} 个字符`;
|
||||
}
|
||||
if (
|
||||
article.category !== 'all' &&
|
||||
!insights.categories.some((c) => c.id === article.category)
|
||||
)
|
||||
// Every article needs a real category: the site renders it as the tag, breadcrumb and list filter.
|
||||
if (!insights.categories.some((c) => c.id === article.category))
|
||||
errors.category = '请选择所属分类';
|
||||
const date = String(article.date ?? '');
|
||||
if (!isValidPublishDate(date))
|
||||
@@ -132,40 +123,40 @@ export function articleErrors(article, insights) {
|
||||
errors.body = '正文包含不支持的内容,请清除格式后重试';
|
||||
if (
|
||||
!Array.isArray(article.related) ||
|
||||
article.related.length > 3 ||
|
||||
article.related.length > RELATED_LIMIT ||
|
||||
new Set(article.related).size !== article.related.length ||
|
||||
article.related.some(
|
||||
(id) => id === article.id || !insights.articles.some((a) => a.id === id),
|
||||
)
|
||||
)
|
||||
errors.related = '最多选择 3 篇有效的其他文章';
|
||||
errors.related = `最多选择 ${RELATED_LIMIT} 篇有效的其他文章`;
|
||||
return errors;
|
||||
}
|
||||
|
||||
const ARTICLE_TEXT_FIELDS = [
|
||||
'id',
|
||||
'title',
|
||||
'category',
|
||||
'date',
|
||||
'summary',
|
||||
'tag',
|
||||
'body',
|
||||
];
|
||||
|
||||
// Articles keep only the website schema with trimmed text; other insights keys are preserved
|
||||
// per the page-config contract (unknown fields survive a save).
|
||||
export function normalizeInsights(value) {
|
||||
const data = value ?? emptyInsights();
|
||||
return {
|
||||
...data,
|
||||
categories: (data.categories || []).map((c) => ({
|
||||
...c,
|
||||
id: String(c.id),
|
||||
name: String(c.name ?? '').trim(),
|
||||
})),
|
||||
articles: (data.articles || []).map((a) => ({
|
||||
...a,
|
||||
...Object.fromEntries(
|
||||
[
|
||||
'id',
|
||||
'title',
|
||||
'category',
|
||||
'date',
|
||||
'subtitle',
|
||||
'summary',
|
||||
'tag',
|
||||
'body',
|
||||
].map((key) => [key, String(a[key] ?? '').trim()]),
|
||||
ARTICLE_TEXT_FIELDS.map((key) => [key, String(a[key] ?? '').trim()]),
|
||||
),
|
||||
toc: a.toc !== false,
|
||||
related: [...(a.related || [])],
|
||||
})),
|
||||
};
|
||||
@@ -207,13 +198,21 @@ export function removeArticle(insights, id) {
|
||||
};
|
||||
}
|
||||
|
||||
// Array order is the display order on the official site.
|
||||
export function filterArticles(insights, keyword, category) {
|
||||
const query = keyword.trim().toLocaleLowerCase();
|
||||
return insights.articles
|
||||
.filter(
|
||||
(a) =>
|
||||
(category === 'all' || a.category === category) &&
|
||||
`${a.title} ${a.summary}`.toLocaleLowerCase().includes(query),
|
||||
)
|
||||
.sort((a, b) => b.date.localeCompare(a.date));
|
||||
return insights.articles.filter(
|
||||
(a) =>
|
||||
(category === 'all' || a.category === category) &&
|
||||
`${a.title} ${a.summary}`.toLocaleLowerCase().includes(query),
|
||||
);
|
||||
}
|
||||
|
||||
export function moveArticle(insights, id, offset) {
|
||||
const from = insights.articles.findIndex((a) => a.id === id);
|
||||
const to = from + offset;
|
||||
if (from < 0 || to < 0 || to >= insights.articles.length) return insights;
|
||||
const articles = [...insights.articles];
|
||||
[articles[from], articles[to]] = [articles[to], articles[from]];
|
||||
return { ...insights, articles };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user