diff options
author | Fabio Berger <me@fabioberger.com> | 2018-04-18 17:19:27 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-18 17:19:27 +0800 |
commit | f6f2991a442da156a3dbdeca84683a6c9f46d0e1 (patch) | |
tree | 4951024bfba56d5c3268556c4d4e2ca0ab4f6d17 /packages/react-docs | |
parent | 823f2db09fba8f19806ba3fa4bd679e12a58be3f (diff) | |
parent | 4ea222bbffd7a378712320a2f366d5bbee5920af (diff) | |
download | dexon-sol-tools-f6f2991a442da156a3dbdeca84683a6c9f46d0e1.tar dexon-sol-tools-f6f2991a442da156a3dbdeca84683a6c9f46d0e1.tar.gz dexon-sol-tools-f6f2991a442da156a3dbdeca84683a6c9f46d0e1.tar.bz2 dexon-sol-tools-f6f2991a442da156a3dbdeca84683a6c9f46d0e1.tar.lz dexon-sol-tools-f6f2991a442da156a3dbdeca84683a6c9f46d0e1.tar.xz dexon-sol-tools-f6f2991a442da156a3dbdeca84683a6c9f46d0e1.tar.zst dexon-sol-tools-f6f2991a442da156a3dbdeca84683a6c9f46d0e1.zip |
Merge pull request #535 from 0xProject/fix/commentRendering
Docs: Fix Type Comment Rendering
Diffstat (limited to 'packages/react-docs')
-rw-r--r-- | packages/react-docs/CHANGELOG.json | 4 | ||||
-rw-r--r-- | packages/react-docs/src/components/type_definition.tsx | 43 |
2 files changed, 46 insertions, 1 deletions
diff --git a/packages/react-docs/CHANGELOG.json b/packages/react-docs/CHANGELOG.json index 951ed84e0..0b0d6bdc8 100644 --- a/packages/react-docs/CHANGELOG.json +++ b/packages/react-docs/CHANGELOG.json @@ -9,6 +9,10 @@ { "note": "Added support for rendering nested function types within interface types", "pr": 519 + }, + { + "note": "Improve type comment rendering", + "pr": 535 } ] }, diff --git a/packages/react-docs/src/components/type_definition.tsx b/packages/react-docs/src/components/type_definition.tsx index 7a1c86da5..605b58fbd 100644 --- a/packages/react-docs/src/components/type_definition.tsx +++ b/packages/react-docs/src/components/type_definition.tsx @@ -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,43 @@ 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) { + segments[i] = `**${s}**`; + return; + } else if (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; + } } |