Skip to content

Commit 5846bf9

Browse files
committed
Implement first pass of MDXs
This implements the basic idea of MDXs. Though we still need to figure out how to best handle the layout mechanism. cc/ @ChristopherBiscardi, @jxnblk, @timneutkens --- Related #454
1 parent 1a4044c commit 5846bf9

File tree

7 files changed

+278
-2
lines changed

7 files changed

+278
-2
lines changed

Diff for: packages/mdx/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function createMdxAstCompiler(options) {
3030
const fn = unified()
3131
.use(toMDAST, options)
3232
.use(remarkMdx, options)
33+
.use(mdxHastToJsx, options) // Set JSX compiler early so it can be overridden
3334
.use(squeeze, options)
3435
.use(toMDXAST, options)
3536

@@ -81,8 +82,6 @@ function applyHastPluginsAndCompilers(compiler, options) {
8182
}
8283
})
8384

84-
compiler.use(mdxHastToJsx, options)
85-
8685
for (const compilerPlugin of compilers) {
8786
compiler.use(compilerPlugin, options)
8887
}

Diff for: packages/remark-mdxs/index.js

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const visit = require('unist-util-visit')
2+
const remove = require('unist-util-remove')
3+
const {toJSX} = require('@mdx-js/mdx/mdx-hast-to-jsx')
4+
5+
module.exports = function({delimiter = 'hr'}) {
6+
this.Compiler = tree => {
7+
const splits = []
8+
const documents = []
9+
10+
const importNodes = tree.children.filter(n => n.type === 'import')
11+
const exportNodes = tree.children.filter(n => n.type === 'export')
12+
13+
const layout = exportNodes.find(node => node.default)
14+
15+
// We don't care about imports and exports when handling
16+
// multiple MDX documents
17+
let mdxsTree = remove(remove(tree, 'export'), 'import')
18+
const {children} = mdxsTree
19+
20+
visit(mdxsTree, node => {
21+
if (node.tagName === delimiter) {
22+
splits.push(children.indexOf(node))
23+
}
24+
})
25+
26+
let previousSplit = 0
27+
for (let i = 0; i < splits.length; i++) {
28+
const split = splits[i]
29+
documents.push(children.slice(previousSplit, split))
30+
previousSplit = split + 1
31+
}
32+
33+
documents.push(children.slice(previousSplit))
34+
35+
const jsxFragments = documents
36+
.map(nodes => nodes.map(toJSX).join('\n'))
37+
.map((jsx, i) =>
38+
`
39+
function MDXSContent${i}({ components, ...props }) {
40+
return (
41+
${jsx.trim()}
42+
)
43+
}
44+
`.trim()
45+
)
46+
47+
const defaultExport = `
48+
const MDXSWrapper = props => [
49+
${jsxFragments.map((_, i) => ` <MDXSContent${i} {...props} />`).join(',\n')}
50+
]
51+
52+
export default MDXWrapper
53+
`.trim()
54+
55+
return [
56+
importNodes.map(n => n.value.trim()).join('\n'),
57+
'',
58+
exportNodes
59+
.filter(n => !n.default)
60+
.map(n => n.value.trim())
61+
.join('\n'),
62+
'',
63+
`const MDXSLayout = ${
64+
layout
65+
? layout.value
66+
.replace(/^export\s+default\s+/, '')
67+
.replace(/;\s*$/, '')
68+
: '"wrapper"'
69+
}`,
70+
'',
71+
jsxFragments.join('\n\n'),
72+
'',
73+
defaultExport
74+
].join('\n')
75+
}
76+
}

Diff for: packages/remark-mdxs/license

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2019 John Otander and Brent Jackson
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

Diff for: packages/remark-mdxs/package.json

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"name": "remark-mdxs",
3+
"version": "1.0.0-rc.0",
4+
"description": "Support for multiple MDX documents in a single file",
5+
"license": "MIT",
6+
"keywords": [
7+
"mdx",
8+
"mdxs",
9+
"markdown",
10+
"react",
11+
"jsx",
12+
"remark",
13+
"mdxast"
14+
],
15+
"homepage": "https://mdxjs.com",
16+
"repository": "mdx-js/mdx",
17+
"bugs": "https://github.com/mdx-js/mdx/issues",
18+
"author": "John Otander <[email protected]> (https://johno.com)",
19+
"contributors": [
20+
"John Otander <[email protected]> (http://johnotander.com)",
21+
"Brent Jackson <[email protected]> (https://jxnblk.com)",
22+
"Tim Neutkens <[email protected]>",
23+
"Matija Marohnić <[email protected]>",
24+
"Titus Wormer <[email protected]> (https://wooorm.com)"
25+
],
26+
"files": [
27+
"index.js"
28+
],
29+
"dependencies": {
30+
"@babel/core": "^7.2.2",
31+
"@babel/helper-plugin-utils": "^7.0.0",
32+
"@babel/plugin-proposal-object-rest-spread": "^7.3.2",
33+
"@babel/plugin-syntax-jsx": "^7.2.0",
34+
"is-alphabetical": "^1.0.2",
35+
"remark-parse": "^6.0.0",
36+
"unified": "^7.0.0",
37+
"unist-util-remove": "^1.0.1",
38+
"unist-util-visit": "^1.4.0"
39+
},
40+
"peerDependencies": {
41+
"@mdx-js/mdx": "*"
42+
},
43+
"scripts": {
44+
"test": "jest"
45+
},
46+
"jest": {
47+
"testEnvironment": "node"
48+
},
49+
"devDependencies": {
50+
"jest": "^24.0.0",
51+
"remark-stringify": "^6.0.4",
52+
"vfile": "^4.0.0"
53+
}
54+
}

Diff for: packages/remark-mdxs/readme.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# [remark][]-[mdx][]s
2+
3+
[![Build Status][build-badge]][build]
4+
[![lerna][lerna-badge]][lerna]
5+
[![Join the community on Spectrum][spectrum-badge]][spectrum]
6+
7+
> :warning: This project is currently in alpha
8+
9+
[MDXs][] syntax support for [remark][].
10+
11+
## Installation
12+
13+
```sh
14+
npm install --save remark-mdxs
15+
```
16+
17+
## Contribute
18+
19+
See [`contributing.md` in `mdx-js/mdx`][contributing] for ways to get started.
20+
21+
This organisation has a [Code of Conduct][coc].
22+
By interacting with this repository, organisation, or community you agree to
23+
abide by its terms.
24+
25+
## License
26+
27+
[MIT][] © [Titus Wormer][author] and [John Otander][author2]
28+
29+
<!-- Definitions -->
30+
31+
[build]: https://travis-ci.org/mdx-js/mdx
32+
33+
[build-badge]: https://travis-ci.org/mdx-js/mdx.svg?branch=master
34+
35+
[lerna]: https://lernajs.io/
36+
37+
[lerna-badge]: https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg
38+
39+
[spectrum]: https://spectrum.chat/mdx
40+
41+
[spectrum-badge]: https://withspectrum.github.io/badge/badge.svg
42+
43+
[contributing]: https://github.com/mdx-js/mdx/blob/master/contributing.md
44+
45+
[coc]: https://github.com/mdx-js/mdx/blob/master/code-of-conduct.md
46+
47+
[mit]: license
48+
49+
[remark]: https://github.com/remarkjs/remark
50+
51+
[mdx]: https://github.com/mdx-js/mdx
52+
53+
[author]: https://wooorm.com
54+
55+
[author2]: https://johno.com

Diff for: packages/remark-mdxs/test/__snapshots__/test.js.snap

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`correctly transpiles 1`] = `
4+
"/* @jsx mdx */
5+
import Foo from './bar'
6+
7+
export const author = 'fred'
8+
9+
const MDXSLayout = Foo
10+
11+
function MDXSContent0({ components, ...props }) {
12+
return (
13+
<h1>{\`Hello, world! \`}<Foo bar={{ baz: 'qux' }} /></h1>
14+
)
15+
}
16+
17+
function MDXSContent1({ components, ...props }) {
18+
return (
19+
<Baz>
20+
Hi!
21+
</Baz>
22+
)
23+
}
24+
25+
function MDXSContent2({ components, ...props }) {
26+
return (
27+
<h1>{\`I'm another document\`}</h1>
28+
29+
30+
<p>{\`over here.\`}</p>
31+
)
32+
}
33+
34+
const MDXSWrapper = props => [
35+
<MDXSContent0 {...props} />,
36+
<MDXSContent1 {...props} />,
37+
<MDXSContent2 {...props} />
38+
]
39+
40+
export default MDXWrapper"
41+
`;

Diff for: packages/remark-mdxs/test/test.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const mdx = require('../../mdx')
2+
const remarkMdxs = require('..')
3+
4+
const FIXTURE = `
5+
import Foo from './bar'
6+
export const author = 'fred'
7+
export default Foo
8+
9+
# Hello, world! <Foo bar={{ baz: 'qux' }} />
10+
11+
---
12+
13+
<Baz>
14+
Hi!
15+
</Baz>
16+
17+
---
18+
19+
# I'm another document
20+
21+
over here.
22+
`
23+
24+
it('correctly transpiles', async () => {
25+
const result = await mdx(FIXTURE, {
26+
remarkPlugins: [remarkMdxs]
27+
})
28+
29+
expect(result).toMatchSnapshot()
30+
})

0 commit comments

Comments
 (0)