andrewbranch / gatsby-remark-vscode

Gatsby plugin to provide VS Code’s syntax highlighting to Markdown code fences
MIT License
294 stars 27 forks source link

highlighting doesn't working with custom MDXProvider, MDXRenderer #141

Closed klaskow closed 3 years ago

klaskow commented 3 years ago

Hi, i wanted to add excerpt to post listing by adding div wrapper to my mdx files.

Screenshot_0039

So I created custom MDXProvider and MDXRenderer like this:

<MDXProvider
  components={{
    wrapper: ({ onlyExcerpt = false, children }) => {
      let updatedChildren = [...children]

      if (onlyExcerpt) {
        updatedChildren = children.filter((child, _) => {
          return child.props && child.props["data-excerpt"]
        })
      }

      return <>{updatedChildren}</>
    },
  }}
>
  <MDXRenderer onlyExcerpt={true}>{body}</MDXRenderer>
</MDXProvider>

It kinda works because I get my excerpt with code, but without highlighting. But if i remove my onlyExcerpt={true} highlightings works fine, but the whole post is displayed (not only excerpt which i wanted). It works too if I duplicate custom rerenderer with the default one.

What is wrong with below condition that my highlighting doesn't work?

if (onlyExcerpt) {
  updatedChildren = children.filter((child, _) => {
    return child.props && child.props["data-excerpt"]
  })
}
andrewbranch commented 3 years ago

Can you post your gatsby-config.js?

klaskow commented 3 years ago

Hi, sure. This is my gatsby-config.js

module.exports = {
  siteMetadata: {
    // ...
  },
  plugins: [
    `gatsby-transformer-sharp`,
    `gatsby-plugin-react-helmet`,
    `gatsby-plugin-sharp`,
    {
      resolve: `gatsby-plugin-emotion`,
      options: {
        sourceMap: true,
        autoLabel: "dev-only",
        labelFormat: `[local]`,
        cssPropOptimization: true,
      },
    },
    {
      resolve: `gatsby-plugin-mdx`,
      options: {
        extensions: [`.mdx`, `.md`],
        defaultLayouts: {
          default: require.resolve("./src/components/layout.tsx"),
        },
        gatsbyRemarkPlugins: [
          {
            resolve: `gatsby-remark-images`,
            options: {
              maxWidth: 990,
            },
          },
          { resolve: `gatsby-remark-gifs` },
          {
            resolve: `gatsby-remark-vscode`,
            options: {
              theme: "Monokai",
            },
          },
        ],
      },
    },
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `posts`,
        path: `${__dirname}/content/posts/publish`,
      },
    },
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `images`,
        path: `${__dirname}/images`,
      },
    },
  ],
}
klaskow commented 3 years ago

Hi, I found a solution, we should not exclude child.props["mdxType"] === "style" in return statement. Issue to close.