Rendering Markdown files using solid-start and MDX
Sometimes, it's easier to write down content in markdown. However, solid-start doesn't render these by default.
Thankfully, using vite and the mdx project, it's pretty easy to get everything set up.
Dependencies
In an existing solid start project (if you don't have one created already, refer to the getting started docs), install @vinxi/plugin-mdx
vite.config.js
Add the plugin to your vite.config.js
file
Usage
Simple markdown component
From a jsx or tsx file, the markdown file can then be imported and used directly as a component
MDX
Render JSX inside the component
A .md
will be limited to expressing plain markdown. It might instead be useful to render more complex pages by importing and rendering components from within the markdown file.
.mdx
files allow you to render jsx from within a markdown file. For example, if an mdx
file contains the following:
It will render the Counter
component from the ./components/Counter
inline in the markdown file. Like so:
Frontmatter
From a .mdx
file, you can expose properties that can then be read where the file is embedded.
For instance, the root notes page of this site uses a title
, url
, and date
property on each markdown file in order to render the note index.
Page
The markdown file can also be treated as a page. To do this, use the (.mdx)[https://mdxjs.com/] file extension.
Navigating to /test
will render the markdown file as a page. (this is how this page is rendered)
Code Syntax Highlighting
Using remark-expressive-code
, code blocks can be highlighted.
Install the needed packages:
Feel free to read up on these packages to understand what they offer. But for this use case, the following configuration will show highlighted code.
Add these imports in vite.config.js
And then configure the remark and rehype plugins: