Skip to content

Instantly share code, notes, and snippets.

@ryanto
Last active January 7, 2022 02:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanto/832f5e48a227776128166f428e84430a to your computer and use it in GitHub Desktop.
Save ryanto/832f5e48a227776128166f428e84430a to your computer and use it in GitHub Desktop.
module.exports.addDefaultImport = function addImport(tree, mod, name) {
tree.children.unshift({
type: 'import',
value: `import _${name} from '${mod}'`,
});
return `_${name}`;
};
import { visit } from 'unist-util-visit';
import { addDefaultImport } from './helpers.js';
import sizeOf from 'image-size';
let nextPublic = `${process.cwd()}/public/`;
export const images = () => {
return tree => {
let Image = addDefaultImport(tree, 'next/image', 'Image');
visit(tree, 'image', node => {
if (node.url.startsWith('/')) {
let { height, width } = sizeOf(`${nextPublic}/${node.url}`);
node.type = 'jsx';
node.value = `<${Image}
src="${node.url}"
alt="${node.alt}"
height={${height}}
width={${width}}
/>`;
}
});
};
};
import nextMdx from '@next/mdx';
import { images } from './src/remark/images.mjs';
let withMDX = nextMdx({
options: {
remarkPlugins: [images],
},
});
export default withMDX({
reactStrictMode: true,
pageExtensions: ['js', 'jsx', 'mdx'],
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment