Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
format with prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
jescalan committed May 18, 2020
1 parent 7f2adf8 commit 0122d1c
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 52 deletions.
2 changes: 1 addition & 1 deletion __tests__/babelPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test('transforms as intended', () => {
const { content, filename } = loadFixture('basic')
const { code } = transform(content, {
plugins: [plugin(mockNextOptions, mockPluginOptions)],
filename
filename,
})
expect(code).toMatchSnapshot()
})
Expand Down
2 changes: 1 addition & 1 deletion __tests__/fixtures/basic/layouts/docs-page.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { frontMatter as introData } from '../pages/docs/intro.mdx'
import { frontMatter as advancedData } from '../pages/docs/advanced.mdx'

export default frontMatter => {
export default (frontMatter) => {
return function docsPageLayout({ children }) {
return (
<>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { frontMatter as introData } from '../pages/docs/intro.mdx'
import { frontMatter as advancedData } from '../pages/docs/advanced.mdx'

export default frontMatter => {
export default (frontMatter) => {
return function docsPageLayout({ children }) {
return (
<>
<p>LAYOUT TEMPLATE</p>
<h1>{frontMatter.title}</h1>
<p>
All frontMatter: {JSON.stringify(frontMatter)}
</p>
<p>All frontMatter: {JSON.stringify(frontMatter)}</p>
<p>
Other docs: {introData.title}, {advancedData.title}
</p>
Expand Down
7 changes: 5 additions & 2 deletions __tests__/fixtures/extend-frontmatter-async/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ const withMdxEnhanced = require('../../..')
module.exports = withMdxEnhanced({
extendFrontMatter: {
process: (mdxContent, frontMatter) => {
return new Promise(resolve =>
return new Promise((resolve) =>
setTimeout(() => {
return resolve({
__async: 'this data is async',
layout: 'docs-page',
reversePath: frontMatter.__resourcePath.split('').reverse().join('')
reversePath: frontMatter.__resourcePath
.split('')
.reverse()
.join(''),
})
}, 50)
)
Expand Down
6 changes: 2 additions & 4 deletions __tests__/fixtures/extend-frontmatter/layouts/docs-page.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { frontMatter as introData } from '../pages/docs/intro.mdx'
import { frontMatter as advancedData } from '../pages/docs/advanced.mdx'

export default frontMatter => {
export default (frontMatter) => {
return function docsPageLayout({ children }) {
return (
<>
<p>LAYOUT TEMPLATE</p>
<h1>{frontMatter.title}</h1>
<p>
All frontMatter: {JSON.stringify(frontMatter)}
</p>
<p>All frontMatter: {JSON.stringify(frontMatter)}</p>
<p>
Other docs: {introData.title}, {advancedData.title}
</p>
Expand Down
2 changes: 1 addition & 1 deletion __tests__/fixtures/extend-frontmatter/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = withMdxEnhanced({
return {
__outline: 'outline stuff',
layout: 'docs-page',
reversePath: frontMatter.__resourcePath.split('').reverse().join('')
reversePath: frontMatter.__resourcePath.split('').reverse().join(''),
}
},
},
Expand Down
2 changes: 1 addition & 1 deletion __tests__/fixtures/file-extensions/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const withMdxEnhanced = require('../../..')

module.exports = withMdxEnhanced({
fileExtensions: ['mdx', 'md']
fileExtensions: ['mdx', 'md'],
})()
2 changes: 1 addition & 1 deletion __tests__/fixtures/layouts-path/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ const withMdxEnhanced = require('../../..')

module.exports = withMdxEnhanced({
layoutPath: 'snargles',
defaultLayout: true
defaultLayout: true,
})()
2 changes: 1 addition & 1 deletion __tests__/fixtures/layouts-path/snargles/index.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { frontMatter as introData } from '../pages/docs/intro.mdx'
import { frontMatter as advancedData } from '../pages/docs/advanced.mdx'

export default frontMatter => {
export default (frontMatter) => {
return function docsPageLayout({ children }) {
return (
<>
Expand Down
4 changes: 2 additions & 2 deletions __tests__/fixtures/on-content/next.config-mock.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const withMdxEnhanced = require('../../..')

module.exports = configFn =>
module.exports = (configFn) =>
withMdxEnhanced({
onContent: mdxContent => configFn(mdxContent)
onContent: (mdxContent) => configFn(mdxContent),
})()
2 changes: 1 addition & 1 deletion __tests__/fixtures/on-content/next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const withMdxEnhanced = require('../../..')

module.exports = withMdxEnhanced({
onContent: mdxContent => processContent(mdxContent)
onContent: (mdxContent) => processContent(mdxContent),
})()

function processContent(content) {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/fixtures/scan-mdx-content/layouts/docs-page.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { frontMatter as introData } from '../pages/docs/intro.mdx'
import { frontMatter as advancedData } from '../pages/docs/advanced.mdx'
export default frontMatter => {
export default (frontMatter) => {
const __scans = frontMatter.__scans
return function docsPageLayout({ children }) {
return (
Expand Down
8 changes: 4 additions & 4 deletions __tests__/fixtures/scan-mdx-content/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ const withMdxEnhanced = require('../../..')
module.exports = withMdxEnhanced({
scan: {
hasSnargles: {
pattern: /<Snargles.*.*\/>/
pattern: /<Snargles.*.*\/>/,
},
snarglesName: {
pattern: /<Snargles.*name=['"](.*)['"].*\/>/,
transform: arr => arr[1] // An optional callback function that transforms the result of the match operation
}
}
transform: (arr) => arr[1], // An optional callback function that transforms the result of the match operation
},
},
})()
30 changes: 18 additions & 12 deletions loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ module.exports = async function mdxEnhancedLoader(src) {
// Checks if there's a layout, if there is, resolve the layout and wrap the content in it.
processLayout
.call(this, options, data, content, resourcePath, scans)
.then(result => callback(null, result))
.catch(err => callback(err))
.then((result) => callback(null, result))
.catch((err) => callback(err))
}
function scanContent(options, content) {
const { mdxEnhancedPluginOptions: pluginOpts } = options
Expand All @@ -57,31 +57,36 @@ function scanContent(options, content) {
return acc
}, {})
}
async function processLayout(options, frontMatter, content, resourcePath, scans) {
async function processLayout(
options,
frontMatter,
content,
resourcePath,
scans
) {
const { mdxEnhancedPluginOptions: pluginOpts } = options

const extendedFm = await extendFrontMatter({
content,
frontMatter: {
...frontMatter,
__resourcePath: resourcePath,
__scans: scans
__scans: scans,
},
phase: 'loader',
extendFm: pluginOpts.extendFrontMatter
extendFm: pluginOpts.extendFrontMatter,
})

const mergedFrontMatter = {
...frontMatter,
...extendedFm,
__resourcePath: resourcePath,
__scans: scans
__resourcePath: resourcePath,
__scans: scans,
}

// If no layout is provided and the default layout setting is not on, return the
// content directly.
if (!mergedFrontMatter.layout && !pluginOpts.defaultLayout)
return content
if (!mergedFrontMatter.layout && !pluginOpts.defaultLayout) return content

// Set the default if the option is active and there's no layout
if (!mergedFrontMatter.layout && pluginOpts.defaultLayout) {
Expand All @@ -104,9 +109,10 @@ async function processLayout(options, frontMatter, content, resourcePath, scans)
'|'
)})`


const matches = await new Promise((resolve, reject) => {
glob(layoutMatcher, (err, matches) => err ? reject(err) : resolve(matches))
glob(layoutMatcher, (err, matches) =>
err ? reject(err) : resolve(matches)
)
})

if (!matches.length) {
Expand All @@ -119,7 +125,7 @@ async function processLayout(options, frontMatter, content, resourcePath, scans)
if (onContent && this._compiler.name === 'server') {
onContent({
...mergedFrontMatter,
content
content,
})
}

Expand Down
28 changes: 12 additions & 16 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ async function extendFrontMatter({
content,
frontMatter,
phase,
extendFm
extendFm,
} = {}) {
if (!extendFm || !extendFm.process) return {}
if (extendFm.phase !== 'both' && extendFm.phase !== phase) return {}
Expand All @@ -14,30 +14,26 @@ async function extendFrontMatter({
}
module.exports.extendFrontMatter = extendFrontMatter

function generateFrontmatterPath(
filePath,
root
) {
function generateFrontmatterPath(filePath, root) {
const filePathNormalized = normalizeToUnixPath(filePath)
const dirnameNormalized = normalizeToUnixPath(__dirname)

return normalizeToUnixPath(path.join(
root,
'.mdx-data',
`${md5(filePathNormalized.replace(dirnameNormalized, ''))}.json`
))

return normalizeToUnixPath(
path.join(
root,
'.mdx-data',
`${md5(filePathNormalized.replace(dirnameNormalized, ''))}.json`
)
)
}
module.exports.generateFrontmatterPath = generateFrontmatterPath

// md5 hash a string
function md5(str) {
return crypto
.createHash('md5')
.update(str)
.digest('hex')
return crypto.createHash('md5').update(str).digest('hex')
}

function normalizeToUnixPath(str) {
return str.replace(/\\/g, '/')
}
module.exports.normalizeToUnixPath = normalizeToUnixPath
module.exports.normalizeToUnixPath = normalizeToUnixPath

0 comments on commit 0122d1c

Please sign in to comment.