-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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: Prevent broken lists in auto-generated readmes #68301
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,17 +9,14 @@ 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`. | ||
* Converter for strings that are already formatted as Markdown. | ||
* | ||
* @param {string} [str] | ||
* @param {string} [input] | ||
* @return {string} The trimmed input if it is contentful, otherwise an empty string. | ||
*/ | ||
function normalizeTrailingNewline( str ) { | ||
if ( ! str?.trim() ) { | ||
return undefined; | ||
} | ||
return str.replace( /\n*$/, '\n' ); | ||
} | ||
json2md.converters.md = ( input ) => { | ||
return input?.trim() || ''; | ||
}; | ||
|
||
export function generateMarkdownDocs( { typeDocs, subcomponentTypeDocs } ) { | ||
const mainDocsJson = [ | ||
|
@@ -28,7 +25,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 ), | ||
]; | ||
|
||
|
@@ -39,7 +36,7 @@ export function generateMarkdownDocs( { typeDocs, subcomponentTypeDocs } ) { | |
{ | ||
h3: subcomponentTypeDoc.displayName, | ||
}, | ||
normalizeTrailingNewline( subcomponentTypeDoc.description ), | ||
{ md: subcomponentTypeDoc.description }, | ||
...generateMarkdownPropsJson( subcomponentTypeDoc.props, { | ||
headingLevel: 4, | ||
} ), | ||
|
@@ -49,5 +46,5 @@ export function generateMarkdownDocs( { typeDocs, subcomponentTypeDocs } ) { | |
|
||
return json2md( | ||
[ ...mainDocsJson, ...subcomponentDocsJson ].filter( Boolean ) | ||
); | ||
).replace( /\n+$/gm, '\n' ); // clean unnecessary consecutive newlines | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noting that this cleans up only trailing ones, maybe we want to update the comment to reflect that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realized you can add custom converters like this, which makes things more simpler and readable overall. When you use a converter,
json2md
adds a trailing newline automatically.The removal of redundant newlines is moved to the end of this file, which will clean the entire output.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL, this is very cool!