Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite how MDX is parsed #1039

Merged
merged 14 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ packages/gatsby-theme-mdx/src/components/header.js
packages/gatsby-theme-mdx/src/components/layout.js
packages/gatsby-theme-mdx/src/components/search.js

packages/remark-mdx

# tmp
examples/vue
packages/vue
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ artifacts
.*cache
public
.npmrc
.nyc_output/
coverage/
1 change: 1 addition & 0 deletions .remarkrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const wooorm = require('retext-preset-wooorm')
module.exports = {
plugins: [
'./packages/remark-mdx',
'./packages/remark-mdxjs',
'preset-wooorm',
'preset-prettier',
[
Expand Down
2 changes: 0 additions & 2 deletions docs/advanced/api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ const mdxText = fs.readFileSync('hello.mdx', 'utf8')
const jsx = mdx.sync(mdxText)
```

<!-- TODO: links should be updated. Can we also inline this example? -->

MDX’s [runtime][] package has [example][] usage.

## Compiler
Expand Down
2 changes: 0 additions & 2 deletions docs/advanced/contributing.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import Redirect from 'gatsby-theme-mdx/src/components/redirect'

<Redirect to="/contributing" />
2 changes: 0 additions & 2 deletions docs/advanced/custom-loader.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import Redirect from 'gatsby-theme-mdx/src/components/redirect'

<Redirect to="/guides/custom-loader" />
2 changes: 0 additions & 2 deletions docs/advanced/retext-plugins.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import Redirect from 'gatsby-theme-mdx/src/components/redirect'

<Redirect to="/advanced/using-plugins#using-retext-plugins" />
5 changes: 0 additions & 5 deletions docs/advanced/runtime.mdx
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import Doc from '@mdx-js/runtime/readme.md'

<!--
TODO: add a nicer, smaller intro to the runtime: when to use it, when not to.
We can also move it to `api` maybe?
-->

# Runtime

<Doc />
2 changes: 0 additions & 2 deletions docs/advanced/specification.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import Redirect from 'gatsby-theme-mdx/src/components/redirect'

<Redirect to="/advanced/ast" />
2 changes: 0 additions & 2 deletions docs/advanced/sync-api.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import Redirect from 'gatsby-theme-mdx/src/components/redirect'

<Redirect to="/advanced/api#sync-api" />
5 changes: 0 additions & 5 deletions docs/advanced/typescript.mdx
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
# TypeScript

<!--
TODO: this feels like it’s placed weirdly.
Where could it go?
-->

Below are some basic typings to get started with:

```tsx
Expand Down
2 changes: 0 additions & 2 deletions docs/advanced/writing-a-plugin.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import Redirect from 'gatsby-theme-mdx/src/components/redirect'

<Redirect to="/guides/custom-loader" />
2 changes: 0 additions & 2 deletions docs/contributing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ If you’re having issues installing locally you might need to run
- [Using Pull Requests](https://help.github.com/articles/about-pull-requests/)
- [GitHub Help](https://help.github.com)

<!-- Definitions -->

[coc]: https://github.com/mdx-js/.github/blob/master/code-of-conduct.md
[support]: https://mdxjs.com/support
[unit-test]: https://twitter.com/sindresorhus/status/579306280495357953
Expand Down
2 changes: 0 additions & 2 deletions docs/editor-plugins.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import Redirect from 'gatsby-theme-mdx/src/components/redirect'

<Redirect to="/editors" />
2 changes: 0 additions & 2 deletions docs/getting-started/typescript.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import Redirect from 'gatsby-theme-mdx/src/components/redirect'

<Redirect to="/advanced/typescript" />
4 changes: 2 additions & 2 deletions docs/guides/wrapper-customization.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,5 @@ or Brent Jackson’s [MDX Blocks](https://github.com/jxnblk/mdx-blocks)

---

- <https://mxstbr.blog/2017/02/react-children-deepdive>
- <https://github.com/jxnblk/mdx-blocks>
- [React children deep dive](https://mxstbr.blog/2017/02/react-children-deepdive)
- [MDX Blocks](https://github.com/jxnblk/mdx-blocks)
2 changes: 0 additions & 2 deletions docs/plugins.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import Redirect from 'gatsby-theme-mdx/src/components/redirect'

<Redirect to="/advanced/plugins" />
2 changes: 0 additions & 2 deletions docs/support.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ Spending the extra time up front can help save everyone time in the long run.

[Read the Contributing guidelines on the MDX website][contributing].

<!-- Definitions -->

[coc]: https://github.com/mdx-js/.github/blob/master/code-of-conduct.md
[rubberduck]: https://rubberduckdebugging.com
[xy]: https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/66378#66378
Expand Down
2 changes: 0 additions & 2 deletions docs/syntax.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import Redirect from 'gatsby-theme-mdx/src/components/redirect'

<Redirect to="/getting-started#syntax" />
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@
"docs-build": "gatsby build",
"docs-deploy": "now && now alias $(pbpaste) mdxjs.com && now alias $(pbpaste) www.mdxjs.com",
"format": "yarn lint --fix",
"lint": "eslint . --cache --ext js,jsx,md,mdx -f friendly",
"lint": "eslint . --cache --ext js,jsx -f friendly",
"now-build": "yarn build && yarn docs-build",
"postinstall": "yarn-deduplicate || exit 0",
"prepublishOnly": "yarn build",
"pretest": "yarn build",
"publish": "lerna publish --force-publish=\"*\"",
"publish-ci": "lerna publish -y --canary --preid ci --pre-dist-tag ci",
"publish-next": "lerna publish --force-publish=\"*\" --dist-tag next --preid rc",
"test": "jest --runInBand --detectOpenHandles"
"test": "jest --runInBand --detectOpenHandles && nyc --reporter lcov tape packages/remark-mdx/test/index.js"
},
"devDependencies": {
"@babel/core": "7.9.6",
"@babel/plugin-syntax-jsx": "7.8.3",
"@babel/preset-env": "7.9.6",
"@babel/preset-react": "7.9.4",
"@mapbox/rehype-prism": "0.4.0",
"@mdx-js/mdx": "^1.6.2",
"@mdx-js/react": "^1.6.2",
"@mdx-js/test-util": "^1.6.2",
"@mdx-js/vue": "^1.6.2",
"@mdx-js/mdx": "^1.6.3",
"@mdx-js/react": "^1.6.3",
"@mdx-js/test-util": "^1.6.3",
"@mdx-js/vue": "^1.6.3",
"@pkgr/rollup": "0.10.3",
"@vue/babel-preset-jsx": "1.1.2",
"@vue/test-utils": "1.0.3",
Expand Down Expand Up @@ -73,8 +73,9 @@
"rehype-katex": "3.0.0",
"remark-autolink-headings": "6.0.0",
"remark-math": "2.0.1",
"remark-mdx": "^1.6.2",
"remark-parse": "8.0.2",
"remark-mdx": "1.6.3",
"remark-mdxjs": "1.6.3",
"remark-parse": "8.0.1",
"remark-preset-prettier": "0.4.0",
"remark-preset-wooorm": "7.0.0",
"remark-slug": "6.0.0",
Expand Down Expand Up @@ -103,7 +104,7 @@
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
"#pre-commit": "lint-staged"
}
},
"jest": {
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-theme-mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
"react-helmet": "6.0.0",
"react-live": "2.2.2",
"remark-emoji": "2.1.0",
"remark-mdx-remove-exports": "^1.6.3",
"remark-mdx-remove-imports": "^1.6.3",
"remark-mdxjs-remove-exports": "^1.6.3",
"remark-mdxjs-remove-imports": "^1.6.3",
"remark-slug": "6.0.0",
"theme-ui": "0.3.1"
}
Expand Down
4 changes: 3 additions & 1 deletion packages/gatsby-theme-mdx/src/components/mdx-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react'
import CodePreview from './code-preview'
import CodeBlock from './code-block'
import Link from './link'
import Redirect from './redirect'

const heading = Tag => props => (
<Tag {...props}>
Expand Down Expand Up @@ -35,5 +36,6 @@ export default {
h3: heading('h3'),
h4: heading('h4'),
h5: heading('h5'),
h6: heading('h6')
h6: heading('h6'),
Redirect
}
4 changes: 2 additions & 2 deletions packages/gatsby-theme-mdx/src/components/playground-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {MDXProvider, mdx as createElement} from '@mdx-js/react'
import * as Rebass from '@rebass/emotion'
import {ThemeContext} from '@emotion/core'
import {css} from 'theme-ui'
import removeImports from 'remark-mdx-remove-imports'
import removeExports from 'remark-mdx-remove-exports'
import removeImports from 'remark-mdxjs-remove-imports'
import removeExports from 'remark-mdxjs-remove-exports'

import CodeBlock from './code-block'

Expand Down
4 changes: 2 additions & 2 deletions packages/mdx/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const unified = require('unified')
const toMDAST = require('remark-parse')
const remarkMdx = require('remark-mdx')
const remarkMdxJs = require('remark-mdxjs')
const footnotes = require('remark-footnotes')
const squeeze = require('remark-squeeze-paragraphs')
const visit = require('unist-util-visit')
const raw = require('hast-util-raw')
const toMDXAST = require('./md-ast-to-mdx-ast')
const mdxAstToMdxHast = require('./mdx-ast-to-mdx-hast')
const mdxHastToJsx = require('./mdx-hast-to-jsx')

Expand All @@ -30,9 +30,9 @@ function createMdxAstCompiler(options) {
const fn = unified()
.use(toMDAST, options)
.use(remarkMdx, options)
.use(remarkMdxJs, options)
.use(footnotes, options)
.use(squeeze, options)
.use(toMDXAST, options)

plugins.forEach(plugin => {
// Handle [plugin, pluginOptions] syntax
Expand Down
13 changes: 0 additions & 13 deletions packages/mdx/md-ast-to-mdx-ast.js

This file was deleted.

21 changes: 14 additions & 7 deletions packages/mdx/mdx-ast-to-mdx-hast.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const toHAST = require('mdast-util-to-hast')
const all = require('mdast-util-to-hast/lib/all')
const detab = require('detab')
const u = require('unist-builder')

Expand Down Expand Up @@ -61,32 +62,38 @@ function mdxAstToMdxHast() {
h(node, 'code', props, [u('text', value)])
])
},
// To do: rename to `mdxJsImport`
import(h, node) {
return Object.assign({}, node, {
type: 'import'
})
},
// To do: rename to `mdxJsExport`
export(h, node) {
return Object.assign({}, node, {
type: 'export'
})
},
comment(h, node) {
mdxBlockElement(h, node) {
return Object.assign({}, node, {children: all(h, node)})
},
mdxSpanElement(h, node) {
return Object.assign({}, node, {children: all(h, node)})
},
mdxBlockExpression(h, node) {
return Object.assign({}, node, {
type: 'comment'
type: 'mdxBlockExpression'
})
},
jsx(h, node) {
mdxSpanExpression(h, node) {
return Object.assign({}, node, {
type: 'jsx'
type: 'mdxSpanExpression'
})
}
}

const hast = toHAST(tree, {
handlers,
// Enable passing of HTML nodes to HAST as raw nodes
allowDangerousHtml: true
handlers
})

return hast
Expand Down
Loading