Improve rendering of type definition comments
This commit is contained in:
@@ -9,6 +9,10 @@
|
|||||||
{
|
{
|
||||||
"note": "Added support for rendering nested function types within interface types",
|
"note": "Added support for rendering nested function types within interface types",
|
||||||
"pr": 519
|
"pr": 519
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"note": "Improve type comment rendering",
|
||||||
|
"pr": 535
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -122,7 +122,9 @@ export class TypeDefinition extends React.Component<TypeDefinitionProps, TypeDef
|
|||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
<div style={{ maxWidth: 620 }}>
|
<div style={{ maxWidth: 620 }}>
|
||||||
{customType.comment && <Comment comment={customType.comment} className="py2" />}
|
{customType.comment && (
|
||||||
|
<Comment comment={this._formatComment(customType.comment)} className="py2" />
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
@@ -132,4 +134,40 @@ export class TypeDefinition extends React.Component<TypeDefinitionProps, TypeDef
|
|||||||
shouldShowAnchor,
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user