This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.js
85 lines (70 loc) · 1.99 KB
/
loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const { getOptions } = require('loader-utils')
const mdx = require('@mdx-js/mdx')
const matter = require('gray-matter')
const stringifyObject = require('stringify-object')
const normalizeNewline = require('normalize-newline')
const EXREG = /export\sdefault\s\(/g
const MODREG = /^(import|export)\s/
const SLIDEREG = /\n---\n/
const TRANSREG = /^export const transition.*\s(.*)/
const hasDefaultExport = str => /export default/.test(str);
const defaultLayout = (str) => {
if (!hasDefaultExport(str)) {
str = `import {DefaultSlide} from './slides'
export default DefaultSlide
${str}`;
}
return str;
}
module.exports = async function (src) {
const callback = this.async()
const options = getOptions(this) || {}
const { data, content } = matter(src)
const transitions = [];
const inlineModules = []
const slides = normalizeNewline(content)
.split(SLIDEREG)
.map(str => defaultLayout(str))
.map(str => mdx.sync(str, options))
.map(str => str.trim())
.map(str => str.replace(EXREG, '('))
.map(str => {
const lines = str.split('\n')
let transition = null;
lines.forEach(line => {
let match = line.match(TRANSREG);
if (match && match[1]) {
transition = match[1];
}
})
transitions.push(transition);
return lines.filter(str => !TRANSREG.test(str))
.filter(Boolean)
.join('\n')
})
.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.filter(function (el, i, arr) {
return arr.indexOf(el) === i;
}).join('\n')}
export const transitions = [
${transitions.join(',')}
]
export default [
${slides.join(',\n\n')}
]`
return callback(null, code)
}