From 3f79f5f7e8da46c85af6f55e3b0c18efdda1b1a9 Mon Sep 17 00:00:00 2001 From: Jody Heavener Date: Sun, 23 Jan 2022 19:09:39 -0400 Subject: [PATCH 01/15] feat(content-docs): exclude draft docs from builds, sidebars --- .../__fixtures__/simple-site/docs/draft.md | 5 + .../__tests__/__snapshots__/docs.test.ts.snap | 5 + .../__snapshots__/index.test.ts.snap | 154 ++++++++++++++++-- .../src/__tests__/docs.test.ts | 43 +++++ .../src/__tests__/frontMatter.test.ts | 16 ++ .../src/docs.ts | 4 + .../src/frontMatter.ts | 1 + .../src/plugin-content-docs.d.ts | 4 + .../src/routes.ts | 48 +++--- .../src/sidebars/__tests__/generator.test.ts | 42 +++++ .../src/sidebars/__tests__/processor.test.ts | 29 ++++ .../src/sidebars/generator.ts | 5 +- .../src/sidebars/processor.ts | 7 + .../src/sidebars/types.ts | 1 + website/_dogfooding/_docs tests/draft.md | 7 + .../_dogfooding/_docs tests/tests/draft.md | 7 + website/_dogfooding/docs-tests-sidebars.js | 1 + .../docs/api/plugins/plugin-content-docs.md | 1 + 18 files changed, 337 insertions(+), 43 deletions(-) create mode 100644 packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/simple-site/docs/draft.md create mode 100644 website/_dogfooding/_docs tests/draft.md create mode 100644 website/_dogfooding/_docs tests/tests/draft.md diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/simple-site/docs/draft.md b/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/simple-site/docs/draft.md new file mode 100644 index 000000000000..dfc31e3f3594 --- /dev/null +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/simple-site/docs/draft.md @@ -0,0 +1,5 @@ +--- +draft: true +--- + +This is a draft document diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/docs.test.ts.snap b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/docs.test.ts.snap index eaed1c90bbbb..fd9e3739fd70 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/docs.test.ts.snap +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/docs.test.ts.snap @@ -11,6 +11,11 @@ exports[`simple site custom pagination 1`] = ` }, "prev": undefined, }, + { + "id": "draft", + "next": undefined, + "prev": undefined, + }, { "id": "foo/bar", "next": undefined, diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap index 55f087ee4e7d..d3bca1a52bb7 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap @@ -23,6 +23,7 @@ These sidebar document ids do not exist: Available document ids are: - doc with space +- draft - foo/bar - foo/baz - headingAsTitle @@ -59,6 +60,7 @@ exports[`simple website content 1`] = ` "title": "baz", }, "id": "foo/baz", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -107,6 +109,7 @@ exports[`simple website content 2`] = ` "title": "Hello, World !", }, "id": "hello", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": undefined, @@ -149,6 +152,7 @@ exports[`simple website content 3`] = ` "title": "Bar", }, "id": "foo/bar", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": undefined, @@ -268,6 +272,11 @@ exports[`simple website content 5`] = ` "path": "/docs/doc with space", "sidebar": undefined, }, + { + "id": "draft", + "path": "/docs/draft", + "sidebar": undefined, + }, { "id": "foo/bar", "path": "/docs/foo/bar", @@ -392,7 +401,24 @@ exports[`simple website content: data 1`] = ` \\"permalink\\": \\"/docs/doc with space\\", \\"tags\\": [], \\"version\\": \\"current\\", - \\"frontMatter\\": {} + \\"frontMatter\\": {}, + \\"isDraft\\": false +}", + "site-docs-draft-md-5b1.json": "{ + \\"unversionedId\\": \\"draft\\", + \\"id\\": \\"draft\\", + \\"title\\": \\"draft\\", + \\"description\\": \\"This is a draft document\\", + \\"source\\": \\"@site/docs/draft.md\\", + \\"sourceDirName\\": \\".\\", + \\"slug\\": \\"/draft\\", + \\"permalink\\": \\"/docs/draft\\", + \\"tags\\": [], + \\"version\\": \\"current\\", + \\"frontMatter\\": { + \\"draft\\": true + }, + \\"isDraft\\": false }", "site-docs-foo-bar-md-8c2.json": "{ \\"unversionedId\\": \\"foo/bar\\", @@ -412,6 +438,7 @@ exports[`simple website content: data 1`] = ` \\"pagination_next\\": null, \\"pagination_prev\\": null }, + \\"isDraft\\": false, \\"sidebar\\": \\"docs\\" }", "site-docs-foo-baz-md-a69.json": "{ @@ -448,6 +475,7 @@ exports[`simple website content: data 1`] = ` } ] }, + \\"isDraft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"Bar\\", @@ -470,6 +498,7 @@ exports[`simple website content: data 1`] = ` \\"tags\\": [], \\"version\\": \\"current\\", \\"frontMatter\\": {}, + \\"isDraft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"rootTryToEscapeSlug\\", @@ -510,6 +539,7 @@ exports[`simple website content: data 1`] = ` ], \\"slug\\": \\"/\\" }, + \\"isDraft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"My heading as title\\", @@ -530,7 +560,8 @@ exports[`simple website content: data 1`] = ` \\"version\\": \\"current\\", \\"frontMatter\\": { \\"custom_edit_url\\": null - } + }, + \\"isDraft\\": false }", "site-docs-lorem-md-b27.json": "{ \\"unversionedId\\": \\"lorem\\", @@ -547,7 +578,8 @@ exports[`simple website content: data 1`] = ` \\"frontMatter\\": { \\"custom_edit_url\\": \\"https://github.com/customUrl/docs/lorem.md\\", \\"unrelated_front_matter\\": \\"won't be part of metadata\\" - } + }, + \\"isDraft\\": false }", "site-docs-root-absolute-slug-md-db5.json": "{ \\"unversionedId\\": \\"rootAbsoluteSlug\\", @@ -565,6 +597,7 @@ exports[`simple website content: data 1`] = ` \\"pagination_next\\": \\"headingAsTitle\\", \\"pagination_prev\\": \\"foo/baz\\" }, + \\"isDraft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"baz pagination_label\\", @@ -591,6 +624,7 @@ exports[`simple website content: data 1`] = ` \\"pagination_next\\": \\"headingAsTitle\\", \\"pagination_prev\\": \\"foo/baz\\" }, + \\"isDraft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"baz pagination_label\\", @@ -617,6 +651,7 @@ exports[`simple website content: data 1`] = ` \\"pagination_next\\": \\"headingAsTitle\\", \\"pagination_prev\\": \\"foo/baz\\" }, + \\"isDraft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"baz pagination_label\\", @@ -643,6 +678,7 @@ exports[`simple website content: data 1`] = ` \\"pagination_next\\": \\"headingAsTitle\\", \\"pagination_prev\\": \\"foo/baz\\" }, + \\"isDraft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"baz pagination_label\\", @@ -666,7 +702,8 @@ exports[`simple website content: data 1`] = ` \\"version\\": \\"current\\", \\"frontMatter\\": { \\"slug\\": \\"/absoluteSlug\\" - } + }, + \\"isDraft\\": false }", "site-docs-slugs-relative-slug-md-d1c.json": "{ \\"unversionedId\\": \\"slugs/relativeSlug\\", @@ -681,7 +718,8 @@ exports[`simple website content: data 1`] = ` \\"version\\": \\"current\\", \\"frontMatter\\": { \\"slug\\": \\"relativeSlug\\" - } + }, + \\"isDraft\\": false }", "site-docs-slugs-resolved-slug-md-02b.json": "{ \\"unversionedId\\": \\"slugs/resolvedSlug\\", @@ -696,7 +734,8 @@ exports[`simple website content: data 1`] = ` \\"version\\": \\"current\\", \\"frontMatter\\": { \\"slug\\": \\"./hey/ho/../resolvedSlug\\" - } + }, + \\"isDraft\\": false }", "site-docs-slugs-try-to-escape-slug-md-70d.json": "{ \\"unversionedId\\": \\"slugs/tryToEscapeSlug\\", @@ -711,7 +750,8 @@ exports[`simple website content: data 1`] = ` \\"version\\": \\"current\\", \\"frontMatter\\": { \\"slug\\": \\"../../../../../../../../tryToEscapeSlug\\" - } + }, + \\"isDraft\\": false }", "tag-docs-tags-tag-1-b3f.json": "{ \\"name\\": \\"tag 1\\", @@ -885,6 +925,11 @@ exports[`simple website content: data 1`] = ` \\"title\\": \\"Hoo hoo, if this path tricks you...\\", \\"description\\": \\"\\" }, + \\"draft\\": { + \\"id\\": \\"draft\\", + \\"title\\": \\"draft\\", + \\"description\\": \\"This is a draft document\\" + }, \\"foo/bar\\": { \\"id\\": \\"foo/bar\\", \\"title\\": \\"Bar\\", @@ -982,6 +1027,11 @@ exports[`simple website content: global data 1`] = ` "path": "/docs/doc with space", "sidebar": undefined, }, + { + "id": "draft", + "path": "/docs/draft", + "sidebar": undefined, + }, { "id": "foo/bar", "path": "/docs/foo/bar", @@ -1155,6 +1205,14 @@ exports[`simple website content: route config 1`] = ` }, "path": "/docs/doc with space", }, + { + "component": "@theme/DocItem", + "exact": true, + "modules": { + "content": "@site/docs/draft.md", + }, + "path": "/docs/draft", + }, { "component": "@theme/DocItem", "exact": true, @@ -1404,6 +1462,7 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle { "frontMatter": {}, "id": "API/Core APIs/Client API", + "isDraft": false, "sidebarPosition": 0, "source": "@site/docs/3-API/01_Core APIs/0 --- Client API.md", "sourceDirName": "3-API/01_Core APIs", @@ -1413,6 +1472,7 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle { "frontMatter": {}, "id": "API/Core APIs/Server API", + "isDraft": false, "sidebarPosition": 1, "source": "@site/docs/3-API/01_Core APIs/1 --- Server API.md", "sourceDirName": "3-API/01_Core APIs", @@ -1422,6 +1482,7 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle { "frontMatter": {}, "id": "API/Extension APIs/Plugin API", + "isDraft": false, "sidebarPosition": 0, "source": "@site/docs/3-API/02_Extension APIs/0. Plugin API.md", "sourceDirName": "3-API/02_Extension APIs", @@ -1431,6 +1492,7 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle { "frontMatter": {}, "id": "API/Extension APIs/Theme API", + "isDraft": false, "sidebarPosition": 1, "source": "@site/docs/3-API/02_Extension APIs/1. Theme API.md", "sourceDirName": "3-API/02_Extension APIs", @@ -1440,6 +1502,7 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle { "frontMatter": {}, "id": "API/api-end", + "isDraft": false, "sidebarPosition": 3, "source": "@site/docs/3-API/03_api-end.md", "sourceDirName": "3-API", @@ -1449,6 +1512,7 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle { "frontMatter": {}, "id": "API/api-overview", + "isDraft": false, "sidebarPosition": 0, "source": "@site/docs/3-API/00_api-overview.md", "sourceDirName": "3-API", @@ -1461,6 +1525,7 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle "sidebar_position": 1, }, "id": "Guides/guide1", + "isDraft": false, "sidebarPosition": 1, "source": "@site/docs/Guides/z-guide1.md", "sourceDirName": "Guides", @@ -1472,6 +1537,7 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle "id": "guide2", }, "id": "Guides/guide2", + "isDraft": false, "sidebarPosition": 2, "source": "@site/docs/Guides/02-guide2.md", "sourceDirName": "Guides", @@ -1484,6 +1550,7 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle "sidebar_position": 2.5, }, "id": "Guides/guide2.5", + "isDraft": false, "sidebarPosition": 2.5, "source": "@site/docs/Guides/0-guide2.5.md", "sourceDirName": "Guides", @@ -1496,6 +1563,7 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle "sidebar_position": 3, }, "id": "Guides/guide3", + "isDraft": false, "sidebarPosition": 3, "source": "@site/docs/Guides/guide3.md", "sourceDirName": "Guides", @@ -1507,6 +1575,7 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle "id": "guide4", }, "id": "Guides/guide4", + "isDraft": false, "sidebarPosition": undefined, "source": "@site/docs/Guides/a-guide4.md", "sourceDirName": "Guides", @@ -1518,6 +1587,7 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle "id": "guide5", }, "id": "Guides/guide5", + "isDraft": false, "sidebarPosition": undefined, "source": "@site/docs/Guides/b-guide5.md", "sourceDirName": "Guides", @@ -1527,6 +1597,7 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle { "frontMatter": {}, "id": "getting-started", + "isDraft": false, "sidebarPosition": 0, "source": "@site/docs/0-getting-started.md", "sourceDirName": ".", @@ -1536,6 +1607,7 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle { "frontMatter": {}, "id": "installation", + "isDraft": false, "sidebarPosition": 1, "source": "@site/docs/1-installation.md", "sourceDirName": ".", @@ -1563,6 +1635,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "getting-started", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1590,6 +1663,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "installation", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1623,6 +1697,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha "sidebar_position": 1, }, "id": "Guides/guide1", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1655,6 +1730,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha "id": "guide2", }, "id": "Guides/guide2", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1688,6 +1764,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha "sidebar_position": 2.5, }, "id": "Guides/guide2.5", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1721,6 +1798,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha "sidebar_position": 3, }, "id": "Guides/guide3", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1753,6 +1831,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha "id": "guide4", }, "id": "Guides/guide4", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1785,6 +1864,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha "id": "guide5", }, "id": "Guides/guide5", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1815,6 +1895,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "API/api-overview", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1845,6 +1926,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "API/Core APIs/Client API", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1875,6 +1957,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "API/Core APIs/Server API", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1905,6 +1988,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "API/Extension APIs/Plugin API", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1935,6 +2019,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "API/Extension APIs/Theme API", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1965,6 +2050,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "API/api-end", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": undefined, @@ -2144,6 +2230,7 @@ exports[`site with partial autogenerated sidebars docs in partially generated si "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "API/api-end", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -2171,6 +2258,7 @@ exports[`site with partial autogenerated sidebars docs in partially generated si "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "API/api-overview", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -2201,6 +2289,7 @@ exports[`site with partial autogenerated sidebars docs in partially generated si "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "API/Extension APIs/Plugin API", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -2231,6 +2320,7 @@ exports[`site with partial autogenerated sidebars docs in partially generated si "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "API/Extension APIs/Theme API", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": undefined, @@ -2292,6 +2382,7 @@ exports[`versioned website (community) content 1`] = ` "title": "Team title translated", }, "id": "team", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": undefined, @@ -2316,6 +2407,7 @@ exports[`versioned website (community) content 2`] = ` "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "version-1.0.0/team", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": undefined, @@ -2369,6 +2461,7 @@ exports[`versioned website (community) content: data 1`] = ` \\"tags\\": [], \\"version\\": \\"1.0.0\\", \\"frontMatter\\": {}, + \\"isDraft\\": false, \\"sidebar\\": \\"version-1.0.0/community\\" }", "site-i-18-n-en-docusaurus-plugin-content-docs-community-current-team-md-7e5.json": "{ @@ -2385,6 +2478,7 @@ exports[`versioned website (community) content: data 1`] = ` \\"frontMatter\\": { \\"title\\": \\"Team title translated\\" }, + \\"isDraft\\": false, \\"sidebar\\": \\"community\\" }", "version-1-0-0-metadata-prop-608.json": "{ @@ -2576,6 +2670,7 @@ exports[`versioned website content 1`] = ` ], }, "id": "foo/bar", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -2616,6 +2711,7 @@ exports[`versioned website content 2`] = ` "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "version-1.0.1/foo/bar", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -2645,6 +2741,7 @@ exports[`versioned website content 3`] = ` "slug": "/", }, "id": "hello", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": undefined, @@ -2674,6 +2771,7 @@ exports[`versioned website content 4`] = ` "slug": "/", }, "id": "version-1.0.1/hello", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": undefined, @@ -2701,6 +2799,7 @@ exports[`versioned website content 5`] = ` "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "version-1.0.0/foo/baz", + "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -2864,6 +2963,7 @@ exports[`versioned website content: data 1`] = ` } ] }, + \\"isDraft\\": false, \\"sidebar\\": \\"docs\\", \\"next\\": { \\"title\\": \\"hello\\", @@ -2884,6 +2984,7 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"/\\" }, + \\"isDraft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"bar\\", @@ -2903,7 +3004,8 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"current\\", \\"frontMatter\\": { \\"slug\\": \\"/absoluteSlug\\" - } + }, + \\"isDraft\\": false }", "site-docs-slugs-relative-slug-md-d1c.json": "{ \\"unversionedId\\": \\"slugs/relativeSlug\\", @@ -2918,7 +3020,8 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"current\\", \\"frontMatter\\": { \\"slug\\": \\"relativeSlug\\" - } + }, + \\"isDraft\\": false }", "site-docs-slugs-resolved-slug-md-02b.json": "{ \\"unversionedId\\": \\"slugs/resolvedSlug\\", @@ -2933,7 +3036,8 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"current\\", \\"frontMatter\\": { \\"slug\\": \\"./hey/ho/../resolvedSlug\\" - } + }, + \\"isDraft\\": false }", "site-docs-slugs-try-to-escape-slug-md-70d.json": "{ \\"unversionedId\\": \\"slugs/tryToEscapeSlug\\", @@ -2948,7 +3052,8 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"current\\", \\"frontMatter\\": { \\"slug\\": \\"../../../../../../../../tryToEscapeSlug\\" - } + }, + \\"isDraft\\": false }", "site-i-18-n-en-docusaurus-plugin-content-docs-version-1-0-0-hello-md-fe5.json": "{ \\"unversionedId\\": \\"hello\\", @@ -2964,6 +3069,7 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"/\\" }, + \\"isDraft\\": false, \\"sidebar\\": \\"version-1.0.0/docs\\", \\"previous\\": { \\"title\\": \\"baz\\", @@ -2984,6 +3090,7 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"barSlug\\" }, + \\"isDraft\\": false, \\"sidebar\\": \\"version-1.0.0/docs\\", \\"next\\": { \\"title\\": \\"baz\\", @@ -3002,6 +3109,7 @@ exports[`versioned website content: data 1`] = ` \\"tags\\": [], \\"version\\": \\"1.0.0\\", \\"frontMatter\\": {}, + \\"isDraft\\": false, \\"sidebar\\": \\"version-1.0.0/docs\\", \\"previous\\": { \\"title\\": \\"bar\\", @@ -3024,6 +3132,7 @@ exports[`versioned website content: data 1`] = ` \\"tags\\": [], \\"version\\": \\"1.0.1\\", \\"frontMatter\\": {}, + \\"isDraft\\": false, \\"sidebar\\": \\"VersionedSideBarNameDoesNotMatter/docs\\", \\"next\\": { \\"title\\": \\"hello\\", @@ -3044,6 +3153,7 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"/\\" }, + \\"isDraft\\": false, \\"sidebar\\": \\"VersionedSideBarNameDoesNotMatter/docs\\", \\"previous\\": { \\"title\\": \\"bar\\", @@ -3064,6 +3174,7 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"/rootAbsoluteSlug\\" }, + \\"isDraft\\": false, \\"sidebar\\": \\"version-1.0.1/docs\\" }", "site-versioned-docs-version-with-slugs-root-relative-slug-md-32a.json": "{ @@ -3079,7 +3190,8 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"withSlugs\\", \\"frontMatter\\": { \\"slug\\": \\"rootRelativeSlug\\" - } + }, + \\"isDraft\\": false }", "site-versioned-docs-version-with-slugs-root-resolved-slug-md-aee.json": "{ \\"unversionedId\\": \\"rootResolvedSlug\\", @@ -3094,7 +3206,8 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"withSlugs\\", \\"frontMatter\\": { \\"slug\\": \\"./hey/ho/../rootResolvedSlug\\" - } + }, + \\"isDraft\\": false }", "site-versioned-docs-version-with-slugs-root-try-to-escape-slug-md-b5d.json": "{ \\"unversionedId\\": \\"rootTryToEscapeSlug\\", @@ -3109,7 +3222,8 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"withSlugs\\", \\"frontMatter\\": { \\"slug\\": \\"../../../../../../../../rootTryToEscapeSlug\\" - } + }, + \\"isDraft\\": false }", "site-versioned-docs-version-with-slugs-slugs-absolute-slug-md-47a.json": "{ \\"unversionedId\\": \\"slugs/absoluteSlug\\", @@ -3124,7 +3238,8 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"withSlugs\\", \\"frontMatter\\": { \\"slug\\": \\"/absoluteSlug\\" - } + }, + \\"isDraft\\": false }", "site-versioned-docs-version-with-slugs-slugs-relative-slug-md-a95.json": "{ \\"unversionedId\\": \\"slugs/relativeSlug\\", @@ -3139,7 +3254,8 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"withSlugs\\", \\"frontMatter\\": { \\"slug\\": \\"relativeSlug\\" - } + }, + \\"isDraft\\": false }", "site-versioned-docs-version-with-slugs-slugs-resolved-slug-md-5a1.json": "{ \\"unversionedId\\": \\"slugs/resolvedSlug\\", @@ -3154,7 +3270,8 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"withSlugs\\", \\"frontMatter\\": { \\"slug\\": \\"./hey/ho/../resolvedSlug\\" - } + }, + \\"isDraft\\": false }", "site-versioned-docs-version-with-slugs-slugs-try-to-escape-slug-md-4e1.json": "{ \\"unversionedId\\": \\"slugs/tryToEscapeSlug\\", @@ -3169,7 +3286,8 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"withSlugs\\", \\"frontMatter\\": { \\"slug\\": \\"../../../../../../../../tryToEscapeSlug\\" - } + }, + \\"isDraft\\": false }", "tag-docs-next-tags-bar-tag-1-a8f.json": "{ \\"name\\": \\"barTag 1\\", diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts index 0623a345cc2f..194828706381 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts @@ -211,6 +211,7 @@ describe('simple site', () => { 'rootTryToEscapeSlug.md', 'headingAsTitle.md', 'doc with space.md', + 'draft.md', 'foo/bar.md', 'foo/baz.md', 'slugs/absoluteSlug.md', @@ -240,6 +241,7 @@ describe('simple site', () => { pagination_prev: null, }, tags: [], + isDraft: false, }); await defaultTestUtils.testMeta(path.join('hello.md'), { version: 'current', @@ -267,6 +269,7 @@ describe('simple site', () => { permalink: '/docs/tags/tag-3', }, ], + isDraft: false, }); }); @@ -316,6 +319,7 @@ describe('simple site', () => { permalink: '/docs/tags/tag2-custom-permalink', }, ], + isDraft: false, }); }); @@ -337,6 +341,7 @@ describe('simple site', () => { unrelated_front_matter: "won't be part of metadata", }, tags: [], + isDraft: false, }); }); @@ -389,6 +394,7 @@ describe('simple site', () => { permalink: '/docs/tags/tag2-custom-permalink', }, ], + isDraft: false, }); expect(editUrlFunction).toHaveBeenCalledTimes(1); @@ -434,6 +440,32 @@ describe('simple site', () => { formattedLastUpdatedAt: '10/14/2018', lastUpdatedBy: 'Author', tags: [], + isDraft: false, + }); + }); + + it('docs with draft frontmatter', async () => { + process.env.NODE_ENV = 'production'; + + const {defaultTestUtils} = await loadSite(); + + await defaultTestUtils.testMeta('draft.md', { + version: 'current', + id: 'draft', + unversionedId: 'draft', + sourceDirName: '.', + permalink: '/docs/draft', + sidebarPosition: undefined, + slug: '/draft', + title: 'draft', + editUrl: undefined, + formattedLastUpdatedAt: undefined, + description: 'This is a draft document', + frontMatter: { + draft: true, + }, + tags: [], + isDraft: true, }); }); @@ -617,6 +649,7 @@ describe('versioned site', () => { permalink: '/docs/next/tags/barTag-3-permalink', }, ], + isDraft: false, }); await currentVersionTestUtils.testMeta(path.join('hello.md'), { id: 'hello', @@ -631,6 +664,7 @@ describe('versioned site', () => { slug: '/', }, tags: [], + isDraft: false, }); }); @@ -648,6 +682,7 @@ describe('versioned site', () => { frontMatter: {slug: 'barSlug'}, version: '1.0.0', tags: [], + isDraft: false, }); await version100TestUtils.testMeta(path.join('hello.md'), { id: 'version-1.0.0/hello', @@ -664,6 +699,7 @@ describe('versioned site', () => { source: '@site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md', tags: [], + isDraft: false, }); await version101TestUtils.testMeta(path.join('foo', 'bar.md'), { id: 'version-1.0.1/foo/bar', @@ -676,6 +712,7 @@ describe('versioned site', () => { version: '1.0.1', frontMatter: {}, tags: [], + isDraft: false, }); await version101TestUtils.testMeta(path.join('hello.md'), { id: 'version-1.0.1/hello', @@ -690,6 +727,7 @@ describe('versioned site', () => { slug: '/', }, tags: [], + isDraft: false, }); }); @@ -786,6 +824,7 @@ describe('versioned site', () => { '@site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md', editUrl: hardcodedEditUrl, tags: [], + isDraft: false, }); expect(editUrlFunction).toHaveBeenCalledTimes(1); @@ -830,6 +869,7 @@ describe('versioned site', () => { editUrl: 'https://github.com/facebook/docusaurus/edit/main/website/versioned_docs/version-1.0.0/hello.md', tags: [], + isDraft: false, }); }); @@ -865,6 +905,7 @@ describe('versioned site', () => { editUrl: 'https://github.com/facebook/docusaurus/edit/main/website/docs/hello.md', tags: [], + isDraft: false, }); }); @@ -901,6 +942,7 @@ describe('versioned site', () => { editUrl: 'https://github.com/facebook/docusaurus/edit/main/website/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md', tags: [], + isDraft: false, }); }); @@ -938,6 +980,7 @@ describe('versioned site', () => { editUrl: 'https://github.com/facebook/docusaurus/edit/main/website/i18n/fr/docusaurus-plugin-content-docs/current/hello.md', tags: [], + isDraft: false, }); }); }); diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/frontMatter.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/frontMatter.test.ts index 40f2f3f54fee..3a0855fe582a 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/frontMatter.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/frontMatter.test.ts @@ -378,3 +378,19 @@ describe('toc min/max consistency', () => { ], }); }); + +describe('validateDocFrontMatter draft', () => { + testField({ + prefix: 'draft', + validFrontMatters: [{draft: true}, {draft: false}], + convertibleFrontMatter: [ + [{draft: 'true'}, {draft: true}], + [{draft: 'false'}, {draft: false}], + ], + invalidFrontMatters: [ + [{draft: 'yes'}, 'must be a boolean'], + [{draft: 'no'}, 'must be a boolean'], + [{draft: ''}, 'must be a boolean'], + ], + }); +}); diff --git a/packages/docusaurus-plugin-content-docs/src/docs.ts b/packages/docusaurus-plugin-content-docs/src/docs.ts index ee247ec0a97a..de46ce3ea3b6 100644 --- a/packages/docusaurus-plugin-content-docs/src/docs.ts +++ b/packages/docusaurus-plugin-content-docs/src/docs.ts @@ -131,6 +131,9 @@ function doProcessDocMetadata({ } = parseMarkdownString(content); const frontMatter = validateDocFrontMatter(unsafeFrontMatter); + const isDraft = + (frontMatter.draft && process.env.NODE_ENV === 'production') || false; + const { custom_edit_url: customEditURL, @@ -260,6 +263,7 @@ function doProcessDocMetadata({ : undefined, sidebarPosition, frontMatter, + isDraft, }; } diff --git a/packages/docusaurus-plugin-content-docs/src/frontMatter.ts b/packages/docusaurus-plugin-content-docs/src/frontMatter.ts index 52e5af68b383..5c7d3bc97dba 100644 --- a/packages/docusaurus-plugin-content-docs/src/frontMatter.ts +++ b/packages/docusaurus-plugin-content-docs/src/frontMatter.ts @@ -38,6 +38,7 @@ const DocFrontMatterSchema = Joi.object({ parse_number_prefixes: Joi.boolean(), pagination_next: Joi.string().allow(null), pagination_prev: Joi.string().allow(null), + draft: Joi.boolean(), ...FrontMatterTOCHeadingLevels, }).unknown(); diff --git a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts index 271bfa4923db..db0ec9e10143 100644 --- a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts +++ b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts @@ -345,6 +345,8 @@ declare module '@docusaurus/plugin-content-docs' { * @see {@link DocMetadata.prev} */ pagination_prev?: string | null; + /** Should this doc be excluded from production builds? */ + draft?: boolean; }; export type LastUpdateData = { @@ -404,6 +406,8 @@ declare module '@docusaurus/plugin-content-docs' { tags: Tag[]; /** Front matter, as-is. */ frontMatter: DocFrontMatter & {[key: string]: unknown}; + /** Is the doc going to be excluded from production builds? */ + isDraft: boolean; }; export type DocMetadata = DocMetadataBase & diff --git a/packages/docusaurus-plugin-content-docs/src/routes.ts b/packages/docusaurus-plugin-content-docs/src/routes.ts index 3ef6e60ba146..c76783046aca 100644 --- a/packages/docusaurus-plugin-content-docs/src/routes.ts +++ b/packages/docusaurus-plugin-content-docs/src/routes.ts @@ -70,31 +70,33 @@ export async function createDocRoutes({ docItemComponent: string; }): Promise { return Promise.all( - docs.map(async (metadataItem) => { - await actions.createData( - // Note that this created data path must be in sync with - // metadataPath provided to mdx-loader. - `${docuHash(metadataItem.source)}.json`, - JSON.stringify(metadataItem, null, 2), - ); + docs + .filter((metadataItem) => !metadataItem.isDraft) + .map(async (metadataItem) => { + await actions.createData( + // Note that this created data path must be in sync with + // metadataPath provided to mdx-loader. + `${docuHash(metadataItem.source)}.json`, + JSON.stringify(metadataItem, null, 2), + ); - const docRoute: RouteConfig = { - path: metadataItem.permalink, - component: docItemComponent, - exact: true, - modules: { - content: metadataItem.source, - }, - // Because the parent (DocPage) comp need to access it easily - // This permits to render the sidebar once without unmount/remount when - // navigating (and preserve sidebar state) - ...(metadataItem.sidebar && { - sidebar: metadataItem.sidebar, - }), - }; + const docRoute: RouteConfig = { + path: metadataItem.permalink, + component: docItemComponent, + exact: true, + modules: { + content: metadataItem.source, + }, + // Because the parent (DocPage) comp need to access it easily + // This permits to render the sidebar once without unmount/remount + // when navigating (and preserve sidebar state) + ...(metadataItem.sidebar && { + sidebar: metadataItem.sidebar, + }), + }; - return docRoute; - }), + return docRoute; + }), ); } diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/generator.test.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/generator.test.ts index 66281f746b68..840fc8607fc0 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/generator.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/generator.test.ts @@ -505,4 +505,46 @@ describe('DefaultSidebarItemsGenerator', () => { - intro" `); }); + + it('excludes items for draft docs', async () => { + const sidebarSlice = await DefaultSidebarItemsGenerator({ + numberPrefixParser: DefaultNumberPrefixParser, + item: { + type: 'autogenerated', + dirName: '.', + }, + version: { + versionName: 'current', + contentPath: '', + }, + docs: [ + { + id: 'doc1', + source: 'doc1.md', + sourceDirName: '.', + sidebarPosition: 1, + frontMatter: { + draft: true, + }, + isDraft: true, + }, + { + id: 'doc2', + source: 'doc2.md', + sourceDirName: '.', + sidebarPosition: 2, + frontMatter: { + draft: false, + }, + isDraft: false, + }, + ], + options: { + sidebarCollapsed: true, + sidebarCollapsible: true, + }, + }); + + expect(sidebarSlice).toEqual([{type: 'doc', id: 'doc2'}] as Sidebar); + }); }); diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/processor.test.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/processor.test.ts index 500eefbfe1dd..deb654591f50 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/processor.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/processor.test.ts @@ -247,4 +247,33 @@ describe('processSidebars', () => { ], } as ProcessedSidebars); }); + + it('excludes items for draft docs', async () => { + const unprocessedSidebars: NormalizedSidebars = { + someSidebar: [ + {type: 'doc', id: 'doc1'}, + {type: 'doc', id: 'doc2'}, + ], + }; + + const processedSidebar = await testProcessSidebars( + unprocessedSidebars, + {}, + { + docs: [ + { + id: 'doc1', + isDraft: true, + }, + { + id: 'doc2', + isDraft: false, + }, + ], + }, + ); + expect(processedSidebar).toEqual({ + someSidebar: [{type: 'doc', id: 'doc2'}], + }); + }); }); diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/generator.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/generator.ts index 4642897f3df4..115b1bd6c08b 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/generator.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/generator.ts @@ -53,7 +53,8 @@ export const DefaultSidebarItemsGenerator: SidebarItemsGenerator = async ({ item: {dirName: autogenDir}, categoriesMetadata, }) => { - const docsById = createDocsByIdIndex(allDocs); + const visibleDocs = allDocs.filter((doc) => !doc.isDraft); + const docsById = createDocsByIdIndex(visibleDocs); const findDoc = (docId: string): SidebarItemsGeneratorDoc | undefined => docsById[docId]; const getDoc = (docId: string): SidebarItemsGeneratorDoc => { @@ -83,7 +84,7 @@ Available doc IDs: doc.sourceDirName.startsWith(addTrailingSlash(autogenDir)) ); } - const docs = allDocs.filter(isInAutogeneratedDir); + const docs = visibleDocs.filter(isInAutogeneratedDir); if (docs.length === 0) { logger.warn`No docs found in path=${autogenDir}: can't auto-generate a sidebar.`; diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts index 7e83dd08a921..7a943d9f08ec 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts @@ -39,6 +39,7 @@ function toSidebarItemsGeneratorDoc( 'source', 'sourceDirName', 'sidebarPosition', + 'isDraft', ]); } @@ -95,6 +96,12 @@ async function processSidebar( if (item.type === 'autogenerated') { return processAutoGeneratedItem(item); } + if ( + item.type === 'doc' && + docs.find((doc) => doc.id === item.id)?.isDraft + ) { + return []; + } return [item]; } diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/types.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/types.ts index 2dfaf5b7f553..e5c63d1f05a9 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/types.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/types.ts @@ -227,6 +227,7 @@ export type SidebarItemsGeneratorDoc = Pick< | 'source' | 'sourceDirName' | 'sidebarPosition' + | 'isDraft' >; export type SidebarItemsGeneratorVersion = Pick< VersionMetadata, diff --git a/website/_dogfooding/_docs tests/draft.md b/website/_dogfooding/_docs tests/draft.md new file mode 100644 index 000000000000..6b43d6e58b85 --- /dev/null +++ b/website/_dogfooding/_docs tests/draft.md @@ -0,0 +1,7 @@ +--- +draft: true +--- + +# Draft test + +This page should only be visible in local development diff --git a/website/_dogfooding/_docs tests/tests/draft.md b/website/_dogfooding/_docs tests/tests/draft.md new file mode 100644 index 000000000000..6b43d6e58b85 --- /dev/null +++ b/website/_dogfooding/_docs tests/tests/draft.md @@ -0,0 +1,7 @@ +--- +draft: true +--- + +# Draft test + +This page should only be visible in local development diff --git a/website/_dogfooding/docs-tests-sidebars.js b/website/_dogfooding/docs-tests-sidebars.js index 804301319943..d274c0a6a33e 100644 --- a/website/_dogfooding/docs-tests-sidebars.js +++ b/website/_dogfooding/docs-tests-sidebars.js @@ -109,6 +109,7 @@ const sidebars = { }, ], }, + 'draft', ], anotherSidebar: ['dummy'], }; diff --git a/website/docs/api/plugins/plugin-content-docs.md b/website/docs/api/plugins/plugin-content-docs.md index 7ef92f29f5b1..86642d6117b5 100644 --- a/website/docs/api/plugins/plugin-content-docs.md +++ b/website/docs/api/plugins/plugin-content-docs.md @@ -262,6 +262,7 @@ Accepted fields: | `image` | `string` | `undefined` | Cover or thumbnail image that will be used when displaying the link to your post. | | `slug` | `string` | File path | Allows to customize the document url (`//`). Support multiple patterns: `slug: my-doc`, `slug: /my/path/myDoc`, `slug: /`. | | `tags` | `Tag[]` | `undefined` | A list of strings or objects of two string fields `label` and `permalink` to tag to your docs. | +| `draft` | `boolean` | `false` | Whether to exclude this document from production builds and sidebars. | From ec77ca91d406ba1484487dbd7c3ce33814fe9795 Mon Sep 17 00:00:00 2001 From: Jody Heavener Date: Mon, 4 Apr 2022 20:36:15 -0300 Subject: [PATCH 02/15] Remove sisdebar filtering --- .../src/sidebars/__tests__/generator.test.ts | 42 ------------------- .../src/sidebars/__tests__/processor.test.ts | 29 ------------- .../src/sidebars/generator.ts | 5 +-- .../src/sidebars/processor.ts | 7 ---- .../src/sidebars/types.ts | 1 - 5 files changed, 2 insertions(+), 82 deletions(-) diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/generator.test.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/generator.test.ts index 840fc8607fc0..66281f746b68 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/generator.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/generator.test.ts @@ -505,46 +505,4 @@ describe('DefaultSidebarItemsGenerator', () => { - intro" `); }); - - it('excludes items for draft docs', async () => { - const sidebarSlice = await DefaultSidebarItemsGenerator({ - numberPrefixParser: DefaultNumberPrefixParser, - item: { - type: 'autogenerated', - dirName: '.', - }, - version: { - versionName: 'current', - contentPath: '', - }, - docs: [ - { - id: 'doc1', - source: 'doc1.md', - sourceDirName: '.', - sidebarPosition: 1, - frontMatter: { - draft: true, - }, - isDraft: true, - }, - { - id: 'doc2', - source: 'doc2.md', - sourceDirName: '.', - sidebarPosition: 2, - frontMatter: { - draft: false, - }, - isDraft: false, - }, - ], - options: { - sidebarCollapsed: true, - sidebarCollapsible: true, - }, - }); - - expect(sidebarSlice).toEqual([{type: 'doc', id: 'doc2'}] as Sidebar); - }); }); diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/processor.test.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/processor.test.ts index deb654591f50..500eefbfe1dd 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/processor.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/processor.test.ts @@ -247,33 +247,4 @@ describe('processSidebars', () => { ], } as ProcessedSidebars); }); - - it('excludes items for draft docs', async () => { - const unprocessedSidebars: NormalizedSidebars = { - someSidebar: [ - {type: 'doc', id: 'doc1'}, - {type: 'doc', id: 'doc2'}, - ], - }; - - const processedSidebar = await testProcessSidebars( - unprocessedSidebars, - {}, - { - docs: [ - { - id: 'doc1', - isDraft: true, - }, - { - id: 'doc2', - isDraft: false, - }, - ], - }, - ); - expect(processedSidebar).toEqual({ - someSidebar: [{type: 'doc', id: 'doc2'}], - }); - }); }); diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/generator.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/generator.ts index 115b1bd6c08b..4642897f3df4 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/generator.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/generator.ts @@ -53,8 +53,7 @@ export const DefaultSidebarItemsGenerator: SidebarItemsGenerator = async ({ item: {dirName: autogenDir}, categoriesMetadata, }) => { - const visibleDocs = allDocs.filter((doc) => !doc.isDraft); - const docsById = createDocsByIdIndex(visibleDocs); + const docsById = createDocsByIdIndex(allDocs); const findDoc = (docId: string): SidebarItemsGeneratorDoc | undefined => docsById[docId]; const getDoc = (docId: string): SidebarItemsGeneratorDoc => { @@ -84,7 +83,7 @@ Available doc IDs: doc.sourceDirName.startsWith(addTrailingSlash(autogenDir)) ); } - const docs = visibleDocs.filter(isInAutogeneratedDir); + const docs = allDocs.filter(isInAutogeneratedDir); if (docs.length === 0) { logger.warn`No docs found in path=${autogenDir}: can't auto-generate a sidebar.`; diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts index 7a943d9f08ec..7e83dd08a921 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts @@ -39,7 +39,6 @@ function toSidebarItemsGeneratorDoc( 'source', 'sourceDirName', 'sidebarPosition', - 'isDraft', ]); } @@ -96,12 +95,6 @@ async function processSidebar( if (item.type === 'autogenerated') { return processAutoGeneratedItem(item); } - if ( - item.type === 'doc' && - docs.find((doc) => doc.id === item.id)?.isDraft - ) { - return []; - } return [item]; } diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/types.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/types.ts index e5c63d1f05a9..2dfaf5b7f553 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/types.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/types.ts @@ -227,7 +227,6 @@ export type SidebarItemsGeneratorDoc = Pick< | 'source' | 'sourceDirName' | 'sidebarPosition' - | 'isDraft' >; export type SidebarItemsGeneratorVersion = Pick< VersionMetadata, From a9597a34575a9628419d6435e04dec98af46c30b Mon Sep 17 00:00:00 2001 From: Jody Heavener Date: Mon, 4 Apr 2022 20:51:07 -0300 Subject: [PATCH 03/15] Rename isDraft to draft --- .../__tests__/__snapshots__/docs.test.ts.snap | 18 ++- .../__snapshots__/index.test.ts.snap | 144 ++++++++---------- .../src/__tests__/docs.test.ts | 36 ++--- .../src/docs.ts | 4 +- .../src/plugin-content-docs.d.ts | 2 +- .../src/routes.ts | 2 +- 6 files changed, 101 insertions(+), 105 deletions(-) diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/docs.test.ts.snap b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/docs.test.ts.snap index fd9e3739fd70..dce8065faf05 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/docs.test.ts.snap +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/docs.test.ts.snap @@ -6,15 +6,21 @@ exports[`simple site custom pagination 1`] = ` { "id": "doc with space", "next": { - "permalink": "/docs/foo/bar", - "title": "Bar", + "permalink": "/docs/draft", + "title": "draft", }, "prev": undefined, }, { "id": "draft", - "next": undefined, - "prev": undefined, + "next": { + "permalink": "/docs/foo/bar", + "title": "Bar", + }, + "prev": { + "permalink": "/docs/doc with space", + "title": "Hoo hoo, if this path tricks you...", + }, }, { "id": "foo/bar", @@ -168,6 +174,10 @@ exports[`simple site custom pagination 1`] = ` "id": "doc with space", "type": "doc", }, + { + "id": "draft", + "type": "doc", + }, { "collapsed": false, "collapsible": true, diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap index d3bca1a52bb7..4a0a4be2984c 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap @@ -43,6 +43,7 @@ Available document ids are: exports[`simple website content 1`] = ` { "description": "Images", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -60,7 +61,6 @@ exports[`simple website content 1`] = ` "title": "baz", }, "id": "foo/baz", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -96,6 +96,7 @@ exports[`simple website content 1`] = ` exports[`simple website content 2`] = ` { "description": "Hi, Endilie here :)", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -109,7 +110,6 @@ exports[`simple website content 2`] = ` "title": "Hello, World !", }, "id": "hello", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": undefined, @@ -142,6 +142,7 @@ exports[`simple website content 2`] = ` exports[`simple website content 3`] = ` { "description": "This is custom description", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -152,7 +153,6 @@ exports[`simple website content 3`] = ` "title": "Bar", }, "id": "foo/bar", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": undefined, @@ -402,7 +402,7 @@ exports[`simple website content: data 1`] = ` \\"tags\\": [], \\"version\\": \\"current\\", \\"frontMatter\\": {}, - \\"isDraft\\": false + \\"draft\\": false }", "site-docs-draft-md-5b1.json": "{ \\"unversionedId\\": \\"draft\\", @@ -418,7 +418,7 @@ exports[`simple website content: data 1`] = ` \\"frontMatter\\": { \\"draft\\": true }, - \\"isDraft\\": false + \\"draft\\": false }", "site-docs-foo-bar-md-8c2.json": "{ \\"unversionedId\\": \\"foo/bar\\", @@ -438,7 +438,7 @@ exports[`simple website content: data 1`] = ` \\"pagination_next\\": null, \\"pagination_prev\\": null }, - \\"isDraft\\": false, + \\"draft\\": false, \\"sidebar\\": \\"docs\\" }", "site-docs-foo-baz-md-a69.json": "{ @@ -475,7 +475,7 @@ exports[`simple website content: data 1`] = ` } ] }, - \\"isDraft\\": false, + \\"draft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"Bar\\", @@ -498,7 +498,7 @@ exports[`simple website content: data 1`] = ` \\"tags\\": [], \\"version\\": \\"current\\", \\"frontMatter\\": {}, - \\"isDraft\\": false, + \\"draft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"rootTryToEscapeSlug\\", @@ -539,7 +539,7 @@ exports[`simple website content: data 1`] = ` ], \\"slug\\": \\"/\\" }, - \\"isDraft\\": false, + \\"draft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"My heading as title\\", @@ -561,7 +561,7 @@ exports[`simple website content: data 1`] = ` \\"frontMatter\\": { \\"custom_edit_url\\": null }, - \\"isDraft\\": false + \\"draft\\": false }", "site-docs-lorem-md-b27.json": "{ \\"unversionedId\\": \\"lorem\\", @@ -579,7 +579,7 @@ exports[`simple website content: data 1`] = ` \\"custom_edit_url\\": \\"https://github.com/customUrl/docs/lorem.md\\", \\"unrelated_front_matter\\": \\"won't be part of metadata\\" }, - \\"isDraft\\": false + \\"draft\\": false }", "site-docs-root-absolute-slug-md-db5.json": "{ \\"unversionedId\\": \\"rootAbsoluteSlug\\", @@ -597,7 +597,7 @@ exports[`simple website content: data 1`] = ` \\"pagination_next\\": \\"headingAsTitle\\", \\"pagination_prev\\": \\"foo/baz\\" }, - \\"isDraft\\": false, + \\"draft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"baz pagination_label\\", @@ -624,7 +624,7 @@ exports[`simple website content: data 1`] = ` \\"pagination_next\\": \\"headingAsTitle\\", \\"pagination_prev\\": \\"foo/baz\\" }, - \\"isDraft\\": false, + \\"draft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"baz pagination_label\\", @@ -651,7 +651,7 @@ exports[`simple website content: data 1`] = ` \\"pagination_next\\": \\"headingAsTitle\\", \\"pagination_prev\\": \\"foo/baz\\" }, - \\"isDraft\\": false, + \\"draft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"baz pagination_label\\", @@ -678,7 +678,7 @@ exports[`simple website content: data 1`] = ` \\"pagination_next\\": \\"headingAsTitle\\", \\"pagination_prev\\": \\"foo/baz\\" }, - \\"isDraft\\": false, + \\"draft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"baz pagination_label\\", @@ -703,7 +703,7 @@ exports[`simple website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"/absoluteSlug\\" }, - \\"isDraft\\": false + \\"draft\\": false }", "site-docs-slugs-relative-slug-md-d1c.json": "{ \\"unversionedId\\": \\"slugs/relativeSlug\\", @@ -719,7 +719,7 @@ exports[`simple website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"relativeSlug\\" }, - \\"isDraft\\": false + \\"draft\\": false }", "site-docs-slugs-resolved-slug-md-02b.json": "{ \\"unversionedId\\": \\"slugs/resolvedSlug\\", @@ -735,7 +735,7 @@ exports[`simple website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"./hey/ho/../resolvedSlug\\" }, - \\"isDraft\\": false + \\"draft\\": false }", "site-docs-slugs-try-to-escape-slug-md-70d.json": "{ \\"unversionedId\\": \\"slugs/tryToEscapeSlug\\", @@ -751,7 +751,7 @@ exports[`simple website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"../../../../../../../../tryToEscapeSlug\\" }, - \\"isDraft\\": false + \\"draft\\": false }", "tag-docs-tags-tag-1-b3f.json": "{ \\"name\\": \\"tag 1\\", @@ -1462,7 +1462,6 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle { "frontMatter": {}, "id": "API/Core APIs/Client API", - "isDraft": false, "sidebarPosition": 0, "source": "@site/docs/3-API/01_Core APIs/0 --- Client API.md", "sourceDirName": "3-API/01_Core APIs", @@ -1472,7 +1471,6 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle { "frontMatter": {}, "id": "API/Core APIs/Server API", - "isDraft": false, "sidebarPosition": 1, "source": "@site/docs/3-API/01_Core APIs/1 --- Server API.md", "sourceDirName": "3-API/01_Core APIs", @@ -1482,7 +1480,6 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle { "frontMatter": {}, "id": "API/Extension APIs/Plugin API", - "isDraft": false, "sidebarPosition": 0, "source": "@site/docs/3-API/02_Extension APIs/0. Plugin API.md", "sourceDirName": "3-API/02_Extension APIs", @@ -1492,7 +1489,6 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle { "frontMatter": {}, "id": "API/Extension APIs/Theme API", - "isDraft": false, "sidebarPosition": 1, "source": "@site/docs/3-API/02_Extension APIs/1. Theme API.md", "sourceDirName": "3-API/02_Extension APIs", @@ -1502,7 +1498,6 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle { "frontMatter": {}, "id": "API/api-end", - "isDraft": false, "sidebarPosition": 3, "source": "@site/docs/3-API/03_api-end.md", "sourceDirName": "3-API", @@ -1512,7 +1507,6 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle { "frontMatter": {}, "id": "API/api-overview", - "isDraft": false, "sidebarPosition": 0, "source": "@site/docs/3-API/00_api-overview.md", "sourceDirName": "3-API", @@ -1525,7 +1519,6 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle "sidebar_position": 1, }, "id": "Guides/guide1", - "isDraft": false, "sidebarPosition": 1, "source": "@site/docs/Guides/z-guide1.md", "sourceDirName": "Guides", @@ -1537,7 +1530,6 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle "id": "guide2", }, "id": "Guides/guide2", - "isDraft": false, "sidebarPosition": 2, "source": "@site/docs/Guides/02-guide2.md", "sourceDirName": "Guides", @@ -1550,7 +1542,6 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle "sidebar_position": 2.5, }, "id": "Guides/guide2.5", - "isDraft": false, "sidebarPosition": 2.5, "source": "@site/docs/Guides/0-guide2.5.md", "sourceDirName": "Guides", @@ -1563,7 +1554,6 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle "sidebar_position": 3, }, "id": "Guides/guide3", - "isDraft": false, "sidebarPosition": 3, "source": "@site/docs/Guides/guide3.md", "sourceDirName": "Guides", @@ -1575,7 +1565,6 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle "id": "guide4", }, "id": "Guides/guide4", - "isDraft": false, "sidebarPosition": undefined, "source": "@site/docs/Guides/a-guide4.md", "sourceDirName": "Guides", @@ -1587,7 +1576,6 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle "id": "guide5", }, "id": "Guides/guide5", - "isDraft": false, "sidebarPosition": undefined, "source": "@site/docs/Guides/b-guide5.md", "sourceDirName": "Guides", @@ -1597,7 +1585,6 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle { "frontMatter": {}, "id": "getting-started", - "isDraft": false, "sidebarPosition": 0, "source": "@site/docs/0-getting-started.md", "sourceDirName": ".", @@ -1607,7 +1594,6 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle { "frontMatter": {}, "id": "installation", - "isDraft": false, "sidebarPosition": 1, "source": "@site/docs/1-installation.md", "sourceDirName": ".", @@ -1631,11 +1617,11 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 1`] = ` { "description": "Getting started text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "getting-started", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1659,11 +1645,11 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 2`] = ` { "description": "Installation text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "installation", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1690,6 +1676,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 3`] = ` { "description": "Guide 1 text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -1697,7 +1684,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha "sidebar_position": 1, }, "id": "Guides/guide1", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1724,13 +1710,13 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 4`] = ` { "description": "Guide 2 text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { "id": "guide2", }, "id": "Guides/guide2", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1757,6 +1743,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 5`] = ` { "description": "Guide 2.5 text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -1764,7 +1751,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha "sidebar_position": 2.5, }, "id": "Guides/guide2.5", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1791,6 +1777,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 6`] = ` { "description": "Guide 3 text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -1798,7 +1785,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha "sidebar_position": 3, }, "id": "Guides/guide3", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1825,13 +1811,13 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 7`] = ` { "description": "Guide 4 text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { "id": "guide4", }, "id": "Guides/guide4", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1858,13 +1844,13 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 8`] = ` { "description": "Guide 5 text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { "id": "guide5", }, "id": "Guides/guide5", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1891,11 +1877,11 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 9`] = ` { "description": "API Overview text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "API/api-overview", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1922,11 +1908,11 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 10`] = ` { "description": "Client API text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "API/Core APIs/Client API", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1953,11 +1939,11 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 11`] = ` { "description": "Server API text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "API/Core APIs/Server API", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -1984,11 +1970,11 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 12`] = ` { "description": "Plugin API text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "API/Extension APIs/Plugin API", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -2015,11 +2001,11 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 13`] = ` { "description": "Theme API text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "API/Extension APIs/Theme API", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -2046,11 +2032,11 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 14`] = ` { "description": "API End text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "API/api-end", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": undefined, @@ -2226,11 +2212,11 @@ exports[`site with partial autogenerated sidebars 2 (fix #4638) sidebar is parti exports[`site with partial autogenerated sidebars docs in partially generated sidebar have correct metadata 1`] = ` { "description": "API End text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "API/api-end", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -2254,11 +2240,11 @@ exports[`site with partial autogenerated sidebars docs in partially generated si exports[`site with partial autogenerated sidebars docs in partially generated sidebar have correct metadata 2`] = ` { "description": "API Overview text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "API/api-overview", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -2285,11 +2271,11 @@ exports[`site with partial autogenerated sidebars docs in partially generated si exports[`site with partial autogenerated sidebars docs in partially generated sidebar have correct metadata 3`] = ` { "description": "Plugin API text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "API/Extension APIs/Plugin API", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -2316,11 +2302,11 @@ exports[`site with partial autogenerated sidebars docs in partially generated si exports[`site with partial autogenerated sidebars docs in partially generated sidebar have correct metadata 4`] = ` { "description": "Theme API text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "API/Extension APIs/Theme API", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": undefined, @@ -2376,13 +2362,13 @@ exports[`site with partial autogenerated sidebars sidebar is partially autogener exports[`versioned website (community) content 1`] = ` { "description": "Team current version (translated)", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { "title": "Team title translated", }, "id": "team", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": undefined, @@ -2403,11 +2389,11 @@ exports[`versioned website (community) content 1`] = ` exports[`versioned website (community) content 2`] = ` { "description": "Team 1.0.0", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "version-1.0.0/team", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": undefined, @@ -2461,7 +2447,7 @@ exports[`versioned website (community) content: data 1`] = ` \\"tags\\": [], \\"version\\": \\"1.0.0\\", \\"frontMatter\\": {}, - \\"isDraft\\": false, + \\"draft\\": false, \\"sidebar\\": \\"version-1.0.0/community\\" }", "site-i-18-n-en-docusaurus-plugin-content-docs-community-current-team-md-7e5.json": "{ @@ -2478,7 +2464,7 @@ exports[`versioned website (community) content: data 1`] = ` \\"frontMatter\\": { \\"title\\": \\"Team title translated\\" }, - \\"isDraft\\": false, + \\"draft\\": false, \\"sidebar\\": \\"community\\" }", "version-1-0-0-metadata-prop-608.json": "{ @@ -2656,6 +2642,7 @@ exports[`versioned website (community) getPathToWatch 1`] = ` exports[`versioned website content 1`] = ` { "description": "This is next version of bar.", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -2670,7 +2657,6 @@ exports[`versioned website content 1`] = ` ], }, "id": "foo/bar", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -2707,11 +2693,11 @@ exports[`versioned website content 1`] = ` exports[`versioned website content 2`] = ` { "description": "Bar 1.0.1 !", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "version-1.0.1/foo/bar", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -2735,13 +2721,13 @@ exports[`versioned website content 2`] = ` exports[`versioned website content 3`] = ` { "description": "Hello next !", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { "slug": "/", }, "id": "hello", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": undefined, @@ -2765,13 +2751,13 @@ exports[`versioned website content 3`] = ` exports[`versioned website content 4`] = ` { "description": "Hello 1.0.1 !", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { "slug": "/", }, "id": "version-1.0.1/hello", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": undefined, @@ -2795,11 +2781,11 @@ exports[`versioned website content 4`] = ` exports[`versioned website content 5`] = ` { "description": "Baz 1.0.0 ! This will be deleted in next subsequent versions.", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, "id": "version-1.0.0/foo/baz", - "isDraft": false, "lastUpdatedAt": undefined, "lastUpdatedBy": undefined, "next": { @@ -2963,7 +2949,7 @@ exports[`versioned website content: data 1`] = ` } ] }, - \\"isDraft\\": false, + \\"draft\\": false, \\"sidebar\\": \\"docs\\", \\"next\\": { \\"title\\": \\"hello\\", @@ -2984,7 +2970,7 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"/\\" }, - \\"isDraft\\": false, + \\"draft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"bar\\", @@ -3005,7 +2991,7 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"/absoluteSlug\\" }, - \\"isDraft\\": false + \\"draft\\": false }", "site-docs-slugs-relative-slug-md-d1c.json": "{ \\"unversionedId\\": \\"slugs/relativeSlug\\", @@ -3021,7 +3007,7 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"relativeSlug\\" }, - \\"isDraft\\": false + \\"draft\\": false }", "site-docs-slugs-resolved-slug-md-02b.json": "{ \\"unversionedId\\": \\"slugs/resolvedSlug\\", @@ -3037,7 +3023,7 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"./hey/ho/../resolvedSlug\\" }, - \\"isDraft\\": false + \\"draft\\": false }", "site-docs-slugs-try-to-escape-slug-md-70d.json": "{ \\"unversionedId\\": \\"slugs/tryToEscapeSlug\\", @@ -3053,7 +3039,7 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"../../../../../../../../tryToEscapeSlug\\" }, - \\"isDraft\\": false + \\"draft\\": false }", "site-i-18-n-en-docusaurus-plugin-content-docs-version-1-0-0-hello-md-fe5.json": "{ \\"unversionedId\\": \\"hello\\", @@ -3069,7 +3055,7 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"/\\" }, - \\"isDraft\\": false, + \\"draft\\": false, \\"sidebar\\": \\"version-1.0.0/docs\\", \\"previous\\": { \\"title\\": \\"baz\\", @@ -3090,7 +3076,7 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"barSlug\\" }, - \\"isDraft\\": false, + \\"draft\\": false, \\"sidebar\\": \\"version-1.0.0/docs\\", \\"next\\": { \\"title\\": \\"baz\\", @@ -3109,7 +3095,7 @@ exports[`versioned website content: data 1`] = ` \\"tags\\": [], \\"version\\": \\"1.0.0\\", \\"frontMatter\\": {}, - \\"isDraft\\": false, + \\"draft\\": false, \\"sidebar\\": \\"version-1.0.0/docs\\", \\"previous\\": { \\"title\\": \\"bar\\", @@ -3132,7 +3118,7 @@ exports[`versioned website content: data 1`] = ` \\"tags\\": [], \\"version\\": \\"1.0.1\\", \\"frontMatter\\": {}, - \\"isDraft\\": false, + \\"draft\\": false, \\"sidebar\\": \\"VersionedSideBarNameDoesNotMatter/docs\\", \\"next\\": { \\"title\\": \\"hello\\", @@ -3153,7 +3139,7 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"/\\" }, - \\"isDraft\\": false, + \\"draft\\": false, \\"sidebar\\": \\"VersionedSideBarNameDoesNotMatter/docs\\", \\"previous\\": { \\"title\\": \\"bar\\", @@ -3174,7 +3160,7 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"/rootAbsoluteSlug\\" }, - \\"isDraft\\": false, + \\"draft\\": false, \\"sidebar\\": \\"version-1.0.1/docs\\" }", "site-versioned-docs-version-with-slugs-root-relative-slug-md-32a.json": "{ @@ -3191,7 +3177,7 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"rootRelativeSlug\\" }, - \\"isDraft\\": false + \\"draft\\": false }", "site-versioned-docs-version-with-slugs-root-resolved-slug-md-aee.json": "{ \\"unversionedId\\": \\"rootResolvedSlug\\", @@ -3207,7 +3193,7 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"./hey/ho/../rootResolvedSlug\\" }, - \\"isDraft\\": false + \\"draft\\": false }", "site-versioned-docs-version-with-slugs-root-try-to-escape-slug-md-b5d.json": "{ \\"unversionedId\\": \\"rootTryToEscapeSlug\\", @@ -3223,7 +3209,7 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"../../../../../../../../rootTryToEscapeSlug\\" }, - \\"isDraft\\": false + \\"draft\\": false }", "site-versioned-docs-version-with-slugs-slugs-absolute-slug-md-47a.json": "{ \\"unversionedId\\": \\"slugs/absoluteSlug\\", @@ -3239,7 +3225,7 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"/absoluteSlug\\" }, - \\"isDraft\\": false + \\"draft\\": false }", "site-versioned-docs-version-with-slugs-slugs-relative-slug-md-a95.json": "{ \\"unversionedId\\": \\"slugs/relativeSlug\\", @@ -3255,7 +3241,7 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"relativeSlug\\" }, - \\"isDraft\\": false + \\"draft\\": false }", "site-versioned-docs-version-with-slugs-slugs-resolved-slug-md-5a1.json": "{ \\"unversionedId\\": \\"slugs/resolvedSlug\\", @@ -3271,7 +3257,7 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"./hey/ho/../resolvedSlug\\" }, - \\"isDraft\\": false + \\"draft\\": false }", "site-versioned-docs-version-with-slugs-slugs-try-to-escape-slug-md-4e1.json": "{ \\"unversionedId\\": \\"slugs/tryToEscapeSlug\\", @@ -3287,7 +3273,7 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"../../../../../../../../tryToEscapeSlug\\" }, - \\"isDraft\\": false + \\"draft\\": false }", "tag-docs-next-tags-bar-tag-1-a8f.json": "{ \\"name\\": \\"barTag 1\\", diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts index 194828706381..57c0ad8b7046 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts @@ -241,7 +241,7 @@ describe('simple site', () => { pagination_prev: null, }, tags: [], - isDraft: false, + draft: false, }); await defaultTestUtils.testMeta(path.join('hello.md'), { version: 'current', @@ -269,7 +269,7 @@ describe('simple site', () => { permalink: '/docs/tags/tag-3', }, ], - isDraft: false, + draft: false, }); }); @@ -319,7 +319,7 @@ describe('simple site', () => { permalink: '/docs/tags/tag2-custom-permalink', }, ], - isDraft: false, + draft: false, }); }); @@ -341,7 +341,7 @@ describe('simple site', () => { unrelated_front_matter: "won't be part of metadata", }, tags: [], - isDraft: false, + draft: false, }); }); @@ -394,7 +394,7 @@ describe('simple site', () => { permalink: '/docs/tags/tag2-custom-permalink', }, ], - isDraft: false, + draft: false, }); expect(editUrlFunction).toHaveBeenCalledTimes(1); @@ -440,7 +440,7 @@ describe('simple site', () => { formattedLastUpdatedAt: '10/14/2018', lastUpdatedBy: 'Author', tags: [], - isDraft: false, + draft: false, }); }); @@ -465,7 +465,7 @@ describe('simple site', () => { draft: true, }, tags: [], - isDraft: true, + draft: true, }); }); @@ -649,7 +649,7 @@ describe('versioned site', () => { permalink: '/docs/next/tags/barTag-3-permalink', }, ], - isDraft: false, + draft: false, }); await currentVersionTestUtils.testMeta(path.join('hello.md'), { id: 'hello', @@ -664,7 +664,7 @@ describe('versioned site', () => { slug: '/', }, tags: [], - isDraft: false, + draft: false, }); }); @@ -682,7 +682,7 @@ describe('versioned site', () => { frontMatter: {slug: 'barSlug'}, version: '1.0.0', tags: [], - isDraft: false, + draft: false, }); await version100TestUtils.testMeta(path.join('hello.md'), { id: 'version-1.0.0/hello', @@ -699,7 +699,7 @@ describe('versioned site', () => { source: '@site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md', tags: [], - isDraft: false, + draft: false, }); await version101TestUtils.testMeta(path.join('foo', 'bar.md'), { id: 'version-1.0.1/foo/bar', @@ -712,7 +712,7 @@ describe('versioned site', () => { version: '1.0.1', frontMatter: {}, tags: [], - isDraft: false, + draft: false, }); await version101TestUtils.testMeta(path.join('hello.md'), { id: 'version-1.0.1/hello', @@ -727,7 +727,7 @@ describe('versioned site', () => { slug: '/', }, tags: [], - isDraft: false, + draft: false, }); }); @@ -824,7 +824,7 @@ describe('versioned site', () => { '@site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md', editUrl: hardcodedEditUrl, tags: [], - isDraft: false, + draft: false, }); expect(editUrlFunction).toHaveBeenCalledTimes(1); @@ -869,7 +869,7 @@ describe('versioned site', () => { editUrl: 'https://github.com/facebook/docusaurus/edit/main/website/versioned_docs/version-1.0.0/hello.md', tags: [], - isDraft: false, + draft: false, }); }); @@ -905,7 +905,7 @@ describe('versioned site', () => { editUrl: 'https://github.com/facebook/docusaurus/edit/main/website/docs/hello.md', tags: [], - isDraft: false, + draft: false, }); }); @@ -942,7 +942,7 @@ describe('versioned site', () => { editUrl: 'https://github.com/facebook/docusaurus/edit/main/website/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md', tags: [], - isDraft: false, + draft: false, }); }); @@ -980,7 +980,7 @@ describe('versioned site', () => { editUrl: 'https://github.com/facebook/docusaurus/edit/main/website/i18n/fr/docusaurus-plugin-content-docs/current/hello.md', tags: [], - isDraft: false, + draft: false, }); }); }); diff --git a/packages/docusaurus-plugin-content-docs/src/docs.ts b/packages/docusaurus-plugin-content-docs/src/docs.ts index de46ce3ea3b6..2e6fca68ebb1 100644 --- a/packages/docusaurus-plugin-content-docs/src/docs.ts +++ b/packages/docusaurus-plugin-content-docs/src/docs.ts @@ -131,7 +131,7 @@ function doProcessDocMetadata({ } = parseMarkdownString(content); const frontMatter = validateDocFrontMatter(unsafeFrontMatter); - const isDraft = + const draft = (frontMatter.draft && process.env.NODE_ENV === 'production') || false; const { @@ -263,7 +263,7 @@ function doProcessDocMetadata({ : undefined, sidebarPosition, frontMatter, - isDraft, + draft, }; } diff --git a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts index db0ec9e10143..3087821f24fe 100644 --- a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts +++ b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts @@ -407,7 +407,7 @@ declare module '@docusaurus/plugin-content-docs' { /** Front matter, as-is. */ frontMatter: DocFrontMatter & {[key: string]: unknown}; /** Is the doc going to be excluded from production builds? */ - isDraft: boolean; + draft: boolean; }; export type DocMetadata = DocMetadataBase & diff --git a/packages/docusaurus-plugin-content-docs/src/routes.ts b/packages/docusaurus-plugin-content-docs/src/routes.ts index c76783046aca..0b7f60c7e729 100644 --- a/packages/docusaurus-plugin-content-docs/src/routes.ts +++ b/packages/docusaurus-plugin-content-docs/src/routes.ts @@ -71,7 +71,7 @@ export async function createDocRoutes({ }): Promise { return Promise.all( docs - .filter((metadataItem) => !metadataItem.isDraft) + .filter((metadataItem) => !metadataItem.draft) .map(async (metadataItem) => { await actions.createData( // Note that this created data path must be in sync with From 94300f405f6acca131be094fd1a089330a58d0df Mon Sep 17 00:00:00 2001 From: Jody Heavener Date: Mon, 4 Apr 2022 21:35:34 -0300 Subject: [PATCH 04/15] Filter draft docs out early --- .../__tests__/__snapshots__/docs.test.ts.snap | 17 +-- .../__snapshots__/index.test.ts.snap | 103 ++++-------------- .../src/__tests__/docs.test.ts | 82 +++++--------- .../src/docs.ts | 10 +- .../src/index.ts | 8 +- .../src/plugin-content-docs.d.ts | 2 - .../src/routes.ts | 48 ++++---- 7 files changed, 79 insertions(+), 191 deletions(-) diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/docs.test.ts.snap b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/docs.test.ts.snap index dce8065faf05..eaed1c90bbbb 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/docs.test.ts.snap +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/docs.test.ts.snap @@ -5,22 +5,11 @@ exports[`simple site custom pagination 1`] = ` "pagination": [ { "id": "doc with space", - "next": { - "permalink": "/docs/draft", - "title": "draft", - }, - "prev": undefined, - }, - { - "id": "draft", "next": { "permalink": "/docs/foo/bar", "title": "Bar", }, - "prev": { - "permalink": "/docs/doc with space", - "title": "Hoo hoo, if this path tricks you...", - }, + "prev": undefined, }, { "id": "foo/bar", @@ -174,10 +163,6 @@ exports[`simple site custom pagination 1`] = ` "id": "doc with space", "type": "doc", }, - { - "id": "draft", - "type": "doc", - }, { "collapsed": false, "collapsible": true, diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap index 4a0a4be2984c..635c832d3605 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap @@ -43,7 +43,6 @@ Available document ids are: exports[`simple website content 1`] = ` { "description": "Images", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -96,7 +95,6 @@ exports[`simple website content 1`] = ` exports[`simple website content 2`] = ` { "description": "Hi, Endilie here :)", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -142,7 +140,6 @@ exports[`simple website content 2`] = ` exports[`simple website content 3`] = ` { "description": "This is custom description", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -401,8 +398,7 @@ exports[`simple website content: data 1`] = ` \\"permalink\\": \\"/docs/doc with space\\", \\"tags\\": [], \\"version\\": \\"current\\", - \\"frontMatter\\": {}, - \\"draft\\": false + \\"frontMatter\\": {} }", "site-docs-draft-md-5b1.json": "{ \\"unversionedId\\": \\"draft\\", @@ -417,8 +413,7 @@ exports[`simple website content: data 1`] = ` \\"version\\": \\"current\\", \\"frontMatter\\": { \\"draft\\": true - }, - \\"draft\\": false + } }", "site-docs-foo-bar-md-8c2.json": "{ \\"unversionedId\\": \\"foo/bar\\", @@ -438,7 +433,6 @@ exports[`simple website content: data 1`] = ` \\"pagination_next\\": null, \\"pagination_prev\\": null }, - \\"draft\\": false, \\"sidebar\\": \\"docs\\" }", "site-docs-foo-baz-md-a69.json": "{ @@ -475,7 +469,6 @@ exports[`simple website content: data 1`] = ` } ] }, - \\"draft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"Bar\\", @@ -498,7 +491,6 @@ exports[`simple website content: data 1`] = ` \\"tags\\": [], \\"version\\": \\"current\\", \\"frontMatter\\": {}, - \\"draft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"rootTryToEscapeSlug\\", @@ -539,7 +531,6 @@ exports[`simple website content: data 1`] = ` ], \\"slug\\": \\"/\\" }, - \\"draft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"My heading as title\\", @@ -560,8 +551,7 @@ exports[`simple website content: data 1`] = ` \\"version\\": \\"current\\", \\"frontMatter\\": { \\"custom_edit_url\\": null - }, - \\"draft\\": false + } }", "site-docs-lorem-md-b27.json": "{ \\"unversionedId\\": \\"lorem\\", @@ -578,8 +568,7 @@ exports[`simple website content: data 1`] = ` \\"frontMatter\\": { \\"custom_edit_url\\": \\"https://github.com/customUrl/docs/lorem.md\\", \\"unrelated_front_matter\\": \\"won't be part of metadata\\" - }, - \\"draft\\": false + } }", "site-docs-root-absolute-slug-md-db5.json": "{ \\"unversionedId\\": \\"rootAbsoluteSlug\\", @@ -597,7 +586,6 @@ exports[`simple website content: data 1`] = ` \\"pagination_next\\": \\"headingAsTitle\\", \\"pagination_prev\\": \\"foo/baz\\" }, - \\"draft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"baz pagination_label\\", @@ -624,7 +612,6 @@ exports[`simple website content: data 1`] = ` \\"pagination_next\\": \\"headingAsTitle\\", \\"pagination_prev\\": \\"foo/baz\\" }, - \\"draft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"baz pagination_label\\", @@ -651,7 +638,6 @@ exports[`simple website content: data 1`] = ` \\"pagination_next\\": \\"headingAsTitle\\", \\"pagination_prev\\": \\"foo/baz\\" }, - \\"draft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"baz pagination_label\\", @@ -678,7 +664,6 @@ exports[`simple website content: data 1`] = ` \\"pagination_next\\": \\"headingAsTitle\\", \\"pagination_prev\\": \\"foo/baz\\" }, - \\"draft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"baz pagination_label\\", @@ -702,8 +687,7 @@ exports[`simple website content: data 1`] = ` \\"version\\": \\"current\\", \\"frontMatter\\": { \\"slug\\": \\"/absoluteSlug\\" - }, - \\"draft\\": false + } }", "site-docs-slugs-relative-slug-md-d1c.json": "{ \\"unversionedId\\": \\"slugs/relativeSlug\\", @@ -718,8 +702,7 @@ exports[`simple website content: data 1`] = ` \\"version\\": \\"current\\", \\"frontMatter\\": { \\"slug\\": \\"relativeSlug\\" - }, - \\"draft\\": false + } }", "site-docs-slugs-resolved-slug-md-02b.json": "{ \\"unversionedId\\": \\"slugs/resolvedSlug\\", @@ -734,8 +717,7 @@ exports[`simple website content: data 1`] = ` \\"version\\": \\"current\\", \\"frontMatter\\": { \\"slug\\": \\"./hey/ho/../resolvedSlug\\" - }, - \\"draft\\": false + } }", "site-docs-slugs-try-to-escape-slug-md-70d.json": "{ \\"unversionedId\\": \\"slugs/tryToEscapeSlug\\", @@ -750,8 +732,7 @@ exports[`simple website content: data 1`] = ` \\"version\\": \\"current\\", \\"frontMatter\\": { \\"slug\\": \\"../../../../../../../../tryToEscapeSlug\\" - }, - \\"draft\\": false + } }", "tag-docs-tags-tag-1-b3f.json": "{ \\"name\\": \\"tag 1\\", @@ -1617,7 +1598,6 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 1`] = ` { "description": "Getting started text", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -1645,7 +1625,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 2`] = ` { "description": "Installation text", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -1676,7 +1655,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 3`] = ` { "description": "Guide 1 text", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -1710,7 +1688,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 4`] = ` { "description": "Guide 2 text", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -1743,7 +1720,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 5`] = ` { "description": "Guide 2.5 text", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -1777,7 +1753,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 6`] = ` { "description": "Guide 3 text", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -1811,7 +1786,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 7`] = ` { "description": "Guide 4 text", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -1844,7 +1818,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 8`] = ` { "description": "Guide 5 text", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -1877,7 +1850,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 9`] = ` { "description": "API Overview text", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -1908,7 +1880,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 10`] = ` { "description": "Client API text", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -1939,7 +1910,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 11`] = ` { "description": "Server API text", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -1970,7 +1940,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 12`] = ` { "description": "Plugin API text", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -2001,7 +1970,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 13`] = ` { "description": "Theme API text", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -2032,7 +2000,6 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 14`] = ` { "description": "API End text", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -2212,7 +2179,6 @@ exports[`site with partial autogenerated sidebars 2 (fix #4638) sidebar is parti exports[`site with partial autogenerated sidebars docs in partially generated sidebar have correct metadata 1`] = ` { "description": "API End text", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -2240,7 +2206,6 @@ exports[`site with partial autogenerated sidebars docs in partially generated si exports[`site with partial autogenerated sidebars docs in partially generated sidebar have correct metadata 2`] = ` { "description": "API Overview text", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -2271,7 +2236,6 @@ exports[`site with partial autogenerated sidebars docs in partially generated si exports[`site with partial autogenerated sidebars docs in partially generated sidebar have correct metadata 3`] = ` { "description": "Plugin API text", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -2302,7 +2266,6 @@ exports[`site with partial autogenerated sidebars docs in partially generated si exports[`site with partial autogenerated sidebars docs in partially generated sidebar have correct metadata 4`] = ` { "description": "Theme API text", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -2362,7 +2325,6 @@ exports[`site with partial autogenerated sidebars sidebar is partially autogener exports[`versioned website (community) content 1`] = ` { "description": "Team current version (translated)", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -2389,7 +2351,6 @@ exports[`versioned website (community) content 1`] = ` exports[`versioned website (community) content 2`] = ` { "description": "Team 1.0.0", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -2447,7 +2408,6 @@ exports[`versioned website (community) content: data 1`] = ` \\"tags\\": [], \\"version\\": \\"1.0.0\\", \\"frontMatter\\": {}, - \\"draft\\": false, \\"sidebar\\": \\"version-1.0.0/community\\" }", "site-i-18-n-en-docusaurus-plugin-content-docs-community-current-team-md-7e5.json": "{ @@ -2464,7 +2424,6 @@ exports[`versioned website (community) content: data 1`] = ` \\"frontMatter\\": { \\"title\\": \\"Team title translated\\" }, - \\"draft\\": false, \\"sidebar\\": \\"community\\" }", "version-1-0-0-metadata-prop-608.json": "{ @@ -2642,7 +2601,6 @@ exports[`versioned website (community) getPathToWatch 1`] = ` exports[`versioned website content 1`] = ` { "description": "This is next version of bar.", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -2693,7 +2651,6 @@ exports[`versioned website content 1`] = ` exports[`versioned website content 2`] = ` { "description": "Bar 1.0.1 !", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -2721,7 +2678,6 @@ exports[`versioned website content 2`] = ` exports[`versioned website content 3`] = ` { "description": "Hello next !", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -2751,7 +2707,6 @@ exports[`versioned website content 3`] = ` exports[`versioned website content 4`] = ` { "description": "Hello 1.0.1 !", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -2781,7 +2736,6 @@ exports[`versioned website content 4`] = ` exports[`versioned website content 5`] = ` { "description": "Baz 1.0.0 ! This will be deleted in next subsequent versions.", - "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -2949,7 +2903,6 @@ exports[`versioned website content: data 1`] = ` } ] }, - \\"draft\\": false, \\"sidebar\\": \\"docs\\", \\"next\\": { \\"title\\": \\"hello\\", @@ -2970,7 +2923,6 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"/\\" }, - \\"draft\\": false, \\"sidebar\\": \\"docs\\", \\"previous\\": { \\"title\\": \\"bar\\", @@ -2990,8 +2942,7 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"current\\", \\"frontMatter\\": { \\"slug\\": \\"/absoluteSlug\\" - }, - \\"draft\\": false + } }", "site-docs-slugs-relative-slug-md-d1c.json": "{ \\"unversionedId\\": \\"slugs/relativeSlug\\", @@ -3006,8 +2957,7 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"current\\", \\"frontMatter\\": { \\"slug\\": \\"relativeSlug\\" - }, - \\"draft\\": false + } }", "site-docs-slugs-resolved-slug-md-02b.json": "{ \\"unversionedId\\": \\"slugs/resolvedSlug\\", @@ -3022,8 +2972,7 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"current\\", \\"frontMatter\\": { \\"slug\\": \\"./hey/ho/../resolvedSlug\\" - }, - \\"draft\\": false + } }", "site-docs-slugs-try-to-escape-slug-md-70d.json": "{ \\"unversionedId\\": \\"slugs/tryToEscapeSlug\\", @@ -3038,8 +2987,7 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"current\\", \\"frontMatter\\": { \\"slug\\": \\"../../../../../../../../tryToEscapeSlug\\" - }, - \\"draft\\": false + } }", "site-i-18-n-en-docusaurus-plugin-content-docs-version-1-0-0-hello-md-fe5.json": "{ \\"unversionedId\\": \\"hello\\", @@ -3055,7 +3003,6 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"/\\" }, - \\"draft\\": false, \\"sidebar\\": \\"version-1.0.0/docs\\", \\"previous\\": { \\"title\\": \\"baz\\", @@ -3076,7 +3023,6 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"barSlug\\" }, - \\"draft\\": false, \\"sidebar\\": \\"version-1.0.0/docs\\", \\"next\\": { \\"title\\": \\"baz\\", @@ -3095,7 +3041,6 @@ exports[`versioned website content: data 1`] = ` \\"tags\\": [], \\"version\\": \\"1.0.0\\", \\"frontMatter\\": {}, - \\"draft\\": false, \\"sidebar\\": \\"version-1.0.0/docs\\", \\"previous\\": { \\"title\\": \\"bar\\", @@ -3118,7 +3063,6 @@ exports[`versioned website content: data 1`] = ` \\"tags\\": [], \\"version\\": \\"1.0.1\\", \\"frontMatter\\": {}, - \\"draft\\": false, \\"sidebar\\": \\"VersionedSideBarNameDoesNotMatter/docs\\", \\"next\\": { \\"title\\": \\"hello\\", @@ -3139,7 +3083,6 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"/\\" }, - \\"draft\\": false, \\"sidebar\\": \\"VersionedSideBarNameDoesNotMatter/docs\\", \\"previous\\": { \\"title\\": \\"bar\\", @@ -3160,7 +3103,6 @@ exports[`versioned website content: data 1`] = ` \\"frontMatter\\": { \\"slug\\": \\"/rootAbsoluteSlug\\" }, - \\"draft\\": false, \\"sidebar\\": \\"version-1.0.1/docs\\" }", "site-versioned-docs-version-with-slugs-root-relative-slug-md-32a.json": "{ @@ -3176,8 +3118,7 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"withSlugs\\", \\"frontMatter\\": { \\"slug\\": \\"rootRelativeSlug\\" - }, - \\"draft\\": false + } }", "site-versioned-docs-version-with-slugs-root-resolved-slug-md-aee.json": "{ \\"unversionedId\\": \\"rootResolvedSlug\\", @@ -3192,8 +3133,7 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"withSlugs\\", \\"frontMatter\\": { \\"slug\\": \\"./hey/ho/../rootResolvedSlug\\" - }, - \\"draft\\": false + } }", "site-versioned-docs-version-with-slugs-root-try-to-escape-slug-md-b5d.json": "{ \\"unversionedId\\": \\"rootTryToEscapeSlug\\", @@ -3208,8 +3148,7 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"withSlugs\\", \\"frontMatter\\": { \\"slug\\": \\"../../../../../../../../rootTryToEscapeSlug\\" - }, - \\"draft\\": false + } }", "site-versioned-docs-version-with-slugs-slugs-absolute-slug-md-47a.json": "{ \\"unversionedId\\": \\"slugs/absoluteSlug\\", @@ -3224,8 +3163,7 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"withSlugs\\", \\"frontMatter\\": { \\"slug\\": \\"/absoluteSlug\\" - }, - \\"draft\\": false + } }", "site-versioned-docs-version-with-slugs-slugs-relative-slug-md-a95.json": "{ \\"unversionedId\\": \\"slugs/relativeSlug\\", @@ -3240,8 +3178,7 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"withSlugs\\", \\"frontMatter\\": { \\"slug\\": \\"relativeSlug\\" - }, - \\"draft\\": false + } }", "site-versioned-docs-version-with-slugs-slugs-resolved-slug-md-5a1.json": "{ \\"unversionedId\\": \\"slugs/resolvedSlug\\", @@ -3256,8 +3193,7 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"withSlugs\\", \\"frontMatter\\": { \\"slug\\": \\"./hey/ho/../resolvedSlug\\" - }, - \\"draft\\": false + } }", "site-versioned-docs-version-with-slugs-slugs-try-to-escape-slug-md-4e1.json": "{ \\"unversionedId\\": \\"slugs/tryToEscapeSlug\\", @@ -3272,8 +3208,7 @@ exports[`versioned website content: data 1`] = ` \\"version\\": \\"withSlugs\\", \\"frontMatter\\": { \\"slug\\": \\"../../../../../../../../tryToEscapeSlug\\" - }, - \\"draft\\": false + } }", "tag-docs-next-tags-bar-tag-1-a8f.json": "{ \\"name\\": \\"barTag 1\\", diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts index 57c0ad8b7046..fa06585aa47e 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts @@ -87,7 +87,7 @@ function createTestUtils({ expectedMetadata: Optional< DocMetadataBase, 'source' | 'lastUpdatedBy' | 'lastUpdatedAt' | 'editUrl' - >, + > | null, ) { const docFile = await readDoc(docFileSource); const metadata = await processDocMetadata({ @@ -96,17 +96,21 @@ function createTestUtils({ context, options, }); - expect(metadata).toEqual({ - lastUpdatedBy: undefined, - lastUpdatedAt: undefined, - editUrl: undefined, - source: path.posix.join( - '@site', - posixPath(path.relative(siteDir, versionMetadata.contentPath)), - posixPath(docFileSource), - ), - ...expectedMetadata, - }); + if (expectedMetadata === null) { + expect(metadata).toBeNull(); + } else { + expect(metadata).toEqual({ + lastUpdatedBy: undefined, + lastUpdatedAt: undefined, + editUrl: undefined, + source: path.posix.join( + '@site', + posixPath(path.relative(siteDir, versionMetadata.contentPath)), + posixPath(docFileSource), + ), + ...expectedMetadata, + }); + } } async function testSlug(docFileSource: string, expectedPermalink: string) { @@ -128,14 +132,16 @@ function createTestUtils({ }[]; sidebars: Sidebars; }> { - const rawDocs = docFiles.map((docFile) => - processDocMetadata({ - docFile, - versionMetadata, - context, - options, - }), - ); + const rawDocs = docFiles + .map((docFile) => + processDocMetadata({ + docFile, + versionMetadata, + context, + options, + }), + ) + .filter(Boolean); const sidebars = await loadSidebars(versionMetadata.sidebarFilePath, { sidebarItemsGenerator: ({defaultSidebarItemsGenerator, ...args}) => defaultSidebarItemsGenerator({...args}), @@ -241,7 +247,6 @@ describe('simple site', () => { pagination_prev: null, }, tags: [], - draft: false, }); await defaultTestUtils.testMeta(path.join('hello.md'), { version: 'current', @@ -269,7 +274,6 @@ describe('simple site', () => { permalink: '/docs/tags/tag-3', }, ], - draft: false, }); }); @@ -319,7 +323,6 @@ describe('simple site', () => { permalink: '/docs/tags/tag2-custom-permalink', }, ], - draft: false, }); }); @@ -341,7 +344,6 @@ describe('simple site', () => { unrelated_front_matter: "won't be part of metadata", }, tags: [], - draft: false, }); }); @@ -394,7 +396,6 @@ describe('simple site', () => { permalink: '/docs/tags/tag2-custom-permalink', }, ], - draft: false, }); expect(editUrlFunction).toHaveBeenCalledTimes(1); @@ -440,7 +441,6 @@ describe('simple site', () => { formattedLastUpdatedAt: '10/14/2018', lastUpdatedBy: 'Author', tags: [], - draft: false, }); }); @@ -449,24 +449,7 @@ describe('simple site', () => { const {defaultTestUtils} = await loadSite(); - await defaultTestUtils.testMeta('draft.md', { - version: 'current', - id: 'draft', - unversionedId: 'draft', - sourceDirName: '.', - permalink: '/docs/draft', - sidebarPosition: undefined, - slug: '/draft', - title: 'draft', - editUrl: undefined, - formattedLastUpdatedAt: undefined, - description: 'This is a draft document', - frontMatter: { - draft: true, - }, - tags: [], - draft: true, - }); + await defaultTestUtils.testMeta('draft.md', null); }); it('docs with slugs', async () => { @@ -649,7 +632,6 @@ describe('versioned site', () => { permalink: '/docs/next/tags/barTag-3-permalink', }, ], - draft: false, }); await currentVersionTestUtils.testMeta(path.join('hello.md'), { id: 'hello', @@ -664,7 +646,6 @@ describe('versioned site', () => { slug: '/', }, tags: [], - draft: false, }); }); @@ -682,7 +663,6 @@ describe('versioned site', () => { frontMatter: {slug: 'barSlug'}, version: '1.0.0', tags: [], - draft: false, }); await version100TestUtils.testMeta(path.join('hello.md'), { id: 'version-1.0.0/hello', @@ -699,7 +679,6 @@ describe('versioned site', () => { source: '@site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md', tags: [], - draft: false, }); await version101TestUtils.testMeta(path.join('foo', 'bar.md'), { id: 'version-1.0.1/foo/bar', @@ -712,7 +691,6 @@ describe('versioned site', () => { version: '1.0.1', frontMatter: {}, tags: [], - draft: false, }); await version101TestUtils.testMeta(path.join('hello.md'), { id: 'version-1.0.1/hello', @@ -727,7 +705,6 @@ describe('versioned site', () => { slug: '/', }, tags: [], - draft: false, }); }); @@ -824,7 +801,6 @@ describe('versioned site', () => { '@site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md', editUrl: hardcodedEditUrl, tags: [], - draft: false, }); expect(editUrlFunction).toHaveBeenCalledTimes(1); @@ -869,7 +845,6 @@ describe('versioned site', () => { editUrl: 'https://github.com/facebook/docusaurus/edit/main/website/versioned_docs/version-1.0.0/hello.md', tags: [], - draft: false, }); }); @@ -905,7 +880,6 @@ describe('versioned site', () => { editUrl: 'https://github.com/facebook/docusaurus/edit/main/website/docs/hello.md', tags: [], - draft: false, }); }); @@ -942,7 +916,6 @@ describe('versioned site', () => { editUrl: 'https://github.com/facebook/docusaurus/edit/main/website/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md', tags: [], - draft: false, }); }); @@ -980,7 +953,6 @@ describe('versioned site', () => { editUrl: 'https://github.com/facebook/docusaurus/edit/main/website/i18n/fr/docusaurus-plugin-content-docs/current/hello.md', tags: [], - draft: false, }); }); }); diff --git a/packages/docusaurus-plugin-content-docs/src/docs.ts b/packages/docusaurus-plugin-content-docs/src/docs.ts index 2e6fca68ebb1..4ad86df6f253 100644 --- a/packages/docusaurus-plugin-content-docs/src/docs.ts +++ b/packages/docusaurus-plugin-content-docs/src/docs.ts @@ -120,7 +120,7 @@ function doProcessDocMetadata({ versionMetadata: VersionMetadata; context: LoadContext; options: MetadataOptions; -}): DocMetadataBase { +}): DocMetadataBase | null { const {source, content, lastUpdate, contentPath, filePath} = docFile; const {siteDir, i18n} = context; @@ -131,8 +131,9 @@ function doProcessDocMetadata({ } = parseMarkdownString(content); const frontMatter = validateDocFrontMatter(unsafeFrontMatter); - const draft = - (frontMatter.draft && process.env.NODE_ENV === 'production') || false; + if ((frontMatter.draft && process.env.NODE_ENV === 'production') || false) { + return null; + } const { custom_edit_url: customEditURL, @@ -263,7 +264,6 @@ function doProcessDocMetadata({ : undefined, sidebarPosition, frontMatter, - draft, }; } @@ -272,7 +272,7 @@ export function processDocMetadata(args: { versionMetadata: VersionMetadata; context: LoadContext; options: MetadataOptions; -}): DocMetadataBase { +}): DocMetadataBase | null { try { return doProcessDocMetadata(args); } catch (err) { diff --git a/packages/docusaurus-plugin-content-docs/src/index.ts b/packages/docusaurus-plugin-content-docs/src/index.ts index a0dc21768960..f9fba8e049a3 100644 --- a/packages/docusaurus-plugin-content-docs/src/index.ts +++ b/packages/docusaurus-plugin-content-docs/src/index.ts @@ -132,7 +132,7 @@ export default async function pluginContentDocs( async loadContent() { async function loadVersionDocsBase( versionMetadata: VersionMetadata, - ): Promise { + ): Promise<(DocMetadataBase | null)[]> { const docFiles = await readVersionDocs(versionMetadata, options); if (docFiles.length === 0) { throw new Error( @@ -158,9 +158,9 @@ export default async function pluginContentDocs( async function doLoadVersion( versionMetadata: VersionMetadata, ): Promise { - const docs: DocMetadataBase[] = await loadVersionDocsBase( - versionMetadata, - ); + const docs = (await loadVersionDocsBase(versionMetadata)).filter( + Boolean, + ) as DocMetadataBase[]; const sidebars = await loadSidebars(versionMetadata.sidebarFilePath, { sidebarItemsGenerator: options.sidebarItemsGenerator, diff --git a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts index 3087821f24fe..79402240d2f3 100644 --- a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts +++ b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts @@ -406,8 +406,6 @@ declare module '@docusaurus/plugin-content-docs' { tags: Tag[]; /** Front matter, as-is. */ frontMatter: DocFrontMatter & {[key: string]: unknown}; - /** Is the doc going to be excluded from production builds? */ - draft: boolean; }; export type DocMetadata = DocMetadataBase & diff --git a/packages/docusaurus-plugin-content-docs/src/routes.ts b/packages/docusaurus-plugin-content-docs/src/routes.ts index 0b7f60c7e729..3ef6e60ba146 100644 --- a/packages/docusaurus-plugin-content-docs/src/routes.ts +++ b/packages/docusaurus-plugin-content-docs/src/routes.ts @@ -70,33 +70,31 @@ export async function createDocRoutes({ docItemComponent: string; }): Promise { return Promise.all( - docs - .filter((metadataItem) => !metadataItem.draft) - .map(async (metadataItem) => { - await actions.createData( - // Note that this created data path must be in sync with - // metadataPath provided to mdx-loader. - `${docuHash(metadataItem.source)}.json`, - JSON.stringify(metadataItem, null, 2), - ); + docs.map(async (metadataItem) => { + await actions.createData( + // Note that this created data path must be in sync with + // metadataPath provided to mdx-loader. + `${docuHash(metadataItem.source)}.json`, + JSON.stringify(metadataItem, null, 2), + ); - const docRoute: RouteConfig = { - path: metadataItem.permalink, - component: docItemComponent, - exact: true, - modules: { - content: metadataItem.source, - }, - // Because the parent (DocPage) comp need to access it easily - // This permits to render the sidebar once without unmount/remount - // when navigating (and preserve sidebar state) - ...(metadataItem.sidebar && { - sidebar: metadataItem.sidebar, - }), - }; + const docRoute: RouteConfig = { + path: metadataItem.permalink, + component: docItemComponent, + exact: true, + modules: { + content: metadataItem.source, + }, + // Because the parent (DocPage) comp need to access it easily + // This permits to render the sidebar once without unmount/remount when + // navigating (and preserve sidebar state) + ...(metadataItem.sidebar && { + sidebar: metadataItem.sidebar, + }), + }; - return docRoute; - }), + return docRoute; + }), ); } From d56232a57461d8a9d455966c9ba1169db674826a Mon Sep 17 00:00:00 2001 From: sebastienlorber Date: Fri, 8 Apr 2022 17:09:15 +0200 Subject: [PATCH 05/15] sync draft frontmatter doc for docs/blog --- website/docs/api/plugins/plugin-content-blog.md | 2 +- website/docs/api/plugins/plugin-content-docs.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/api/plugins/plugin-content-blog.md b/website/docs/api/plugins/plugin-content-blog.md index dbd658b6812e..27db9055ecc6 100644 --- a/website/docs/api/plugins/plugin-content-blog.md +++ b/website/docs/api/plugins/plugin-content-blog.md @@ -174,7 +174,7 @@ Accepted fields: | `title` | `string` | Markdown title | The blog post title. | | `date` | `string` | File name or file creation time | The blog post creation date. If not specified, this can be extracted from the file or folder name, e.g, `2021-04-15-blog-post.mdx`, `2021-04-15-blog-post/index.mdx`, `2021/04/15/blog-post.mdx`. Otherwise, it is the Markdown file creation time. | | `tags` | `Tag[]` | `undefined` | A list of strings or objects of two string fields `label` and `permalink` to tag to your post. | -| `draft` | `boolean` | `false` | A boolean flag to indicate that the blog post is work-in-progress and therefore should not be published yet. However, draft blog posts will be displayed during development. | +| `draft` | `boolean` | `false` | A boolean flag to indicate that the blog post is work-in-progress. Draft blog posts will only be displayed during development. | | `hide_table_of_contents` | `boolean` | `false` | Whether to hide the table of contents to the right. | | `toc_min_heading_level` | `number` | `2` | The minimum heading level shown in the table of contents. Must be between 2 and 6 and lower or equal to the max value. | | `toc_max_heading_level` | `number` | `3` | The max heading level shown in the table of contents. Must be between 2 and 6. | diff --git a/website/docs/api/plugins/plugin-content-docs.md b/website/docs/api/plugins/plugin-content-docs.md index 86642d6117b5..e5971ee2fa59 100644 --- a/website/docs/api/plugins/plugin-content-docs.md +++ b/website/docs/api/plugins/plugin-content-docs.md @@ -262,7 +262,7 @@ Accepted fields: | `image` | `string` | `undefined` | Cover or thumbnail image that will be used when displaying the link to your post. | | `slug` | `string` | File path | Allows to customize the document url (`//`). Support multiple patterns: `slug: my-doc`, `slug: /my/path/myDoc`, `slug: /`. | | `tags` | `Tag[]` | `undefined` | A list of strings or objects of two string fields `label` and `permalink` to tag to your docs. | -| `draft` | `boolean` | `false` | Whether to exclude this document from production builds and sidebars. | +| `draft` | `boolean` | `false` | A boolean flag to indicate that a document is a work-in-progress. Draft documents will only be displayed during development. | From f4e40c4a916c3591096ab55bb742a0811991ea62 Mon Sep 17 00:00:00 2001 From: sebastienlorber Date: Fri, 8 Apr 2022 17:12:39 +0200 Subject: [PATCH 06/15] rename dogfood draft docs to have unique names --- website/_dogfooding/_docs tests/{draft.md => some-draft.md} | 2 +- .../_docs tests/tests/{draft.md => another-draft.md} | 2 +- website/_dogfooding/docs-tests-sidebars.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename website/_dogfooding/_docs tests/{draft.md => some-draft.md} (85%) rename website/_dogfooding/_docs tests/tests/{draft.md => another-draft.md} (82%) diff --git a/website/_dogfooding/_docs tests/draft.md b/website/_dogfooding/_docs tests/some-draft.md similarity index 85% rename from website/_dogfooding/_docs tests/draft.md rename to website/_dogfooding/_docs tests/some-draft.md index 6b43d6e58b85..d9d87847d6c8 100644 --- a/website/_dogfooding/_docs tests/draft.md +++ b/website/_dogfooding/_docs tests/some-draft.md @@ -2,6 +2,6 @@ draft: true --- -# Draft test +# Some Draft This page should only be visible in local development diff --git a/website/_dogfooding/_docs tests/tests/draft.md b/website/_dogfooding/_docs tests/tests/another-draft.md similarity index 82% rename from website/_dogfooding/_docs tests/tests/draft.md rename to website/_dogfooding/_docs tests/tests/another-draft.md index 6b43d6e58b85..685d5541a1cf 100644 --- a/website/_dogfooding/_docs tests/tests/draft.md +++ b/website/_dogfooding/_docs tests/tests/another-draft.md @@ -2,6 +2,6 @@ draft: true --- -# Draft test +# Another Draft This page should only be visible in local development diff --git a/website/_dogfooding/docs-tests-sidebars.js b/website/_dogfooding/docs-tests-sidebars.js index d274c0a6a33e..19740a11e3a0 100644 --- a/website/_dogfooding/docs-tests-sidebars.js +++ b/website/_dogfooding/docs-tests-sidebars.js @@ -80,6 +80,7 @@ const sidebars = { }, ], }, + 'some-draft', { type: 'category', label: 'HTML items tests', @@ -109,7 +110,6 @@ const sidebars = { }, ], }, - 'draft', ], anotherSidebar: ['dummy'], }; From 39b1dca04989f75489698afa8f239a7292fd3c9e Mon Sep 17 00:00:00 2001 From: sebastienlorber Date: Fri, 8 Apr 2022 17:14:48 +0200 Subject: [PATCH 07/15] rename fixture draft doc --- .../docs/{draft.md => doc-draft.md} | 0 .../__snapshots__/index.test.ts.snap | 50 +++++++++---------- .../src/__tests__/docs.test.ts | 4 +- 3 files changed, 27 insertions(+), 27 deletions(-) rename packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/simple-site/docs/{draft.md => doc-draft.md} (100%) diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/simple-site/docs/draft.md b/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/simple-site/docs/doc-draft.md similarity index 100% rename from packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/simple-site/docs/draft.md rename to packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/simple-site/docs/doc-draft.md diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap index 8ac7bf9da688..46b7b40b8642 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap @@ -23,7 +23,7 @@ These sidebar document ids do not exist: Available document ids are: - doc with space -- draft +- doc-draft - foo/bar - foo/baz - headingAsTitle @@ -270,8 +270,8 @@ exports[`simple website content 5`] = ` "sidebar": undefined, }, { - "id": "draft", - "path": "/docs/draft", + "id": "doc-draft", + "path": "/docs/doc-draft", "sidebar": undefined, }, { @@ -386,6 +386,21 @@ exports[`simple website content: data 1`] = ` "permalink": "/docs/rootAbsoluteSlug" } } +}", + "site-docs-doc-draft-md-584.json": "{ + "unversionedId": "doc-draft", + "id": "doc-draft", + "title": "doc-draft", + "description": "This is a draft document", + "source": "@site/docs/doc-draft.md", + "sourceDirName": ".", + "slug": "/doc-draft", + "permalink": "/docs/doc-draft", + "tags": [], + "version": "current", + "frontMatter": { + "draft": true + } }", "site-docs-doc-with-space-md-e90.json": "{ "unversionedId": "doc with space", @@ -399,21 +414,6 @@ exports[`simple website content: data 1`] = ` "tags": [], "version": "current", "frontMatter": {} -}", - "site-docs-draft-md-5b1.json": "{ - "unversionedId": "draft", - "id": "draft", - "title": "draft", - "description": "This is a draft document", - "source": "@site/docs/draft.md", - "sourceDirName": ".", - "slug": "/draft", - "permalink": "/docs/draft", - "tags": [], - "version": "current", - "frontMatter": { - "draft": true - } }", "site-docs-foo-bar-md-8c2.json": "{ "unversionedId": "foo/bar", @@ -909,9 +909,9 @@ exports[`simple website content: data 1`] = ` "title": "Hoo hoo, if this path tricks you...", "description": "" }, - "draft": { - "id": "draft", - "title": "draft", + "doc-draft": { + "id": "doc-draft", + "title": "doc-draft", "description": "This is a draft document" }, "foo/bar": { @@ -1012,8 +1012,8 @@ exports[`simple website content: global data 1`] = ` "sidebar": undefined, }, { - "id": "draft", - "path": "/docs/draft", + "id": "doc-draft", + "path": "/docs/doc-draft", "sidebar": undefined, }, { @@ -1193,9 +1193,9 @@ exports[`simple website content: route config 1`] = ` "component": "@theme/DocItem", "exact": true, "modules": { - "content": "@site/docs/draft.md", + "content": "@site/docs/doc-draft.md", }, - "path": "/docs/draft", + "path": "/docs/doc-draft", }, { "component": "@theme/DocItem", diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts index c4c15134dc75..8d6482ed38c3 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts @@ -217,7 +217,7 @@ describe('simple site', () => { 'rootTryToEscapeSlug.md', 'headingAsTitle.md', 'doc with space.md', - 'draft.md', + 'doc-draft.md', 'foo/bar.md', 'foo/baz.md', 'slugs/absoluteSlug.md', @@ -449,7 +449,7 @@ describe('simple site', () => { const {defaultTestUtils} = await loadSite(); - await defaultTestUtils.testMeta('draft.md', null); + await defaultTestUtils.testMeta('doc-draft.md', null); }); it('docs with slugs', async () => { From a449e1ffb8a5e62184d576e85b558592f44436c4 Mon Sep 17 00:00:00 2001 From: sebastienlorber Date: Fri, 8 Apr 2022 18:22:33 +0200 Subject: [PATCH 08/15] processDocMetadata => revert changes, doc return draft boolean without early filtering --- .../__tests__/__snapshots__/docs.test.ts.snap | 17 +- .../__snapshots__/index.test.ts.snap | 65 +++++++ .../src/__tests__/docs.test.ts | 173 ++++++++++-------- .../src/docs.ts | 28 ++- .../src/index.ts | 13 +- .../src/plugin-content-docs.d.ts | 5 + 6 files changed, 217 insertions(+), 84 deletions(-) diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/docs.test.ts.snap b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/docs.test.ts.snap index eaed1c90bbbb..f05c66e2aa21 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/docs.test.ts.snap +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/docs.test.ts.snap @@ -5,11 +5,22 @@ exports[`simple site custom pagination 1`] = ` "pagination": [ { "id": "doc with space", + "next": { + "permalink": "/docs/doc-draft", + "title": "doc-draft", + }, + "prev": undefined, + }, + { + "id": "doc-draft", "next": { "permalink": "/docs/foo/bar", "title": "Bar", }, - "prev": undefined, + "prev": { + "permalink": "/docs/doc with space", + "title": "Hoo hoo, if this path tricks you...", + }, }, { "id": "foo/bar", @@ -163,6 +174,10 @@ exports[`simple site custom pagination 1`] = ` "id": "doc with space", "type": "doc", }, + { + "id": "doc-draft", + "type": "doc", + }, { "collapsed": false, "collapsible": true, diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap index 46b7b40b8642..b88ea2221fbf 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap @@ -43,6 +43,7 @@ Available document ids are: exports[`simple website content 1`] = ` { "description": "Images", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -95,6 +96,7 @@ exports[`simple website content 1`] = ` exports[`simple website content 2`] = ` { "description": "Hi, Endilie here :)", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -140,6 +142,7 @@ exports[`simple website content 2`] = ` exports[`simple website content 3`] = ` { "description": "This is custom description", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -396,6 +399,7 @@ exports[`simple website content: data 1`] = ` "sourceDirName": ".", "slug": "/doc-draft", "permalink": "/docs/doc-draft", + "draft": false, "tags": [], "version": "current", "frontMatter": { @@ -411,6 +415,7 @@ exports[`simple website content: data 1`] = ` "sourceDirName": ".", "slug": "/doc with space", "permalink": "/docs/doc with space", + "draft": false, "tags": [], "version": "current", "frontMatter": {} @@ -424,6 +429,7 @@ exports[`simple website content: data 1`] = ` "sourceDirName": "foo", "slug": "/foo/bar", "permalink": "/docs/foo/bar", + "draft": false, "tags": [], "version": "current", "frontMatter": { @@ -444,6 +450,7 @@ exports[`simple website content: data 1`] = ` "sourceDirName": "foo", "slug": "/foo/bazSlug.html", "permalink": "/docs/foo/bazSlug.html", + "draft": false, "tags": [ { "label": "tag 1", @@ -488,6 +495,7 @@ exports[`simple website content: data 1`] = ` "sourceDirName": ".", "slug": "/headingAsTitle", "permalink": "/docs/headingAsTitle", + "draft": false, "tags": [], "version": "current", "frontMatter": {}, @@ -510,6 +518,7 @@ exports[`simple website content: data 1`] = ` "sourceDirName": ".", "slug": "/", "permalink": "/docs/", + "draft": false, "tags": [ { "label": "tag-1", @@ -546,6 +555,7 @@ exports[`simple website content: data 1`] = ` "sourceDirName": ".", "slug": "/ipsum", "permalink": "/docs/ipsum", + "draft": false, "editUrl": null, "tags": [], "version": "current", @@ -562,6 +572,7 @@ exports[`simple website content: data 1`] = ` "sourceDirName": ".", "slug": "/lorem", "permalink": "/docs/lorem", + "draft": false, "editUrl": "https://github.com/customUrl/docs/lorem.md", "tags": [], "version": "current", @@ -579,6 +590,7 @@ exports[`simple website content: data 1`] = ` "sourceDirName": ".", "slug": "/rootAbsoluteSlug", "permalink": "/docs/rootAbsoluteSlug", + "draft": false, "tags": [], "version": "current", "frontMatter": { @@ -605,6 +617,7 @@ exports[`simple website content: data 1`] = ` "sourceDirName": ".", "slug": "/rootRelativeSlug", "permalink": "/docs/rootRelativeSlug", + "draft": false, "tags": [], "version": "current", "frontMatter": { @@ -631,6 +644,7 @@ exports[`simple website content: data 1`] = ` "sourceDirName": ".", "slug": "/hey/rootResolvedSlug", "permalink": "/docs/hey/rootResolvedSlug", + "draft": false, "tags": [], "version": "current", "frontMatter": { @@ -657,6 +671,7 @@ exports[`simple website content: data 1`] = ` "sourceDirName": ".", "slug": "/rootTryToEscapeSlug", "permalink": "/docs/rootTryToEscapeSlug", + "draft": false, "tags": [], "version": "current", "frontMatter": { @@ -683,6 +698,7 @@ exports[`simple website content: data 1`] = ` "sourceDirName": "slugs", "slug": "/absoluteSlug", "permalink": "/docs/absoluteSlug", + "draft": false, "tags": [], "version": "current", "frontMatter": { @@ -698,6 +714,7 @@ exports[`simple website content: data 1`] = ` "sourceDirName": "slugs", "slug": "/slugs/relativeSlug", "permalink": "/docs/slugs/relativeSlug", + "draft": false, "tags": [], "version": "current", "frontMatter": { @@ -713,6 +730,7 @@ exports[`simple website content: data 1`] = ` "sourceDirName": "slugs", "slug": "/slugs/hey/resolvedSlug", "permalink": "/docs/slugs/hey/resolvedSlug", + "draft": false, "tags": [], "version": "current", "frontMatter": { @@ -728,6 +746,7 @@ exports[`simple website content: data 1`] = ` "sourceDirName": "slugs", "slug": "/tryToEscapeSlug", "permalink": "/docs/tryToEscapeSlug", + "draft": false, "tags": [], "version": "current", "frontMatter": { @@ -1601,6 +1620,7 @@ exports[`site with custom sidebar items generator sidebarItemsGenerator is calle exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 1`] = ` { "description": "Getting started text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -1628,6 +1648,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 2`] = ` { "description": "Installation text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -1658,6 +1679,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 3`] = ` { "description": "Guide 1 text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -1691,6 +1713,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 4`] = ` { "description": "Guide 2 text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -1723,6 +1746,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 5`] = ` { "description": "Guide 2.5 text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -1756,6 +1780,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 6`] = ` { "description": "Guide 3 text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -1789,6 +1814,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 7`] = ` { "description": "Guide 4 text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -1821,6 +1847,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 8`] = ` { "description": "Guide 5 text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -1853,6 +1880,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 9`] = ` { "description": "API Overview text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -1883,6 +1911,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 10`] = ` { "description": "Client API text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -1913,6 +1942,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 11`] = ` { "description": "Server API text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -1943,6 +1973,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 12`] = ` { "description": "Plugin API text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -1973,6 +2004,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 13`] = ` { "description": "Theme API text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -2003,6 +2035,7 @@ exports[`site with full autogenerated sidebar docs in fully generated sidebar ha exports[`site with full autogenerated sidebar docs in fully generated sidebar have correct metadata 14`] = ` { "description": "API End text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -2182,6 +2215,7 @@ exports[`site with partial autogenerated sidebars 2 (fix #4638) sidebar is parti exports[`site with partial autogenerated sidebars docs in partially generated sidebar have correct metadata 1`] = ` { "description": "API End text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -2209,6 +2243,7 @@ exports[`site with partial autogenerated sidebars docs in partially generated si exports[`site with partial autogenerated sidebars docs in partially generated sidebar have correct metadata 2`] = ` { "description": "API Overview text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -2239,6 +2274,7 @@ exports[`site with partial autogenerated sidebars docs in partially generated si exports[`site with partial autogenerated sidebars docs in partially generated sidebar have correct metadata 3`] = ` { "description": "Plugin API text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -2269,6 +2305,7 @@ exports[`site with partial autogenerated sidebars docs in partially generated si exports[`site with partial autogenerated sidebars docs in partially generated sidebar have correct metadata 4`] = ` { "description": "Theme API text", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -2328,6 +2365,7 @@ exports[`site with partial autogenerated sidebars sidebar is partially autogener exports[`versioned website (community) content 1`] = ` { "description": "Team current version (translated)", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -2354,6 +2392,7 @@ exports[`versioned website (community) content 1`] = ` exports[`versioned website (community) content 2`] = ` { "description": "Team 1.0.0", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -2408,6 +2447,7 @@ exports[`versioned website (community) content: data 1`] = ` "sourceDirName": ".", "slug": "/team", "permalink": "/community/team", + "draft": false, "tags": [], "version": "1.0.0", "frontMatter": {}, @@ -2422,6 +2462,7 @@ exports[`versioned website (community) content: data 1`] = ` "sourceDirName": ".", "slug": "/team", "permalink": "/community/next/team", + "draft": false, "tags": [], "version": "current", "frontMatter": { @@ -2604,6 +2645,7 @@ exports[`versioned website (community) getPathToWatch 1`] = ` exports[`versioned website content 1`] = ` { "description": "This is next version of bar.", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -2654,6 +2696,7 @@ exports[`versioned website content 1`] = ` exports[`versioned website content 2`] = ` { "description": "Bar 1.0.1 !", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -2681,6 +2724,7 @@ exports[`versioned website content 2`] = ` exports[`versioned website content 3`] = ` { "description": "Hello next !", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -2710,6 +2754,7 @@ exports[`versioned website content 3`] = ` exports[`versioned website content 4`] = ` { "description": "Hello 1.0.1 !", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": { @@ -2739,6 +2784,7 @@ exports[`versioned website content 4`] = ` exports[`versioned website content 5`] = ` { "description": "Baz 1.0.0 ! This will be deleted in next subsequent versions.", + "draft": false, "editUrl": undefined, "formattedLastUpdatedAt": undefined, "frontMatter": {}, @@ -2880,6 +2926,7 @@ exports[`versioned website content: data 1`] = ` "sourceDirName": "foo", "slug": "/foo/barSlug", "permalink": "/docs/next/foo/barSlug", + "draft": false, "tags": [ { "label": "barTag 1", @@ -2921,6 +2968,7 @@ exports[`versioned website content: data 1`] = ` "sourceDirName": ".", "slug": "/", "permalink": "/docs/next/", + "draft": false, "tags": [], "version": "current", "frontMatter": { @@ -2941,6 +2989,7 @@ exports[`versioned website content: data 1`] = ` "sourceDirName": "slugs", "slug": "/absoluteSlug", "permalink": "/docs/next/absoluteSlug", + "draft": false, "tags": [], "version": "current", "frontMatter": { @@ -2956,6 +3005,7 @@ exports[`versioned website content: data 1`] = ` "sourceDirName": "slugs", "slug": "/slugs/relativeSlug", "permalink": "/docs/next/slugs/relativeSlug", + "draft": false, "tags": [], "version": "current", "frontMatter": { @@ -2971,6 +3021,7 @@ exports[`versioned website content: data 1`] = ` "sourceDirName": "slugs", "slug": "/slugs/hey/resolvedSlug", "permalink": "/docs/next/slugs/hey/resolvedSlug", + "draft": false, "tags": [], "version": "current", "frontMatter": { @@ -2986,6 +3037,7 @@ exports[`versioned website content: data 1`] = ` "sourceDirName": "slugs", "slug": "/tryToEscapeSlug", "permalink": "/docs/next/tryToEscapeSlug", + "draft": false, "tags": [], "version": "current", "frontMatter": { @@ -3001,6 +3053,7 @@ exports[`versioned website content: data 1`] = ` "sourceDirName": ".", "slug": "/", "permalink": "/docs/1.0.0/", + "draft": false, "tags": [], "version": "1.0.0", "frontMatter": { @@ -3021,6 +3074,7 @@ exports[`versioned website content: data 1`] = ` "sourceDirName": "foo", "slug": "/foo/barSlug", "permalink": "/docs/1.0.0/foo/barSlug", + "draft": false, "tags": [], "version": "1.0.0", "frontMatter": { @@ -3041,6 +3095,7 @@ exports[`versioned website content: data 1`] = ` "sourceDirName": "foo", "slug": "/foo/baz", "permalink": "/docs/1.0.0/foo/baz", + "draft": false, "tags": [], "version": "1.0.0", "frontMatter": {}, @@ -3063,6 +3118,7 @@ exports[`versioned website content: data 1`] = ` "sourceDirName": "foo", "slug": "/foo/bar", "permalink": "/docs/foo/bar", + "draft": false, "tags": [], "version": "1.0.1", "frontMatter": {}, @@ -3081,6 +3137,7 @@ exports[`versioned website content: data 1`] = ` "sourceDirName": ".", "slug": "/", "permalink": "/docs/", + "draft": false, "tags": [], "version": "1.0.1", "frontMatter": { @@ -3101,6 +3158,7 @@ exports[`versioned website content: data 1`] = ` "sourceDirName": ".", "slug": "/rootAbsoluteSlug", "permalink": "/docs/withSlugs/rootAbsoluteSlug", + "draft": false, "tags": [], "version": "withSlugs", "frontMatter": { @@ -3117,6 +3175,7 @@ exports[`versioned website content: data 1`] = ` "sourceDirName": ".", "slug": "/rootRelativeSlug", "permalink": "/docs/withSlugs/rootRelativeSlug", + "draft": false, "tags": [], "version": "withSlugs", "frontMatter": { @@ -3132,6 +3191,7 @@ exports[`versioned website content: data 1`] = ` "sourceDirName": ".", "slug": "/hey/rootResolvedSlug", "permalink": "/docs/withSlugs/hey/rootResolvedSlug", + "draft": false, "tags": [], "version": "withSlugs", "frontMatter": { @@ -3147,6 +3207,7 @@ exports[`versioned website content: data 1`] = ` "sourceDirName": ".", "slug": "/rootTryToEscapeSlug", "permalink": "/docs/withSlugs/rootTryToEscapeSlug", + "draft": false, "tags": [], "version": "withSlugs", "frontMatter": { @@ -3162,6 +3223,7 @@ exports[`versioned website content: data 1`] = ` "sourceDirName": "slugs", "slug": "/absoluteSlug", "permalink": "/docs/withSlugs/absoluteSlug", + "draft": false, "tags": [], "version": "withSlugs", "frontMatter": { @@ -3177,6 +3239,7 @@ exports[`versioned website content: data 1`] = ` "sourceDirName": "slugs", "slug": "/slugs/relativeSlug", "permalink": "/docs/withSlugs/slugs/relativeSlug", + "draft": false, "tags": [], "version": "withSlugs", "frontMatter": { @@ -3192,6 +3255,7 @@ exports[`versioned website content: data 1`] = ` "sourceDirName": "slugs", "slug": "/slugs/hey/resolvedSlug", "permalink": "/docs/withSlugs/slugs/hey/resolvedSlug", + "draft": false, "tags": [], "version": "withSlugs", "frontMatter": { @@ -3207,6 +3271,7 @@ exports[`versioned website content: data 1`] = ` "sourceDirName": "slugs", "slug": "/tryToEscapeSlug", "permalink": "/docs/withSlugs/tryToEscapeSlug", + "draft": false, "tags": [], "version": "withSlugs", "frontMatter": { diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts index 8d6482ed38c3..3d3745fbdce8 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts @@ -14,6 +14,7 @@ import { readDocFile, addDocNavigation, isCategoryIndex, + type DocEnv, } from '../docs'; import {loadSidebars} from '../sidebars'; import type {Sidebars} from '../sidebars/types'; @@ -60,57 +61,58 @@ ${markdown} }; }; +type TestUtilsArg = { + siteDir: string; + context: LoadContext; + versionMetadata: VersionMetadata; + options: MetadataOptions; + env?: DocEnv; +}; + function createTestUtils({ siteDir, context, versionMetadata, options, -}: { - siteDir: string; - context: LoadContext; - versionMetadata: VersionMetadata; - options: MetadataOptions; -}) { + env = 'production', +}: TestUtilsArg) { async function readDoc(docFileSource: string) { return readDocFile(versionMetadata, docFileSource, options); } - function processDocFile(docFile: DocFile) { + async function processDocFile(docFileArg: DocFile | string) { + const docFile: DocFile = + typeof docFileArg === 'string' ? await readDoc(docFileArg) : docFileArg; + return processDocMetadata({ docFile, versionMetadata, options, context, + env, }); } + async function testMeta( docFileSource: string, expectedMetadata: Optional< DocMetadataBase, - 'source' | 'lastUpdatedBy' | 'lastUpdatedAt' | 'editUrl' - > | null, + 'source' | 'lastUpdatedBy' | 'lastUpdatedAt' | 'editUrl' | 'draft' + >, ) { const docFile = await readDoc(docFileSource); - const metadata = await processDocMetadata({ - docFile, - versionMetadata, - context, - options, + const metadata = await processDocFile(docFile); + expect(metadata).toEqual({ + lastUpdatedBy: undefined, + lastUpdatedAt: undefined, + editUrl: undefined, + draft: false, + source: path.posix.join( + '@site', + posixPath(path.relative(siteDir, versionMetadata.contentPath)), + posixPath(docFileSource), + ), + ...expectedMetadata, }); - if (expectedMetadata === null) { - expect(metadata).toBeNull(); - } else { - expect(metadata).toEqual({ - lastUpdatedBy: undefined, - lastUpdatedAt: undefined, - editUrl: undefined, - source: path.posix.join( - '@site', - posixPath(path.relative(siteDir, versionMetadata.contentPath)), - posixPath(docFileSource), - ), - ...expectedMetadata, - }); - } } async function testSlug(docFileSource: string, expectedPermalink: string) { @@ -120,6 +122,7 @@ function createTestUtils({ versionMetadata, context, options, + env, }); expect(metadata.permalink).toEqual(expectedPermalink); } @@ -132,16 +135,15 @@ function createTestUtils({ }[]; sidebars: Sidebars; }> { - const rawDocs = docFiles - .map((docFile) => - processDocMetadata({ - docFile, - versionMetadata, - context, - options, - }), - ) - .filter(Boolean); + const rawDocs = docFiles.map((docFile) => + processDocMetadata({ + docFile, + versionMetadata, + context, + options, + env: 'production', + }), + ); const sidebars = await loadSidebars(versionMetadata.sidebarFilePath, { sidebarItemsGenerator: ({defaultSidebarItemsGenerator, ...args}) => defaultSidebarItemsGenerator({...args}), @@ -185,20 +187,27 @@ describe('simple site', () => { options, }); expect(versionsMetadata).toHaveLength(1); - const [currentVersion] = versionsMetadata; + const currentVersion = versionsMetadata[0]!; + + function createTestUtilsPartial(args: Partial) { + return createTestUtils({ + siteDir, + context, + options, + versionMetadata: currentVersion, + ...args, + }); + } + + const defaultTestUtils = createTestUtilsPartial({}); - const defaultTestUtils = createTestUtils({ - siteDir, - context, - options, - versionMetadata: currentVersion, - }); return { siteDir, context, options, versionsMetadata, defaultTestUtils, + createTestUtilsPartial, currentVersion, }; } @@ -278,13 +287,14 @@ describe('simple site', () => { }); it('docs with editUrl', async () => { - const {siteDir, context, options, currentVersion} = await loadSite({ - options: { - editUrl: 'https://github.com/facebook/docusaurus/edit/main/website', - }, - }); + const {siteDir, context, options, currentVersion, createTestUtilsPartial} = + await loadSite({ + options: { + editUrl: 'https://github.com/facebook/docusaurus/edit/main/website', + }, + }); - const testUtilsLocal = createTestUtils({ + const testUtilsLocal = createTestUtilsPartial({ siteDir, context, options, @@ -352,13 +362,14 @@ describe('simple site', () => { const editUrlFunction: EditUrlFunction = jest.fn(() => hardcodedEditUrl); - const {siteDir, context, options, currentVersion} = await loadSite({ - options: { - editUrl: editUrlFunction, - }, - }); + const {siteDir, context, options, currentVersion, createTestUtilsPartial} = + await loadSite({ + options: { + editUrl: editUrlFunction, + }, + }); - const testUtilsLocal = createTestUtils({ + const testUtilsLocal = createTestUtilsPartial({ siteDir, context, options, @@ -409,14 +420,15 @@ describe('simple site', () => { }); it('docs with last update time and author', async () => { - const {siteDir, context, options, currentVersion} = await loadSite({ - options: { - showLastUpdateAuthor: true, - showLastUpdateTime: true, - }, - }); + const {siteDir, context, options, currentVersion, createTestUtilsPartial} = + await loadSite({ + options: { + showLastUpdateAuthor: true, + showLastUpdateTime: true, + }, + }); - const testUtilsLocal = createTestUtils({ + const testUtilsLocal = createTestUtilsPartial({ siteDir, context, options, @@ -445,11 +457,25 @@ describe('simple site', () => { }); it('docs with draft frontmatter', async () => { - process.env.NODE_ENV = 'production'; + const {createTestUtilsPartial} = await loadSite(); - const {defaultTestUtils} = await loadSite(); + const testUtilsProd = createTestUtilsPartial({ + env: 'production', + }); + await expect( + testUtilsProd.processDocFile('doc-draft.md'), + ).resolves.toMatchObject({ + draft: true, + }); - await defaultTestUtils.testMeta('doc-draft.md', null); + const testUtilsDev = createTestUtilsPartial({ + env: 'development', + }); + await expect( + testUtilsDev.processDocFile('doc-draft.md'), + ).resolves.toMatchObject({ + draft: false, + }); }); it('docs with slugs', async () => { @@ -508,7 +534,7 @@ describe('simple site', () => { it('custom pagination', async () => { const {defaultTestUtils, options, versionsMetadata} = await loadSite(); - const docs = await readVersionDocs(versionsMetadata[0], options); + const docs = await readVersionDocs(versionsMetadata[0]!, options); await expect( defaultTestUtils.generateNavigation(docs), ).resolves.toMatchSnapshot(); @@ -516,7 +542,7 @@ describe('simple site', () => { it('bad pagination', async () => { const {defaultTestUtils, options, versionsMetadata} = await loadSite(); - const docs = await readVersionDocs(versionsMetadata[0], options); + const docs = await readVersionDocs(versionsMetadata[0]!, options); docs.push( createFakeDocFile({ source: 'bad', @@ -552,8 +578,11 @@ describe('versioned site', () => { options, }); expect(versionsMetadata).toHaveLength(4); - const [currentVersion, version101, version100, versionWithSlugs] = - versionsMetadata; + + const currentVersion = versionsMetadata[0]!; + const version101 = versionsMetadata[1]!; + const version100 = versionsMetadata[2]!; + const versionWithSlugs = versionsMetadata[3]!; const currentVersionTestUtils = createTestUtils({ siteDir, diff --git a/packages/docusaurus-plugin-content-docs/src/docs.ts b/packages/docusaurus-plugin-content-docs/src/docs.ts index 0991dc0bf54a..fcaf7263110d 100644 --- a/packages/docusaurus-plugin-content-docs/src/docs.ts +++ b/packages/docusaurus-plugin-content-docs/src/docs.ts @@ -38,6 +38,7 @@ import type { PropNavigationLink, LastUpdateData, VersionMetadata, + DocFrontMatter, } from '@docusaurus/plugin-content-docs'; type LastUpdateOptions = Pick< @@ -110,17 +111,32 @@ export async function readVersionDocs( ); } +export type DocEnv = 'production' | 'development'; + +// docs with draft front matter are only considered draft in production +function isDraftForEnvironment({ + env, + frontMatter, +}: { + frontMatter: DocFrontMatter; + env: DocEnv; +}): boolean { + return (env === 'production' && frontMatter.draft) ?? false; +} + function doProcessDocMetadata({ docFile, versionMetadata, context, options, + env, }: { docFile: DocFile; versionMetadata: VersionMetadata; context: LoadContext; options: MetadataOptions; -}): DocMetadataBase | null { + env: DocEnv; +}): DocMetadataBase { const {source, content, lastUpdate, contentPath, filePath} = docFile; const {siteDir, i18n} = context; @@ -131,10 +147,6 @@ function doProcessDocMetadata({ } = parseMarkdownString(content); const frontMatter = validateDocFrontMatter(unsafeFrontMatter); - if ((frontMatter.draft && process.env.NODE_ENV === 'production') || false) { - return null; - } - const { custom_edit_url: customEditURL, @@ -239,6 +251,8 @@ function doProcessDocMetadata({ return undefined; } + const draft = isDraftForEnvironment({env, frontMatter}); + // Assign all of object properties during instantiation (if possible) for // NodeJS optimization. // Adding properties to object after instantiation will cause hidden @@ -252,6 +266,7 @@ function doProcessDocMetadata({ sourceDirName, slug: docSlug, permalink, + draft, editUrl: customEditURL !== undefined ? customEditURL : getDocEditUrl(), tags: normalizeFrontMatterTags(versionMetadata.tagsPath, frontMatter.tags), version: versionMetadata.versionName, @@ -272,7 +287,8 @@ export function processDocMetadata(args: { versionMetadata: VersionMetadata; context: LoadContext; options: MetadataOptions; -}): DocMetadataBase | null { + env: DocEnv; +}): DocMetadataBase { try { return doProcessDocMetadata(args); } catch (err) { diff --git a/packages/docusaurus-plugin-content-docs/src/index.ts b/packages/docusaurus-plugin-content-docs/src/index.ts index 5f10c6c66455..fb6f45310804 100644 --- a/packages/docusaurus-plugin-content-docs/src/index.ts +++ b/packages/docusaurus-plugin-content-docs/src/index.ts @@ -22,11 +22,13 @@ import { import type {LoadContext, Plugin} from '@docusaurus/types'; import {loadSidebars} from './sidebars'; import {CategoryMetadataFilenamePattern} from './sidebars/generator'; +import type { + DocEnv} from './docs'; import { readVersionDocs, processDocMetadata, addDocNavigation, - getMainDocId, + getMainDocId } from './docs'; import {readVersionsMetadata} from './versions'; import type { @@ -127,7 +129,7 @@ export default async function pluginContentDocs( async loadContent() { async function loadVersionDocsBase( versionMetadata: VersionMetadata, - ): Promise<(DocMetadataBase | null)[]> { + ): Promise { const docFiles = await readVersionDocs(versionMetadata, options); if (docFiles.length === 0) { throw new Error( @@ -145,6 +147,7 @@ export default async function pluginContentDocs( versionMetadata, context, options, + env: process.env.NODE_ENV as DocEnv, }); } return Promise.all(docFiles.map(processVersionDoc)); @@ -153,9 +156,9 @@ export default async function pluginContentDocs( async function doLoadVersion( versionMetadata: VersionMetadata, ): Promise { - const docs = (await loadVersionDocsBase(versionMetadata)).filter( - Boolean, - ) as DocMetadataBase[]; + const docs: DocMetadataBase[] = await loadVersionDocsBase( + versionMetadata, + ); const sidebars = await loadSidebars(versionMetadata.sidebarFilePath, { sidebarItemsGenerator: options.sidebarItemsGenerator, diff --git a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts index 58ff881ba62c..430c8135ba81 100644 --- a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts +++ b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts @@ -392,6 +392,11 @@ declare module '@docusaurus/plugin-content-docs' { slug: string; /** Full URL to this doc, with base URL and version path. */ permalink: string; + /** + * Tells if this doc is a draft and should be filtered + * for current environment + */ + draft: boolean; /** * Position in an autogenerated sidebar slice, acquired through front matter * or number prefix. From 5ad19360d1d5802a44072d2c84b3d81181ef05da Mon Sep 17 00:00:00 2001 From: sebastienlorber Date: Fri, 8 Apr 2022 18:33:40 +0200 Subject: [PATCH 09/15] wire test section --- website/_dogfooding/_docs tests/some-draft.md | 7 ------- website/_dogfooding/_docs tests/test-draft.md | 9 +++++++++ website/_dogfooding/docs-tests-sidebars.js | 2 +- website/docusaurus.config.js | 14 ++++++++++---- 4 files changed, 20 insertions(+), 12 deletions(-) delete mode 100644 website/_dogfooding/_docs tests/some-draft.md create mode 100644 website/_dogfooding/_docs tests/test-draft.md diff --git a/website/_dogfooding/_docs tests/some-draft.md b/website/_dogfooding/_docs tests/some-draft.md deleted file mode 100644 index d9d87847d6c8..000000000000 --- a/website/_dogfooding/_docs tests/some-draft.md +++ /dev/null @@ -1,7 +0,0 @@ ---- -draft: true ---- - -# Some Draft - -This page should only be visible in local development diff --git a/website/_dogfooding/_docs tests/test-draft.md b/website/_dogfooding/_docs tests/test-draft.md new file mode 100644 index 000000000000..2aa775b6e801 --- /dev/null +++ b/website/_dogfooding/_docs tests/test-draft.md @@ -0,0 +1,9 @@ +--- +draft: true +--- + +# Test section + +Welcome to the docs plugin test section + +Note: this draft doc (and the navbar link) should only be visible in local development diff --git a/website/_dogfooding/docs-tests-sidebars.js b/website/_dogfooding/docs-tests-sidebars.js index 19740a11e3a0..0143596f80a1 100644 --- a/website/_dogfooding/docs-tests-sidebars.js +++ b/website/_dogfooding/docs-tests-sidebars.js @@ -14,6 +14,7 @@ const sidebars = { className: 'red', label: 'Index', }, + 'test-draft', 'doc-without-sidebar', 'doc-with-another-sidebar', { @@ -80,7 +81,6 @@ const sidebars = { }, ], }, - 'some-draft', { type: 'category', label: 'HTML items tests', diff --git a/website/docusaurus.config.js b/website/docusaurus.config.js index ff88fba0232e..8c4bebc928a8 100644 --- a/website/docusaurus.config.js +++ b/website/docusaurus.config.js @@ -97,15 +97,15 @@ const config = { loader: require.resolve('swc-loader'), options: { jsc: { - "parser": { - "syntax": "typescript", - "tsx": true + parser: { + syntax: 'typescript', + tsx: true, }, target: 'es2017', }, module: { type: isServer ? 'commonjs' : 'es6', - } + }, }, }), }, @@ -407,6 +407,12 @@ const config = { position: 'left', activeBaseRegex: `/community/`, }, + { + type: 'doc', + docId: 'test-draft', + label: 'Tests', + docsPluginId: 'docs-tests', + }, // right { type: 'docsVersionDropdown', From 18476d039eaa47c8647cc4abf4ad9a343ac43d0d Mon Sep 17 00:00:00 2001 From: sebastienlorber Date: Fri, 8 Apr 2022 18:38:49 +0200 Subject: [PATCH 10/15] minor processItem simplification --- .../docusaurus-plugin-content-docs/src/sidebars/processor.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts index 7e83dd08a921..21ccaf893c7f 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts @@ -88,7 +88,7 @@ async function processSidebar( return [ { ...item, - items: (await Promise.all(item.items.map(processItem))).flat(), + items: await processItems(item.items), }, ]; } From f225a5ca9db04a49097555ccdf46fa9cc5546632 Mon Sep 17 00:00:00 2001 From: sebastienlorber Date: Fri, 8 Apr 2022 19:20:56 +0200 Subject: [PATCH 11/15] add draft sidebar/navbar item filtering --- .../src/globalData.ts | 1 + .../src/index.ts | 12 ++++++++---- .../src/plugin-content-docs.d.ts | 1 + .../src/sidebars/processor.ts | 19 ++++++++++++++++--- .../src/sidebars/types.ts | 1 + .../src/types.ts | 1 + .../src/theme-classic.d.ts | 4 +++- .../src/theme/NavbarItem/DocNavbarItem.tsx | 8 +++++++- .../src/utils/docsUtils.tsx | 12 +++++++++++- 9 files changed, 49 insertions(+), 10 deletions(-) diff --git a/packages/docusaurus-plugin-content-docs/src/globalData.ts b/packages/docusaurus-plugin-content-docs/src/globalData.ts index ebeb61c8228e..bc3b6dd86268 100644 --- a/packages/docusaurus-plugin-content-docs/src/globalData.ts +++ b/packages/docusaurus-plugin-content-docs/src/globalData.ts @@ -72,6 +72,7 @@ export function toGlobalDataVersion(version: LoadedVersion): GlobalVersion { docs: version.docs .map(toGlobalDataDoc) .concat(version.categoryGeneratedIndices.map(toGlobalDataGeneratedIndex)), + draftIds: version.drafts.map((doc) => doc.unversionedId), sidebars: toGlobalSidebars(version.sidebars, version), }; } diff --git a/packages/docusaurus-plugin-content-docs/src/index.ts b/packages/docusaurus-plugin-content-docs/src/index.ts index fb6f45310804..80bf3324f70a 100644 --- a/packages/docusaurus-plugin-content-docs/src/index.ts +++ b/packages/docusaurus-plugin-content-docs/src/index.ts @@ -22,13 +22,12 @@ import { import type {LoadContext, Plugin} from '@docusaurus/types'; import {loadSidebars} from './sidebars'; import {CategoryMetadataFilenamePattern} from './sidebars/generator'; -import type { - DocEnv} from './docs'; +import type {DocEnv} from './docs'; import { readVersionDocs, processDocMetadata, addDocNavigation, - getMainDocId + getMainDocId, } from './docs'; import {readVersionsMetadata} from './versions'; import type { @@ -60,6 +59,7 @@ import type { } from '@docusaurus/plugin-content-docs'; import {createSidebarsUtils} from './sidebars/utils'; import {getCategoryGeneratedIndexMetadataList} from './categoryGeneratedIndex'; +import {partition} from 'lodash'; export default async function pluginContentDocs( context: LoadContext, @@ -156,14 +156,17 @@ export default async function pluginContentDocs( async function doLoadVersion( versionMetadata: VersionMetadata, ): Promise { - const docs: DocMetadataBase[] = await loadVersionDocsBase( + const docsBase: DocMetadataBase[] = await loadVersionDocsBase( versionMetadata, ); + const [drafts, docs] = partition(docsBase, (doc) => doc.draft); + const sidebars = await loadSidebars(versionMetadata.sidebarFilePath, { sidebarItemsGenerator: options.sidebarItemsGenerator, numberPrefixParser: options.numberPrefixParser, docs, + drafts, version: versionMetadata, sidebarOptions: { sidebarCollapsed: options.sidebarCollapsed, @@ -181,6 +184,7 @@ export default async function pluginContentDocs( sidebarsUtils, versionMetadata.sidebarFilePath as string, ), + drafts, sidebars, mainDocId: getMainDocId({docs, sidebarsUtils}), categoryGeneratedIndices: getCategoryGeneratedIndexMetadataList({ diff --git a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts index 430c8135ba81..cae4977804ec 100644 --- a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts +++ b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts @@ -643,6 +643,7 @@ declare module '@docusaurus/plugin-content-docs/client' { path: string; mainDocId: string; // home doc (if docs homepage configured), or first doc docs: GlobalDoc[]; + draftIds: string[]; sidebars?: {[sidebarId: string]: GlobalSidebar}; }; diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts index 21ccaf893c7f..1ea22e1a95e7 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts @@ -26,7 +26,7 @@ import {DefaultSidebarItemsGenerator} from './generator'; import {validateSidebars} from './validation'; import _ from 'lodash'; import combinePromises from 'combine-promises'; -import {isCategoryIndex} from '../docs'; +import {getDocIds, isCategoryIndex} from '../docs'; function toSidebarItemsGeneratorDoc( doc: DocMetadataBase, @@ -55,7 +55,8 @@ async function processSidebar( categoriesMetadata: {[filePath: string]: CategoryMetadataFile}, params: SidebarProcessorParams, ): Promise { - const {sidebarItemsGenerator, numberPrefixParser, docs, version} = params; + const {sidebarItemsGenerator, numberPrefixParser, docs, drafts, version} = + params; // Just a minor lazy transformation optimization const getSidebarItemsGeneratorDocsAndVersion = _.memoize(() => ({ @@ -81,6 +82,16 @@ async function processSidebar( return processItems(generatedItems); } + const allDraftIdsSet = new Set(drafts.flatMap(getDocIds)); + + function filterItem(item: NormalizedSidebarItem): boolean { + if (item.type === 'doc') { + // Remove draft items from sidebar + return !allDraftIdsSet.has(item.id); + } + return true; + } + async function processItem( item: NormalizedSidebarItem, ): Promise { @@ -101,7 +112,9 @@ async function processSidebar( async function processItems( items: NormalizedSidebarItem[], ): Promise { - return (await Promise.all(items.map(processItem))).flat(); + return ( + await Promise.all(items.filter(filterItem).map(processItem)) + ).flat(); } const processedSidebar = await processItems(unprocessedSidebar); diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/types.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/types.ts index 2dfaf5b7f553..17d94b8ca3b7 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/types.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/types.ts @@ -269,6 +269,7 @@ export type SidebarProcessorParams = { sidebarItemsGenerator: SidebarItemsGeneratorOption; numberPrefixParser: NumberPrefixParser; docs: DocMetadataBase[]; + drafts: DocMetadataBase[]; version: VersionMetadata; categoryLabelSlugger: Slugger; sidebarOptions: SidebarOptions; diff --git a/packages/docusaurus-plugin-content-docs/src/types.ts b/packages/docusaurus-plugin-content-docs/src/types.ts index bb9f8e8e15d1..49156b2514ac 100644 --- a/packages/docusaurus-plugin-content-docs/src/types.ts +++ b/packages/docusaurus-plugin-content-docs/src/types.ts @@ -40,6 +40,7 @@ export type VersionTags = { export type LoadedVersion = VersionMetadata & { mainDocId: string; docs: DocMetadata[]; + drafts: DocMetadata[]; sidebars: Sidebars; categoryGeneratedIndices: CategoryGeneratedIndexMetadata[]; }; diff --git a/packages/docusaurus-theme-classic/src/theme-classic.d.ts b/packages/docusaurus-theme-classic/src/theme-classic.d.ts index 7c0deb7dc60a..834cfef91bd1 100644 --- a/packages/docusaurus-theme-classic/src/theme-classic.d.ts +++ b/packages/docusaurus-theme-classic/src/theme-classic.d.ts @@ -750,7 +750,9 @@ declare module '@theme/NavbarItem/DocNavbarItem' { readonly docsPluginId?: string; } - export default function DocsSidebarNavbarItem(props: Props): JSX.Element; + export default function DocsSidebarNavbarItem( + props: Props, + ): JSX.Element | null; } declare module '@theme/NavbarItem/DocSidebarNavbarItem' { diff --git a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocNavbarItem.tsx b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocNavbarItem.tsx index a5e9b06f02bc..1ed30afe4050 100644 --- a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocNavbarItem.tsx +++ b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocNavbarItem.tsx @@ -18,9 +18,15 @@ export default function DocNavbarItem({ label: staticLabel, docsPluginId, ...props -}: Props): JSX.Element { +}: Props): JSX.Element | null { const {activeDoc} = useActiveDocContext(docsPluginId); const doc = useLayoutDoc(docId, docsPluginId); + + // Draft items are not displayed in the navbar. + if (doc === null) { + return null; + } + const activeDocInfimaClassName = getInfimaActiveClassName(props.mobile); return ( diff --git a/packages/docusaurus-theme-common/src/utils/docsUtils.tsx b/packages/docusaurus-theme-common/src/utils/docsUtils.tsx index 13e63bd44def..c3d169c3e836 100644 --- a/packages/docusaurus-theme-common/src/utils/docsUtils.tsx +++ b/packages/docusaurus-theme-common/src/utils/docsUtils.tsx @@ -259,12 +259,22 @@ export function useLayoutDocsSidebar( * * @throws This hook throws if a doc with said ID is not found. */ -export function useLayoutDoc(docId: string, docsPluginId?: string): GlobalDoc { +export function useLayoutDoc( + docId: string, + docsPluginId?: string, +): GlobalDoc | null { const versions = useDocsVersionCandidates(docsPluginId); return useMemo(() => { const allDocs = versions.flatMap((version) => version.docs); const doc = allDocs.find((versionDoc) => versionDoc.id === docId); if (!doc) { + const isDraft = versions + .flatMap((version) => version.draftIds) + .includes(docId); + // drafts should be silently filtered instead of throwing + if (isDraft) { + return null; + } throw new Error( `DocNavbarItem: couldn't find any doc with id "${docId}" in version${ versions.length > 1 ? 's' : '' From b84f4c512972c3bd58ae20e94265330c980d36a6 Mon Sep 17 00:00:00 2001 From: sebastienlorber Date: Fri, 8 Apr 2022 19:22:35 +0200 Subject: [PATCH 12/15] update snapshot --- .../src/__tests__/__snapshots__/index.test.ts.snap | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap index b88ea2221fbf..c8b0408c5ee1 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap @@ -353,6 +353,7 @@ exports[`simple website content 5`] = ` "sidebar": "docs", }, ], + "draftIds": [], "isLast": true, "label": "Next", "mainDocId": "hello", @@ -1111,6 +1112,7 @@ exports[`simple website content: global data 1`] = ` "sidebar": "docs", }, ], + "draftIds": [], "isLast": true, "label": "Next", "mainDocId": "hello", @@ -2542,6 +2544,7 @@ exports[`versioned website (community) content: global data 1`] = ` "sidebar": "community", }, ], + "draftIds": [], "isLast": false, "label": "Next", "mainDocId": "team", @@ -2564,6 +2567,7 @@ exports[`versioned website (community) content: global data 1`] = ` "sidebar": "version-1.0.0/community", }, ], + "draftIds": [], "isLast": true, "label": "1.0.0", "mainDocId": "team", @@ -3647,6 +3651,7 @@ exports[`versioned website content: global data 1`] = ` "sidebar": undefined, }, ], + "draftIds": [], "isLast": false, "label": "Next", "mainDocId": "hello", @@ -3674,6 +3679,7 @@ exports[`versioned website content: global data 1`] = ` "sidebar": "VersionedSideBarNameDoesNotMatter/docs", }, ], + "draftIds": [], "isLast": true, "label": "1.0.1", "mainDocId": "hello", @@ -3706,6 +3712,7 @@ exports[`versioned website content: global data 1`] = ` "sidebar": "version-1.0.0/docs", }, ], + "draftIds": [], "isLast": false, "label": "1.0.0", "mainDocId": "hello", @@ -3763,6 +3770,7 @@ exports[`versioned website content: global data 1`] = ` "sidebar": undefined, }, ], + "draftIds": [], "isLast": false, "label": "withSlugs", "mainDocId": "rootAbsoluteSlug", From d43ed5e322580b35bc0559083e66f3dd9288624d Mon Sep 17 00:00:00 2001 From: sebastienlorber Date: Fri, 8 Apr 2022 19:27:07 +0200 Subject: [PATCH 13/15] test --- .../src/__tests__/__snapshots__/globalData.test.ts.snap | 3 +++ .../src/__tests__/globalData.test.ts | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/globalData.test.ts.snap b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/globalData.test.ts.snap index a646e0241e43..242bf1b74890 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/globalData.test.ts.snap +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/globalData.test.ts.snap @@ -19,6 +19,9 @@ exports[`toGlobalDataVersion generates the right docs, sidebars, and metadata 1` "sidebar": "tutorial", }, ], + "draftIds": [ + "some-draft-id", + ], "isLast": true, "label": "Label", "mainDocId": "main", diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/globalData.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/globalData.test.ts index 2c9a02966317..d90dcb0e8784 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/globalData.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/globalData.test.ts @@ -28,6 +28,13 @@ describe('toGlobalDataVersion', () => { sidebar: 'tutorial', }, ], + drafts: [ + { + unversionedId: 'some-draft-id', + permalink: '/current/draft', + sidebar: undefined, + }, + ], sidebars: { another: [ { From 42fd11d1e6b24c08d12052f718f0b9d6ab467e6d Mon Sep 17 00:00:00 2001 From: sebastienlorber Date: Fri, 8 Apr 2022 19:41:21 +0200 Subject: [PATCH 14/15] fix tests + improve sidebars draft item filtering --- .../src/__tests__/docs.test.ts | 1 + .../__snapshots__/index.test.ts.snap | 25 +++++++++++++++++++ .../src/sidebars/__tests__/index.test.ts | 12 +++++++++ .../src/sidebars/__tests__/processor.test.ts | 1 + .../src/sidebars/processor.ts | 20 +++++++++++---- 5 files changed, 54 insertions(+), 5 deletions(-) diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts index 3d3745fbdce8..44bd95eb55b9 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts @@ -149,6 +149,7 @@ function createTestUtils({ defaultSidebarItemsGenerator({...args}), numberPrefixParser: options.numberPrefixParser, docs: rawDocs, + drafts: [], version: versionMetadata, sidebarOptions: { sidebarCollapsed: false, diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/__snapshots__/index.test.ts.snap index 8657b009a855..f5b2acdd5190 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/__snapshots__/index.test.ts.snap @@ -247,6 +247,31 @@ exports[`loadSidebars sidebars with known sidebar item type 1`] = ` } `; +exports[`loadSidebars sidebars with some draft items 1`] = ` +{ + "docs": [ + { + "collapsed": true, + "collapsible": true, + "items": [ + { + "id": "foo/bar", + "type": "doc", + }, + { + "href": "https://github.com", + "label": "GitHub", + "type": "link", + }, + ], + "label": "Test", + "link": undefined, + "type": "category", + }, + ], +} +`; + exports[`loadSidebars undefined path 1`] = ` { "defaultSidebar": [ diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/index.test.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/index.test.ts index b94ffea65bdf..3a92144ffd8a 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/index.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/index.test.ts @@ -10,6 +10,7 @@ import path from 'path'; import {loadSidebars, DisabledSidebars} from '../index'; import type {SidebarProcessorParams} from '../types'; import {DefaultSidebarItemsGenerator} from '../generator'; +import type {DocMetadata} from '@docusaurus/plugin-content-docs'; describe('loadSidebars', () => { const fixtureDir = path.join(__dirname, '__fixtures__', 'sidebars'); @@ -24,6 +25,7 @@ describe('loadSidebars', () => { frontMatter: {}, }, ], + drafts: [], version: { contentPath: path.join(fixtureDir, 'docs'), contentPathLocalized: path.join(fixtureDir, 'docs'), @@ -37,6 +39,16 @@ describe('loadSidebars', () => { expect(result).toMatchSnapshot(); }); + it('sidebars with some draft items', async () => { + const sidebarPath = path.join(fixtureDir, 'sidebars.json'); + const paramsWithDrafts: SidebarProcessorParams = { + ...params, + drafts: [{id: 'foo/baz'} as DocMetadata, {id: 'hello'} as DocMetadata], + }; + const result = await loadSidebars(sidebarPath, paramsWithDrafts); + expect(result).toMatchSnapshot(); + }); + it('sidebars with deep level of category', async () => { const sidebarPath = path.join(fixtureDir, 'sidebars-category.js'); const result = await loadSidebars(sidebarPath, params); diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/processor.test.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/processor.test.ts index 500eefbfe1dd..079040879ac6 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/processor.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/__tests__/processor.test.ts @@ -46,6 +46,7 @@ describe('processSidebars', () => { const params: SidebarProcessorParams = { sidebarItemsGenerator: StaticSidebarItemsGenerator, docs: [], + drafts: [], version, numberPrefixParser: DefaultNumberPrefixParser, categoryLabelSlugger: createSlugger(), diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts index 1ea22e1a95e7..78741768e2c8 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts @@ -84,10 +84,20 @@ async function processSidebar( const allDraftIdsSet = new Set(drafts.flatMap(getDocIds)); - function filterItem(item: NormalizedSidebarItem): boolean { - if (item.type === 'doc') { - // Remove draft items from sidebar - return !allDraftIdsSet.has(item.id); + const isDraftItem = (item: NormalizedSidebarItem): boolean => { + if (item.type === 'doc' || item.type === 'ref') { + return allDraftIdsSet.has(item.id); + } + // if a category only contains draft items => it's a draft category + if (item.type === 'category') { + return item.items.every(isDraftItem); + } + return false; + }; + + function itemFilter(item: NormalizedSidebarItem): boolean { + if (isDraftItem(item)) { + return false; } return true; } @@ -113,7 +123,7 @@ async function processSidebar( items: NormalizedSidebarItem[], ): Promise { return ( - await Promise.all(items.filter(filterItem).map(processItem)) + await Promise.all(items.filter(itemFilter).map(processItem)) ).flat(); } From a4fc23dc624b179197e066b5eaf4870d55678814 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sat, 9 Apr 2022 09:57:31 +0800 Subject: [PATCH 15/15] refactors --- .../docusaurus-plugin-content-docs/src/docs.ts | 2 +- .../docusaurus-plugin-content-docs/src/index.ts | 4 ++-- .../src/plugin-content-docs.d.ts | 7 ++++--- .../src/sidebars/processor.ts | 15 ++++----------- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/packages/docusaurus-plugin-content-docs/src/docs.ts b/packages/docusaurus-plugin-content-docs/src/docs.ts index fcaf7263110d..d7eae5bd74d9 100644 --- a/packages/docusaurus-plugin-content-docs/src/docs.ts +++ b/packages/docusaurus-plugin-content-docs/src/docs.ts @@ -113,7 +113,7 @@ export async function readVersionDocs( export type DocEnv = 'production' | 'development'; -// docs with draft front matter are only considered draft in production +/** Docs with draft front matter are only considered draft in production. */ function isDraftForEnvironment({ env, frontMatter, diff --git a/packages/docusaurus-plugin-content-docs/src/index.ts b/packages/docusaurus-plugin-content-docs/src/index.ts index 80bf3324f70a..102c80a48531 100644 --- a/packages/docusaurus-plugin-content-docs/src/index.ts +++ b/packages/docusaurus-plugin-content-docs/src/index.ts @@ -59,7 +59,7 @@ import type { } from '@docusaurus/plugin-content-docs'; import {createSidebarsUtils} from './sidebars/utils'; import {getCategoryGeneratedIndexMetadataList} from './categoryGeneratedIndex'; -import {partition} from 'lodash'; +import _ from 'lodash'; export default async function pluginContentDocs( context: LoadContext, @@ -160,7 +160,7 @@ export default async function pluginContentDocs( versionMetadata, ); - const [drafts, docs] = partition(docsBase, (doc) => doc.draft); + const [drafts, docs] = _.partition(docsBase, (doc) => doc.draft); const sidebars = await loadSidebars(versionMetadata.sidebarFilePath, { sidebarItemsGenerator: options.sidebarItemsGenerator, diff --git a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts index cae4977804ec..00ebe047c06c 100644 --- a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts +++ b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts @@ -393,8 +393,7 @@ declare module '@docusaurus/plugin-content-docs' { /** Full URL to this doc, with base URL and version path. */ permalink: string; /** - * Tells if this doc is a draft and should be filtered - * for current environment + * Draft docs will be excluded for production environment. */ draft: boolean; /** @@ -641,8 +640,10 @@ declare module '@docusaurus/plugin-content-docs/client' { label: string; isLast: boolean; path: string; - mainDocId: string; // home doc (if docs homepage configured), or first doc + /** Home doc (if docs homepage configured), or first doc. */ + mainDocId: string; docs: GlobalDoc[]; + /** Unversioned IDs. In development, this list is empty. */ draftIds: string[]; sidebars?: {[sidebarId: string]: GlobalSidebar}; }; diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts index 78741768e2c8..b0153e9dcbbc 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts @@ -82,26 +82,19 @@ async function processSidebar( return processItems(generatedItems); } - const allDraftIdsSet = new Set(drafts.flatMap(getDocIds)); + const draftIds = new Set(drafts.flatMap(getDocIds)); const isDraftItem = (item: NormalizedSidebarItem): boolean => { if (item.type === 'doc' || item.type === 'ref') { - return allDraftIdsSet.has(item.id); + return draftIds.has(item.id); } - // if a category only contains draft items => it's a draft category + // If a category only contains draft items, it should be filtered entirely. if (item.type === 'category') { return item.items.every(isDraftItem); } return false; }; - function itemFilter(item: NormalizedSidebarItem): boolean { - if (isDraftItem(item)) { - return false; - } - return true; - } - async function processItem( item: NormalizedSidebarItem, ): Promise { @@ -123,7 +116,7 @@ async function processSidebar( items: NormalizedSidebarItem[], ): Promise { return ( - await Promise.all(items.filter(itemFilter).map(processItem)) + await Promise.all(items.filter((i) => !isDraftItem(i)).map(processItem)) ).flat(); }