(
source,
fileExtension,
components = {},
props = {}
)
| 28 | * }>} |
| 29 | */ |
| 30 | export default async function compile( |
| 31 | source, |
| 32 | fileExtension, |
| 33 | components = {}, |
| 34 | props = {} |
| 35 | ) { |
| 36 | // Parses the Frontmatter to the VFile and removes from the original source |
| 37 | // cleaning the frontmatter to the source that is going to be parsed by the MDX Compiler |
| 38 | matter(source, { strip: true }); |
| 39 | |
| 40 | // Creates a GitHub slugger to generate the same slugs as GitHub |
| 41 | const slugger = createGitHubSlugger(); |
| 42 | |
| 43 | // Compiles the MDX/Markdown source into a serializable VFile |
| 44 | const compiled = await mdxCompile(source, { |
| 45 | rehypePlugins, |
| 46 | remarkPlugins, |
| 47 | format: fileExtension, |
| 48 | }); |
| 49 | |
| 50 | const interpreter = createInterpreter({ |
| 51 | ...components, |
| 52 | 'react/jsx-runtime': reactRuntime, |
| 53 | }); |
| 54 | |
| 55 | // Run the compiled JavaScript code from MDX |
| 56 | interpreter.run(compiled.toString()); |
| 57 | |
| 58 | // Retrieve the default export from the compiled MDX |
| 59 | const MDXContent = interpreter.exports.default; |
| 60 | |
| 61 | // Render the MDX content directly from the compiler |
| 62 | const content = <MDXContent {...props} components={components} />; |
| 63 | |
| 64 | // Retrieve some parsed data from the VFile metadata |
| 65 | // such as frontmatter and Markdown headings |
| 66 | const { headings = [], matter: frontmatter, readingTime } = source.data; |
| 67 | |
| 68 | headings.forEach(heading => { |
| 69 | // we re-sluggify the links to match the GitHub slugger |
| 70 | // since some also do not come with sluggifed links |
| 71 | heading.data = { ...heading.data, id: slugger(heading.value) }; |
| 72 | }); |
| 73 | |
| 74 | return { content, headings, frontmatter, readingTime }; |
| 75 | } |
no test coverage detected