Last active
December 24, 2023 05:06
-
-
Save solace/eefab1e4ff24ddef86542c94678fbb51 to your computer and use it in GitHub Desktop.
Replace gist links in markdown with an embed using remark in Next.js
This file contains 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
const GistTransformer = { | |
name: 'Gist', | |
shouldTransform(url) { | |
const {host} = new URL(url); | |
return ['gist.github.com'].includes(host); | |
}, | |
getHTML(link) { | |
const url = new URL(link); | |
const id = url.pathname.split('/').pop(); | |
return `<gist id="${id}" />`; | |
}, | |
}; | |
export default GistTransformer; |
This file contains 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 remarkEmbedder from '@remark-embedder/core'; | |
import {MDXRemote} from 'next-mdx-remote'; | |
import {serialize} from 'next-mdx-remote/serialize'; | |
import Gist from 'react-gist'; | |
import GistTransformer from '../lib/GistTransformer'; | |
const components = (path) => ({ | |
gist: Gist, | |
}); | |
function Post({ record }) { | |
return ( | |
<MDXRemote {...record.mdx} components={components(record.frontmatter.path)} lazy /> | |
); | |
} | |
export const getStaticProps = async ({params}) => { | |
// your imported markdown object | |
record.mdx = await serialize(record.content, { | |
scope: record.frontmatter, | |
mdxOptions: { | |
remarkPlugins: [ | |
[remarkEmbedder, { transformers: [GistTransformer] }] | |
], | |
rehypePlugins: [], | |
}, | |
}); | |
return { | |
props: { | |
record, | |
}, | |
}; | |
}; | |
export default Post; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment