Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Components: Warn private API in auto-generated readmes #68317

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions bin/api-docs/gen-components-docs/get-tags-from-storybook.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* External dependencies
*/
import fs from 'node:fs/promises';
import babel from '@babel/core';

/**
* Returns `meta.tags` from a Storybook file.
*
* @param {string} filePath
* @return {Promise<string[]>} Array of tags.
*/
export async function getTagsFromStorybook( filePath ) {
const fileContent = await fs.readFile( filePath, 'utf8' );
const parsedFile = babel.parse( fileContent, {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These bin scripts aren't set up to process TS files yet (e.g. index.story.tsx files), so we can't read data from import()ed meta objects. At the moment I think it's simpler to do a quick AST parse than set up TS transpilation just for this, but we can maybe revisit when there are more use cases.

filename: filePath,
} );

const meta = parsedFile.program.body.find(
( node ) =>
node.type === 'VariableDeclaration' &&
node.declarations[ 0 ].id.name === 'meta'
);

return (
meta.declarations[ 0 ].init.properties
.find( ( node ) => node.key.name === 'tags' )
?.value.elements.map( ( node ) => node.value ) ?? []
);
}
9 changes: 9 additions & 0 deletions bin/api-docs/gen-components-docs/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import path from 'path';
*/
import { generateMarkdownDocs } from './markdown/index.mjs';
import { getDescriptionsForSubcomponents } from './get-subcomponent-descriptions.mjs';
import { getTagsFromStorybook } from './get-tags-from-storybook.mjs';

const MANIFEST_GLOB = 'packages/components/src/**/docs-manifest.json';

Expand Down Expand Up @@ -113,9 +114,17 @@ await Promise.all(
} ) ?? []
);

const tags = await getTagsFromStorybook(
path.resolve(
path.dirname( manifestPath ),
'stories/index.story.tsx'
)
);

const docs = generateMarkdownDocs( {
typeDocs,
subcomponentTypeDocs,
tags,
} );
const outputFile = path.resolve(
path.dirname( manifestPath ),
Expand Down
11 changes: 10 additions & 1 deletion bin/api-docs/gen-components-docs/markdown/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,19 @@ function normalizeTrailingNewline( str ) {
return str.replace( /\n*$/, '\n' );
}

export function generateMarkdownDocs( { typeDocs, subcomponentTypeDocs } ) {
export function generateMarkdownDocs( {
typeDocs,
subcomponentTypeDocs,
tags,
} ) {
const mainDocsJson = [
{ h1: typeDocs.displayName },
'<!-- This file is generated automatically and cannot be edited directly. Make edits via TypeScript types and TSDocs. -->',
tags.includes( 'status-private' ) && [
{
p: '🔒 This component is locked as a [private API](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-private-apis/). We do not yet recommend using this outside of the Gutenberg project.',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In an ideal world, and accoding to @jsnajdr's latest suggestions, a 3rd party developer simply should be unable to use private APIs outside of Gutenberg.

},
],
Comment on lines +32 to +36
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using normal Markdown instead of a div callout, because the intended audience is not on Block Editor Handbook.

{
p: `<p class="callout callout-info">See the <a href="https://wordpress.github.io/gutenberg/?path=/docs/components-${ typeDocs.displayName.toLowerCase() }--docs">WordPress Storybook</a> for more detailed, interactive documentation.</p>`,
},
Expand Down
3 changes: 3 additions & 0 deletions packages/components/src/badge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

<!-- This file is generated automatically and cannot be edited directly. Make edits via TypeScript types and TSDocs. -->

🔒 This component is locked as a [private API](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-private-apis/). We do not yet recommend using this outside of the Gutenberg project.


Comment on lines +6 to +7
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant newlines will be fixed in #68301.

<p class="callout callout-info">See the <a href="https://wordpress.github.io/gutenberg/?path=/docs/components-badge--docs">WordPress Storybook</a> for more detailed, interactive documentation.</p>

## Props
Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/badge/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import type { Meta, StoryObj } from '@storybook/react';
*/
import Badge from '..';

const meta = {
const meta: Meta< typeof Badge > = {
component: Badge,
title: 'Components/Containers/Badge',
id: 'components-badge',
tags: [ 'status-private' ],
} satisfies Meta< typeof Badge >;
};
Comment on lines +11 to +16
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normalizing this declaration to our standard format (not TS satisfies) for easier AST parsing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this 👍 The satisfies syntax seemed to cause some issues when parsing files for component usage as well.


export default meta;

Expand Down
Loading