mirror of
https://github.com/sbrow/thor.git
synced 2026-08-26 11:23:32 -04:00
refactor: Moved OpenGraph fields to a separate struct.
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
<meta property="og:url" content="{{og_url}}">
|
||||
<meta property="og:site_name" content="{{og_site_name}}">
|
||||
<meta property="og:title" content="{{og_title}}">
|
||||
<meta property="og:description" content="{{og_description}}">
|
||||
<meta property="og:locale" content="en_us">
|
||||
<meta property="og:type" content="{{og_type}}">
|
||||
{{#is_article}}<meta property="article:section" content="{{og_section}}">
|
||||
<meta property="article:published_time" content="{{og_published}}">
|
||||
{{/is_article}}<meta property="og:image" content="{{og_image}}">
|
||||
<meta property="og:url" content="{{og.url}}">
|
||||
<meta property="og:site_name" content="{{og.site_name}}">
|
||||
<meta property="og:title" content="{{og.title}}">
|
||||
<meta property="og:description" content="{{og.description}}">
|
||||
<meta property="og:locale" content="{{og.locale}}">
|
||||
<meta property="og:type" content="{{og.type}}">
|
||||
{{#og.is_article}}<meta property="article:section" content="{{og.section}}">
|
||||
<meta property="article:published_time" content="{{og.published_time}}">
|
||||
{{/og.is_article}}<meta property="og:image" content="{{og.image}}">
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import "core:fmt"
|
||||
|
||||
Open_Graph :: struct {
|
||||
title: string,
|
||||
type: string,
|
||||
image: string,
|
||||
url: string,
|
||||
|
||||
description: string,
|
||||
locale: string,
|
||||
site_name: string,
|
||||
|
||||
is_article: bool,
|
||||
published_time: string,
|
||||
modified_time: string,
|
||||
section: string,
|
||||
}
|
||||
|
||||
og_init :: proc(site: ^Site) -> Open_Graph {
|
||||
return {
|
||||
site_name = site.title,
|
||||
description = site.description,
|
||||
image = fmt.tprintf("%s/avatar.jpg", site.base_url),
|
||||
locale = "en_US",
|
||||
}
|
||||
}
|
||||
|
||||
og_for_page :: proc(site: ^Site, page: Page, base: Open_Graph) -> Open_Graph {
|
||||
og := base
|
||||
is_article := page.section != ""
|
||||
og.url = fmt.tprintf("%s%s", site.base_url, page.permalink)
|
||||
og.title = strip_html_tags(page.title)
|
||||
og.type = "article" if is_article else "website"
|
||||
og.is_article = is_article
|
||||
og.section = page.section
|
||||
og.published_time = page.date
|
||||
return og
|
||||
}
|
||||
+22
-34
@@ -29,13 +29,7 @@ Base_Data :: struct {
|
||||
params: json.Value,
|
||||
body: string,
|
||||
title: string,
|
||||
og_site_name: string,
|
||||
og_description: string,
|
||||
og_image: string,
|
||||
og_url: string,
|
||||
og_title: string,
|
||||
og_type: string,
|
||||
is_article: bool,
|
||||
og: Open_Graph,
|
||||
}
|
||||
|
||||
Page_Data :: struct {
|
||||
@@ -43,8 +37,6 @@ Page_Data :: struct {
|
||||
page_title: string,
|
||||
date_iso: string,
|
||||
date_display: string,
|
||||
og_section: string,
|
||||
og_published: string,
|
||||
}
|
||||
|
||||
Home_Data :: struct {
|
||||
@@ -94,13 +86,6 @@ strip_html_tags :: proc(s: string) -> string {
|
||||
return strings.to_string(sb)
|
||||
}
|
||||
|
||||
og_type :: proc(is_article: bool) -> string {
|
||||
if is_article {
|
||||
return "article"
|
||||
}
|
||||
return "website"
|
||||
}
|
||||
|
||||
load_template :: proc(vfs: ^VFS, virtual_path: string) -> mustache.Template {
|
||||
data, ok := vfs_get(vfs, virtual_path)
|
||||
if !ok {
|
||||
@@ -194,9 +179,12 @@ render_site :: proc(site: ^Site) {
|
||||
now = now,
|
||||
author = site.author,
|
||||
params = site.params,
|
||||
og_site_name = site.title,
|
||||
og_description = site.description,
|
||||
og_image = fmt.tprintf("%s/avatar.jpg", site.base_url),
|
||||
og = {
|
||||
site_name = site.title,
|
||||
description = site.description,
|
||||
image = fmt.tprintf("%s/avatar.jpg", site.base_url),
|
||||
locale = "en_US",
|
||||
},
|
||||
}
|
||||
|
||||
// Find home page
|
||||
@@ -309,12 +297,12 @@ render_page_html :: proc(
|
||||
data.body = page.body_html
|
||||
data.date_iso = page.date
|
||||
data.date_display = format_date(page.date)
|
||||
data.og_url = fmt.tprintf("%s%s", site.base_url, page.permalink)
|
||||
data.og_title = strip_html_tags(page.title)
|
||||
data.og_type = og_type(is_article)
|
||||
data.is_article = is_article
|
||||
data.og_section = page.section
|
||||
data.og_published = page.date
|
||||
data.og.url = fmt.tprintf("%s%s", site.base_url, page.permalink)
|
||||
data.og.title = strip_html_tags(page.title)
|
||||
data.og.type = "article" if is_article else "website"
|
||||
data.og.is_article = is_article
|
||||
data.og.section = page.section
|
||||
data.og.published_time = page.date
|
||||
return render_template(content_tpl, data, partials)
|
||||
}
|
||||
|
||||
@@ -340,10 +328,10 @@ render_home_html :: proc(
|
||||
data.title = site.title
|
||||
data.body = home.body_html
|
||||
data.pages = list_pages
|
||||
data.og_url = fmt.tprintf("%s/", site.base_url)
|
||||
data.og_title = site.title
|
||||
data.og_type = "website"
|
||||
data.is_article = false
|
||||
data.og.url = fmt.tprintf("%s/", site.base_url)
|
||||
data.og.title = site.title
|
||||
data.og.type = "website"
|
||||
data.og.is_article = false
|
||||
return render_template(content_tpl, data, partials)
|
||||
}
|
||||
|
||||
@@ -378,16 +366,16 @@ render_section :: proc(
|
||||
data.body = section_index.body_html
|
||||
data.page_title = section_index.title
|
||||
data.title = fmt.tprintf("%s | %s", section_index.title, site.title)
|
||||
data.og_title = section_index.title
|
||||
data.og.title = section_index.title
|
||||
} else {
|
||||
data.page_title = capitalize(section)
|
||||
data.title = fmt.tprintf("%s | %s", capitalize(section), site.title)
|
||||
data.og_title = capitalize(section)
|
||||
data.og.title = capitalize(section)
|
||||
}
|
||||
data.by_year = by_year
|
||||
data.og_url = fmt.tprintf("%s/%s/", site.base_url, section)
|
||||
data.og_type = "website"
|
||||
data.is_article = false
|
||||
data.og.url = fmt.tprintf("%s/%s/", site.base_url, section)
|
||||
data.og.type = "website"
|
||||
data.og.is_article = false
|
||||
return render_template(content_tpl, data, partials)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user