Skip to content

Instantly share code, notes, and snippets.

@solace
Last active December 24, 2023 05:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solace/eefab1e4ff24ddef86542c94678fbb51 to your computer and use it in GitHub Desktop.
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
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;
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