Skip to content

Commit 9252af7

Browse files
committed
Add tests + better untrucated blog post message
1 parent 3da8f60 commit 9252af7

3 files changed

Lines changed: 116 additions & 6 deletions

File tree

packages/docusaurus-plugin-content-blog/src/__tests__/blogUtils.test.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {jest} from '@jest/globals';
89
import {fromPartial} from '@total-typescript/shoehorn';
910
import {
1011
truncate,
1112
parseBlogFileName,
1213
paginateBlogPosts,
1314
applyProcessBlogPosts,
15+
reportUntruncatedBlogPosts,
1416
} from '../blogUtils';
1517
import type {BlogPost} from '@docusaurus/plugin-content-blog';
1618

@@ -32,6 +34,109 @@ describe('truncate', () => {
3234
});
3335
});
3436

37+
describe('reportUntruncatedBlogPosts', () => {
38+
function testPost({
39+
source,
40+
hasTruncateMarker,
41+
}: {
42+
source: string;
43+
hasTruncateMarker: boolean;
44+
}): BlogPost {
45+
return fromPartial({
46+
metadata: {
47+
source,
48+
hasTruncateMarker,
49+
},
50+
});
51+
}
52+
53+
it('throw for untruncated blog posts', () => {
54+
const blogPosts = [
55+
testPost({source: '@site/blog/post1.md', hasTruncateMarker: false}),
56+
testPost({source: '@site/blog/post2.md', hasTruncateMarker: true}),
57+
testPost({
58+
source: '@site/blog/subDir/post3.md',
59+
hasTruncateMarker: false,
60+
}),
61+
];
62+
expect(() =>
63+
reportUntruncatedBlogPosts({blogPosts, onUntruncatedBlogPosts: 'throw'}),
64+
).toThrowErrorMatchingInlineSnapshot(`
65+
"Docusaurus found blog posts without truncation markers:
66+
- "blog/post1.md"
67+
- "blog/subDir/post3.md"
68+
69+
We recommend using truncation markers (\`<!-- truncate -->\` or \`{/* truncate */}\`) in blog posts to create shorter previews on blog paginated lists.
70+
Tip: turn this security off with the \`onUntruncatedBlogPosts: 'ignore'\` blog plugin option."
71+
`);
72+
});
73+
74+
it('warn for untruncated blog posts', () => {
75+
const consoleMock = jest.spyOn(console, 'warn');
76+
77+
const blogPosts = [
78+
testPost({source: '@site/blog/post1.md', hasTruncateMarker: false}),
79+
testPost({source: '@site/blog/post2.md', hasTruncateMarker: true}),
80+
testPost({
81+
source: '@site/blog/subDir/post3.md',
82+
hasTruncateMarker: false,
83+
}),
84+
];
85+
expect(() =>
86+
reportUntruncatedBlogPosts({blogPosts, onUntruncatedBlogPosts: 'warn'}),
87+
).not.toThrow();
88+
89+
expect(consoleMock.mock.calls).toMatchInlineSnapshot(`
90+
[
91+
[
92+
"[WARNING] Docusaurus found blog posts without truncation markers:
93+
- "blog/post1.md"
94+
- "blog/subDir/post3.md"
95+
96+
We recommend using truncation markers (\`<!-- truncate -->\` or \`{/* truncate */}\`) in blog posts to create shorter previews on blog paginated lists.
97+
Tip: turn this security off with the \`onUntruncatedBlogPosts: 'ignore'\` blog plugin option.",
98+
],
99+
]
100+
`);
101+
consoleMock.mockRestore();
102+
});
103+
104+
it('ignore untruncated blog posts', () => {
105+
const logMock = jest.spyOn(console, 'log');
106+
const warnMock = jest.spyOn(console, 'warn');
107+
const errorMock = jest.spyOn(console, 'error');
108+
109+
const blogPosts = [
110+
testPost({source: '@site/blog/post1.md', hasTruncateMarker: false}),
111+
testPost({source: '@site/blog/post2.md', hasTruncateMarker: true}),
112+
testPost({
113+
source: '@site/blog/subDir/post3.md',
114+
hasTruncateMarker: false,
115+
}),
116+
];
117+
expect(() =>
118+
reportUntruncatedBlogPosts({blogPosts, onUntruncatedBlogPosts: 'ignore'}),
119+
).not.toThrow();
120+
121+
expect(logMock).not.toHaveBeenCalled();
122+
expect(warnMock).not.toHaveBeenCalled();
123+
expect(errorMock).not.toHaveBeenCalled();
124+
logMock.mockRestore();
125+
warnMock.mockRestore();
126+
errorMock.mockRestore();
127+
});
128+
129+
it('does not throw for truncated posts', () => {
130+
const blogPosts = [
131+
testPost({source: '@site/blog/post1.md', hasTruncateMarker: true}),
132+
testPost({source: '@site/blog/post2.md', hasTruncateMarker: true}),
133+
];
134+
expect(() =>
135+
reportUntruncatedBlogPosts({blogPosts, onUntruncatedBlogPosts: 'throw'}),
136+
).not.toThrow();
137+
});
138+
});
139+
35140
describe('paginateBlogPosts', () => {
36141
const blogPosts = [
37142
{id: 'post1', metadata: {}, content: 'Foo 1'},

packages/docusaurus-plugin-content-blog/src/blogUtils.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ export function reportUntruncatedBlogPosts({
5959
(p) => !p.metadata.hasTruncateMarker,
6060
);
6161
if (onUntruncatedBlogPosts !== 'ignore' && untruncatedBlogPosts.length > 0) {
62-
const message = `Docusaurus found untruncated blog posts:
63-
${untruncatedBlogPosts
64-
.map((p) => aliasedSitePathToRelativePath(p.metadata.source))
65-
.join('\n- ')}
66-
You can turn off this settings by setting onUntruncatedBlogPosts to 'ignore' in your docusaurus config file`;
62+
const message = logger.interpolate`Docusaurus found blog posts without truncation markers:
63+
- ${untruncatedBlogPosts
64+
.map((p) => logger.path(aliasedSitePathToRelativePath(p.metadata.source)))
65+
.join('\n- ')}
66+
67+
We recommend using truncation markers (code=${`<!-- truncate -->`} or code=${`{/* truncate */}`}) in blog posts to create shorter previews on blog paginated lists.
68+
Tip: turn this security off with the code=${`onUntruncatedBlogPosts: 'ignore'`} blog plugin option.`;
6769
logger.report(onUntruncatedBlogPosts)(message);
6870
}
6971
}

website/docusaurus.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,10 @@ export default async function createConfigAsync() {
496496
blogDescription: 'Read blog posts about Docusaurus from the team',
497497
blogSidebarCount: 'ALL',
498498
blogSidebarTitle: 'All our posts',
499-
onUntruncatedBlogPosts: 'throw',
499+
onUntruncatedBlogPosts:
500+
process.env.DOCUSAURUS_CURRENT_LOCALE !== defaultLocale
501+
? 'warn'
502+
: 'throw',
500503
onInlineTags:
501504
process.env.DOCUSAURUS_CURRENT_LOCALE !== defaultLocale
502505
? 'warn'

0 commit comments

Comments
 (0)