1
1
mirror of https://github.com/jxnblk/mdx-deck.git synced 2024-11-30 00:44:20 +03:00
mdx-deck/lib/loader.js
John Otander 0f086ad288 Fix regex
2018-07-31 09:28:14 -06:00

46 lines
1.0 KiB
JavaScript

const mdx = require('@mdx-js/mdx')
const matter = require('gray-matter')
const stringifyObject = require('stringify-object')
const EXREG = /export\sdefault\s\(/g
const MODREG = /^(import|export)\s/
const SLIDEREG = /\n---\n/
module.exports = async function (src) {
const callback = this.async()
const { data, content } = matter(src)
const inlineModules = []
const slides = content.split(SLIDEREG)
.map(str => mdx.sync(str))
.map(str => str.trim())
.map(str => str.replace(EXREG, '('))
.map(str => {
const lines = str.split('\n')
inlineModules.push(
...lines.filter(str => MODREG.test(str))
)
return lines.filter(str => !MODREG.test(str))
.filter(Boolean)
.join('\n')
})
const {
modules = []
} = data
const code = `import React from 'react'
import { MDXTag } from '@mdx-js/tag'
${modules.join('\n')}
${inlineModules.join('\n')}
export const meta = ${stringifyObject(data)}
export default [
${slides.join(',\n\n')}
]`
return callback(null, code)
}