Skip to content

Commit

Permalink
[hashicorp#19] support .md and other file extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
nfagerlund committed Aug 15, 2019
1 parent 4413846 commit 954f202
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ The default mdx nextjs plugin takes care of point number one, but nothing else.
mdxEnhanced({
layoutPath: 'somePath/otherPath',
defaultLayout: true,
fileExtensions: ['mdx', 'md'],
remarkPlugins: [],
rehypePlugins: [],
extendFrontMatter: {
Expand All @@ -66,6 +67,12 @@ Directory used to resolve page layout when `layout` key present in front matter.
Set value to `true` to treat `index.[extension]` within `layoutPath` as the default layout for any `.mdx` file that a layout has not been specified for.

### fileExtensions

> `array` | optional | **default: `['mdx', 'md']`**
Array of file extensions that should be processed as MDX pages.

### remarkPlugins

> `array` | optional
Expand Down
15 changes: 9 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ const debug = require('debug')('next-mdx-enhanced')

module.exports = (pluginOptions = {}) => (nextConfig = {}) => {
if (!pluginOptions.layoutPath) pluginOptions.layoutPath = 'layouts'
if (!pluginOptions.fileExtensions) pluginOptions.fileExtensions = ['mdx', 'md']

// Set default pageExtensions if not set already
if (!nextConfig.pageExtensions) {
nextConfig.pageExtensions = ['jsx', 'js']
}

// Add mdx as a page extension so that mdx files are compiled as pages
if (nextConfig.pageExtensions.indexOf('mdx') === -1) {
nextConfig.pageExtensions.unshift('mdx')
}
// Add supported file extensions as page extensions so that mdx files are compiled as pages
pluginOptions.fileExtensions.forEach(ext => {
if (nextConfig.pageExtensions.indexOf(ext) === -1) {
nextConfig.pageExtensions.unshift(ext)
}
})

// Set default 'phase' for extendFrontMatter option
if (
Expand All @@ -31,7 +34,7 @@ module.exports = (pluginOptions = {}) => (nextConfig = {}) => {
webpack(config, options) {
// Add mdx webpack loader stack
config.module.rules.push({
test: /\.mdx?$/,
test: new RegExp(`\\.(${ pluginOptions.fileExtensions.join('|') })$`),
use: [
options.defaultLoaders.babel,
{
Expand Down Expand Up @@ -70,7 +73,7 @@ module.exports = (pluginOptions = {}) => (nextConfig = {}) => {
return extractFrontMatter(pluginOptions, files, compilation.context)
},
files: {
pattern: '**/*.mdx',
pattern: `**/*.{ ${pluginOptions.fileExtensions.join(',')} }`,
options: { cwd: config.context },
addFilesAsDependencies: true
}
Expand Down

0 comments on commit 954f202

Please sign in to comment.