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

Output images are corrupted #186

Open
tconroy opened this issue Aug 12, 2022 · 1 comment
Open

Output images are corrupted #186

tconroy opened this issue Aug 12, 2022 · 1 comment

Comments

@tconroy
Copy link

tconroy commented Aug 12, 2022

mdx-bundler version: 9.0.1
node version: v17.8.0
npm version: 8.5.5

I am using mdx-bundler + remark-mdx-images to output images with mdx blog posts.

I am running into an issue where the input image is valid, but the output image appears to be corrupt (i.e is not visible in browser, and I get this error message from macos Preview when trying to open it):

Screen Capture 2022-08-12 at 13 14 34@2x

MDX file:

![](./hello-world.png 'Hello World')
const { code, frontmatter } = await bundleMDX({
  source: indexFile.content,
  files: filesObject,
  mdxOptions: options => ({
    remarkPlugins: [
      ...(options.remarkPlugins ?? []),
      remarkSlug,
      remarkMdxImages,
      remarkGfm,
    ],
    rehypePlugins: [
      ...(options.rehypePlugins ?? []),
      [rehypeAutolinkHeadings, { behavior: 'wrap' }],
      [rehypePrettyCode, rehypePrettyCodeOptions],
    ],
  }),
  esbuildOptions: options => {
    options.loader = {
      ...options.loader,
      '.gif': 'file',
      '.jpeg': 'file',
      '.jpg': 'file',
      '.png': 'file',
      '.svg': 'file',
      '.webp': 'file',
    }

    const basePath = path.resolve(`/build/_assets/${rootDir}/img`)
    options.outdir = path.resolve(`public/${basePath}`)
    options.publicPath = basePath

    // Set write to true so that esbuild will output the files.
    options.write = true

    return options
  },
})

Correctly copied to output dir:
Screen Capture 2022-08-12 at 13 16 50@2x

Trying to view it in browser (returns 200 but file isn't visible):
Screen Capture 2022-08-12 at 13 18 34@2x

Visiting the file URL directly:
Screen Capture 2022-08-12 at 13 19 54@2x

@phongsakornp
Copy link

phongsakornp commented Dec 2, 2022

I had the same problem. I fix it by defining the current working directory (cwd).

const { code, frontmatter } = await bundleMDX({
      cwd: `/path/to/site/content`,
      ..
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants