-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby-theme-docz): add custom theme support
- Loading branch information
1 parent
bf4e440
commit fdfddcb
Showing
9 changed files
with
213 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
exports.createPagesStatefully = require('./src/node/createPagesStatefully') | ||
exports.onCreateBabelConfig = require('./src/node/onCreateBabelConfig') | ||
exports.onPreInit = require('./src/node/onPreInit') | ||
exports.sourceNodes = require('./src/node/sourceNodes') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const fs = require('fs-extra') | ||
const path = require('path') | ||
const { touch, compiled } = require('docz-utils') | ||
const { parseConfig } = require('../utils/parseConfig') | ||
|
||
const fromTemplates = filepath => | ||
path.resolve(__dirname, '../../templates', filepath) | ||
|
||
const mountComponentPath = paths => component => | ||
path.join(paths.app, 'components', component || '') | ||
|
||
const touchTemplateWithPaths = paths => async (filepath, opts) => { | ||
const componentPath = mountComponentPath(paths) | ||
const dest = componentPath(filepath.replace('.tpl', '')) | ||
const template = await compiled(fromTemplates(filepath), { minimize: false }) | ||
const raw = template(opts) | ||
await touch(dest, raw) | ||
} | ||
|
||
module.exports = async opts => { | ||
const { paths, ...config } = await parseConfig(opts) | ||
const componentPath = mountComponentPath(paths) | ||
const touchTemplate = touchTemplateWithPaths(paths) | ||
|
||
await fs.ensureDir(componentPath()) | ||
await fs.copy(fromTemplates('Seo.tpl.js'), componentPath('Seo.js')) | ||
await fs.copy(fromTemplates('Link.tpl.js'), componentPath('Link.js')) | ||
await touchTemplate('Layout.tpl.js', { theme: config.theme }) | ||
|
||
return Promise.resolve() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React, { Fragment } from 'react' | ||
import PropTypes from 'prop-types' | ||
import { StaticQuery, graphql } from 'gatsby' | ||
import { AsyncRoute, useComponents } from 'docz' | ||
import { MDXProvider } from '@mdx-js/tag' | ||
import Theme from '<%- theme %>' | ||
|
||
import { Link } from './Link' | ||
import SEO from './Seo' | ||
|
||
const query = graphql` | ||
query Layout { | ||
doczDb { | ||
id | ||
db | ||
} | ||
} | ||
` | ||
|
||
const Route = ({ children, ...props }) => { | ||
const components = useComponents() | ||
const NotFound = components.NotFound | ||
if (!props.entry) return <NotFound /> | ||
|
||
return ( | ||
<MDXProvider components={components}> | ||
<AsyncRoute | ||
{...props} | ||
asyncComponent={() => <Fragment>{children}</Fragment>} | ||
/> | ||
</MDXProvider> | ||
) | ||
} | ||
|
||
const parseDatabase = data => { | ||
try { | ||
return JSON.parse(data.doczDb.db) | ||
} catch (err) { | ||
return {} | ||
} | ||
} | ||
|
||
const Layout = ({ children, ...defaultProps }) => { | ||
const { pageContext: ctx } = defaultProps | ||
return ( | ||
<StaticQuery | ||
query={query} | ||
render={data => { | ||
const db = parseDatabase(data) | ||
const entry = db.entries.find(entry => entry.filepath === ctx.filepath) | ||
|
||
return ( | ||
<Fragment> | ||
{entry && <SEO title={entry.value.name} />} | ||
<Theme db={db} linkComponent={Link}> | ||
<Route {...defaultProps} entry={entry}> | ||
{children} | ||
</Route> | ||
</Theme> | ||
</Fragment> | ||
) | ||
}} | ||
/> | ||
) | ||
} | ||
|
||
Layout.propTypes = { | ||
color: PropTypes.string, | ||
children: PropTypes.node.isRequired, | ||
} | ||
|
||
export default Layout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from 'react' | ||
import { Link as BaseLink } from 'gatsby' | ||
|
||
export const Link = props => ( | ||
<BaseLink | ||
{...props} | ||
getProps={({ isCurrent, ...customProps }) => | ||
isCurrent ? { className: `${props.className} active` } : null | ||
} | ||
/> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import Helmet from 'react-helmet' | ||
import { StaticQuery, graphql } from 'gatsby' | ||
|
||
function SEO({ description, lang, meta, keywords, title }) { | ||
return ( | ||
<StaticQuery | ||
query={detailsQuery} | ||
render={data => { | ||
const db = JSON.parse(data.doczDb.db) | ||
const metaDescription = description || db.config.title | ||
|
||
return ( | ||
<Helmet | ||
htmlAttributes={{ lang }} | ||
title={title} | ||
titleTemplate={`%s | ${db.config.title}`} | ||
meta={[ | ||
{ | ||
name: `description`, | ||
content: metaDescription, | ||
}, | ||
{ | ||
property: `og:title`, | ||
content: title, | ||
}, | ||
{ | ||
property: `og:description`, | ||
content: metaDescription, | ||
}, | ||
{ | ||
property: `og:type`, | ||
content: `website`, | ||
}, | ||
{ | ||
name: `twitter:card`, | ||
content: `summary`, | ||
}, | ||
{ | ||
name: `twitter:title`, | ||
content: title, | ||
}, | ||
{ | ||
name: `twitter:description`, | ||
content: metaDescription, | ||
}, | ||
] | ||
.concat( | ||
keywords.length > 0 | ||
? { | ||
name: `keywords`, | ||
content: keywords.join(`, `), | ||
} | ||
: [] | ||
) | ||
.concat(meta)} | ||
/> | ||
) | ||
}} | ||
/> | ||
) | ||
} | ||
|
||
SEO.defaultProps = { | ||
lang: `en`, | ||
meta: [], | ||
keywords: [], | ||
} | ||
|
||
SEO.propTypes = { | ||
description: PropTypes.string, | ||
lang: PropTypes.string, | ||
meta: PropTypes.array, | ||
keywords: PropTypes.arrayOf(PropTypes.string), | ||
title: PropTypes.string.isRequired, | ||
} | ||
|
||
export default SEO | ||
|
||
const detailsQuery = graphql` | ||
query DefaultSEOQuery { | ||
doczDb { | ||
id | ||
db | ||
} | ||
} | ||
` |