feat(news): 正文结构化 + 站内出口 + 实时阅读时长(Batch 2 内页证据密度)

Batch 2 的实现本体。补的是「内容」不是「语法」:`/news` 与 `/insights`
此前各只有 250 字左右的正文。

① 两篇公开正文 238/313 字 → 914/758 字,全部从既有真源派生(METHODOLOGY
   四阶段 / SERVICES 四组 / PRODUCTS 六款 / commitments.ts 响应承诺),
   **零新增客户案例、零新增效果数字**。
② 详情页正文由整块 `whitespace-pre-line` 改为小标题 + 段落
   (`parseArticleBlocks`,三条判据:单行 + ≤20 字 + 不以句末标点收尾 ——
   误判成标题比漏判更糟)。
③ 正文末尾新增站内公开入口(`NEWS_RELATED_LINKS`)。此前详情页是死胡同,
   只有「返回列表」与「预约咨询」。href 由单测**双向**反查:既命中
   PRODUCTS/SERVICES 常量,又要求文件系统里 page.tsx 真实存在。
④ 阅读时长由正文字数实时算出(450 字/分钟),不手写不落库。
⑤ `/insights` 卡片显式写出「收录于」哪个原生栏目。
   **不新增 `/insights/[slug]`** —— 聚合页只能读、不能拥有别的模型的实体,
   复制内容会造成 canonical 冲突。

判别性测试:`news-article.test.ts` 11 例 + `news-detail-content-v3.test.tsx` 4 例
(负对照:关掉解析器 ⇒ 3 条转红)+ e2e UJ-04/UJ-10 三条新断言
(负对照:注回整块渲染后 UJ-04 转红)。
This commit is contained in:
zhangxiang committed 2026-11-25 09:01:10 +08:00
1 parent 9c4dd576d5
commit 59e31c3f6e
12 files changed
+514 -18

No files matched your search

@@ -56,4 +56,26 @@ describe('洞察筛选器的分母纪律', () => {
expect(screen.getAllByTestId('insight-card-methodology')).toHaveLength(1);
expect(screen.queryAllByTestId('insight-card-writing')).toHaveLength(0);
});
it('卡片显式写出「收录于」哪个原生栏目(聚合页不冒充文章的所有者)', () => {
render(<InsightsContent entries={[METH, WRITING]} />);
const channels = screen.getAllByTestId('insight-channel').map((el) => el.textContent);
expect(channels).toHaveLength(2);
expect(channels.some((t) => t?.includes('方法论'))).toBe(true);
expect(channels.some((t) => t?.includes('技术与观点'))).toBe(true);
});
it('只有算了阅读时长的条目才显示分钟数(页面型条目不留「约 0 分钟」)', () => {
render(
<InsightsContent
entries={[
{ ...METH, readingMinutes: undefined },
{ ...WRITING, readingMinutes: 3 },
]}
/>,
);
const badges = screen.getAllByTestId('insight-reading-minutes');
expect(badges).toHaveLength(1);
expect(badges[0]!.textContent).toContain('约 3 分钟');
});
});
@@ -19,6 +19,11 @@ export interface InsightEntry {
date: string;
href: string;
industry?: string;
/**
* 阅读时长(分钟)。由正文实际字数算出(`src/lib/news-article.ts`),不是手写标注。
* 页面型条目(方法论)没有单篇正文,留空 ⇒ 卡片不显示该标签,而不是显示「约 0 分钟」。
*/
readingMinutes?: number;
}
const KIND_LABEL: Record<InsightKind, string> = {
@@ -27,6 +32,25 @@ const KIND_LABEL: Record<InsightKind, string> = {
writing: '技术与观点',
};
/**
* 每条洞察的**内容归属地**。
*
* 为什么要明说(2026-11-25 Batch 2):本页是聚合枢纽,但它展示的文章实际住在其
* 原生栏目里——技术观点在 `/news`、方法论在 `/methodology`、客户成果在 `/cases`。
* 此前卡片直接链到 `/news/<slug>`,而页面上没有任何地方说明这件事,
* 读者点「技术与观点」却落到「新闻动态」,看起来像跳错页;
* 想按路径族找 `/insights/<slug>` 的检查也永远命中 0。
*
* **不做 `/insights/[slug]` 的理由**:那会把同一个 ContentItem 同时挂在两个 URL 下,
* 制造 canonical 冲突——与本仓 `navigation.ts` 的「轴 = 视图,模型 = 真源」不变式冲突。
* 正确做法是把归属地写在脸上,而不是复制一份内容。
*/
const KIND_CHANNEL: Record<InsightKind, { label: string }> = {
methodology: { label: '方法论' },
case: { label: '客户成果' },
writing: { label: '技术与观点(新闻动态)' },
};
/**
* 洞察枢纽 —— 对标 Bain「Our Latest Insights」/ Porsche「In Focus: News & Trends」。
*
@@ -110,6 +134,11 @@ export default function InsightsContent({ entries }: { entries: InsightEntry[] }
{entry.industry && (
<span className="text-xs text-text-muted">· {entry.industry}</span>
)}
{entry.readingMinutes && (
<span className="text-xs text-text-muted tabular-nums" data-testid="insight-reading-minutes">
· 约 {entry.readingMinutes} 分钟
</span>
)}
{entry.date && (
<span className="ml-auto text-xs text-text-muted tabular-nums">{entry.date}</span>
)}
@@ -118,6 +147,10 @@ export default function InsightsContent({ entries }: { entries: InsightEntry[] }
{entry.excerpt && (
<p className="mt-2 text-sm text-text-secondary leading-relaxed">{entry.excerpt}</p>
)}
{/* 归属地显式写出:本页是聚合视图,文章本体住在原生栏目里(见 KIND_CHANNEL 注释) */}
<p className="mt-3 text-xs text-text-muted" data-testid="insight-channel">
收录于{KIND_CHANNEL[entry.kind].label}
</p>
<span className="mt-4 inline-flex items-center gap-1 text-sm font-medium text-text-primary group-hover:text-brand-ink">
阅读
<ArrowRight className="w-4 h-4" aria-hidden="true" />
+3
View File
@@ -2,6 +2,7 @@ import { Metadata } from 'next';
import { COMPANY_INFO } from '@/lib/constants';
import { getPublishedItems } from '@/lib/cms/data-server';
import type { NewsItem } from '@/lib/constants/news';
import { readingMinutes } from '@/lib/news-article';
import InsightsContent, { type InsightEntry } from './insights-content';
export const metadata: Metadata = {
@@ -31,6 +32,8 @@ async function buildEntries(): Promise<InsightEntry[]> {
excerpt: data.excerpt || '',
date: data.date || '',
href: `/news/${data.id || item.slug}`,
// 阅读时长由正文实际字数算出,与详情页顶部那枚标签同源(src/lib/news-article.ts)
readingMinutes: readingMinutes(data.content),
};
});
@@ -0,0 +1,85 @@
/**
* 新闻详情页的三条 UI 契约(Batch 2「内页证据密度」,2026-11-25):
* 1. 正文按「小标题 + 段落」渲染,而不是整块 `whitespace-pre-line`(扩写后后者会把
* 「我们是谁」「我们不承诺什么」与正文糊成同字号);
* 2. 正文末尾提供**站内公开入口**(此前详情页是死胡同,只有返回列表与预约咨询);
* 3. 阅读时长由正文字数实时算出,不手写。
*
* 每条都能失败:把 parseArticleBlocks 换回整块 pre-line、删掉 related links 区块、
* 把 readingMinutes 写死成常量,对应断言各自转红。
*/
import { describe, it, expect, jest } from '@jest/globals';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import NewsDetailContentV3 from './news-detail-content-v3';
import { NEWS, NEWS_RELATED_LINKS } from '@/lib/constants/news';
import { parseArticleBlocks } from '@/lib/news-article';
jest.mock('@/components/ui/scroll-reveal', () => ({
ScrollReveal: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
}));
jest.mock('@/components/ui/static-link', () => ({
StaticLink: ({ children, href, ...rest }: { children: React.ReactNode; href: string; [k: string]: unknown }) => (
<a href={href} {...rest}>{children}</a>
),
}));
jest.mock('@/components/ui/cta-button', () => ({
CTAButton: ({ children, href }: { children: React.ReactNode; href: string }) => <a href={href}>{children}</a>,
}));
jest.mock('@/components/ui/page-decoration', () => ({
SectionLabel: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
EASE_OUT: [0.22, 1, 0.36, 1],
}));
jest.mock('framer-motion', () => ({
motion: { div: ({ children }: { children: React.ReactNode }) => <div>{children}</div> },
}));
const realNews = NEWS[0]!;
describe('NewsDetailContentV3 —— 正文结构与出口', () => {
it('正文小标题渲染成 h2,段落渲染成 p(不再是一整块 pre-line 文本)', () => {
const { container } = render(<NewsDetailContentV3 news={realNews} />);
const headings = parseArticleBlocks(realNews.content).filter((b) => b.kind === 'heading');
// 前提:源文真的有标题(否则下面的断言是空转)
expect(headings.length).toBeGreaterThan(0);
const h2Texts = Array.from(container.querySelectorAll('h2')).map((el) => el.textContent);
for (const h of headings) {
expect(h2Texts).toContain(h.text);
}
});
it('正文里出现站内公开入口,且链接数 = 真源条数', () => {
render(<NewsDetailContentV3 news={realNews} />);
const block = screen.getByTestId('news-related-links');
expect(block).toBeInTheDocument();
const anchors = block.querySelectorAll('a');
expect(anchors).toHaveLength(NEWS_RELATED_LINKS.length);
for (const link of NEWS_RELATED_LINKS) {
expect(block.querySelector(`a[href="${link.href}"]`)).toBeTruthy();
}
});
it('阅读时长由正文字数算出,而不是写死的数字', () => {
const { unmount } = render(<NewsDetailContentV3 news={realNews} />);
const long = screen.getByTestId('news-reading-minutes').textContent ?? '';
unmount();
// 同一组件喂一篇**极短**正文,分钟数必须变小(写死常量时这条会红)
const shortNews = { ...realNews, content: '很短。' };
render(<NewsDetailContentV3 news={shortNews} />);
const short = screen.getByTestId('news-reading-minutes').textContent ?? '';
expect(long).toMatch(/约 \d+ 分钟/);
expect(short).toMatch(/约 1 分钟/);
expect(long).not.toBe(short);
});
it('粘贴进 HTML 标签的旧数据不会被当成标签渲染(按纯文本输出)', () => {
// CMS 里既有行可能有 `<p>` 包裹(e2e/cms-workflow 造的就是这种)。
// 组件走纯文本渲染 ⇒ 用户看到的是字面量,而不是被解析出的 DOM。
const news = { ...realNews, content: '<p>旧的 HTML 包装</p>' };
const { container } = render(<NewsDetailContentV3 news={news} />);
expect(container.querySelector('p > p')).toBeNull();
expect(screen.getByText('<p>旧的 HTML 包装</p>')).toBeInTheDocument();
});
});
@@ -5,10 +5,11 @@ import { StaticLink } from '@/components/ui/static-link';
import { CTAButton } from '@/components/ui/cta-button';
import { CTA_LABELS } from '@/lib/constants/cta-labels';
import { Badge } from '@/components/ui/badge';
import { Calendar, ArrowLeft, Newspaper } from 'lucide-react';
import { Calendar, ArrowLeft, Newspaper, ArrowUpRight } from 'lucide-react';
import { SectionLabel, EASE_OUT } from '@/components/ui/page-decoration';
import { motion } from 'framer-motion';
import type { NewsItem } from '@/lib/constants/news';
import { NEWS_RELATED_LINKS, type NewsItem } from '@/lib/constants/news';
import { parseArticleBlocks, readingMinutes } from '@/lib/news-article';
function RelatedNewsCard({ news, index }: { news: NewsItem; index: number }) {
return (
@@ -58,6 +59,8 @@ interface NewsDetailContentV3Props {
}
export default function NewsDetailContentV3({ news, relatedNews = [] }: NewsDetailContentV3Props) {
// 正文结构(标题 / 段落)在渲染期解析,不改数据模型 —— 理由见 src/lib/news-article.ts 顶部注释。
const blocks = parseArticleBlocks(news.content);
return (
<div className="min-h-screen bg-bg-primary text-ink overflow-x-hidden">
@@ -83,11 +86,15 @@ export default function NewsDetailContentV3({ news, relatedNews = [] }: NewsDeta
{news.title}
</h1>
<div className="flex items-center gap-6 mt-8 text-text-muted text-sm">
<div className="flex flex-wrap items-center gap-6 mt-8 text-text-muted text-sm">
<div className="flex items-center gap-2">
<Calendar className="w-4 h-4" />
{news.date}
</div>
{/* 阅读时长由正文字数实时算出(src/lib/news-article.ts),不是手写标注 */}
<span className="tabular-nums" data-testid="news-reading-minutes">
约 {readingMinutes(news.content)} 分钟
</span>
</div>
<div className="mt-10 h-px bg-gradient-to-r from-brand/50 via-border-primary to-transparent" />
@@ -122,8 +129,51 @@ export default function NewsDetailContentV3({ news, relatedNews = [] }: NewsDeta
</div>
{/* Article Content */}
<div className="text-text-secondary text-base sm:text-lg leading-[1.9] whitespace-pre-line">
{news.content}
<div className="text-text-secondary text-base sm:text-lg leading-[1.9]">
{blocks.map((block, i) =>
block.kind === 'heading' ? (
<h2
key={i}
className="mt-12 mb-4 text-xl sm:text-2xl font-bold text-text-primary tracking-tight first:mt-0"
>
{block.text}
</h2>
) : (
// 段落用 `whitespace-pre-line` 保留源文里的手工换行(列表项、序号行),
// 块与块之间的分隔改由 `<p>` 的 margin 承担 —— 与整块 pre-line 的区别就在这里。
<p key={i} className="mt-5 first:mt-0 whitespace-pre-line">
{block.text}
</p>
),
)}
</div>
{/* 正文提到的公开入口 —— 详情页此前是一条死胡同(只有「返回列表」与「预约咨询」) */}
<div className="mt-16 sm:mt-20 pt-10 border-t border-border-primary" data-testid="news-related-links">
<SectionLabel>继续阅读</SectionLabel>
{/* `list-plain` 不是装饰:globals 的 `ul li::before` 给所有列表加品牌红菱形,
四个入口各一颗就是 4 处红触达点凭空多出来(面积纪律按区算)。 */}
<ul className="list-plain grid gap-px bg-border-primary sm:grid-cols-2">
{NEWS_RELATED_LINKS.map((link) => (
<li key={link.href} className="bg-bg-primary">
<StaticLink
href={link.href}
className="group flex h-full flex-col gap-2 p-5 transition-colors duration-hover hover:bg-bg-secondary"
>
<span className="flex items-start justify-between gap-3">
<span className="text-base font-semibold text-text-primary leading-snug">
{link.title}
</span>
<ArrowUpRight
className="mt-0.5 h-4 w-4 shrink-0 text-brand-ink"
aria-hidden="true"
/>
</span>
<span className="text-sm text-text-secondary leading-relaxed">{link.reason}</span>
</StaticLink>
</li>
))}
</ul>
</div>
</ScrollReveal>