Small expander meta formatting (#3713)

* Small expander meta formatting

* Updates
This commit is contained in:
Jaco Greeff 2020-09-18 12:17:12 +02:00 committed by GitHub
parent 32db999a90
commit 88c511d536
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,6 +29,16 @@ export interface Props {
withHidden?: boolean;
}
function splitSingle (value: string[], sep: string): string[] {
return value.reduce((result: string[], value: string): string[] => {
return value.split(sep).reduce((result: string[], value: string) => result.concat(value), result);
}, []);
}
function splitParts (value: string): string[] {
return ['[', ']'].reduce((result: string[], sep) => splitSingle(result, sep), [value]);
}
function formatMeta (meta?: Meta): React.ReactNode | null {
if (!meta || !meta.documentation.length) {
return null;
@ -36,12 +46,13 @@ function formatMeta (meta?: Meta): React.ReactNode | null {
const strings = meta.documentation.map((doc) => doc.toString().trim());
const firstEmpty = strings.findIndex((doc) => !doc.length);
return (
const parts = splitParts((
firstEmpty === -1
? strings
: strings.slice(0, firstEmpty)
).join(' ');
).join(' ').replace(/\\/g, '').replace(/`/g, ''));
return <>{parts.map((part, index) => index % 2 ? <em key={index}>[{part}]</em> : <span key={index}>{part}</span>)}&nbsp;</>;
}
function Expander ({ children, className = '', help, helpIcon, isOpen, isPadded, summary, summaryHead, summaryMeta, summarySub, withHidden }: Props): React.ReactElement<Props> {