UNPKG

3.52 kBMarkdownView Raw
1# `mdx.macro`
2
3[![Babel Macro](https://img.shields.io/badge/babel--macro-%F0%9F%8E%A3-f5da55.svg?style=flat-square)](https://github.com/kentcdodds/babel-plugin-macros)
4
5[![npm version](https://img.shields.io/badge/npm-0.2.5-brightgreen.svg)](https://github.com/weyert/mdx.macro)
6
7A babel-macro for converting mdx into an inline component.
8
9```markdown
10## This is some MDX source
11
12<SomeComponent />
13
14~~strikethrough~~
15```
16
17```js
18import { mdx, imports } from 'mdx.macro'
19import { MDXTag } from '@mdx-js/tag'
20imports() // copies import statements from markdown file to here
21
22const SomeMDXComponent = mdx('./markdown.md')
23
24generates...
25
26```js
27const SomeMDXComponent = ({ components, ...props }) => (
28 <MDXTag name="wrapper" components={components}>
29 <MDXTag name="h2" components={components}>{`This is some MDX source`}</MDXTag>{' '}
30 <SomeComponent />{' '}
31 <MDXTag name="p" components={components}>
32 <MDXTag
33 name="del"
34 components={components}
35 parentName="p"
36 >
37 {`strikethrough`}
38 </MDXTag>
39 </MDXTag>
40 </MDXTag>
41)
42```
43
44### Getting started
45
46#### Set up an application
47
48 Recommended setup - set up an application from scratch
49
50 [yarn](https://yarnpkg.com/en/docs/cli/) or [npm](https://docs.npmjs.com/cli/install) can be used
51
52 create a package.json file
53 ```
54 npm init
55
56 yarn init
57 ```
58
59 install webpack and webpack-cli as dev dependencies
60 ```
61 npm i webpack webpack-cli webpack-dev-server html-webpack-plugin -D
62
63 yarn add webpack webpack-cli webpack-dev-server html-webpack-plugin -D
64 ```
65
66 add to package.json
67 ```
68 "scripts": {
69 "start": "webpack-dev-server --mode development --open",
70 "build": "webpack --mode production"
71 },
72 ```
73
74 install and save react and react-dom
75 ```
76 npm i react react-dom
77
78 yarn add react react-dom
79 ```
80
81 install and save the following dev dependencies
82 ```
83 npm i @babel/core babel-loader @babel/preset-env @babel/preset-react -D
84
85 yarn add @babel/core babel-loader @babel/preset-env @babel/preset-react -D
86 ```
87
88 create a [webpack](https://webpack.js.org/guides/getting-started/#using-a-configuration) config. Example of a basic webpack config file:
89 ```
90 const HtmlWebPackPlugin = require("html-webpack-plugin");
91
92 const htmlPlugin = new HtmlWebPackPlugin({
93 template: "./src/index.html",
94 filename: "./index.html"
95 });
96
97 module.exports = {
98 module: {
99 rules: [
100 {
101 test: /\.js$/,
102 exclude: /node_modules/,
103 use: {
104 loader: "babel-loader"
105 }
106 }
107 ]
108 },
109 plugins: [htmlPlugin]
110 };
111 ```
112
113 create a .babelrc file and add the following presets
114 ```
115 {
116 "presets": ["@babel/preset-env", "@babel/preset-react"]
117 }
118 ```
119
120#### Install and save the following dev dependencies
121 ```
122 npm i @innerfuse/mdx.macro babel-plugin-macros @mdx-js/tag -D
123
124 yarn add @innerfuse/mdx-macro babel-plugin-macros @mdx-js/tag -D
125 ```
126
127#### Add [babel-plugin-macros](https://github.com/kentcdodds/babel-plugin-macros/blob/master/other/docs/user.md) to your babel config file
128 ```
129 "plugins": [
130 "babel-plugin-macros"
131 ]
132 ```
133
134### Known Problems
135
136If you use a React component which is not defined in the Javascript file and is not imported the application will stop working and you will get an error similar to `__jsxFilename not defined`. If this is the case ensure you have the component referred in the Markdown file is defined.
\No newline at end of file