Skip to content

Commit 6d00394

Browse files
authored
Allow examples to have associated markdown files (#335)
1 parent edb58dd commit 6d00394

File tree

8 files changed

+77
-56
lines changed

8 files changed

+77
-56
lines changed

docs/docs-app/actions/index.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
import {text} from 'd3-request';
2-
import {docPages} from '../constants/pages';
2+
import {docPages, examplePages} from '../constants/pages';
33
export function loadContent() {
44
return (dispatch, getState) => {
5-
docPages.forEach(documentationSection => {
6-
documentationSection.children.forEach(docPage => {
7-
const filename = docPage.content.markdown;
8-
const contents = getState();
9-
if (contents.markdownPages.get(filename)) {
10-
// already loaded
11-
return;
12-
}
13-
dispatch(loadContentStart(filename));
14-
text(filename, (error, response) => {
15-
dispatch(loadContentSuccess(filename, error ? error.target.response : response));
16-
});
5+
docPages.forEach(loadDoc(dispatch, getState));
6+
examplePages.forEach(loadDoc(dispatch, getState));
7+
};
8+
}
9+
10+
function loadDoc(dispatch, getState) {
11+
return documentationSection => {
12+
documentationSection.children.forEach(docPage => {
13+
const filename = docPage.content.markdown;
14+
if (!filename) {
15+
return;
16+
}
17+
const contents = getState();
18+
if (contents.markdownPages.get(filename)) {
19+
// already loaded
20+
return;
21+
}
22+
dispatch(loadContentStart(filename));
23+
text(filename, (error, response) => {
24+
dispatch(loadContentSuccess(filename, error ? error.target.response : response));
1725
});
1826
});
1927
};

docs/docs-app/components/documentation-page.js

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,5 @@
11
import React, {Component} from 'react';
2-
import marked from 'marked';
3-
4-
import {docsRouting} from '../constants/pages';
5-
import {showCase} from '../../../showcase/index';
6-
7-
const INJECTION_REG = /<!-- INJECT:"(.+)\" -->/g;
8-
9-
function injectExamplesIntoHtml(content) {
10-
return content.split(INJECTION_REG).map((__html, index) => {
11-
const Example = showCase[__html];
12-
if (!Example) {
13-
/* eslint-disable react/no-danger */
14-
return (<div
15-
className="markdown-body"
16-
key={`body-${index}`}
17-
dangerouslySetInnerHTML={{__html}} />);
18-
/* eslint-enable react/no-danger */
19-
}
20-
return (
21-
<div className="markdown-example" key={`example-${index}`}>
22-
<Example />
23-
</div>
24-
);
25-
});
26-
}
2+
import {injectExamplesIntoHtml, convertMarkdownToReact} from './utils';
273

284
class DocumentationPage extends Component {
295
componentWillReceiveProps(nextProps) {
@@ -34,23 +10,12 @@ class DocumentationPage extends Component {
3410

3511
render() {
3612
const {markdownPages, route} = this.props;
37-
38-
const renderer = new marked.Renderer();
39-
renderer.link = (href, title, text) => {
40-
41-
if (docsRouting[href]) {
42-
return `<a href=${docsRouting[href]} title=${title}>${text}</a>`;
43-
}
44-
return `<a href=${href} title=${title}>${text}</a>`;
45-
};
46-
4713
const content = markdownPages.get(route.content.markdown, '');
48-
const __html = marked(content, {renderer});
4914

5015
return (
5116
<div className="documentation-page f fg">
5217
<div className="markdown" ref="documentionContainer">
53-
{injectExamplesIntoHtml(__html)}
18+
{injectExamplesIntoHtml(convertMarkdownToReact(content))}
5419
</div>
5520
</div>
5621
);

docs/docs-app/components/example-page.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, {Component} from 'react';
2-
2+
import {convertMarkdownToReact, injectExamplesIntoHtml} from './utils';
33
class ExamplePage extends Component {
44

55
componentDidMount() {
@@ -10,12 +10,17 @@ class ExamplePage extends Component {
1010
}
1111

1212
render() {
13-
const {route} = this.props;
13+
const {markdownPages, route} = this.props;
1414
const ExampleComponent = route.content.component;
1515

16+
const content = markdownPages.get(route.content.markdown, '');
17+
1618
return (
1719
<div className="example-page">
1820
<ExampleComponent forExample />
21+
{content && <div className="markdown" ref="documentionContainer">
22+
{injectExamplesIntoHtml(convertMarkdownToReact(content))}
23+
</div>}
1924
</div>
2025
);
2126
}

docs/docs-app/components/header.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ const renderTab = (tab, index) => (
2020
);
2121

2222
const renderGithubLink = () => (
23-
<a href="https://github.com/uber/react-vis" className="link"><i className="icon icon-github" /></a>
23+
<a href="https://github.com/uber/react-vis" className="link" key="github-link">
24+
<i className="icon icon-github" />
25+
</a>
2426
);
2527

2628
const mappedTabs = tabs.map(renderTab).concat(renderGithubLink());

docs/docs-app/components/home.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ const sections = [{
2525

2626
function renderSection(section, index) {
2727
const content = [(
28-
<div className={`fcol fg bullet-info ${(index % 2) ? '' : 'bullet-info-reversed'}`}>
28+
<div
29+
className={`fcol fg bullet-info ${(index % 2) ? '' : 'bullet-info-reversed'}`}
30+
key={`section-${index}`}>
2931
<div className="bullet-point-title">
3032
{section.title}
3133
</div>
@@ -34,12 +36,12 @@ function renderSection(section, index) {
3436
</div>
3537
</div>
3638
), (
37-
<div className="bullet-example">
39+
<div className="bullet-example" key={`bullet-example-${index}`}>
3840
{section.component}
3941
</div>
4042
)];
4143
return (
42-
<div className="bullet-point f">
44+
<div className="bullet-point f" key={`bullet-${index}`}>
4345
{!(index % 2) ? content.reverse() : content}
4446
</div>
4547
);

docs/docs-app/components/utils.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React from 'react';
2+
import marked from 'marked';
3+
4+
import {docsRouting} from '../constants/pages';
5+
import {showCase} from '../../../showcase/index';
6+
7+
const INJECTION_REG = /<!-- INJECT:"(.+)\" -->/g;
8+
9+
export function injectExamplesIntoHtml(content) {
10+
return content.split(INJECTION_REG).map((__html, index) => {
11+
const Example = showCase[__html];
12+
if (!Example) {
13+
/* eslint-disable react/no-danger */
14+
return (<div
15+
className="markdown-body"
16+
key={`body-${index}`}
17+
dangerouslySetInnerHTML={{__html}} />);
18+
/* eslint-enable react/no-danger */
19+
}
20+
return (
21+
<div className="markdown-example" key={`example-${index}`}>
22+
<Example />
23+
</div>
24+
);
25+
});
26+
}
27+
28+
export function convertMarkdownToReact(content) {
29+
const renderer = new marked.Renderer();
30+
renderer.link = (href, title, text) =>
31+
`<a href=${docsRouting[href] || href} title=${title}>${text}</a>`;
32+
33+
return marked(content, {renderer});
34+
}

docs/docs-app/constants/pages.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const examplePages = generatePath([
3434
}, {
3535
name: 'Candlestick',
3636
content: {
37+
markdown: getDocUrl('examples/extensibility.md'),
3738
pageType: 'example',
3839
component: Candlestick
3940
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Extensibility
2+
3+
react-vis is easily extensible! If we don't have what you want it's easy to make! For instance, the above chart
4+
was made by simply extending abstract series and adding a little sugar. You can check out the code for your self [here](https://github.com/uber/react-vis/blob/master/examples/candlestick/candlestick.js)

0 commit comments

Comments
 (0)