Improve rendering of type definition comments

This commit is contained in:
Fabio Berger
2018-04-18 15:28:36 +09:00
parent 64c5c5eb40
commit ea6706a2af
2 changed files with 43 additions and 1 deletions

View File

@@ -9,6 +9,10 @@
{
"note": "Added support for rendering nested function types within interface types",
"pr": 519
},
{
"note": "Improve type comment rendering",
"pr": 535
}
]
},

View File

@@ -122,7 +122,9 @@ export class TypeDefinition extends React.Component<TypeDefinitionProps, TypeDef
</pre>
</div>
<div style={{ maxWidth: 620 }}>
{customType.comment && <Comment comment={customType.comment} className="py2" />}
{customType.comment && (
<Comment comment={this._formatComment(customType.comment)} className="py2" />
)}
</div>
</div>
);
@@ -132,4 +134,40 @@ export class TypeDefinition extends React.Component<TypeDefinitionProps, TypeDef
shouldShowAnchor,
});
}
/**
* Type definition comments usually describe the type as a whole or the individual
* properties within the type. Since TypeDoc just treats these comments simply as
* one paragraph, we do some additional formatting so that we can display individual
* property comments on their own lines.
* E.g:
* Interface SomeConfig
* {
* networkId: number,
* derivationPath: string,
* }
* networkId: The ethereum networkId to set as the chainId from EIP155
* derivationPath: Initial derivation path to use e.g 44'/60'/0'
*
* Each property description should be on a new line.
*/
private _formatComment(text: string) {
const NEW_LINE_REGEX = /(\r\n|\n|\r)/gm;
const sanitizedText = text.replace(NEW_LINE_REGEX, ' ');
const PROPERTY_DESCRIPTION_DIVIDER = ':';
if (!_.includes(sanitizedText, PROPERTY_DESCRIPTION_DIVIDER)) {
return sanitizedText;
}
const segments = sanitizedText.split(PROPERTY_DESCRIPTION_DIVIDER);
_.each(segments, (s: string, i: number) => {
if (i === 0 || i === segments.length - 1) {
return;
}
const words = s.split(' ');
const property = words[words.length - 1];
words[words.length - 1] = `\n\n${property}`;
segments[i] = words.join(' ');
});
const final = segments.join(PROPERTY_DESCRIPTION_DIVIDER);
return final;
}
}