Last active
January 7, 2022 02:30
-
-
Save ryanto/832f5e48a227776128166f428e84430a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports.addDefaultImport = function addImport(tree, mod, name) { | |
tree.children.unshift({ | |
type: 'import', | |
value: `import _${name} from '${mod}'`, | |
}); | |
return `_${name}`; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} | |
/>`; | |
} | |
}); | |
}; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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