Skip to content

Adding Frontmatter Reader: Support summary card (OG. Twitter)#2321

Open
sspaeti wants to merge 10 commits into
rust-lang:masterfrom
sspaeti:adding_frontmatter
Open

Adding Frontmatter Reader: Support summary card (OG. Twitter)#2321
sspaeti wants to merge 10 commits into
rust-lang:masterfrom
sspaeti:adding_frontmatter

Conversation

@sspaeti

@sspaeti sspaeti commented Feb 20, 2024

Copy link
Copy Markdown
Contributor

See issues #1416 and specific description in my comment: #1416 (comment).

This works for my site dedp.online, but I'd appreciate some help to integrate it nicely. It's more hackable and hard-coded to these frontmatter I used:

struct FrontMatter {
    title: String,
    description: String,
    featured_image_url: String,
}

Would be nice to read them dynamically. Also, I'm not sure if I added it at the right points.

PS: I also tried to implement a preprocessor (see #1416 (comment)), but with this PR that one is not needed anymore. Also, I guess this should be integrated into mdBook, and can't be as Preprocessor only?

@szabgab

szabgab commented Apr 3, 2025

Copy link
Copy Markdown
Contributor

Hi, would you have time to rebase this to make it easier to review it, please?

Comment thread Cargo.toml Outdated
toml = "0.5.11" # Do not update, see https://github.com/rust-lang/mdBook/issues/2037
topological-sort = "0.2.2"
gray_matter = "0.2" #Fronmatter
serde_yaml = "0.9"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

serde_yaml was deprecated recently, please use serde_yml

use serde::Deserialize;

fn extract_frontmatter(content: &str) -> Option<&str> {
let start = content.find("---")? + 3; // Find the end of the first `---` + 3 to move past it

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that --- can appear anywhere in a regular markdown content, I'd restrict this to the case when the file starts with ---.

@rustbot

This comment has been minimized.

sspaeti and others added 3 commits February 12, 2026 20:38
Adds a `frontmatter` feature flag that parses YAML frontmatter from
chapter content and injects OG/Twitter meta tag data into the
Handlebars template context. All custom code is isolated in a single
module (frontmatter.rs) with 3 one-liner integration points behind
#[cfg(feature = "frontmatter")] for easy future upgrades.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@rustbot

This comment has been minimized.

@sspaeti

sspaeti commented Feb 12, 2026

Copy link
Copy Markdown
Contributor Author

@szabgab I finally updated my book, and therefore also rebased this branch.

The pre-processor https://github.com/sspaeti/mdbook-frontmatter-reader is no longer needed and has been integrated directly into frontmatter.rs (see changes).

Again, in my chapters in my mdbook, I can now add this in the frontmatter

---
title: "Business Intelligence, Semantic Layer, Modern OLAP, Data Virtualization"
description: "The history of semantic SQL"
featured_image_url: "https://www.dedp.online/images/features/ce-semantic-sql.png"
---

and that gets rendered in the HTML header:
image

FYI, if anyone is doing similar things - I added these hard-coded to my index.hbs as these do not change:

  Meta tag: <meta name="author" content="Simon Späti">
  Meta tag: <meta property="og:type" content="book">
  Meta tag: <meta name="twitter:card" content="summary_large_image"/>
  Meta tag: <meta name="twitter:creator" content="@sspaeti"/>

You can check the outcome in my book live at https://dedp.online/part-2/4-ce/semantic-layer-business-intelligence.html.

I hope that works, not sure if I have thought about everything, as this is my first mdBook submission, but it works great for my book for a long time :)

Update: You also need to update index.hbs with something like - see initial comment at #1416 (comment):

<head>
...
        {{#if is_frontmatter }}
        <meta property="og:title" content="{{ og_title }}">
        <meta property="og:type" content="book">
        <meta property="og:image" content="{{ og_image_url }}">
        <meta property="og:url" content="{{ base_url }}">
        <meta property="og:description" content="{{ og_description }}">
        <meta name="author" content="{{ author }}">
		..dynamically read what's defined in frontmatter?..
        {{else}}
        <!-- Add static Open Graph Meta Tags for Social Media Exposure -->
        <meta property="og:title" content="Data Engineering Design Patterns: Mastering Convergent Evolution">
        <meta property="og:type" content="book">
        <meta property="og:image" content="https://www.dedp.online/static/og-image.png">
        <meta property="og:url" content="https://www.dedp.online">
        <meta property="og:description" content="A data engineering book to understanding design patterns, exploring the concept of convergent evolution and its significance.">
        <meta name="author" content="Simon Späti">
        {{/if}}
...
</head>

@sspaeti sspaeti requested a review from szabgab February 12, 2026 21:44
@rustbot

This comment has been minimized.

@ObserverOfTime

ObserverOfTime commented May 30, 2026

Copy link
Copy Markdown

I tried this PR but don't see any tags in the generated HTML. Do I have to modify index.hbs in some way?

Also, serde_yml is unsound and unmaintained and should be replaced.

@sspaeti

sspaeti commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

I tried this PR but don't see any tags in the generated HTML. Do I have to modify index.hbs in some way?

Also, serde_yml is unsound and unmaintained and should be replaced.

Yes, you need to add it to index.hbs see initial comment #1416 (comment), or what I have at this moment:

        <!-- Open Graph Meta Tags for Social Media Exposure -->
        {{#if is_frontmatter}}
        <meta name="title" property="og:title" content="{{ og_title }}">
        <meta name="description" property="og:description" content="{{ og_description }}">
        <meta name="twitter:title" content="{{ og_title }}"/>
        <meta name="twitter:description" content="{{ og_description }}"/>
        {{else}}
        <meta name="title" property="og:title" content="Data Engineering Design Patterns: Timeless Practices for Data Engineers">
        <meta name="description" property="og:description" content="A data engineering book to understanding design patterns, exploring the concept of convergent evolution and its significance.">
        <meta name="twitter:title" content="Data Engineering Design Patterns: Timeless Practices for Data Engineers"/>
        <meta name="twitter:description" content="A data engineering book to understanding design patterns, exploring the concept of convergent evolution and its significance."/>
        {{/if}}
        {{#if og_image_url}}
        <meta property="og:image" content="{{ og_image_url }}">
        <meta name="twitter:image" content="{{ og_image_url }}"/>
        {{else}}
        <meta property="og:image" content="https://www.dedp.online/static/og-image.png">
        <meta name="twitter:image" content="https://www.dedp.online/static/og-image.png"/>
        {{/if}}

        <meta name="author" content="Simon Späti">
        <meta property="og:type" content="book">
        <meta name="twitter:card" content="summary_large_image"/>
        <meta name="twitter:creator" content="@sspaeti"/>

@ObserverOfTime

Copy link
Copy Markdown

Shouldn't that be part of the PR? :)

@sspaeti

sspaeti commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Shouldn't that be part of the PR? :)

You are probably right! I just added it e944348 (not tested with plain mdBook yet, but should work, hopefully) - I didn't add it initially because I thought index.rbs is to be customized by each book, but I saw there's a template. I think the above comment would make sense.

@rustbot

rustbot commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

<meta property="og:image" content="{{ og_image_url }}">
<meta name="twitter:image" content="{{ og_image_url }}"/>
{{else}}
<meta property="og:image" content="https://avatars.githubusercontent.com/u/5430905?s=48&v=4">

@sspaeti sspaeti Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note I added the Rust logo from mdBook GitHub icon - please add or link a mdBook icon that you want to be shown on social media, if there's one. This is the image that will be shown when sharing online. As well as the line below twitter:image.

View changes since the review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: waiting on a review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants