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

45 lines
1014 B
JavaScript
Raw Normal View History

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
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)
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, ''))
.map(str => {
2018-07-29 01:32:05 +03:00
const lines = str.split('\n')
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-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')}
${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)
}