I am currently trying to setup a Gatsby's blog.
While looking at the doc I came across Mdx, a way to use React component in Markdown file (which is an awesome concept by the way!).
However, I quickly ran into a limitation: it seems like at the moment mdx only supports "Gatsby images". For example, if I put this in the .mdx file...

... this code will generate a Gatsby image.
This is great, but now what if I want to generate "classic" images? Some libraries like React Slick Carousel needs a simple <img>
tag.
For example, the following .mdx code won't work:
# Introduction
import Slider from "react-slick";
export const settings = {
focusOnSelect: true,
infinite: true,
slidesToShow: 3,
slidesToScroll: 1,
speed: 500
};
<Slider {...settings}>
<div>
<img src={"./salty_egg.jpg"}/>
</div>
<div>
<img src={"./salty_egg_2.jpg"}/>
</div>
</Slider>
(See the corresponding GH issue here)
The only way I found to to make it work is to use shortcodes and imports:
index.mdx
---
title: Hello World
date: "2015-05-01T22:12:03.284Z"
path: "/first-post"
---
import image1 from "./assets/slick/salty_egg.jpg";
import image2 from "./assets/slick/salty_egg_2.jpg";
<SlickCarousel
settings={{
infinite: true,
slidesToShow: 1,
slidesToScroll: 1,
speed: 500,
}}
images={[
{
src: image1,
alt: "hip",
},
{
src: image2,
alt: "hop",
},
]}
/>
layout.js
import { MDXProvider } from "@mdx-js/react";
import { Message } from "theme-ui";
import SlickCarousel from "./post/slick-carousel";
const shortcodes = {SlickCarousel };
const layoutContent = data => (
<React.Fragment>
<GlobalStyle />
<main
style={{
background: `white`,
}}
>
<MDXProvider components={shortcodes}>{children}</MDXProvider>
</main>
</React.Fragment>
post/slick-carousel.js
import React from "react";
import Slider from "react-slick";
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
const SlickCarousel = ({ settings, images }) => (
<Slider {...settings}>
{images.map((image, i) => (
<div key={i}>
<img width="350" src={`${image.src}`} alt={`${image.alt}`}></img>
</div>
))}
</Slider>
);
export default SlickCarousel;
My questions:
- This implementation kinda defeat the promise of MDX: having all the component code in the markdown. How can I improve my code?
- In post/slick-carousel.js do you see how I could generate a Gatsby image rather than just an image tag?
Thanks a lot!