Blog › Jekyll

Adding OpenGraph link previews in Jekyll without breaking TOC

Set up jekyll-linkpreview for Notion-style OpenGraph bookmark cards in Chirpy and override its Liquid templates to stop h2 headings from polluting the TOC.

Adding OpenGraph link previews in Jekyll without breaking TOC

Plain Markdown links give readers no context before they click, so I added jekyll-linkpreview to render Notion-style OpenGraph bookmark cards. That immediately polluted Jekyll Chirpy’s right-hand table of contents with external page titles—here is why it happened and how overriding the plugin’s Liquid templates fixed it.

Verified with Jekyll 4.x, jekyll-theme-chirpy v7.x, and jekyll-linkpreview 0.3.x.


Technical posts frequently point readers to official documentation, RFCs, or GitHub repositories. Standard Markdown syntax ([label](url)) is fast to write, but a bare line of text gives no visual cue about the destination page.

An OpenGraph bookmark card pulls og:title, og:description, og:image, and the domain at build time and renders a compact preview card—similar to Notion’s Web Bookmark block:

1
2
3
4
5
<!-- Standard Markdown link -->
[Spring Framework Reference](https://docs.spring.io)

<!-- OpenGraph bookmark card via jekyll-linkpreview -->
{% linkpreview "https://docs.spring.io" %}

jekyll-linkpreview fetches external URLs during jekyll build. That comes with two real trade-offs:

  • Slower cold builds: Every uncached URL adds an HTTP round-trip to your build time (caching .linkpreview_cache/ in GitHub Actions mitigates this).
  • Brittle against bot protection: Sites behind Cloudflare challenges or strict rate limits return 403/429 responses and fall back to a bare link. Use linkpreview for stable documentation and GitHub links, and stick to standard Markdown links for paywalled or bot-gated URLs.

Step 1: Install and register jekyll-linkpreview

During jekyll build, the plugin intercepts every linkpreview Liquid tag, fetches the target HTML, parses OpenGraph meta tags, and renders them through an HTML template.

flowchart TD
    A["linkpreview Liquid tag"] -->|"jekyll build"| B["jekyll-linkpreview plugin"]
    B -->|"HTTP GET + OG parse"| C["External website"]
    C -->|"og:title / og:image"| B
    B -->|"_includes/linkpreview.html"| D["Rendered bookmark card"]

Add the gem to Gemfile

1
2
3
4
# Gemfile
group :jekyll_plugins do
  gem 'jekyll-linkpreview', '~> 0.3.0'
end

Install the dependency:

1
bundle install

Register the plugin in _config.yml

1
2
3
# _config.yml
plugins:
  - jekyll-linkpreview

Remember that custom Jekyll plugins require deploying via GitHub Actions rather than GitHub Pages’ legacy branch builder (covered in my Chirpy starter setup guide).


The problem: bookmark cards polluting Chirpy’s TOC

Right after adding {% linkpreview "https://github.com" %} to a test post, the card rendered fine in the body—but the right-hand table of contents broke completely:

Chirpy table of contents polluted by external link titles from jekyll-linkpreview Figure 1. External page titles injected into Chirpy’s right-hand TOC because the default template used an h2 element.

Root cause in the plugin’s default markup

Inspecting the generated HTML in DevTools showed the culprit immediately:

1
2
3
4
5
6
7
8
9
10
<!-- Default DOM generated by jekyll-linkpreview -->
<div class="jekyll-linkpreview-wrapper">
  <div class="jekyll-linkpreview-body">
    <!-- Root cause: card title is wrapped in an <h2> tag -->
    <h2 class="jekyll-linkpreview-title">
      <a href="https://github.com">GitHub: Let's build from here</a>
    </h2>
    <div class="jekyll-linkpreview-description">...</div>
  </div>
</div>

Chirpy’s TOC script (tocbot) scans .content for every <h2> and <h3> element and treats them as section headings. Because jekyll-linkpreview’s built-in template renders each card title as an <h2>, adding two or three external references fills your outline with third-party site titles—and also breaks semantic heading hierarchy for screen readers and search engines.


The fix: override the plugin’s Liquid templates

jekyll-linkpreview checks your project’s _includes/ directory before falling back to its bundled templates. By adding _includes/linkpreview.html, we can replace <h2> with a neutral <div> while keeping the exact same visual styling.

Step 1: Create _includes/linkpreview.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!-- _includes/linkpreview.html -->
<div class="jekyll-linkpreview-wrapper">
  <div class="jekyll-linkpreview-wrapper-inner">
    <div class="jekyll-linkpreview-content">
      <div class="jekyll-linkpreview-body">
        <!-- Replaced <h2> with <div> so Chirpy's tocbot ignores bookmark titles -->
        <div class="jekyll-linkpreview-title">
          <a href="{{ url }}" target="_blank" rel="noopener noreferrer">{{ title }}</a>
        </div>
        {% if description %}
          <div class="jekyll-linkpreview-description">{{ description }}</div>
        {% endif %}
        <div class="jekyll-linkpreview-footer">
          <a href="{{ url }}" target="_blank" rel="noopener noreferrer">{{ domain }}</a>
        </div>
      </div>
    </div>
  </div>
  {% if image %}
    <div class="jekyll-linkpreview-image" style="background-image: url('{{ image }}');"></div>
  {% endif %}
</div>

Step 2: Style the card for light and dark mode

Chirpy exposes CSS custom properties (--main-border-color, --card-bg, --text-color, --text-muted-color) that switch automatically when the reader toggles dark mode. Create assets/css/linkpreview.css and load it through _includes/metadata-hook.html (never by overwriting Chirpy’s head.html):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* assets/css/linkpreview.css */
.jekyll-linkpreview-wrapper {
  margin: 1.5rem 0;
  border: 1px solid var(--main-border-color, #e9ecef);
  border-radius: 8px;
  overflow: hidden;
  background-color: var(--card-bg, #ffffff);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.jekyll-linkpreview-wrapper:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.jekyll-linkpreview-wrapper-inner {
  display: flex;
  justify-content: space-between;
}

.jekyll-linkpreview-title a {
  font-weight: 600;
  color: var(--text-color, #212529);
  text-decoration: none;
}

.jekyll-linkpreview-description {
  font-size: 0.875rem;
  color: var(--text-muted-color, #6c757d);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

Handling sites without OpenGraph tags (linkpreview_nog.html)

One production edge case shows up quickly once you start linking to older documentation sites or plain GitHub pages: some URLs have no og:* metadata at all. When that happens, jekyll-linkpreview renders a separate fallback template called linkpreview_nog.html—and its default version also uses an <h2> tag.

Create _includes/linkpreview_nog.html with the same <div> substitution so non-OG links never slip <h2> tags back into your TOC:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- _includes/linkpreview_nog.html -->
<div class="jekyll-linkpreview-wrapper">
  <div class="jekyll-linkpreview-wrapper-inner">
    <div class="jekyll-linkpreview-content">
      <div class="jekyll-linkpreview-body">
        <div class="jekyll-linkpreview-title">
          <a href="{{ url }}" target="_blank" rel="noopener noreferrer">{{ title }}</a>
        </div>
        <div class="jekyll-linkpreview-footer">
          <a href="{{ url }}" target="_blank" rel="noopener noreferrer">{{ domain }}</a>
        </div>
      </div>
    </div>
  </div>
</div>

Verification and live result

Rebuilding locally (bundle exec jekyll serve --livereload) confirmed that bookmark cards render cleanly in both light and dark modes while Chirpy’s right-hand TOC lists only the post’s actual section headings.

Clean OpenGraph link preview card with unaffected Chirpy table of contents Figure 2. Final bookmark card rendered via the overridden Liquid template with a clean TOC.

Here is a live example rendered directly in this post:

1
{% linkpreview "https://github.com/cmsong111/blog" %}
tech blog. Contribute to cmsong111/blog development by creating an account on GitHub.

Operational caveats and checklist

Cache .linkpreview_cache/ in CI. If your blog grows to dozens of posts with external cards, fetching every URL on every push slows down GitHub Actions and risks transient network failures. Add actions/cache for .linkpreview_cache in pages-deploy.yml.

  • Always override both templates: Fixing linkpreview.html alone leaves linkpreview_nog.html injecting <h2> tags on non-OG URLs.
  • Load custom CSS via _includes/metadata-hook.html: Keeps your stylesheet cleanly separated from Chirpy’s upstream head.html.
  • Add rel="noopener noreferrer": Prevents newly opened external tabs from accessing window.opener.

Verification checklist

  • jekyll-linkpreview added to Gemfile (:jekyll_plugins group) and _config.yml
  • Both _includes/linkpreview.html and _includes/linkpreview_nog.html use <div> instead of <h2>
  • assets/css/linkpreview.css uses Chirpy CSS variables for dark-mode compatibility
  • Right-hand TOC verified to contain only actual post headings
This post is licensed under CC BY 4.0 by the author.