-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Add CliInvocationExample component
- Loading branch information
Showing
7 changed files
with
115 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import CodeBlock from '@theme/CodeBlock'; | ||
import React, {Suspense, useMemo} from 'react'; | ||
import {useLoadModule} from './CodeExample'; | ||
|
||
interface CliInvocationExampleProps { | ||
path: string; | ||
lineStart?: number; | ||
lineEnd?: number; | ||
startAfter?: string; // marker that indicates beginning of code snippet | ||
endBefore?: string; // marker that indicates ending of code snippet | ||
} | ||
|
||
const CliInvocationExample: React.FC<CliInvocationExampleProps> = ({...props}) => { | ||
return ( | ||
<Suspense> | ||
<CliInvocationExampleInner {...props} /> | ||
</Suspense> | ||
); | ||
}; | ||
|
||
const CliInvocationExampleInner: React.FC<CliInvocationExampleProps> = (props) => { | ||
const {path, lineStart, lineEnd, startAfter, endBefore, ...extraProps} = props; | ||
const language = 'shell'; | ||
|
||
const cacheKey = JSON.stringify(props); | ||
const {content, error} = useLoadModule(cacheKey, path, lineStart, lineEnd, startAfter, endBefore); | ||
|
||
const [command, result] = useMemo(() => { | ||
const [command, ...rest] = content.split('\n\n'); | ||
return [command, rest.join('\n\n')]; | ||
}, [content]); | ||
|
||
if (error) { | ||
return <div style={{color: 'red', padding: '1rem', border: '1px solid red'}}>{error}</div>; | ||
} | ||
|
||
return ( | ||
<> | ||
<div className="cli-invocation-example-command"> | ||
<CodeBlock language={language} {...extraProps}> | ||
{command || 'Loading...'} | ||
</CodeBlock> | ||
</div> | ||
{command && result && ( | ||
<div className="cli-invocation-example-result"> | ||
<CodeBlock language={language} {...extraProps}> | ||
{result || 'Loading...'} | ||
</CodeBlock> | ||
</div> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default CliInvocationExample; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters