Skip to content

Commit

Permalink
Components: Prevent broken lists in auto-generated readmes
Browse files Browse the repository at this point in the history
  • Loading branch information
mirka committed Dec 25, 2024
1 parent 887243c commit cce06a5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
21 changes: 6 additions & 15 deletions bin/api-docs/gen-components-docs/markdown/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,9 @@ import json2md from 'json2md';
*/
import { generateMarkdownPropsJson } from './props.mjs';

/**
* If the string is contentful, ensure that it ends with a single newline.
* Otherwise normalize to `undefined`.
*
* @param {string} [str]
*/
function normalizeTrailingNewline( str ) {
if ( ! str?.trim() ) {
return undefined;
}
return str.replace( /\n*$/, '\n' );
}
json2md.converters.md = ( input ) => {
return input?.trim() ? input : '';
};

export function generateMarkdownDocs( { typeDocs, subcomponentTypeDocs } ) {
const mainDocsJson = [
Expand All @@ -28,7 +19,7 @@ export function generateMarkdownDocs( { typeDocs, subcomponentTypeDocs } ) {
{
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>`,
},
normalizeTrailingNewline( typeDocs.description ),
{ md: typeDocs.description },
...generateMarkdownPropsJson( typeDocs.props ),
];

Expand All @@ -39,7 +30,7 @@ export function generateMarkdownDocs( { typeDocs, subcomponentTypeDocs } ) {
{
h3: subcomponentTypeDoc.displayName,
},
normalizeTrailingNewline( subcomponentTypeDoc.description ),
{ md: subcomponentTypeDoc.description },
...generateMarkdownPropsJson( subcomponentTypeDoc.props, {
headingLevel: 4,
} ),
Expand All @@ -49,5 +40,5 @@ export function generateMarkdownDocs( { typeDocs, subcomponentTypeDocs } ) {

return json2md(
[ ...mainDocsJson, ...subcomponentDocsJson ].filter( Boolean )
);
).replace( /\n+$/gm, '\n' ); // clean unnecessary consecutive newlines
}
2 changes: 1 addition & 1 deletion bin/api-docs/gen-components-docs/markdown/props.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export function generateMarkdownPropsJson( props, { headingLevel = 2 } = {} ) {

return [
{ [ `h${ headingLevel + 1 }` ]: `\`${ key }\`` },
prop.description,
{
ul: [
`Type: \`${ renderPropType( prop.type ) }\``,
Expand All @@ -42,6 +41,7 @@ export function generateMarkdownPropsJson( props, { headingLevel = 2 } = {} ) {
`Default: \`${ prop.defaultValue.value }\``,
].filter( Boolean ),
},
{ md: prop.description },
];
} )
.filter( Boolean );
Expand Down

0 comments on commit cce06a5

Please sign in to comment.