Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(docs,blog,pages): add support for "unlisted" front matter - hide md content in production #8004

Merged
merged 54 commits into from Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
e0acbd0
Exclude unlisted docs
jodyheavener Aug 24, 2022
9e19e0f
Exclude unlisted pages
jodyheavener Aug 24, 2022
760dca9
Exclude unlisted blog posts
jodyheavener Aug 24, 2022
cdb1822
Merge branch 'main' into jh/unlisted
slorber Oct 21, 2022
d03283d
dedup unlisted/draft logic
slorber Oct 21, 2022
f25969a
add page unlisted metadata
slorber Oct 21, 2022
62b1ecf
use unlisted metadata in mdx page
slorber Oct 21, 2022
bda2d10
optimize unlisted global data size: no attribute if unlisted false/un…
slorber Oct 21, 2022
8b84d30
deduplicate blog sidebar item filtering
slorber Oct 21, 2022
de872c2
improve project draft/unlisted dogfood
slorber Oct 21, 2022
0aa11e1
extract theme visibility filtering logic to theme-common
slorber Oct 21, 2022
c92b5ab
refactor DocSidebarItems
slorber Oct 21, 2022
149382d
revert category return null
slorber Oct 21, 2022
66e1d8f
revert useless Link changes
slorber Oct 21, 2022
799c6dc
blog post metadata is required
slorber Oct 21, 2022
82a93f1
revert weird sortPosts dogfood
slorber Oct 21, 2022
f488c50
remove linebreak
slorber Oct 21, 2022
fc6d0cc
Merge branch 'main' into jh/unlisted
slorber Oct 27, 2022
c4430ae
exclude unlisted blog posts from feed
slorber Oct 27, 2022
55dd501
Add unlisted tag dogfooding
slorber Oct 27, 2022
80a110a
add better visibility dogfooding + ability to simulate prod content v…
slorber Oct 27, 2022
1da33a3
extract toSidebarDocItemLinkProp, fix edge case + add test to prevent…
slorber Oct 27, 2022
bb1b17b
Add support for unlisted blog tags
slorber Oct 27, 2022
0266f6d
Add support for unlisted blog tags
slorber Oct 27, 2022
94de481
Add theme <Unlisted/> banner/metadata component
slorber Oct 27, 2022
6fef19e
fix test
slorber Oct 27, 2022
ebbf832
improve dogfooding for visibility
slorber Oct 27, 2022
56006ae
replicate unlisted tag logic to docs, extract getTagVisibility
slorber Oct 27, 2022
1b1bb83
add getTagVisibility test + fix bug
slorber Oct 27, 2022
b24a2a0
filter unlisted tags from the docs tags index
slorber Oct 27, 2022
73e28be
fix blog test snapshots
slorber Oct 27, 2022
6bf9f89
fix docs tests snapshots
slorber Oct 27, 2022
a887b9b
Merge branch 'main' into jh/unlisted
slorber Oct 28, 2022
b750f12
fix addDocNavigation => unlistedIds should be computed inside
slorber Oct 28, 2022
0746def
refactor method with 4 params to take 1 object param, use Set instead…
slorber Oct 28, 2022
b8d802a
fix MDX page unlisted position
slorber Oct 28, 2022
fa5823f
draftIds comment
slorber Oct 28, 2022
810ad5d
blog processing: make it use Error Cause like docs
slorber Oct 28, 2022
c974473
add error cause for page source file processing
slorber Oct 28, 2022
b56ba7f
add page draft support
slorber Oct 28, 2022
2730365
add "unlisted" doc + missing FrontMatter doc section for pages plugin
slorber Oct 28, 2022
59d3453
Merge branch 'main' into jh/unlisted
slorber Nov 2, 2022
7ad04e0
improve unlisted default messages
slorber Nov 2, 2022
86801b3
improve unlisted frontMatter docs
slorber Nov 2, 2022
3bf18c5
shared validation of draft/unlisted + add "draft XOR unlisted" rule
slorber Nov 2, 2022
7dde1eb
add more dogfood cases for unlisted/draft category indexes
slorber Nov 3, 2022
d81b518
filter unlisted category index from navigation
slorber Nov 3, 2022
d720989
better unlisted tests covering more edge cases
slorber Nov 3, 2022
cddd5ec
remove useless extra env param
slorber Nov 3, 2022
638dbe3
extract useVisibleSidebarItems
slorber Nov 3, 2022
54f1dc7
category doc link should be removed if linking to an unlisted doc
slorber Nov 3, 2022
5a2726d
add limitation comment
slorber Nov 3, 2022
6840195
fix draft dogfood typo
slorber Nov 3, 2022
3c81af8
breadcrumb of unlisted category index shouldn't be clickable
slorber Nov 3, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view

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>
slorber marked this conversation as resolved.
Show resolved Hide resolved
<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
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,
slorber marked this conversation as resolved.
Show resolved Hide resolved
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