Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

Commit 310f039

Browse files
committed
Make progress on rendering custom samples over predefined samples
1 parent 5f19a79 commit 310f039

File tree

3 files changed

+121
-42
lines changed

3 files changed

+121
-42
lines changed

packages/api-explorer/lib/create-docs.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,21 @@ module.exports = (oas, apiSetting) => {
5050
slug: tag,
5151
type: 'endpoint',
5252
category: { apiSetting },
53-
api: { method },
53+
api: {
54+
method,
55+
examples: {
56+
codes: [
57+
{
58+
language: 'javascript',
59+
code: 'console.log(1);',
60+
},
61+
{
62+
language: 'curl',
63+
code: 'curl http://example.com',
64+
},
65+
],
66+
},
67+
},
5468
swagger: { path },
5569
// Uncomment this if you want some body content blocks
5670
// body,

packages/api-explorer/src/CodeSample.jsx

Lines changed: 102 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,61 +5,122 @@ const Oas = require('./lib/Oas');
55

66
const { Operation } = Oas;
77

8+
const syntaxHighlighter = require('@readme/syntax-highlighter');
9+
810
const generateCodeSnippet = require('./lib/generate-code-snippet');
911

10-
function CodeSample({ oas, setLanguage, operation, formData, language }) {
11-
return (
12-
<div className="code-sample tabber-parent">
13-
{(() => {
14-
if (!oas[extensions.SAMPLES_ENABLED]) {
15-
return <div className="hub-no-code">No code samples available</div>;
16-
}
12+
class CodeSample extends React.Component {
13+
constructor(props) {
14+
super(props);
15+
this.state = {
16+
customCodeSampleTab: 0,
17+
};
18+
this.setCustomCodeSampleTab = this.setCustomCodeSampleTab.bind(this);
19+
}
20+
setCustomCodeSampleTab(index) {
21+
this.setState({ customCodeSampleTab: index });
22+
}
23+
24+
render() {
25+
const { oas, setLanguage, operation, formData, language, customCodeSamples } = this.props;
1726

18-
const snippet = generateCodeSnippet(oas, operation, formData, language);
27+
return (
28+
<div className="code-sample tabber-parent">
29+
{(() => {
30+
if (!oas[extensions.SAMPLES_ENABLED]) {
31+
return <div className="hub-no-code">No code samples available</div>;
32+
}
1933

20-
return (
21-
<div>
22-
<ul className="code-sample-tabs">
23-
{// TODO add `is-lang-${lang}` class, to body?
24-
oas[extensions.SAMPLES_LANGUAGES].map(lang => (
25-
<li key={lang}>
26-
{
27-
// eslint-disable-next-line jsx-a11y/href-no-hash
28-
<a
29-
href="#"
30-
className={`hub-lang-switch-${lang}`}
31-
onClick={e => {
32-
e.preventDefault();
33-
setLanguage(lang);
34-
}}
35-
>
36-
{generateCodeSnippet.getLangName(lang)}
37-
</a>
38-
}
39-
</li>
40-
))}
41-
</ul>
34+
const snippet = generateCodeSnippet(oas, operation, formData, language);
4235

43-
<div className="hub-code-auto">
44-
<pre
45-
className={`tomorrow-night hub-lang hub-lang-${language}`}
46-
// eslint-disable-next-line react/no-danger
47-
dangerouslySetInnerHTML={{ __html: snippet }}
48-
/>
36+
if (customCodeSamples.length) {
37+
return (
38+
<div>
39+
<ul className="code-sample-tabs">
40+
{customCodeSamples.map(example => (
41+
<li key={example.language}>
42+
{
43+
// eslint-disable-next-line jsx-a11y/href-no-hash
44+
<a
45+
href="#"
46+
className={`hub-lang-switch-${example.language}`}
47+
onClick={e => {
48+
e.preventDefault();
49+
setLanguage(example.language);
50+
}}
51+
>
52+
{generateCodeSnippet.getLangName(example.language)}
53+
</a>
54+
}
55+
</li>
56+
))}
57+
</ul>
58+
<div className="code-sample-body">
59+
{customCodeSamples.map((example, index) => {
60+
return (
61+
<pre
62+
className="tomorrow-night tabber-body"
63+
style={{ display: index === this.state.customCodeSampleTab ? 'block' : '' }}
64+
key={index} // eslint-disable-line react/no-array-index-key
65+
// eslint-disable-next-line react/no-danger
66+
dangerouslySetInnerHTML={{
67+
__html: syntaxHighlighter(example.code, example.language, true),
68+
}}
69+
/>
70+
);
71+
})}
72+
</div>
73+
</div>
74+
);
75+
}
76+
return (
77+
<div>
78+
<ul className="code-sample-tabs">
79+
{// TODO add `is-lang-${lang}` class, to body?
80+
oas[extensions.SAMPLES_LANGUAGES].map(lang => (
81+
<li key={lang}>
82+
{
83+
// eslint-disable-next-line jsx-a11y/href-no-hash
84+
<a
85+
href="#"
86+
className={`hub-lang-switch-${lang}`}
87+
onClick={e => {
88+
e.preventDefault();
89+
setLanguage(lang);
90+
}}
91+
>
92+
{generateCodeSnippet.getLangName(lang)}
93+
</a>
94+
}
95+
</li>
96+
))}
97+
</ul>
98+
99+
<div className="hub-code-auto">
100+
<pre
101+
className={`tomorrow-night hub-lang hub-lang-${language}`}
102+
// eslint-disable-next-line react/no-danger
103+
dangerouslySetInnerHTML={{ __html: snippet }}
104+
/>
105+
</div>
49106
</div>
50-
</div>
51-
);
52-
})()}
53-
</div>
54-
);
107+
);
108+
})()}
109+
</div>
110+
);
111+
}
55112
}
56113

57114
CodeSample.propTypes = {
58115
oas: PropTypes.instanceOf(Oas).isRequired,
59116
setLanguage: PropTypes.func.isRequired,
60117
operation: PropTypes.instanceOf(Operation).isRequired,
61118
formData: PropTypes.shape({}).isRequired,
119+
customCodeSamples: PropTypes.shape([]),
62120
language: PropTypes.string.isRequired,
63121
};
64122

123+
CodeSample.defaultProps = {
124+
customCodeSamples: [],
125+
};
65126
module.exports = CodeSample;

packages/api-explorer/src/Doc.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ class Doc extends React.Component {
180180
operation={this.getOperation()}
181181
formData={this.state.formData}
182182
language={this.props.language}
183+
customCodeSamples={this.props.doc.api.examples.codes}
183184
/>
184185
);
185186
}
@@ -306,6 +307,9 @@ Doc.propTypes = {
306307
type: PropTypes.string.isRequired,
307308
api: PropTypes.shape({
308309
method: PropTypes.string.isRequired,
310+
examples: PropTypes.shape({
311+
codes: PropTypes.arrayOf(PropTypes.shape({})),
312+
}),
309313
}),
310314
swagger: PropTypes.shape({
311315
path: PropTypes.string.isRequired,

0 commit comments

Comments
 (0)