Skip to content

Commit

Permalink
Exclude unlisted blog posts
Browse files Browse the repository at this point in the history
  • Loading branch information
jodyheavener committed Aug 27, 2022
1 parent 9e19e0f commit 760dca9
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 36 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -50,6 +50,14 @@ exports[`atom has feed item for each post 1`] = `
<uri>https://sebastienlorber.com</uri>
</author>
</entry>
<entry>
<title type="html"><![CDATA[unlisted]]></title>
<id>/unlisted</id>
<link href="https://docusaurus.io/myBaseUrl/blog/unlisted"/>
<updated>2020-02-27T00:00:00.000Z</updated>
<summary type="html"><![CDATA[this post is unlisted]]></summary>
<content type="html"><![CDATA[<p>this post is unlisted</p>]]></content>
</entry>
<entry>
<title type="html"><![CDATA[some heading]]></title>
<id>/heading-as-title</id>
Expand Down Expand Up @@ -135,6 +143,15 @@ exports[`json has feed item for each post 1`] = `
},
"tags": []
},
{
"id": "/unlisted",
"content_html": "<p>this post is unlisted</p>",
"url": "https://docusaurus.io/myBaseUrl/blog/unlisted",
"title": "unlisted",
"summary": "this post is unlisted",
"date_modified": "2020-02-27T00:00:00.000Z",
"tags": []
},
{
"id": "/heading-as-title",
"content_html": "",
Expand Down Expand Up @@ -218,6 +235,14 @@ exports[`rss has feed item for each post 1`] = `
<description><![CDATA[simple url slug]]></description>
<content:encoded><![CDATA[<p>simple url slug</p>]]></content:encoded>
</item>
<item>
<title><![CDATA[unlisted]]></title>
<link>https://docusaurus.io/myBaseUrl/blog/unlisted</link>
<guid>/unlisted</guid>
<pubDate>Thu, 27 Feb 2020 00:00:00 GMT</pubDate>
<description><![CDATA[this post is unlisted]]></description>
<content:encoded><![CDATA[<p>this post is unlisted</p>]]></content:encoded>
</item>
<item>
<title><![CDATA[some heading]]></title>
<link>https://docusaurus.io/myBaseUrl/blog/heading-as-title</link>
Expand Down
Expand Up @@ -365,6 +365,21 @@ describe('validateBlogPostFrontMatter draft', () => {
});
});

describe('validateBlogPostFrontMatter unlisted', () => {
testField({
fieldName: 'unlisted',
validFrontMatters: [{unlisted: true}, {unlisted: false}],
convertibleFrontMatter: [
[{unlisted: 'true'}, {unlisted: true}],
[{unlisted: 'false'}, {unlisted: false}],
],
invalidFrontMatters: [
[{unlisted: 'yes'}, 'must be a boolean'],
[{unlisted: 'no'}, 'must be a boolean'],
],
});
});

describe('validateBlogPostFrontMatter hide_table_of_contents', () => {
testField({
fieldName: 'hide_table_of_contents',
Expand Down
Expand Up @@ -172,6 +172,7 @@ describe('blog plugin', () => {
title: 'Happy 1st Birthday Slash! (translated)',
},
hasTruncateMarker: false,
unlisted: false,
});

expect(
Expand Down Expand Up @@ -215,6 +216,7 @@ describe('blog plugin', () => {
title: 'date-matter',
},
hasTruncateMarker: false,
unlisted: false,
});

expect({
Expand Down Expand Up @@ -252,6 +254,7 @@ describe('blog plugin', () => {
},
],
hasTruncateMarker: false,
unlisted: false,
});

expect({
Expand Down Expand Up @@ -289,6 +292,7 @@ describe('blog plugin', () => {
},
tags: [],
hasTruncateMarker: false,
unlisted: false,
});

expect({
Expand All @@ -314,13 +318,14 @@ describe('blog plugin', () => {
title: 'date-matter',
},
hasTruncateMarker: false,
unlisted: false,
});
});

it('builds simple website blog with localized dates', async () => {
const siteDir = path.join(__dirname, '__fixtures__', 'website');
const blogPostsFrench = await getBlogPosts(siteDir, {}, getI18n('fr'));
expect(blogPostsFrench).toHaveLength(8);
expect(blogPostsFrench).toHaveLength(9);
expect(blogPostsFrench[0]!.metadata.formattedDate).toMatchInlineSnapshot(
`"6 mars 2021"`,
);
Expand All @@ -337,13 +342,13 @@ describe('blog plugin', () => {
`"27 février 2020"`,
);
expect(blogPostsFrench[5]!.metadata.formattedDate).toMatchInlineSnapshot(
`"2 janvier 2019"`,
`"27 février 2020"`,
);
expect(blogPostsFrench[6]!.metadata.formattedDate).toMatchInlineSnapshot(
`"1 janvier 2019"`,
`"2 janvier 2019"`,
);
expect(blogPostsFrench[7]!.metadata.formattedDate).toMatchInlineSnapshot(
`"14 décembre 2018"`,
`"1 janvier 2019"`,
);
});

Expand Down Expand Up @@ -372,7 +377,7 @@ describe('blog plugin', () => {
expect(blogPost.metadata.editUrl).toEqual(hardcodedEditUrl);
});

expect(editUrlFunction).toHaveBeenCalledTimes(8);
expect(editUrlFunction).toHaveBeenCalledTimes(9);

expect(editUrlFunction).toHaveBeenCalledWith({
blogDirPath: 'blog',
Expand Down Expand Up @@ -471,6 +476,7 @@ describe('blog plugin', () => {
prevItem: undefined,
nextItem: undefined,
hasTruncateMarker: false,
unlisted: false,
});
});

Expand Down
17 changes: 16 additions & 1 deletion packages/docusaurus-plugin-content-blog/src/blogUtils.ts
Expand Up @@ -33,6 +33,7 @@ import type {
BlogPost,
BlogTags,
BlogPaginated,
BlogPostFrontMatter,
} from '@docusaurus/plugin-content-blog';
import type {BlogContentPaths, BlogMarkdownLoaderOptions} from './types';

Expand Down Expand Up @@ -186,6 +187,16 @@ async function parseBlogPostMarkdownFile(blogSourceAbsolute: string) {
const defaultReadingTime: ReadingTimeFunction = ({content, options}) =>
readingTime(content, options).minutes;

function isHiddenForProduction({
type,
frontMatter,
}: {
type: 'draft' | 'unlisted';
frontMatter: BlogPostFrontMatter;
}): boolean {
return (process.env.NODE_ENV === 'production' && frontMatter[type]) ?? false;
}

async function processBlogSourceFile(
blogSourceRelative: string,
contentPaths: BlogContentPaths,
Expand Down Expand Up @@ -219,7 +230,10 @@ async function processBlogSourceFile(

const aliasedSource = aliasedSitePath(blogSourceAbsolute, siteDir);

if (frontMatter.draft && process.env.NODE_ENV === 'production') {
const draft = isHiddenForProduction({type: 'draft', frontMatter});
const unlisted = isHiddenForProduction({type: 'unlisted', frontMatter});

if (draft) {
return undefined;
}

Expand Down Expand Up @@ -326,6 +340,7 @@ async function processBlogSourceFile(
hasTruncateMarker: truncateMarker.test(content),
authors,
frontMatter,
unlisted,
},
content,
};
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-content-blog/src/frontMatter.ts
Expand Up @@ -33,6 +33,7 @@ const BlogFrontMatterSchema = Joi.object<BlogPostFrontMatter>({
description: Joi.string().allow(''),
tags: FrontMatterTagsSchema,
draft: Joi.boolean(),
unlisted: Joi.boolean(),
date: Joi.date().raw(),

// New multi-authors front matter:
Expand Down
16 changes: 11 additions & 5 deletions packages/docusaurus-plugin-content-blog/src/index.ts
Expand Up @@ -112,6 +112,9 @@ export default async function pluginContentBlog(
const baseBlogUrl = normalizeUrl([baseUrl, routeBasePath]);
const blogTagsListPath = normalizeUrl([baseBlogUrl, tagsBasePath]);
const blogPosts = await generateBlogPosts(contentPaths, context, options);
const listedBlogPosts = blogPosts.filter(
(blogPost) => !blogPost.metadata.unlisted,
);

if (!blogPosts.length) {
return {
Expand All @@ -125,8 +128,8 @@ export default async function pluginContentBlog(
}

// Colocate next and prev metadata.
blogPosts.forEach((blogPost, index) => {
const prevItem = index > 0 ? blogPosts[index - 1] : null;
listedBlogPosts.forEach((blogPost, index) => {
const prevItem = index > 0 ? listedBlogPosts[index - 1] : null;
if (prevItem) {
blogPost.metadata.prevItem = {
title: prevItem.metadata.title,
Expand All @@ -135,7 +138,9 @@ export default async function pluginContentBlog(
}

const nextItem =
index < blogPosts.length - 1 ? blogPosts[index + 1] : null;
index < listedBlogPosts.length - 1
? listedBlogPosts[index + 1]
: null;
if (nextItem) {
blogPost.metadata.nextItem = {
title: nextItem.metadata.title,
Expand All @@ -145,15 +150,15 @@ export default async function pluginContentBlog(
});

const blogListPaginated: BlogPaginated[] = paginateBlogPosts({
blogPosts,
blogPosts: listedBlogPosts,
blogTitle,
blogDescription,
postsPerPageOption,
basePageUrl: baseBlogUrl,
});

const blogTags: BlogTags = getBlogTags({
blogPosts,
blogPosts: listedBlogPosts,
postsPerPageOption,
blogDescription,
blogTitle,
Expand Down Expand Up @@ -242,6 +247,7 @@ export default async function pluginContentBlog(
items: sidebarBlogPosts.map((blogPost) => ({
title: blogPost.metadata.title,
permalink: blogPost.metadata.permalink,
unlisted: blogPost.metadata.unlisted,
})),
},
null,
Expand Down

0 comments on commit 760dca9

Please sign in to comment.