MDX Preview lets you preview MDX seamlessly. Write markdown / JSX, see it live refresh and get instant visual feedback.
Instant preview as you type without starting a dev server or building
To get started:
yarn install or
npm install if necessary so that you have the npm dependencies in your workspace, and navigate to your
mdx file; or just create an untitled file, change language mode (from command palette) to mdx and type some mdx code.
- Open command palette, and type "MDX: Open Preview"; or click the magnifying glass.
MDX Extension is recommended for syntax highlighting of MDX files.
You can apply custom layout to the MDX file by
- Exporting a default layout value using
import Layout from '../components/Layout';
export default Layout
Specifying a path to a custom layout config file in
mdx-preview.preview.mdx.customLayoutFilePath extension setting. For example, the absolute path to the
../components/Layout file above.
When nothing is specified, by default it will apply VS Code Markdown styles. You can turn that off by
mdx-preview.preview.useVscodeMarkdownStyles extension settings or "MDX: Toggle VSCode Markdown Styles" command.
You can import other files with
import AnotherMDX from './AnotherMDX.mdx'
#root element, you can also preview that using MDX Preview. For example, you can preview the
index.js file from your react app:
import ReactDOM from 'react';
import App from './App';
VS Code webview limitations
- Service worker / Local storage are not available.
MemoryRouter if you are using React Router.
Code will only be evaluated inside VS Code extension webview's isolated iframe. The MDX files can only require dependencies within your active workspace folders. By default, only HTTPS content is allowed within the webview. Of course, you still need to make sure you trust the MDX file you preview, AND trust the files inside your workspace. Note that with the default Content Security Policy, you would not be able to preview a LiveEditor.
You can change your security settings through
mdx-preview.preview.security extension setting or "MDX: Change Security Settings" command.
This extension contributes the following settings:
mdx-preview.preview.previewOnChange: If set to true, previews on file change; If set to false, previews on file save
mdx-preview.preview.security: Security policy settings
mdx-preview.preview.useVscodeMarkdownStyles: Use VS Code Markdown Styles for layout.
mdx-preview.preview.useWhiteBackground: Use white background regardless of current theme settings.
mdx-preview.preview.mdx.customLayoutFilePath: Path of custom layout file to use
How it works
MDX Preview transpiles your
.mdx file using
@mdx-js/mdx, sends the initial file to the webview, and recursively fetches local dependencies (from your workspace) and npm dependencies (from
node_modules directory) from your workspace using polestar. MDX Preview has provided built-in dependencies for MDX rendering like
- [ ] Scroll Sync
- [ ] TypeScript support
- [ ] remark/rehype plugins
- [ ] Integrations with gatsby / x0 /...
This extension is not possible without the help from James and the open source polestar library.
Saying thanks to these awesome open source projects as well: