feat: add Open Graph and Twitter Card meta tags#36
Conversation
- Add [site] og_image config option for default social preview image - Add per-page image: frontmatter field to override the site default - Wire OGImage through Site and Image through Page/frontmatter/walker - Add OGURL and OGImage to PageData, computed in renderPage - Render OG and Twitter Card meta tags in layout.html <head> - Only render when base_url is set (og:url requires absolute URLs) - Add tests for ogURL and ogImage helper functions Closes engineervix#11
There was a problem hiding this comment.
Pull request overview
Adds Open Graph and Twitter Card metadata generation to the site builder so rendered pages include correct social sharing previews, with site-level and per-page image configuration.
Changes:
- Introduces
[site] og_imageconfig plus per-pageimage:frontmatter override. - Computes
og:urland resolvedog:imageduring page rendering and passes them into templates. - Renders OG/Twitter
<meta>tags intemplates/layout.htmlwhenbase_urlis configured, with test coverage for URL/image selection logic.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| templates/layout.html | Conditionally renders OG/Twitter meta tags in <head> based on computed OGURL/OGImage. |
| internal/nav/walker.go | Propagates image: frontmatter into Page.Image. |
| internal/nav/types.go | Adds Site.OGImage and Page.Image fields for social preview images. |
| internal/nav/nav.go | Wires cfg.Site.OGImage into nav.Site. |
| internal/nav/frontmatter.go | Parses per-page image: frontmatter key. |
| internal/config/config.go | Adds [site] og_image config option. |
| internal/builder/og_test.go | Adds tests for og:url and og:image selection behavior. |
| internal/builder/builder.go | Computes OGURL/OGImage during render and exposes them to templates. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- ogURL now uses hasScheme() so a relative base_url ('/kwelea') returns
empty instead of producing invalid og:url like '/kwelea/getting-started/'
- ogImage resolves relative image paths ('/assets/og.png') against
base_url when base_url is absolute; absolute image URLs pass through;
relative base_url leaves relative images as-is
- Add tests: relative base_url, scheme-only base_url, relative image
resolution (site default, page frontmatter, with/without trailing slash,
relative base_url edge case)
Addresses Copilot review feedback on PR engineervix#36
|
All three points addressed in 6574e39: 1. 2. 3. |
Summary
Generate Open Graph and Twitter Card
<meta>tags in<head>so pages render correctly when shared on social platforms.Changes
Config (
internal/config/config.go)[site] og_imageoption for a default social preview image URLFrontmatter (
internal/nav/frontmatter.go)image:field to override the site-level defaultTypes & wiring
Site.OGImage— propagated throughnav.NewSitefromconfig.SiteConfigPage.Image— populated from frontmatter inwalker.goPageData.OGURLandPageData.OGImage— computed inrenderPageand passed to templatesTemplate (
templates/layout.html)Render the following tags in
<head>, only whenbase_urlis set:Tests (
internal/builder/og_test.go)TestOGURL— 4 sub-tests: https base_url, trailing slash, root page, no base_urlTestOGImage— 4 sub-tests: page override, site fallback, neither set, page-onlyBehaviour
base_urlnot setbase_urlset, noog_imageog:imageomittedbase_urlset,[site] og_imageconfiguredog:imageimage:in frontmatterog_imagefor that pageVerification
Closes #11