Skip to main content
Reggie Pangilinan
HomeBlogAbout
GitHubLinkedIn

© 2026 reggiepangilinan.com All rights reserved.

GitHubLinkedIn
Auto-Embedding GitHub Gists in Contentful Rich Text with…
←Back to Blog

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.

December 2, 2026·Reggie Pangilinan·2 min read
Auto-Embedding GitHub Gists in Contentful Rich Text with Lazy-Loaded PrismJS

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=...)

  • .js embed 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!

Auto-Embedding GitHub Gists with Lazy PrismJS
Auto-Embedding GitHub Gists with Lazy PrismJS

🪶 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: prismjs is only loaded via await import('prismjs') on the client when a GitHubGist component 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 specific prismjs/components/prism-* grammar (plus its dependency, like prism-javascript before prism-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.com in connect-src

  • https://avatars.githubusercontent.com in img-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.

Keep Reading

Related Posts

December 2, 2026

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.

Next.jsContentful
December 3, 2026

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.

Next.jsTypeScript
December 3, 2026

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.

Next.jsContentful
Topics
Next.jsContentfulReactPerformanceTypeScript

Share this article

Found this helpful? Copy the link or scan the QR code to share it.

←All blog posts