2018-07-28 21:21:36 +03:00
|
|
|
const mdx = require('@mdx-js/mdx')
|
|
|
|
const matter = require('gray-matter')
|
2018-07-28 23:46:11 +03:00
|
|
|
const stringifyObject = require('stringify-object')
|
2018-07-28 21:21:36 +03:00
|
|
|
|
|
|
|
const EXREG = /export\sdefault\s/g
|
2018-07-29 01:13:50 +03:00
|
|
|
const MODREG = /^(import|export)\s/
|
2018-07-28 21:21:36 +03:00
|
|
|
|
|
|
|
module.exports = async function (src) {
|
|
|
|
const callback = this.async()
|
|
|
|
|
|
|
|
const { data, content } = matter(src)
|
|
|
|
|
2018-07-29 01:13:50 +03:00
|
|
|
const inlineModules = []
|
2018-07-28 21:21:36 +03:00
|
|
|
const slides = content.split('---\n')
|
|
|
|
.map(str => mdx.sync(str))
|
|
|
|
.map(str => str.trim())
|
|
|
|
.map(str => str.replace(EXREG, ''))
|
2018-07-29 01:13:50 +03:00
|
|
|
.map(str => {
|
2018-07-29 01:32:05 +03:00
|
|
|
const lines = str.split('\n')
|
2018-07-29 01:13:50 +03:00
|
|
|
inlineModules.push(
|
|
|
|
...lines.filter(str => MODREG.test(str))
|
|
|
|
)
|
|
|
|
return lines.filter(str => !MODREG.test(str))
|
2018-07-29 01:32:05 +03:00
|
|
|
.filter(Boolean)
|
|
|
|
.join('\n')
|
2018-07-29 01:13:50 +03:00
|
|
|
})
|
2018-07-28 21:21:36 +03:00
|
|
|
|
|
|
|
const {
|
2018-07-28 23:46:11 +03:00
|
|
|
modules = []
|
2018-07-28 21:21:36 +03:00
|
|
|
} = data
|
|
|
|
|
|
|
|
const code = `import React from 'react'
|
|
|
|
import { MDXTag } from '@mdx-js/tag'
|
2018-07-28 23:46:11 +03:00
|
|
|
${modules.join('\n')}
|
2018-07-29 01:13:50 +03:00
|
|
|
${inlineModules.join('\n')}
|
2018-07-28 23:46:11 +03:00
|
|
|
|
|
|
|
export const meta = ${stringifyObject(data)}
|
2018-07-28 21:21:36 +03:00
|
|
|
|
|
|
|
export default [
|
|
|
|
${slides.join(',\n\n')}
|
|
|
|
]`
|
|
|
|
|
|
|
|
return callback(null, code)
|
|
|
|
}
|