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

fix(gatsby-remark-responsive-iframe): Support MDX by visiting JSX nodes #13913

Merged
merged 2 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion packages/gatsby-remark-responsive-iframe/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"@babel/core": "^7.0.0",
"babel-preset-gatsby-package": "^0.1.4",
"cross-env": "^5.1.4",
"remark": "^9.0.0",
"remark": "^10.0.1",
"remark-mdx": "^1.0.14",
"semver": "^6.0.0",
"unist-util-find": "^1.0.1"
},
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-remark-responsive-iframe#readme",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`gatsby-remark-responsive-iframe can copy JSX images 1`] = `
"
<div
class=\\"gatsby-resp-iframe-wrapper\\"
style=\\"padding-bottom: NaN%; position: relative; height: 0; overflow: hidden;\\"
>
<iframe url=\\"http://www.example.com/\\" style=\\"border:0;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
\\"></iframe>

</div>
"
`;

exports[`gatsby-remark-responsive-iframe doesn't transform an iframe with dimensions: '100%' '100' 1`] = `
" <iframe url=\\"http://www.example.com/\\" width=\\"100%\\" height=\\"100\\"></iframe>
"
Expand Down
26 changes: 26 additions & 0 deletions packages/gatsby-remark-responsive-iframe/src/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
const Remark = require(`remark`)
const find = require(`unist-util-find`)
const _ = require(`lodash`)
const semver = require(`semver`)

const plugin = require(`../`)

const testInNode8OrHigher = (title, ...args) => {
const isNode8OrHigher = semver.satisfies(process.version, `>=8`)
if (isNode8OrHigher) {
it(title, ...args)
} else {
it.skip(`skipped on Node 7 or lower: ${title}`, ...args)
}
}

const remark = new Remark().data(`settings`, {
commonmark: true,
footnotes: true,
Expand Down Expand Up @@ -73,6 +83,22 @@ describe(`gatsby-remark-responsive-iframe`, () => {
})
})

testInNode8OrHigher(`can copy JSX images`, async () => {
const mdx = require(`remark-mdx`)

const markdownAST = remark().use(mdx).parse(`
<iframe url="http://www.example.com/" style="border:0;" width="600px" height="400px"></iframe>
`)

const transformed = await plugin({ markdownAST })
const node = find(transformed, function(node) {
return node.type === `html`
})

expect(node).toBeDefined()
expect(node.value).toMatchSnapshot()
Copy link
Contributor

@pieh pieh May 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yikes, it seems like snapshot assertion doesn't work well with testInNode8OrHigher (tests fail with 1 snapshot obsolete in node 6)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll update the test to not use a snapshot 👍

})

const shouldntTransform = [
[`100%`, `100`],
[`100`, `100%`],
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-remark-responsive-iframe/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = ({ markdownAST }, pluginOptions = {}) =>
wrapperStyle: ``,
}
const options = _.defaults(pluginOptions, defaults)
visit(markdownAST, `html`, node => {
visit(markdownAST, [`html`, `jsx`], node => {
const $ = cheerio.load(node.value)
const iframe = $(`iframe, object`)
if (iframe.length) {
Expand Down