From b1cb9054646c319028c42ffecce3daaa04d1ee7e Mon Sep 17 00:00:00 2001 From: Paul Sealock Date: Tue, 25 Jul 2023 15:29:43 +1200 Subject: [PATCH] WooCommerce Docs: Handle multiple category posts (#39352) --- .../example-docs/testing/integration-tests.md | 7 ++++++ .../woocommerce-docs/scripts/manifest.json | 16 +++++++++---- .../example-docs/testing/integration-tests.md | 7 ++++++ .../example-docs/testing/unit-tests.md | 2 +- .../lib/__tests__/generate-manifest.ts | 23 +++++++++++++++++++ .../src/md-docs/lib/generate-manifest.ts | 6 ++++- 6 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 plugins/woocommerce-docs/example-docs/testing/integration-tests.md create mode 100644 tools/monorepo-utils/src/md-docs/lib/__tests__/fixtures/example-docs/testing/integration-tests.md diff --git a/plugins/woocommerce-docs/example-docs/testing/integration-tests.md b/plugins/woocommerce-docs/example-docs/testing/integration-tests.md new file mode 100644 index 00000000000..689e5e6a52e --- /dev/null +++ b/plugins/woocommerce-docs/example-docs/testing/integration-tests.md @@ -0,0 +1,7 @@ +--- +post_title: Integration Testing +--- + +## Integration Tests + +Elementary, my dear Watson! Write integration tests. diff --git a/plugins/woocommerce-docs/scripts/manifest.json b/plugins/woocommerce-docs/scripts/manifest.json index 9c76e0da8c2..8061403fe7d 100644 --- a/plugins/woocommerce-docs/scripts/manifest.json +++ b/plugins/woocommerce-docs/scripts/manifest.json @@ -7,7 +7,7 @@ "post_title": "Local Development", "edit_url": "https://github.com/woocommerce/woocommerce/edit/trunk/plugins/woocommerce-docs/example-docs/get-started/local-development.md", "url": "https://raw.githubusercontent.com/woocommerce/woocommerce/trunk/plugins/woocommerce-docs/example-docs/get-started/local-development.md", - "id": "c068ce54044fa44c760a69bd71ef21274f2a5a37" + "id": "c387a046f8307a4f459091a2c45e82263b45e777" } ], "categories": [ @@ -18,7 +18,7 @@ "post_title": "Install the Plugin", "edit_url": "https://github.com/woocommerce/woocommerce/edit/trunk/plugins/woocommerce-docs/example-docs/get-started/installation/install-plugin.md", "url": "https://raw.githubusercontent.com/woocommerce/woocommerce/trunk/plugins/woocommerce-docs/example-docs/get-started/installation/install-plugin.md", - "id": "fb59bd01dda7b090e5b0a557948e155a6b679d6a" + "id": "c4ac8c48c3445186189d15f01028eacd517b9707" } ], "categories": [] @@ -30,7 +30,7 @@ "post_title": "What Went Wrong?", "edit_url": "https://github.com/woocommerce/woocommerce/edit/trunk/plugins/woocommerce-docs/example-docs/get-started/troubleshooting/what-went-wrong.md", "url": "https://raw.githubusercontent.com/woocommerce/woocommerce/trunk/plugins/woocommerce-docs/example-docs/get-started/troubleshooting/what-went-wrong.md", - "id": "1f88c4d039e72c059c928ab475ad1ea0a02c8abb" + "id": "f87dcc9b8177ad2d8549ef7298d66fb7c8616d71" } ], "categories": [] @@ -44,11 +44,17 @@ "post_title": "Unit Testing", "edit_url": "https://github.com/woocommerce/woocommerce/edit/trunk/plugins/woocommerce-docs/example-docs/testing/unit-tests.md", "url": "https://raw.githubusercontent.com/woocommerce/woocommerce/trunk/plugins/woocommerce-docs/example-docs/testing/unit-tests.md", - "id": "120770c899215a889246b47ac883e4dda1f97b8b" + "id": "1b9de005cc5da3b49d12c24837a5a574aa58ad96" + }, + { + "post_title": "Integration Testing", + "edit_url": "https://github.com/woocommerce/woocommerce/edit/trunk/plugins/woocommerce-docs/example-docs/testing/integration-tests.md", + "url": "https://raw.githubusercontent.com/woocommerce/woocommerce/trunk/plugins/woocommerce-docs/example-docs/testing/integration-tests.md", + "id": "ee3fd61c9d7b13614e4431708c336f20c9779e1f" } ], "categories": [] } ], - "hash": "7f0f6f0c39187a5ee2f939d7a0af56538aaeb7f040f56e6dd62458e34a0491bd" + "hash": "84a06ceffaced22a955a131c1272ec9a1d14ae0a7cf9bac4ed21874384f86e9a" } \ No newline at end of file diff --git a/tools/monorepo-utils/src/md-docs/lib/__tests__/fixtures/example-docs/testing/integration-tests.md b/tools/monorepo-utils/src/md-docs/lib/__tests__/fixtures/example-docs/testing/integration-tests.md new file mode 100644 index 00000000000..689e5e6a52e --- /dev/null +++ b/tools/monorepo-utils/src/md-docs/lib/__tests__/fixtures/example-docs/testing/integration-tests.md @@ -0,0 +1,7 @@ +--- +post_title: Integration Testing +--- + +## Integration Tests + +Elementary, my dear Watson! Write integration tests. diff --git a/tools/monorepo-utils/src/md-docs/lib/__tests__/fixtures/example-docs/testing/unit-tests.md b/tools/monorepo-utils/src/md-docs/lib/__tests__/fixtures/example-docs/testing/unit-tests.md index 58e42ceb3c5..990fefd4c7b 100644 --- a/tools/monorepo-utils/src/md-docs/lib/__tests__/fixtures/example-docs/testing/unit-tests.md +++ b/tools/monorepo-utils/src/md-docs/lib/__tests__/fixtures/example-docs/testing/unit-tests.md @@ -1,5 +1,5 @@ --- -category_title: Unit Testing +post_title: Unit Testing --- ## Unit Test diff --git a/tools/monorepo-utils/src/md-docs/lib/__tests__/generate-manifest.ts b/tools/monorepo-utils/src/md-docs/lib/__tests__/generate-manifest.ts index 479cf908b5e..b1f1f25e7f3 100644 --- a/tools/monorepo-utils/src/md-docs/lib/__tests__/generate-manifest.ts +++ b/tools/monorepo-utils/src/md-docs/lib/__tests__/generate-manifest.ts @@ -38,6 +38,29 @@ describe( 'generateManifest', () => { ); } ); + it( 'should generate a manifest with categories that contain all markdown files in a location as individual posts', async () => { + // generate the manifest from fixture directory + const manifest = await generateManifestFromDirectory( + rootDir, + dir, + 'example-docs', + 'https://example.com', + 'https://example.com/edit' + ); + + const topLevelCategories = manifest.categories; + + expect( topLevelCategories[ 1 ].category_title ).toEqual( + 'Testing WooCommerce' + ); + + const posts = topLevelCategories[ 1 ].posts; + + expect( posts ).toHaveLength( 2 ); + expect( posts[ 0 ].post_title ).toEqual( 'Unit Testing' ); + expect( posts[ 1 ].post_title ).toEqual( 'Integration Testing' ); + } ); + it( 'should create categories with titles where there is no index README', async () => { const manifest = await generateManifestFromDirectory( rootDir, diff --git a/tools/monorepo-utils/src/md-docs/lib/generate-manifest.ts b/tools/monorepo-utils/src/md-docs/lib/generate-manifest.ts index f481e1b7487..f436ad6d1d3 100644 --- a/tools/monorepo-utils/src/md-docs/lib/generate-manifest.ts +++ b/tools/monorepo-utils/src/md-docs/lib/generate-manifest.ts @@ -54,12 +54,16 @@ async function processDirectory( const markdownFiles = glob.sync( path.join( subDirectory, '*.md' ) ); + // If there are markdown files in this directory, add a posts array to the category. Otherwise, assume its a top level category that will contain subcategories. + if ( markdownFiles.length > 0 ) { + category.posts = []; + } + markdownFiles.forEach( ( filePath ) => { if ( filePath !== readmePath || ! checkReadme ) { // Skip README.md which we have already processed. const fileContent = fs.readFileSync( filePath, 'utf-8' ); const fileFrontmatter = generatePostFrontMatter( fileContent ); - category.posts = []; if ( baseUrl.includes( 'github' ) ) { fileFrontmatter.edit_url = generateFileUrl(