|
| 1 | +const { getOptions } = require('loader-utils'); |
| 2 | +const mdx = require('@mdx-js/mdx'); |
| 3 | +const matter = require('gray-matter'); |
| 4 | +const normalizeNewline = require('normalize-newline'); |
| 5 | + |
| 6 | +const EXREG = /export\sdefault\s/g; |
| 7 | +const MODREG = /^(import|export)\s/; |
| 8 | +const SLIDEREG = /\n---\n/; |
| 9 | + |
| 10 | +const nameForSlide = index => `MDXContentWrapper${index}`; |
| 11 | + |
| 12 | +module.exports = async function(src) { |
| 13 | + const { data, content } = matter(src); |
| 14 | + |
| 15 | + const inlineModules = []; |
| 16 | + |
| 17 | + const callback = this.async(); |
| 18 | + const options = Object.assign({}, getOptions(this), { |
| 19 | + filepath: this.resourcePath |
| 20 | + }); |
| 21 | + |
| 22 | + /* |
| 23 | + Step 1: |
| 24 | + * Set aside all inline import and export statements from the mdx file. |
| 25 | + * When mdx.sync compiles MDX into JSX, it will stub any component that doesn't have a corresponding |
| 26 | + * import. Therefore, we will re-add all of the imports/exports to each |
| 27 | + * slide (then remove and add them again!). |
| 28 | + */ |
| 29 | + const slides = normalizeNewline(content) |
| 30 | + .split('\n') |
| 31 | + .map(line => { |
| 32 | + if (MODREG.test(line)) { |
| 33 | + inlineModules.push(line); |
| 34 | + } |
| 35 | + return line; |
| 36 | + }) |
| 37 | + .filter(line => !MODREG.test(line)) |
| 38 | + .filter(Boolean) |
| 39 | + .join('\n') |
| 40 | + /* |
| 41 | + Step 2: |
| 42 | + * Split the MDX file by occurences of `---`. This is a reserved symbol |
| 43 | + * to denote slide boundaries. |
| 44 | + */ |
| 45 | + .split(SLIDEREG) |
| 46 | + /* |
| 47 | + Step 3: |
| 48 | + * As referenced before, we need to add the imports and exports to |
| 49 | + * every slide again. That way mdx.sync can find the component definitions |
| 50 | + * for any custom components used in the MDX file. |
| 51 | + */ |
| 52 | + .map( |
| 53 | + slide => ` |
| 54 | +${inlineModules.join('\n')}\n |
| 55 | +${slide}` |
| 56 | + ) |
| 57 | + /* |
| 58 | + Step 4: |
| 59 | + * Use mdx.sync to compile a separate JSX component for each slide |
| 60 | + * written in MDX. |
| 61 | + */ |
| 62 | + .map(slide => mdx.sync(slide, options)) |
| 63 | + /* |
| 64 | + Step 5: |
| 65 | + * mdx.sync will attempt to default export the component generated for each |
| 66 | + * slide. However, we have multiple slides and thus multiple generated components. |
| 67 | + * We can't export multiple defaults, so we must remove all existing occurences of |
| 68 | + * `export default`. |
| 69 | + */ |
| 70 | + .map(slide => slide.replace(EXREG, '')) |
| 71 | + /* |
| 72 | + Step 6: |
| 73 | + * Remove the inline exports/imports again. We don't want duplicate import/export |
| 74 | + * statements littered throughout the file output. |
| 75 | + */ |
| 76 | + .map(slide => |
| 77 | + slide |
| 78 | + .split('\n') |
| 79 | + .filter(line => !MODREG.test(line)) |
| 80 | + .filter(Boolean) |
| 81 | + .join('\n') |
| 82 | + ) |
| 83 | + .map(slide => slide.trim()) |
| 84 | + /* |
| 85 | + Step 7: |
| 86 | + * The generate component from mdx.sync assumes it's the only component that |
| 87 | + * will inhabit a file. It has const definitions outside of the auto-named MDXContent |
| 88 | + * component. This would be fine if we weren't generating a component for each |
| 89 | + * slide. However, in our case we would generate a lot of duplicate variable names. |
| 90 | + * Thus, the easiest solution is to wrap each mdx.sync-generated component+const |
| 91 | + * definitions in another component that is uniquely named (using slide index). |
| 92 | + */ |
| 93 | + .map((slide, i) => { |
| 94 | + const wrapperName = nameForSlide(i); |
| 95 | + return `function ${wrapperName}(props) { |
| 96 | + ${slide} |
| 97 | + return (<MDXContent {...props} />); |
| 98 | +}; |
| 99 | +${wrapperName}.isMDXComponent = true;`; |
| 100 | + }); |
| 101 | + |
| 102 | + const { modules = [] } = data; |
| 103 | + let wrapperNames = []; |
| 104 | + /* |
| 105 | + Step 8: |
| 106 | + * Begin composing the final output. Include React, mdx, modules, and the inline |
| 107 | + * export/import statements that we removed in Step 6. |
| 108 | + */ |
| 109 | + let allCode = `/* @jsx mdx */ |
| 110 | +import React from 'react' |
| 111 | +import { mdx } from '@mdx-js/react' |
| 112 | +${modules.join('\n')} |
| 113 | +${inlineModules |
| 114 | + .filter(function(el, i, arr) { |
| 115 | + return arr.indexOf(el) === i; |
| 116 | + }) |
| 117 | + .join('\n')}\n\n`; |
| 118 | + /* |
| 119 | + Step 9: |
| 120 | + * Add in the slide component definitions. Keep track of the component names. |
| 121 | + */ |
| 122 | + slides.forEach((s, i) => { |
| 123 | + allCode += s + '\n\n'; |
| 124 | + wrapperNames.push(nameForSlide(i)); |
| 125 | + }); |
| 126 | + /* |
| 127 | + Step 10: |
| 128 | + * Finally, declare the default export as an array of the slide components. |
| 129 | + * See /examples/MDX/TestMDX.js for how to import and use the generated slide |
| 130 | + * components. |
| 131 | + */ |
| 132 | + const footer = `export const slideCount = ${slides.length};\n\n |
| 133 | +export default [${wrapperNames}]`; |
| 134 | + allCode += footer; |
| 135 | + |
| 136 | + return callback(null, allCode); |
| 137 | +}; |
0 commit comments