Set page layout dynamically (#4084)

This commit is contained in:
Alexey Pyltsyn
2021-11-11 03:27:46 +03:00
committed by GitHub
parent 0b7fd7b7a4
commit e206d8cdf1
183 changed files with 11 additions and 183 deletions

View File

@@ -1,4 +1,5 @@
const fm = require('gray-matter');
const path = require('path');
// Makes mdx in next.js suck less by injecting necessary exports so that
// the docs are still readable on github.
@@ -15,7 +16,16 @@ const fm = require('gray-matter');
module.exports = async function (src) {
const callback = this.async();
const {content, data} = fm(src);
const layout = data.layout || 'Home';
const pageParentDir = path
.dirname(path.relative('./src/pages', this.resourcePath))
.split(path.sep)
.shift();
const layoutMap = {
blog: 'Post',
learn: 'Learn',
reference: 'API',
};
const layout = layoutMap[pageParentDir] || 'Home';
const code =
`import withLayout from 'components/Layout/Layout${layout}';