feat: Improved opengraph defaults.

This commit is contained in:
Spencer Brower
2026-07-20 14:27:27 -04:00
parent 1cb13a6a55
commit 566d6f0768
7 changed files with 285 additions and 66 deletions
+6 -7
View File
@@ -6,6 +6,7 @@
- [ ] Only publish referenced assets. - [ ] Only publish referenced assets.
- [ ] Split `load_page` into frontmatter-parse + body-process phases so draft pages can skip the markdown pipeline entirely - [ ] Split `load_page` into frontmatter-parse + body-process phases so draft pages can skip the markdown pipeline entirely
- [ ] Use spall to find ways to reduce run time. - [ ] Use spall to find ways to reduce run time.
- [ ] Consider using `#soa` for Page lists.
## Memory Management ## Memory Management
@@ -28,14 +29,12 @@
- [ ] Clean up the default layouts - [ ] Clean up the default layouts
- [ ] Add `-production` flag - [ ] Add `-production` flag
- sets `-minify` - sets `-minify`
- [ ] Open Graph - [x] Open Graph
- [x] mustache data keys for opengraph, etc. - [x] mustache data keys for opengraph, etc.
- [ ] OpenGraph meta tags — verify all fields match production site - [x] OpenGraph meta tags — verify all fields match production site
- [ ] set opengraph tags / description automatically if unset. (Like hugo does) - [x] set opengraph tags / description automatically if unset. (Like hugo does)
- [ ] We can't use avatar.jpg as the default site image, that's unique to sbrow.github.io. We need to set that in the frontmatter of content/index.html. or possibly in the config - [x] We can't use avatar.jpg as the default site image.
- [ ] Add `og Open_Graph` to `Config_File` and if `Some`, use it as the base - [x] Add `og Open_Graph` to `Config_File`.
site og instead of `og_init()`?
- If we go this route, `og_init` might not be the best name.
- [ ] Author should be a struct adhering to https://schema.org/author - [ ] Author should be a struct adhering to https://schema.org/author
- [ ] Block attributes on code fences (`{ #ex-1 }`) — hello-world.md - [ ] Block attributes on code fences (`{ #ex-1 }`) — hello-world.md
- [ ] include-code shortcode (`{{< include-code ... >}}`) — i-ported-fd-to-odin - [ ] include-code shortcode (`{{< include-code ... >}}`) — i-ported-fd-to-odin
+4
View File
@@ -17,8 +17,10 @@ Page :: struct {
title: string, title: string,
description: string, description: string,
date: string, date: string,
lastmod: string,
menu: string, menu: string,
body_html: string, body_html: string,
og: Open_Graph,
draft: bool, draft: bool,
is_starred: bool, is_starred: bool,
_is_index: bool `private`, _is_index: bool `private`,
@@ -132,10 +134,12 @@ load_page :: proc(
page.title = fm.title page.title = fm.title
page.description = fm.description page.description = fm.description
page.date = fm.date page.date = fm.date
page.lastmod = fm.lastmod
page.draft = fm.draft page.draft = fm.draft
page.is_starred = fm.isStarred page.is_starred = fm.isStarred
page.menu = fm.menu page.menu = fm.menu
page.layout = fm.layout if fm.layout != "" else infer_layout(section, is_index) page.layout = fm.layout if fm.layout != "" else infer_layout(section, is_index)
page.og = fm.og
if strings.has_suffix(file_path, ".html") { if strings.has_suffix(file_path, ".html") {
page.body_html = strings.clone(body) page.body_html = strings.clone(body)
+25 -2
View File
@@ -8,11 +8,13 @@ Frontmatter :: struct {
title: string, title: string,
description: string, description: string,
date: string, date: string,
lastmod: string,
publishDate: string, publishDate: string,
draft: bool,
isStarred: bool,
menu: string, menu: string,
layout: string, layout: string,
og: Open_Graph,
draft: bool,
isStarred: bool,
} }
// parse_frontmatter splits raw file content into a Frontmatter struct and the // parse_frontmatter splits raw file content into a Frontmatter struct and the
@@ -49,11 +51,13 @@ parse_frontmatter :: proc(content: string) -> (fm: Frontmatter, body: string, ok
fm.title = json_get_string(obj, "title") fm.title = json_get_string(obj, "title")
fm.description = json_get_string(obj, "description") fm.description = json_get_string(obj, "description")
fm.date = json_get_string(obj, "date") fm.date = json_get_string(obj, "date")
fm.lastmod = json_get_string(obj, "lastmod")
fm.publishDate = json_get_string(obj, "publishDate") fm.publishDate = json_get_string(obj, "publishDate")
fm.draft = json_get_bool(obj, "draft") fm.draft = json_get_bool(obj, "draft")
fm.isStarred = json_get_bool(obj, "isStarred") fm.isStarred = json_get_bool(obj, "isStarred")
fm.menu = json_get_string(obj, "menu") fm.menu = json_get_string(obj, "menu")
fm.layout = json_get_string(obj, "layout") fm.layout = json_get_string(obj, "layout")
fm.og = json_get_open_graph(obj, "og")
ok = true ok = true
return return
@@ -77,3 +81,22 @@ json_get_bool :: proc(obj: json.Object, key: string) -> bool {
return false return false
} }
json_get_open_graph :: proc(obj: json.Object, key: string) -> Open_Graph {
og: Open_Graph
if v, ok := obj[key]; ok {
if inner, ok2 := v.(json.Object); ok2 {
og.title = json_get_string(inner, "title")
og.type = json_get_string(inner, "type")
og.image = json_get_string(inner, "image")
og.url = json_get_string(inner, "url")
og.description = json_get_string(inner, "description")
og.locale = json_get_string(inner, "locale")
og.site_name = json_get_string(inner, "site_name")
og.published_time = json_get_string(inner, "published_time")
og.modified_time = json_get_string(inner, "modified_time")
og.section = json_get_string(inner, "section")
}
}
return og
}
+137
View File
@@ -0,0 +1,137 @@
package main
import "core:strings"
strip_html_tags :: proc(s: string, allocator := context.allocator) -> string {
sb := strings.builder_make(allocator)
defer strings.builder_destroy(&sb)
in_tag := false
start := 0
for i in 0 ..< len(s) {
if s[i] == '<' && !in_tag {
if i > start {
strings.write_string(&sb, s[start:i])
}
in_tag = true
} else if s[i] == '>' && in_tag {
in_tag = false
start = i + 1
}
}
if start == 0 {
return s
}
if !in_tag && start < len(s) {
strings.write_string(&sb, s[start:])
}
return strings.to_string(sb)
}
unescape_html :: proc(s: string) -> string {
sb := strings.builder_make()
defer strings.builder_destroy(&sb)
start := 0
for i in 0 ..< len(s) {
if s[i] != '&' {
continue
}
semi := strings.index(s[i:], ";")
if semi < 0 {
break
}
entity := s[i : i + semi + 1]
replacement := ""
switch entity {
case "&amp;": replacement = "&"
case "&lt;": replacement = "<"
case "&gt;": replacement = ">"
case "&quot;": replacement = "\""
case "&#39;", "&apos;": replacement = "'"
case: continue
}
if i > start {
strings.write_string(&sb, s[start:i])
}
strings.write_string(&sb, replacement)
start = i + semi + 1
}
if start == 0 {
return s
}
if start < len(s) {
strings.write_string(&sb, s[start:])
}
return strings.to_string(sb)
}
// generate_summary produces a plain-text summary of an HTML fragment.
// Blocks (paragraphs, headings, list items) are extracted, their tags
// stripped, entities decoded, and accumulated word-by-word until the
// max_words threshold is crossed — at which point the rest of the
// current block is included before stopping. Mirrors Hugo's default
// summary behavior.
generate_summary :: proc(html: string, max_words: int = 70) -> string {
separated, _ := strings.replace_all(html, "</p>", "\n\n", context.temp_allocator)
separated, _ = strings.replace_all(separated, "</h1>", "\n\n")
separated, _ = strings.replace_all(separated, "</h2>", "\n\n")
separated, _ = strings.replace_all(separated, "</h3>", "\n\n")
separated,_ = strings.replace_all(separated, "</h4>", "\n\n")
separated, _ = strings.replace_all(separated, "</h5>", "\n\n")
separated, _ = strings.replace_all(separated, "</h6>", "\n\n")
separated, _ = strings.replace_all(separated, "</li>", "\n\n")
separated, _ = strings.replace_all(separated, "</blockquote>", "\n\n")
stripped := strip_html_tags(separated, context.temp_allocator)
plain := unescape_html(stripped)
blocks := strings.split(plain, "\n\n", allocator = context.temp_allocator)
defer delete(blocks)
sb := strings.builder_make(context.temp_allocator)
defer strings.builder_destroy(&sb)
word_count := 0
first := true
for raw_block in blocks {
block := strings.trim_space(raw_block)
if len(block) == 0 {
continue
}
// Collapse internal whitespace to single spaces.
block_sb := strings.builder_make(context.temp_allocator)
has_content := false
in_space := true
for c in block {
if c == ' ' || c == '\t' || c == '\n' || c == '\r' {
in_space = true
} else {
if in_space && has_content {
strings.write_byte(&block_sb, ' ')
}
strings.write_rune(&block_sb, c)
in_space = false
has_content = true
}
}
collapsed := strings.to_string(block_sb)
words := strings.split(collapsed, " ", allocator = context.temp_allocator)
if !first && word_count > 0 {
strings.write_byte(&sb, ' ')
}
strings.write_string(&sb, collapsed)
word_count += len(words)
first = false
delete(words)
if word_count >= max_words {
break
}
}
return strings.to_string(sb)
}
+98 -21
View File
@@ -1,7 +1,5 @@
package main package main
import "core:fmt"
Open_Graph :: struct { Open_Graph :: struct {
title: string, title: string,
type: string, type: string,
@@ -10,31 +8,110 @@ Open_Graph :: struct {
description: string, description: string,
locale: string, locale: string,
site_name: string, site_name: string,
is_article: bool, is_article: Maybe(bool),
published_time: string, published_time: string,
modified_time: string, modified_time: string,
section: string, section: string,
} }
og_init :: proc(site: Site) -> Open_Graph { og_for_site :: proc(site: ^Site) -> Open_Graph {
return { og := site.og
site_name = site.title, if og.site_name == "" {
description = site.description, og.site_name = site.title
image = fmt.tprintf("%s/avatar.jpg", site.base_url), }
locale = "en_US", if og.description == "" {
og.description = site.description
}
if og.locale == "" {
og.locale = "en_US"
} }
}
og_for_page :: proc(site: Site, page: Page, base: Open_Graph) -> Open_Graph {
og := base
is_article := page.section != ""
og.url = page.url
og.title = strip_html_tags(page.title, context.temp_allocator)
og.type = "article" if is_article else "website"
og.is_article = is_article
og.section = page.section
og.published_time = page.date
return og return og
} }
og_for_page :: proc(site_og: Open_Graph, page: Page) -> Open_Graph {
og := site_og
is_article := !page._is_index
if page.url != "" {
og.url = page.url
}
if page.title != "" {
og.title = strip_html_tags(page.title, context.temp_allocator)
} else {
og.title = og.site_name
}
og.type = "article" if is_article else "website"
og.is_article = is_article
if page.section != "" {
og.section = page.section
}
if is_article {
if page.date != "" {
og.published_time = page.date
}
if page.lastmod != "" {
og.modified_time = page.lastmod
} else if page.date != "" {
og.modified_time = page.date
}
}
// Description priority for articles:
// page.og.description > page.description > body summary > inherited
// For non-articles (home, section index), inherited site.og.description
// is the fallback (matches production behavior — home inherits site
// description, section index is empty).
description_set := false
if page.og.description != "" {
og.description = page.og.description
description_set = true
}
if !description_set && page.description != "" {
og.description = page.description
description_set = true
}
if !description_set && is_article && page.body_html != "" {
og.description = generate_summary(page.body_html)
description_set = true
}
if !description_set {
if page._is_index && page.section == "" {
// Home: keep inherited site.og.description.
} else {
og.description = ""
}
}
if page.og.title != "" {
og.title = page.og.title
}
if page.og.type != "" {
og.type = page.og.type
}
if page.og.image != "" {
og.image = page.og.image
}
if page.og.url != "" {
og.url = page.og.url
}
if page.og.locale != "" {
og.locale = page.og.locale
}
if page.og.site_name != "" {
og.site_name = page.og.site_name
}
if page.og.published_time != "" {
og.published_time = page.og.published_time
}
if page.og.modified_time != "" {
og.modified_time = page.og.modified_time
}
if page.og.section != "" {
og.section = page.og.section
}
if page.og.is_article != nil {
og.is_article = page.og.is_article
}
return og
}
+7 -35
View File
@@ -57,32 +57,6 @@ build_page_context :: proc(page: Page) -> Page_Context {
} }
} }
strip_html_tags :: proc(s: string, allocator := context.allocator) -> string {
sb := strings.builder_make(allocator)
defer strings.builder_destroy(&sb)
in_tag := false
start := 0
for i in 0 ..< len(s) {
if s[i] == '<' && !in_tag {
if i > start {
strings.write_string(&sb, s[start:i])
}
in_tag = true
} else if s[i] == '>' && in_tag {
in_tag = false
start = i + 1
}
}
if start == 0 {
return s
}
if !in_tag && start < len(s) {
strings.write_string(&sb, s[start:])
}
return strings.to_string(sb)
}
load_template :: proc(vfs: ^VFS, virtual_path: string) -> mustache.Template { load_template :: proc(vfs: ^VFS, virtual_path: string) -> mustache.Template {
data, ok := vfs_get(vfs, virtual_path) data, ok := vfs_get(vfs, virtual_path)
if !ok { if !ok {
@@ -176,7 +150,7 @@ render_site :: proc(site: ^Site) {
now = now, now = now,
author = site.author, author = site.author,
params = site.params, params = site.params,
og = og_init(site^), og = site.og,
} }
// Find home page // Find home page
@@ -289,7 +263,7 @@ render_page_html :: proc(
data.body = page.body_html data.body = page.body_html
data.date_iso = page.date data.date_iso = page.date
data.date_display = format_date(page.date) data.date_display = format_date(page.date)
data.og = og_for_page(site^, page, base.og) data.og = og_for_page(site.og, page)
return render_template(content_tpl, data, partials) return render_template(content_tpl, data, partials)
} }
@@ -315,10 +289,7 @@ render_home_html :: proc(
data.title = site.title data.title = site.title
data.body = home.body_html data.body = home.body_html
data.pages = list_pages data.pages = list_pages
data.og.url = fmt.tprintf("%s/", site.base_url) data.og = og_for_page(site.og, home)
data.og.title = site.title
data.og.type = "website"
data.og.is_article = false
return render_template(content_tpl, data, partials) return render_template(content_tpl, data, partials)
} }
@@ -347,16 +318,17 @@ render_section :: proc(
data.body = section_index.body_html data.body = section_index.body_html
data.page_title = section_index.title data.page_title = section_index.title
data.title = fmt.tprintf("%s | %s", section_index.title, site.title) data.title = fmt.tprintf("%s | %s", section_index.title, site.title)
data.og.title = section_index.title data.og = og_for_page(site.og, section_index)
} else { } else {
data.page_title = capitalize(section) data.page_title = capitalize(section)
data.title = fmt.tprintf("%s | %s", capitalize(section), site.title) data.title = fmt.tprintf("%s | %s", capitalize(section), site.title)
data.og.title = capitalize(section) data.og.title = capitalize(section)
} data.og.description = ""
data.posts = posts
data.og.url = fmt.tprintf("%s/%s/", site.base_url, section) data.og.url = fmt.tprintf("%s/%s/", site.base_url, section)
data.og.type = "website" data.og.type = "website"
data.og.is_article = false data.og.is_article = false
}
data.posts = posts
return render_template(content_tpl, data, partials) return render_template(content_tpl, data, partials)
} }
+7
View File
@@ -29,6 +29,7 @@ Site :: struct {
params: json.Object, params: json.Object,
features: bit_set[Feature], features: bit_set[Feature],
markdown_extensions: bit_set[md.Extension], markdown_extensions: bit_set[md.Extension],
og: Open_Graph,
} }
Feature :: enum { Feature :: enum {
@@ -51,6 +52,7 @@ Config_File :: struct {
markdown_extensions: json.Value, markdown_extensions: json.Value,
params: json.Value, params: json.Value,
modules: json.Value, modules: json.Value,
og: Open_Graph,
} }
// Configuration loaded from command line arguments. Gets folded in to Site // Configuration loaded from command line arguments. Gets folded in to Site
@@ -107,6 +109,9 @@ init_site :: proc(site: ^Site, args: []string) {
site_apply_cli_flags(site, _flags) site_apply_cli_flags(site, _flags)
site.config_path = path site.config_path = path
// Build the resolved site-level OG now that every other field is set.
site.og = og_for_site(site)
} }
load_config_file :: proc( load_config_file :: proc(
@@ -159,6 +164,8 @@ site_apply_config :: proc(site: ^Site, config: Config_File, config_dir: string)
} }
} }
} }
site.og = config.og
} }
site_apply_path_defaults :: proc(site: ^Site, config_dir: string) { site_apply_path_defaults :: proc(site: ^Site, config_dir: string) {