Skip to content
This repository has been archived by the owner on Jan 24, 2025. It is now read-only.

feat(gatsby-theme-docz): include MDX transclusion support #1436

Merged
merged 1 commit into from
Mar 31, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions core/gatsby-theme-docz/src/base/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,48 @@ import Wrapper from '../wrapper'
import Theme from '../index'
import SEO from './Seo'

const Route = ({ children, entry, ...defaultProps }) => {
const Route = ({ children, entry, isTransclusion, ...defaultProps }) => {
const components = useComponents()
const NotFound = components.notFound
const Layout = components.layout
const props = { ...defaultProps, doc: entry }
if (!entry) return <NotFound />
return (
<Wrapper doc={entry}>
if (!entry && !isTransclusion) return <NotFound />
return isTransclusion ? (
children
) : (
<Wrapper>
<Layout {...props}>{children}</Layout>
</Wrapper>
)
}

const findEntry = (db, ctx) => {
const isIndex = ctx.frontmatter && ctx.frontmatter.route === '/'
const isIndex = ctx && ctx.frontmatter && ctx.frontmatter.route === '/'
const eqIndex = propEq('value.route', '/')
if (!ctx.entry && isIndex) return db.entries.find(eqIndex)
if (ctx && !ctx.entry && isIndex) return db.entries.find(eqIndex)
const filepath = get('entry.filepath', ctx)
return db.entries.find(propEq('value.filepath', filepath))
}

const includesTransclusion = (db, props) => {
const { entries } = db
const filepath = get('_frontmatter.__filemeta.filename', props)
return (
!props.pageContext &&
entries.includes(entries.find(propEq('value.filepath', filepath)))
)
}

const Layout = ({ children, ...defaultProps }) => {
const { pageContext: ctx } = defaultProps
const db = useDbQuery()
const entry = findEntry(db, ctx)
const isTransclusion = includesTransclusion(db, defaultProps)
return (
<Fragment>
{entry && <SEO title={entry.value.name} />}
<Theme db={db} currentEntry={entry}>
<Route {...defaultProps} entry={entry}>
<Route {...defaultProps} entry={entry} isTransclusion={isTransclusion}>
{children}
</Route>
</Theme>
Expand Down