fix: Fixed the image on the homepage.

This commit is contained in:
Spencer Brower
2026-07-10 18:41:31 -04:00
parent c3574c6737
commit 1de6f58024
5 changed files with 165 additions and 63 deletions
+97 -26
View File
@@ -7,22 +7,24 @@ import "core:os"
import "core:strings"
Page_Type :: enum {
Home,
Post,
Standalone,
}
Page :: struct {
type: Page_Type,
slug: string,
permalink: string,
title: string,
date: string,
draft: bool,
is_starred: bool,
menu: string,
body: string,
body_html: string,
bundle_dir: string,
type: Page_Type,
slug: string,
permalink: string,
title: string,
description: string,
date: string,
draft: bool,
is_starred: bool,
menu: string,
body: string,
body_html: string,
bundle_dir: string,
}
// walk_content reads the content directory and returns all non-draft pages
@@ -30,6 +32,7 @@ Page :: struct {
walk_content :: proc(content_path: string, include_drafts: bool) -> []Page {
pages: [dynamic]Page
collect_home(&pages, content_path)
collect_standalone(&pages, content_path)
posts_path := fmt.tprintf("%s/posts", content_path)
@@ -51,6 +54,27 @@ walk_content :: proc(content_path: string, include_drafts: bool) -> []Page {
return pages[:]
}
collect_home :: proc(pages: ^[dynamic]Page, content_path: string) {
html_path := fmt.tprintf("%s/index.html", content_path)
if os.exists(html_path) {
page, ok := load_page(html_path, .Home, "")
if ok {
page.permalink = "/"
append(pages, page)
}
return
}
md_path := fmt.tprintf("%s/index.md", content_path)
if os.exists(md_path) {
page, ok := load_page(md_path, .Home, "")
if ok {
page.permalink = "/"
append(pages, page)
}
}
}
collect_standalone :: proc(pages: ^[dynamic]Page, content_path: string) {
entries, err := os.read_all_directory_by_path(content_path, context.allocator)
if err != nil {
@@ -60,14 +84,17 @@ collect_standalone :: proc(pages: ^[dynamic]Page, content_path: string) {
defer os.file_info_slice_delete(entries, context.allocator)
for entry in entries {
if !strings.has_suffix(entry.name, ".md") {
if !is_content_file(entry.name) {
continue
}
if entry.name == "index.md" || entry.name == "index.html" {
continue
}
if entry.type != .Regular {
continue
}
slug := entry.name[:len(entry.name) - 3]
slug := strip_extension(entry.name)
page, ok := load_page(entry.fullpath, .Standalone, slug)
if ok {
append(pages, page)
@@ -86,16 +113,19 @@ collect_posts :: proc(pages: ^[dynamic]Page, posts_path: string) {
for entry in entries {
switch entry.type {
case .Regular:
if !strings.has_suffix(entry.name, ".md") {
if !is_content_file(entry.name) {
continue
}
slug := entry.name[:len(entry.name) - 3]
slug := strip_extension(entry.name)
page, ok := load_page(entry.fullpath, .Post, slug)
if ok {
append(pages, page)
}
case .Directory:
index_path := fmt.tprintf("%s/index.md", entry.fullpath)
index_path := fmt.tprintf("%s/index.html", entry.fullpath)
if !os.exists(index_path) {
index_path = fmt.tprintf("%s/index.md", entry.fullpath)
}
if !os.exists(index_path) {
continue
}
@@ -105,7 +135,6 @@ collect_posts :: proc(pages: ^[dynamic]Page, posts_path: string) {
append(pages, page)
}
case .Undetermined, .Symlink, .Named_Pipe, .Socket, .Block_Device, .Character_Device:
// Do nothing
}
}
}
@@ -131,17 +160,25 @@ load_page :: proc(
return
}
page.type = page_type
page.slug = slug
page.title = fm.title
page.date = fm.date
page.draft = fm.draft
page.is_starred = fm.isStarred
page.menu = fm.menu
page.body = strings.clone(body)
page.body_html = cm.markdown_to_html_from_string(body, {.Unsafe})
page.type = page_type
page.slug = slug
page.title = fm.title
page.description = fm.description
page.date = fm.date
page.draft = fm.draft
page.is_starred = fm.isStarred
page.menu = fm.menu
page.body = strings.clone(body)
if strings.has_suffix(file_path, ".html") {
page.body_html = strings.clone(body)
} else {
page.body_html = cm.markdown_to_html_from_string(body, {.Unsafe})
}
switch page_type {
case .Home:
page.permalink = "/"
case .Post:
page.permalink = fmt.aprintf("/posts/%s/", slug)
case .Standalone:
@@ -152,3 +189,37 @@ load_page :: proc(
return
}
is_content_file :: proc(name: string) -> bool {
return strings.has_suffix(name, ".md") || strings.has_suffix(name, ".html")
}
strip_extension :: proc(name: string) -> string {
dot := strings.last_index(name, ".")
if dot < 0 {
return name
}
return name[:dot]
}
// copy_static_assets copies non-content files from the content root to the output directory.
copy_static_assets :: proc(content_path: string, output_dir: string) {
entries, err := os.read_all_directory_by_path(content_path, context.allocator)
if err != nil {
return
}
defer os.file_info_slice_delete(entries, context.allocator)
for entry in entries {
if entry.type != .Regular {
continue
}
if is_content_file(entry.name) {
continue
}
dest := fmt.tprintf("%s/%s", output_dir, entry.name)
if err := os.copy_file(dest, entry.fullpath); err != nil {
fmt.eprintfln("thor: cannot copy %s: %v", entry.name, err)
}
}
}