Skip to content

Commit

Permalink
Devdocs: Use Webpack Loader to load READMEs
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Dec 15, 2019
1 parent 26f8a10 commit 80a642a
Show file tree
Hide file tree
Showing 12 changed files with 806 additions and 846 deletions.
13 changes: 7 additions & 6 deletions client/components/readme-viewer/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ export default class ReadmeViewer extends Component {
const { readmeFilePath } = this.props;

try {
const res = await fetch( `/devdocs/service/content?path=${ readmeFilePath }` );
if ( res.ok ) {
const text = await res.text();
this.setState( { readme: htmlToReactParser.parse( text ) } );
}
const res = await import(
/* webpackChunkName: "async-load-readme-[request]" */
/* webpackInclude: /README\.md$/ */
`../../${ readmeFilePath }`
);
this.setState( { readme: htmlToReactParser.parse( res.default ) } );
} catch ( err ) {
// Do nothing.
}
Expand All @@ -55,7 +56,7 @@ export default class ReadmeViewer extends Component {
const editLink = (
<a
className="readme-viewer__doc-edit-link devdocs__doc-edit-link"
href={ `https://github.com/Automattic/wp-calypso/edit/master${ readmeFilePath }` }
href={ `https://github.com/Automattic/wp-calypso/edit/master/client${ readmeFilePath }` }
>
Improve this document on GitHub
</a>
Expand Down
2 changes: 1 addition & 1 deletion client/devdocs/design/blocks.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export default class AppComponents extends React.Component {
</React.Fragment>
) : (
<div>
<ReadmeViewer readmeFilePath="/client/devdocs/blocks/README.md" />
<ReadmeViewer readmeFilePath="devdocs/blocks/README.md" />
<SearchCard
onSearch={ this.onSearch }
initialValue={ this.state.filter }
Expand Down
2 changes: 1 addition & 1 deletion client/devdocs/design/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class DesignAssets extends React.Component {
</React.Fragment>
) : (
<div>
<ReadmeViewer readmeFilePath="/client/devdocs/design/README.md" />
<ReadmeViewer readmeFilePath="devdocs/design/README.md" />
<SearchCard
onSearch={ this.onSearch }
initialValue={ filter }
Expand Down
19 changes: 5 additions & 14 deletions client/devdocs/design/search-collection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,12 @@ const shouldShowInstance = ( example, filter, component ) => {
};

const getReadmeFilePath = ( section, example ) => {
let path = example.props.readmeFilePath;

if ( ! path ) {
return null;
}

if ( ! path.startsWith( '/' ) ) {
path = `/client/${ section === 'design' ? 'components' : section }/${ path }`;
switch ( section ) {
case 'design':
return `components/${ example.props.readmeFilePath }/README.md`;
default:
return `${ section }/${ example.props.readmeFilePath }/README.md`;
}

if ( ! path.endsWith( 'README.md' ) ) {
path = `${ path }/README.md`;
}

return path;
};

const Collection = ( {
Expand Down
17 changes: 7 additions & 10 deletions client/devdocs/doc.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import React from 'react';
/**
* Internal dependencies
*/
import DocService from './service';
import Error from './error';
import DocumentHead from 'components/data/document-head';
import highlight from 'lib/highlight';
Expand Down Expand Up @@ -52,15 +51,13 @@ export default class extends React.Component {
error: null,
} );
this.delayLoadingMessage();
DocService.fetch(
this.props.path,
function( error, body ) {
this.setState( {
body,
error,
} );
}.bind( this )
);
import(
/* webpackChunkName: "async-load-doc-[request]" */
/* webpackInclude: /\.md$/ */
`../../${ this.props.path }`
)
.then( module => this.setState( { body: module.default, error: null } ) )
.catch( error => this.setState( { body: null, error } ) );
};

setBodyScrollPosition = () => {
Expand Down
2 changes: 1 addition & 1 deletion client/devdocs/docs-selectors/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class DocsSelectors extends PureComponent {
<DocsSelectorsSingle { ...{ selector, search } } />
) : (
<div>
<ReadmeViewer readmeFilePath="/client/devdocs/docs-selectors/README.md" />
<ReadmeViewer readmeFilePath="devdocs/docs-selectors/README.md" />
<DocsSelectorsSearch search={ search } />
</div>
) }
Expand Down
4 changes: 0 additions & 4 deletions client/devdocs/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,4 @@ export default {
list: function( filenames, callback ) {
fetchDocsEndpoint( 'list', { files: filenames.join( ',' ) }, callback );
},

fetch: function( path, callback ) {
fetchDocsEndpoint( 'content', { path: path }, callback );
},
};
2 changes: 1 addition & 1 deletion client/devdocs/wizard-component/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class WizardComponent extends Component {
Wizard
</HeaderCake>
<Wizard stepName={ stepName } />
<ReadmeViewer section="design" readmeFilePath="/client/components/wizard/README.md" />
<ReadmeViewer section="design" readmeFilePath="components/wizard/README.md" />
</Main>
);
}
Expand Down
Loading

0 comments on commit 80a642a

Please sign in to comment.