Skip to content

Commit

Permalink
Handle line breaks in props
Browse files Browse the repository at this point in the history
  • Loading branch information
rianbotha committed Apr 24, 2019
1 parent 61a79a8 commit 27ed3fa
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ const content = fs.readFileSync(path.resolve(process.cwd(), filename), 'utf-8');
const component = reactDocs.parse(content);
console.log('| Property | PropType | Required | Default | Description |');
console.log('|----------|----------|----------|---------|-------------|');

for (const name of Object.keys(component.props)) {
const { type, required, description, defaultValue } = component.props[name];
console.log(`| ${name} | \`${formatType(type)}\` | ${required ? 'yes' : ''} | ${defaultValue != null ? `\`${defaultValue.value}\`` : ''} | ${description} |`);
console.log(`| ${name} | \`${formatType(type)}\` | ${required ? 'yes' : ''} | ${defaultValue != null ? `\`${nlToBr(defaultValue.value, true)}\`` : ''} | ${nlToBr(description)} |`);
}

function formatType (type) {
if (type.name === 'union') {
return type.value.map(formatType).join('|');
return type.value.map(formatType).join(' || ');
} else {
return type.name;
}
}

function nlToBr(str, addQuotes = false) {
return str.replace(/\r\n|\r|\n/gi, addQuotes ? '`<br>`' : '<br>');
}

0 comments on commit 27ed3fa

Please sign in to comment.