Skip to content

Commit

Permalink
Fix broken API docs (#161)
Browse files Browse the repository at this point in the history
* Fix broken API docs

* Remove console log
  • Loading branch information
derekju authored Nov 18, 2019
1 parent 352e950 commit e7c426b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
6 changes: 5 additions & 1 deletion gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ const {buildPagePath} = require('./src/utils');
exports.createPages = ({graphql, boundActionCreators}) => {
const {createPage} = boundActionCreators;
return new Promise((resolve, reject) => {
const apiTemplate = path.resolve('./src/templates/api.js');
const docTemplate = path.resolve('./src/templates/doc.js');
const templates = {
api: apiTemplate,
docs: docTemplate,
};

Expand Down Expand Up @@ -41,8 +43,10 @@ exports.createPages = ({graphql, boundActionCreators}) => {
const {localPath, remoteUrl} = buildPagePath(node);
createPage({
path: localPath,
component: templates[node.sourceInstanceName] || templates.docs,
component: templates[node.sourceInstanceName] || templates.api,
context: {
// Needed for api doc query
absolutePath: node.absolutePath,
// Needed for api doc query
remoteUrl,
// Used to query for HTML data on the actual page
Expand Down
57 changes: 57 additions & 0 deletions src/templates/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React from 'react';
import {styled} from 'styletron-react';
import {extractChildMarkdownRemark} from '../utils';

const ApiWrapper = styled('div', {
position: 'relative',
});

const ApiContainer = styled('div', {
paddingTop: '52px',
paddingBottom: '52px',
});

const EditLink = styled('a', {
paddingTop: '4px',
paddingBottom: '4px',
paddingLeft: '8px',
paddingRight: '8px',
position: 'absolute',
top: '16px',
right: 0,
});

class ApiTemplate extends React.Component {
render() {
const {data, pathContext} = this.props;
let html = extractChildMarkdownRemark(data).html || '';

return (
<ApiWrapper className="apiSearch-content">
{pathContext.remoteUrl ? (
<EditLink href={pathContext.remoteUrl}>Edit</EditLink>
) : null}
<ApiContainer
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{__html: html}}
/>
</ApiWrapper>
);
}
}

export const query = graphql`
query ApiQuery($absolutePath: String!) {
allFile(filter: {absolutePath: {eq: $absolutePath}}) {
edges {
node {
childMarkdownRemark {
html
}
}
}
}
}
`;

export default ApiTemplate;
13 changes: 0 additions & 13 deletions src/templates/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,13 @@ const DocsContainer = styled('div', {
paddingBottom: '52px',
});

const EditLink = styled('a', {
paddingTop: '4px',
paddingBottom: '4px',
paddingLeft: '8px',
paddingRight: '8px',
position: 'absolute',
top: '16px',
right: 0,
});

class DocTemplate extends React.Component {
render() {
const {data, pathContext} = this.props;
let html = extractChildMarkdownRemark(data).html || '';

return (
<DocsWrapper className="docSearch-content">
{pathContext.remoteUrl ? (
<EditLink href={pathContext.remoteUrl}>Edit</EditLink>
) : null}
<DocsContainer
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{__html: html}}
Expand Down

0 comments on commit e7c426b

Please sign in to comment.