diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/blogUtils.test.ts b/packages/docusaurus-plugin-content-blog/src/__tests__/blogUtils.test.ts index a340ec61e79e..040240641127 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/blogUtils.test.ts +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/blogUtils.test.ts @@ -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'; @@ -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 (\`\` 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 (\`\` 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'}, diff --git a/packages/docusaurus-plugin-content-blog/src/blogUtils.ts b/packages/docusaurus-plugin-content-blog/src/blogUtils.ts index 65ffe5887fcd..ab7426eac589 100644 --- a/packages/docusaurus-plugin-content-blog/src/blogUtils.ts +++ b/packages/docusaurus-plugin-content-blog/src/blogUtils.ts @@ -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=${``} 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); } } diff --git a/website/docusaurus.config.ts b/website/docusaurus.config.ts index f4f41427031d..8668adef3a26 100644 --- a/website/docusaurus.config.ts +++ b/website/docusaurus.config.ts @@ -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'