Auto-Embedding GitHub Gists in Contentful Rich Text with Lazy-Loaded PrismJS
Paste a Gist link on its own line in Contentful and get syntax-highlighted code—without shipping a syntax highlighter to posts that don't need one.

If you've ever written technical articles in Contentful's Rich Text editor, you've probably hit the same speed bump: there's no great native multi-line syntax-highlighted code block. 🧩
You can build custom embedded entry types for code snippets, or paste raw HTML—but both feel clunky when all you want is to share a clean, versioned snippet.
My workaround on reggiepangilinan.com is simple: paste a GitHub Gist URL on its own line in Contentful, and let RichTextRenderer turn it into an interactive, syntax-highlighted GitHubGist block automatically.
🔍 1. Intercepting Standalone Gist URLs at the Paragraph Level
In src/components/RichTextRenderer/RichTextRenderer.tsx, the custom BLOCKS.PARAGRAPH renderer checks whether a paragraph contains only a single Gist link or Gist script snippet.
parseGitHubGistUrl() handles three common paste formats:
Standard Gist URLs (
https://gist.github.com/username/gistId, with optional revision or?file=...).jsembed URLs (https://gist.github.com/username/gistId.js)Full
<script src="https://gist.github.com/..."></script>tags
Handling this at the paragraph level (instead of inside INLINES.HYPERLINK) avoids invalid HTML like nesting a <div> inside a <p>—while still letting normal inline links to Gists inside a sentence render as regular hyperlinks!

🪶 2. Lazy-Loading PrismJS (One Language Grammar at a Time)
Syntax highlighters can quietly bloat a JavaScript bundle if you import every language up front.
Inside src/components/GitHubGist/GitHubGist.tsx, two choices keep the client bundle lean:
Dynamic PrismJS import:
prismjsis only loaded viaawait import('prismjs')on the client when aGitHubGistcomponent actually mounts. Readers visiting a non-code post download zero syntax-highlighter JavaScript.Per-language grammar imports: Once the Gist metadata comes back from GitHub's API,
highlightCode()maps the file's language (TypeScript,Python,C#,Shell, etc.) and dynamically imports only that specificprismjs/components/prism-*grammar (plus its dependency, likeprism-javascriptbeforeprism-typescript).
🛡️ 3. Security Headers & CSP Considerations
Because GitHubGist fetches Gist JSON from GitHub's public API and displays the Gist author's avatar, src/proxy.ts explicitly allows:
https://api.github.cominconnect-srchttps://avatars.githubusercontent.cominimg-src
That keeps the site's Content Security Policy tight while letting Gist embeds load cleanly without console errors.
✨ Wrapping Up
Intercepting Gist URLs inside RichTextRenderer turned a CMS limitation into a super pleasant authoring experience—and dynamically importing PrismJS per language keeps performance sharp across the rest of the blog.
You can browse both components in RichTextRenderer.tsx and GitHubGist.tsx on GitHub.
Related Posts
How My Related Posts Component Works (Zero Manual Maintenance, Driven by Tags)
A breakdown of how RelatedPosts picks the top 3 related articles using case-insensitive tag overlap, recency tie-breaking, parallel fetching, and 24-hour ISR caching.
Adding AI-Powered Blog Search with Google Gemini in Next.js
How we added an intelligent, Gemini-powered search bar to a Next.js + Contentful blog—covering AI basics for non-specialists, why hybrid search beat Google Search Grounding and Vertex AI Search, real-world free-tier traffic estimates, and built-in cost guardrails.
Running a Full Next.js Blog on Free Tiers: A Contentful + Netlify Case Study
A data-backed case study breaking down Contentful Free, Netlify Free, and GitHub Free quotas (at the time of this writing), with real math on how many visitors, blog posts, and images a personal site can serve before ever paying a cent for infrastructure.