Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions packages/remark-mdx-remove-imports/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const remove = require('unist-util-remove')

module.exports = _options => tree => {
return remove(tree, 'import')
}
21 changes: 21 additions & 0 deletions packages/remark-mdx-remove-imports/license
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2019 John Otander

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
43 changes: 43 additions & 0 deletions packages/remark-mdx-remove-imports/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "remark-mdx-remove-imports",
"version": "0.20.3",
"description": "Remove imports from the MDX AST",
"license": "MIT",
"keywords": [
"mdx",
"markdown",
"react",
"jsx",
"remark",
"mdxast"
],
"homepage": "https://mdxjs.com",
"repository": "mdx-js/mdx",
"bugs": "https://github.com/mdx-js/mdx/issues",
"author": "John Otander <johnotander@gmail.com> (http://johnotander.com)",
"contributors": [
"John Otander <johnotander@gmail.com> (http://johnotander.com)",
"Tim Neutkens <tim@zeit.co>",
"Matija Marohnić <matija.marohnic@gmail.com>",
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"files": [
"index.js"
],
"scripts": {
"test": "jest"
},
"jest": {
"testEnvironment": "node"
},
"devDependencies": {
"jest": "^23.6.0",
"remark-mdx": "0.20.3",
"remark-parse": "^6.0.0",
"remark-stringify": "^6.0.4",
"unified": "^7.0.0"
},
"dependencies": {
"unist-util-remove": "^1.0.1"
}
}
100 changes: 100 additions & 0 deletions packages/remark-mdx-remove-imports/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# [remark][]-[mdx][]-remove-imports

[![Build Status][build-badge]][build]
[![lerna][lerna-badge]][lerna]
[![Join the community on Spectrum][spectrum-badge]][spectrum]

Remove import nodes from the [MDXAST][]. This is useful for scenarios where the imports aren’t needed like an MDX playground.

## Installation

[npm][]:

```shell
npm install --save remark-mdx-remove-imports
```

## Usage

Say we have the following MDX file, `example.mdx`:

```markdown
import { Donut } from 'rebass'

import OtherThing from 'other-place'

export default props => <div {...props} />

# Hello, world!

This is a paragraph
```

And our script, `example.js`, looks as follows:

```javascript
const vfile = require('to-vfile')
const remark = require('remark')
const mdx = require('remark-mdx')
const removeImports = require('remark-mdx-remove-imports')

remark()
.use(mdx)
.use(removeImports)
.process(vfile.readSync('example.md'), function(err, file) {
if (err) throw err
console.log(String(file))
})
```

Now, running `node example` yields:

```markdown
export default props => <div {...props} />

# Hello, world!

This is a paragraph
```

## Contribute

See [`contributing.md` in `mdx-js/mdx`][contributing] for ways to get started.

This organisation has a [Code of Conduct][coc].
By interacting with this repository, organisation, or community you agree to
abide by its terms.

## License

[MIT][] © [John Otander][johno]

<!-- Definitions -->

[build]: https://travis-ci.org/mdx-js/mdx

[build-badge]: https://travis-ci.org/mdx-js/mdx.svg?branch=master

[lerna]: https://lernajs.io/

[lerna-badge]: https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg

[spectrum]: https://spectrum.chat/mdx

[spectrum-badge]: https://withspectrum.github.io/badge/badge.svg

[contributing]: https://github.com/mdx-js/mdx/blob/master/contributing.md

[coc]: https://github.com/mdx-js/mdx/blob/master/code-of-conduct.md

[mit]: license

[remark]: https://github.com/remarkjs/remark

[johno]: https://johno.com

[npm]: https://docs.npmjs.com/cli/install

[mdx]: https://mdxjs.com

[mdxast]: https://github.com/mdx-js/specification#mdxast
14 changes: 14 additions & 0 deletions packages/remark-mdx-remove-imports/test/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`removes imports 1`] = `
"export default props => <div {...props} />

# Hello, world!

This is a paragraph

\`\`\`js
const foo = 'bar'
\`\`\`
"
`;
13 changes: 13 additions & 0 deletions packages/remark-mdx-remove-imports/test/fixture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Donut } from 'rebass'

import OtherThing from 'other-place'

export default props => <div {...props} />

# Hello, world!

This is a paragraph

```js
const foo = 'bar'
```
28 changes: 28 additions & 0 deletions packages/remark-mdx-remove-imports/test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const fs = require('fs')
const path = require('path')
const unified = require('unified')
const remarkParse = require('remark-parse')
const remarkStringify = require('remark-stringify')
const remarkMdx = require('remark-mdx')

const removeImports = require('..')

const fixture = fs.readFileSync(path.join(__dirname, './fixture.md'), 'utf8')

// TODO: Create MDX test utils
const stringify = mdx => {
const result = unified()
.use(remarkParse)
.use(remarkStringify)
.use(remarkMdx)
.use(removeImports)
.processSync(mdx)

return result.contents
}

it('removes imports', () => {
const result = stringify(fixture)

expect(result).toMatchSnapshot()
})
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12630,7 +12630,7 @@ unist-util-remove-position@^1.0.0:
dependencies:
unist-util-visit "^1.1.0"

unist-util-remove@^1.0.0:
unist-util-remove@^1.0.0, unist-util-remove@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/unist-util-remove/-/unist-util-remove-1.0.1.tgz#3e967d2aeb3ee9e7f0ee8354172986fba7ff33a5"
dependencies:
Expand Down