-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Adding Frontmatter Reader: Support summary card (OG. Twitter) #2321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sspaeti
wants to merge
10
commits into
rust-lang:master
Choose a base branch
from
sspaeti:adding_frontmatter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b3c603b
Add feature-gated frontmatter module for OG meta tags
sspaeti c749ea4
add Makefile back
sspaeti c3e8b00
update serde_yml
sspaeti 89ac94b
remove personal makefile
sspaeti 916cb56
add created date and last updated date to front matter
sspaeti 0a193ef
Revert "add created date and last updated date to front matter"
sspaeti e944348
adding open Graph meta tags to templage index file
sspaeti 5153b31
Merge branch 'master' into adding_frontmatter
sspaeti 42273c4
merge master
sspaeti 446612a
fix: test error with property="og:description"
sspaeti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| //! Frontmatter parsing support for mdBook. | ||
| //! | ||
| //! Extracts YAML frontmatter from markdown content and injects | ||
| //! Open Graph / Twitter Card metadata into the Handlebars template context. | ||
|
|
||
| use serde::Deserialize; | ||
| use serde_json::json; | ||
|
|
||
| /// Parsed YAML frontmatter fields. | ||
| #[derive(Deserialize, Debug)] | ||
| pub(crate) struct FrontMatter { | ||
| /// Page title for OG/Twitter meta tags. | ||
| pub title: String, | ||
| /// Page description for OG/Twitter meta tags. | ||
| pub description: String, | ||
| /// Featured image URL for OG/Twitter meta tags. | ||
| pub featured_image_url: String, | ||
| } | ||
|
|
||
| /// Strips YAML frontmatter (between `---` markers) from content, | ||
| /// returning the content without the frontmatter block. | ||
| pub(crate) fn strip_frontmatter(content: &str) -> String { | ||
| let trimmed = content.trim_start(); | ||
| if !trimmed.starts_with("---") { | ||
| return content.to_string(); | ||
| } | ||
| // Find the closing `---` after the opening one | ||
| let after_open = &trimmed[3..]; | ||
| if let Some(end) = after_open.find("\n---") { | ||
| // Skip past the closing `---` and any trailing newline | ||
| let rest = &after_open[end + 4..]; | ||
| rest.trim_start_matches('\n').to_string() | ||
| } else { | ||
| content.to_string() | ||
| } | ||
| } | ||
|
|
||
| /// Parses YAML frontmatter from content and injects OG metadata | ||
| /// into the Handlebars template context data map. | ||
| pub(crate) fn inject_frontmatter_data( | ||
| content: &str, | ||
| data: &mut serde_json::Map<String, serde_json::Value>, | ||
| ) { | ||
| let trimmed = content.trim_start(); | ||
| if !trimmed.starts_with("---") { | ||
| return; | ||
| } | ||
| let after_open = &trimmed[3..]; | ||
| let Some(end) = after_open.find("\n---") else { | ||
| return; | ||
| }; | ||
| let yaml_str = &after_open[..end]; | ||
|
|
||
| match serde_yml::from_str::<FrontMatter>(yaml_str) { | ||
| Ok(fm) => { | ||
| data.insert("is_frontmatter".to_owned(), json!(true)); | ||
| data.insert("og_title".to_owned(), json!(fm.title)); | ||
| data.insert("og_description".to_owned(), json!(fm.description)); | ||
| data.insert("og_image_url".to_owned(), json!(fm.featured_image_url)); | ||
| } | ||
| Err(e) => { | ||
| eprintln!("Frontmatter: deserialization error: {e:?}"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_strip_frontmatter() { | ||
| let input = "---\ntitle: \"Hello\"\n---\n# Content"; | ||
| assert_eq!(strip_frontmatter(input), "# Content"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_strip_no_frontmatter() { | ||
| let input = "# Just content"; | ||
| assert_eq!(strip_frontmatter(input), "# Just content"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_inject_frontmatter_data() { | ||
| let input = "---\ntitle: \"My Title\"\ndescription: \"My Desc\"\nfeatured_image_url: \"https://example.com/img.png\"\n---\n# Content"; | ||
| let mut data = serde_json::Map::new(); | ||
| inject_frontmatter_data(input, &mut data); | ||
| assert_eq!(data["is_frontmatter"], json!(true)); | ||
| assert_eq!(data["og_title"], json!("My Title")); | ||
| assert_eq!(data["og_description"], json!("My Desc")); | ||
| assert_eq!(data["og_image_url"], json!("https://example.com/img.png")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_inject_no_frontmatter() { | ||
| let input = "# Just content"; | ||
| let mut data = serde_json::Map::new(); | ||
| inject_frontmatter_data(input, &mut data); | ||
| assert!(!data.contains_key("is_frontmatter")); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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