Skip to content

Commit 2ac9bb6

Browse files
authored
feat: use Notion page title for announcements (#4456)
* feat: use Notion page title for announcements Use the Notice page title as the announcement title across supported themes, while preserving the existing localized title as a fallback. Resolves #3159 * docs: document announcement title behavior
1 parent de7bd19 commit 2ac9bb6

14 files changed

Lines changed: 72 additions & 11 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { render, screen } from '@testing-library/react'
2+
import CommerceAnnouncement from '@/themes/commerce/components/Announcement'
3+
import ExampleAnnouncement from '@/themes/example/components/Announcement'
4+
import FukasawaAnnouncement from '@/themes/fukasawa/components/Announcement'
5+
import FuwariAnnouncement from '@/themes/fuwari/components/Announcement'
6+
import HexoAnnouncement from '@/themes/hexo/components/Announcement'
7+
import MateryAnnouncement from '@/themes/matery/components/Announcement'
8+
import MovieAnnouncement from '@/themes/movie/components/Announcement'
9+
import NextAnnouncement from '@/themes/next/components/Announcement'
10+
import ThoughtliteAnnouncement from '@/themes/thoughtlite/components/Announcement'
11+
import XuhomeAnnouncement from '@/themes/xuhome/components/Announcement'
12+
13+
jest.mock('@/lib/global', () => ({
14+
useGlobal: () => ({
15+
locale: {
16+
COMMON: {
17+
ANNOUNCEMENT: 'Announcement fallback'
18+
}
19+
}
20+
})
21+
}))
22+
23+
const notice = {
24+
title: 'Custom notice title',
25+
blockMap: { block: {} }
26+
}
27+
28+
describe('announcement titles', () => {
29+
test.each([
30+
['commerce', CommerceAnnouncement, 'post'],
31+
['example', ExampleAnnouncement, 'post'],
32+
['fukasawa', FukasawaAnnouncement, 'post'],
33+
['hexo', HexoAnnouncement, 'post'],
34+
['matery', MateryAnnouncement, 'notice'],
35+
['movie', MovieAnnouncement, 'post'],
36+
['next', NextAnnouncement, 'post'],
37+
['thoughtlite', ThoughtliteAnnouncement, 'post'],
38+
['xuhome', XuhomeAnnouncement, 'post']
39+
])('%s uses the Notion Notice page title', (_name, Component, propName) => {
40+
render(<Component {...{ [propName]: notice }} />)
41+
42+
expect(screen.getByText(/Custom notice title/)).toBeInTheDocument()
43+
})
44+
45+
it('fuwari prefers the Notion Notice page title over its fallback title prop', () => {
46+
render(<FuwariAnnouncement post={notice} title='Announcement fallback' />)
47+
48+
expect(screen.getByText(/Custom notice title/)).toBeInTheDocument()
49+
expect(screen.queryByText('Announcement fallback')).not.toBeInTheDocument()
50+
})
51+
52+
it('keeps the localized fallback when the Notice page has no title', () => {
53+
render(<HexoAnnouncement post={{ blockMap: { block: {} } }} />)
54+
55+
expect(screen.getByText('Announcement fallback')).toBeInTheDocument()
56+
})
57+
})

docs/developer/THEME_MIGRATION_GUIDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ When migrating a new theme, verify all of these:
5757

5858
2. **Notice/announcement block**
5959
- Render Notion content using `NotionPage`.
60+
- If a separate announcement title is rendered, prefer the Notice page `title` and preserve the theme's existing title as a fallback.
6061
- Switchable in theme config.
6162

6263
3. **Notion cover as Hero**

docs/developer/THEME_MIGRATION_GUIDE.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757

5858
2. **公告模块**
5959
- 使用 `NotionPage` 渲染公告内容
60+
- 如展示独立公告标题,应优先使用 Notice 页面的 `title`,并保留主题原有标题作为 fallback
6061
- 可通过主题配置开关启停
6162

6263
3. **Notion 封面作为 Hero**

docs/user-guide/config/notionnext-notice.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ NotionNext 3.10以后的版本开始支持网站公告,欢迎更新体验。
3737

3838
添加一个Type(类型)为Notice的页面即可,此页面是唯一的,网站只能有一个公告。
3939

40+
对于展示独立公告标题的主题,Notice 页面的标题会优先作为公告标题显示;如果没有可用标题,则继续使用主题原有的「公告 / Announcement」等默认或本地化标题。
41+
4042
![Untitled](/legacy/56d8cd9cf2c6ec48.png)
4143

4244

@@ -59,7 +61,7 @@ NotionNext 3.10以后的版本开始支持网站公告,欢迎更新体验。
5961
```HTML
6062
&lt;div className={className}&gt;
6163
&lt;section id='announcement-wrapper' className="dark:text-gray-300 border dark:border-black rounded-xl lg:p-6 p-4 bg-white dark:bg-hexo-black-gray"&gt;
62-
&lt;div&gt;&lt;i className='mr-2 fas fa-bullhorn' /&gt;{locale.COMMON.ANNOUNCEMENT}&lt;/div&gt;
64+
&lt;div&gt;&lt;i className='mr-2 fas fa-bullhorn' /&gt;{post?.title || locale.COMMON.ANNOUNCEMENT}&lt;/div&gt;
6365
{post && (&lt;div id="announcement-content"&gt;
6466
&lt;NotionPage post={post} className='text-center' /&gt;
6567
&lt;/div&gt;)}

themes/commerce/components/Announcement.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const Announcement = ({ post, className }) => {
88
if (post?.blockMap) {
99
return <div className={className}>
1010
<section id='announcement-wrapper' className="dark:text-gray-300 border dark:border-black rounded-xl lg:p-6 p-4 bg-white dark:bg-hexo-black-gray">
11-
<div><i className='mr-2 fas fa-bullhorn' />{locale.COMMON.ANNOUNCEMENT}</div>
11+
<div><i className='mr-2 fas fa-bullhorn' />{post?.title || locale.COMMON.ANNOUNCEMENT}</div>
1212
{post && (<div id="announcement-content">
1313
<NotionPage post={post} className='text-center' />
1414
</div>)}

themes/example/components/Announcement.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const Announcement = ({ post, className }) => {
1818
<aside className='rounded shadow overflow-hidden mb-6'>
1919
<h3 className='text-sm bg-gray-100 text-gray-700 dark:bg-hexo-black-gray dark:text-gray-200 py-3 px-4 dark:border-hexo-black-gray border-b'>
2020
<i className='mr-2 fas fa-bullhorn' />
21-
{locale.COMMON.ANNOUNCEMENT}
21+
{post?.title || locale.COMMON.ANNOUNCEMENT}
2222
</h3>
2323

2424
{post && (

themes/fukasawa/components/Announcement.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const Announcement = ({ post, className }) => {
88
if (post?.blockMap) {
99
return <div className={className}>
1010
<section id='announcement-wrapper' className="dark:text-gray-300 rounded-xl px-2 py-4">
11-
<div><i className='mr-2 fas fa-bullhorn' />{locale.COMMON.ANNOUNCEMENT}</div>
11+
<div><i className='mr-2 fas fa-bullhorn' />{post?.title || locale.COMMON.ANNOUNCEMENT}</div>
1212
{post && (<div id="announcement-content">
1313
<NotionPage post={post} className='text-center ' />
1414
</div>)}

themes/fuwari/components/Announcement.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const Announcement = ({ post, className = '', title = 'Announcement' }) => {
88
return (
99
<section className={`fuwari-card ${className}`}>
1010
<h2 className='text-sm font-semibold mb-2 tracking-wide uppercase text-[var(--fuwari-muted)]'>
11-
{title}
11+
{post?.title || title}
1212
</h2>
1313
<div id='announcement-content' className='text-sm'>
1414
<NotionPage post={post} />

themes/hexo/components/Announcement.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const Announcement = ({ post, className }) => {
88
if (post?.blockMap) {
99
return <div className={className}>
1010
<section id='announcement-wrapper' className="dark:text-gray-300 border dark:border-black rounded-xl lg:p-6 p-4 bg-white dark:bg-hexo-black-gray">
11-
<div><i className='mr-2 fas fa-bullhorn' />{locale.COMMON.ANNOUNCEMENT}</div>
11+
<div><i className='mr-2 fas fa-bullhorn' />{post?.title || locale.COMMON.ANNOUNCEMENT}</div>
1212
{post && (<div id="announcement-content">
1313
<NotionPage post={post} className='text-center' />
1414
</div>)}

themes/matery/components/Announcement.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const Announcement = ({ notice }) => {
1717
className="mb-4 p-2 overflow-auto shadow-md border dark:border-black rounded-xl bg-white dark:bg-hexo-black-gray">
1818
<div className="text-sm flex flex-nowrap justify-between">
1919
<div className="font-light text-gray-600 dark:text-gray-200">
20-
<i className="mx-2 fas fa-bullhorn" />{locale.COMMON.ANNOUNCEMENT}
20+
<i className="mx-2 fas fa-bullhorn" />{notice?.title || locale.COMMON.ANNOUNCEMENT}
2121
</div>
2222
</div>
2323
{notice && (<div id="announcement-content">

0 commit comments

Comments
 (0)