Skip to content

Commit

Permalink
Add tests + better untrucated blog post message
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Aug 9, 2024
1 parent 3da8f60 commit 9252af7
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
* LICENSE file in the root directory of this source tree.
*/

import {jest} from '@jest/globals';
import {fromPartial} from '@total-typescript/shoehorn';
import {
truncate,
parseBlogFileName,
paginateBlogPosts,
applyProcessBlogPosts,
reportUntruncatedBlogPosts,
} from '../blogUtils';
import type {BlogPost} from '@docusaurus/plugin-content-blog';

Expand All @@ -32,6 +34,109 @@ describe('truncate', () => {
});
});

describe('reportUntruncatedBlogPosts', () => {
function testPost({
source,
hasTruncateMarker,
}: {
source: string;
hasTruncateMarker: boolean;
}): BlogPost {
return fromPartial({
metadata: {
source,
hasTruncateMarker,
},
});
}

it('throw for untruncated blog posts', () => {
const blogPosts = [
testPost({source: '@site/blog/post1.md', hasTruncateMarker: false}),
testPost({source: '@site/blog/post2.md', hasTruncateMarker: true}),
testPost({
source: '@site/blog/subDir/post3.md',
hasTruncateMarker: false,
}),
];
expect(() =>
reportUntruncatedBlogPosts({blogPosts, onUntruncatedBlogPosts: 'throw'}),
).toThrowErrorMatchingInlineSnapshot(`
"Docusaurus found blog posts without truncation markers:
- "blog/post1.md"
- "blog/subDir/post3.md"
We recommend using truncation markers (\`<!-- truncate -->\` or \`{/* truncate */}\`) in blog posts to create shorter previews on blog paginated lists.
Tip: turn this security off with the \`onUntruncatedBlogPosts: 'ignore'\` blog plugin option."
`);
});

it('warn for untruncated blog posts', () => {
const consoleMock = jest.spyOn(console, 'warn');

const blogPosts = [
testPost({source: '@site/blog/post1.md', hasTruncateMarker: false}),
testPost({source: '@site/blog/post2.md', hasTruncateMarker: true}),
testPost({
source: '@site/blog/subDir/post3.md',
hasTruncateMarker: false,
}),
];
expect(() =>
reportUntruncatedBlogPosts({blogPosts, onUntruncatedBlogPosts: 'warn'}),
).not.toThrow();

expect(consoleMock.mock.calls).toMatchInlineSnapshot(`
[
[
"[WARNING] Docusaurus found blog posts without truncation markers:
- "blog/post1.md"
- "blog/subDir/post3.md"
We recommend using truncation markers (\`<!-- truncate -->\` or \`{/* truncate */}\`) in blog posts to create shorter previews on blog paginated lists.
Tip: turn this security off with the \`onUntruncatedBlogPosts: 'ignore'\` blog plugin option.",
],
]
`);
consoleMock.mockRestore();
});

it('ignore untruncated blog posts', () => {
const logMock = jest.spyOn(console, 'log');
const warnMock = jest.spyOn(console, 'warn');
const errorMock = jest.spyOn(console, 'error');

const blogPosts = [
testPost({source: '@site/blog/post1.md', hasTruncateMarker: false}),
testPost({source: '@site/blog/post2.md', hasTruncateMarker: true}),
testPost({
source: '@site/blog/subDir/post3.md',
hasTruncateMarker: false,
}),
];
expect(() =>
reportUntruncatedBlogPosts({blogPosts, onUntruncatedBlogPosts: 'ignore'}),
).not.toThrow();

expect(logMock).not.toHaveBeenCalled();
expect(warnMock).not.toHaveBeenCalled();
expect(errorMock).not.toHaveBeenCalled();
logMock.mockRestore();
warnMock.mockRestore();
errorMock.mockRestore();
});

it('does not throw for truncated posts', () => {
const blogPosts = [
testPost({source: '@site/blog/post1.md', hasTruncateMarker: true}),
testPost({source: '@site/blog/post2.md', hasTruncateMarker: true}),
];
expect(() =>
reportUntruncatedBlogPosts({blogPosts, onUntruncatedBlogPosts: 'throw'}),
).not.toThrow();
});
});

describe('paginateBlogPosts', () => {
const blogPosts = [
{id: 'post1', metadata: {}, content: 'Foo 1'},
Expand Down
12 changes: 7 additions & 5 deletions packages/docusaurus-plugin-content-blog/src/blogUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ export function reportUntruncatedBlogPosts({
(p) => !p.metadata.hasTruncateMarker,
);
if (onUntruncatedBlogPosts !== 'ignore' && untruncatedBlogPosts.length > 0) {
const message = `Docusaurus found untruncated blog posts:
${untruncatedBlogPosts
.map((p) => aliasedSitePathToRelativePath(p.metadata.source))
.join('\n- ')}
You can turn off this settings by setting onUntruncatedBlogPosts to 'ignore' in your docusaurus config file`;
const message = logger.interpolate`Docusaurus found blog posts without truncation markers:
- ${untruncatedBlogPosts
.map((p) => logger.path(aliasedSitePathToRelativePath(p.metadata.source)))
.join('\n- ')}
We recommend using truncation markers (code=${`<!-- truncate -->`} or code=${`{/* truncate */}`}) in blog posts to create shorter previews on blog paginated lists.
Tip: turn this security off with the code=${`onUntruncatedBlogPosts: 'ignore'`} blog plugin option.`;
logger.report(onUntruncatedBlogPosts)(message);
}
}
Expand Down
5 changes: 4 additions & 1 deletion website/docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,10 @@ export default async function createConfigAsync() {
blogDescription: 'Read blog posts about Docusaurus from the team',
blogSidebarCount: 'ALL',
blogSidebarTitle: 'All our posts',
onUntruncatedBlogPosts: 'throw',
onUntruncatedBlogPosts:
process.env.DOCUSAURUS_CURRENT_LOCALE !== defaultLocale
? 'warn'
: 'throw',
onInlineTags:
process.env.DOCUSAURUS_CURRENT_LOCALE !== defaultLocale
? 'warn'
Expand Down

0 comments on commit 9252af7

Please sign in to comment.