feat: add @theme/Markdown to allow user modify their own markdown react component

This commit is contained in:
endiliey 2018-09-06 21:33:50 +08:00
parent 1ff6733464
commit d00864d222
8 changed files with 41 additions and 30 deletions

View file

@ -7,7 +7,7 @@ module.exports = function loadConfig(siteDir) {
? customThemePath ? customThemePath
: path.resolve(__dirname, '../theme'); : path.resolve(__dirname, '../theme');
const themeComponents = ['Docs', 'Loading', 'NotFound']; const themeComponents = ['Docs', 'Loading', 'NotFound', 'Markdown'];
themeComponents.forEach(component => { themeComponents.forEach(component => {
if (!require.resolve(path.join(themePath, component))) { if (!require.resolve(path.join(themePath, component))) {
throw new Error( throw new Error(

View file

@ -9,27 +9,10 @@ export default class Docs extends React.Component {
const {route, docsData, siteConfig} = this.props; const {route, docsData, siteConfig} = this.props;
const currentDoc = docsData.find(data => data.path === route.path); const currentDoc = docsData.find(data => data.path === route.path);
const highlight = Object.assign(
{},
{
version: '9.12.0',
theme: 'default'
},
siteConfig.highlight
);
// Use user-provided themeUrl if it exists, else construct one from version and theme.
const highlightThemeURL = highlight.themeUrl
? highlight.themeUrl
: `https://cdnjs.cloudflare.com/ajax/libs/highlight.js/${
highlight.version
}/styles/${highlight.theme}.min.css`;
return ( return (
<Layout {...this.props}> <Layout {...this.props}>
<Helmet> <Helmet>
<title>{(currentDoc && currentDoc.title) || siteConfig.title}</title> <title>{(currentDoc && currentDoc.title) || siteConfig.title}</title>
<link rel="stylesheet" type="text/css" href={highlightThemeURL} />
</Helmet> </Helmet>
<div className={styles.mainContainer}>{this.props.children}</div> <div className={styles.mainContainer}>{this.props.children}</div>
</Layout> </Layout>

View file

@ -2,6 +2,7 @@
import React from 'react'; import React from 'react';
import Markdown from 'remarkable'; import Markdown from 'remarkable';
import Helmet from 'react-helmet';
import highlight from './highlight'; import highlight from './highlight';
import anchors from './anchors'; import anchors from './anchors';
@ -58,16 +59,35 @@ class MarkdownBlock extends React.Component {
} }
render() { render() {
const Container = this.props.container; const {siteConfig} = this.props;
if (!Container) { const highlight = Object.assign(
return <div>{this.content()}</div>; {},
} {
return <Container>{this.content()}</Container>; version: '9.12.0',
theme: 'default'
},
siteConfig.highlight
);
// Use user-provided themeUrl if it exists, else construct one from version and theme.
const highlightThemeURL = highlight.themeUrl
? highlight.themeUrl
: `https://cdnjs.cloudflare.com/ajax/libs/highlight.js/${
highlight.version
}/styles/${highlight.theme}.min.css`;
return (
<div>
<Helmet>
<link rel="stylesheet" type="text/css" href={highlightThemeURL} />
</Helmet>
{this.content()}
</div>
);
} }
} }
MarkdownBlock.defaultProps = { MarkdownBlock.defaultProps = {
container: 'div',
siteConfig: {} siteConfig: {}
}; };

View file

@ -7,7 +7,10 @@ export default class NotFound extends React.Component {
<Layout> <Layout>
<div>404 Page Not Found</div> <div>404 Page Not Found</div>
<div> <div>
<img alt="Not found" src="https://i.imgur.com/yG7L3Lb.gif" /> <img
alt="Not found"
src="https://d2gg9evh47fn9z.cloudfront.net/800px_COLOURBOX3889253.jpg"
/>
</div> </div>
</Layout> </Layout>
); );

View file

@ -5,14 +5,19 @@ module.exports = function(fileString) {
const options = getOptions(this); const options = getOptions(this);
const {body} = fm(fileString); const {body} = fm(fileString);
const source = JSON.stringify(body);
const content = JSON.stringify(body);
// TODO replace all the markdown linking to correct url depends on whether it's versioned/translated/normal docs
// e.g: [test](test.md) become [test](/docs/test)
const siteConfig = JSON.stringify(options.siteConfig); const siteConfig = JSON.stringify(options.siteConfig);
return ( return (
`import React from 'react';\n` + `import React from 'react';\n` +
`import MarkdownBlock from '@core/components/Markdown'\n` + `import Markdown from '@theme/Markdown'\n` +
`export default () => <MarkdownBlock source={${source}} siteConfig={${siteConfig}} />;` `export default () => (
<Markdown siteConfig={${siteConfig}}>
{${content}}
</Markdown>
);`
); );
// return `export default () => <MarkdownBlock source={${source}} siteConfig={${siteConfig}} />`;
}; };