Added theme options
This commit is contained in:
parent
68a6169bc3
commit
ab5c101fcc
|
@ -2,10 +2,28 @@ package models
|
||||||
|
|
||||||
import "io/fs"
|
import "io/fs"
|
||||||
|
|
||||||
|
type ThemeOptionType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
ThemeOptionTypeString ThemeOptionType = iota
|
||||||
|
ThemeOptionTypeInt
|
||||||
|
)
|
||||||
|
|
||||||
|
type ThemeOption struct {
|
||||||
|
Name string
|
||||||
|
Label string
|
||||||
|
Description string
|
||||||
|
Type ThemeOptionType
|
||||||
|
DefaultValue string
|
||||||
|
}
|
||||||
|
|
||||||
type ThemeMeta struct {
|
type ThemeMeta struct {
|
||||||
ID string
|
ID string
|
||||||
Name string
|
Name string
|
||||||
|
|
||||||
|
// Options
|
||||||
|
Options []ThemeOption
|
||||||
|
|
||||||
// Source repo
|
// Source repo
|
||||||
URL string
|
URL string
|
||||||
OverlayFS fs.FS
|
OverlayFS fs.FS
|
||||||
|
|
|
@ -11,6 +11,7 @@ type hugoConfig struct {
|
||||||
Theme string `yaml:"theme"`
|
Theme string `yaml:"theme"`
|
||||||
CanonifyURLs bool `yaml:"canonifyURLs,omitempty"`
|
CanonifyURLs bool `yaml:"canonifyURLs,omitempty"`
|
||||||
Permalinks permalinksConfig `yaml:"permalinks,omitempty"`
|
Permalinks permalinksConfig `yaml:"permalinks,omitempty"`
|
||||||
|
Params map[string]any `yaml:"params,omitempty"`
|
||||||
|
|
||||||
Markup hugoConfigMarkup `yaml:"markup"`
|
Markup hugoConfigMarkup `yaml:"markup"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Provider struct {
|
type Provider struct {
|
||||||
|
@ -123,6 +124,7 @@ func (p *Provider) ReconfigureSite(ctx context.Context, isPreviewConfig bool, co
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Params: p.configureParams(ctx, site, meta),
|
||||||
}
|
}
|
||||||
|
|
||||||
if isPreviewConfig {
|
if isPreviewConfig {
|
||||||
|
@ -146,3 +148,34 @@ func (p *Provider) ReconfigureSite(ctx context.Context, isPreviewConfig bool, co
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Provider) configureParams(ctx context.Context, site models.Site, meta models.ThemeMeta) map[string]any {
|
||||||
|
// Options from the theme
|
||||||
|
themeOptions := make(map[string]any)
|
||||||
|
for _, option := range meta.Options {
|
||||||
|
var typedVal any
|
||||||
|
|
||||||
|
strVal := option.DefaultValue
|
||||||
|
|
||||||
|
switch option.Type {
|
||||||
|
case models.ThemeOptionTypeString:
|
||||||
|
typedVal = strVal
|
||||||
|
case models.ThemeOptionTypeInt:
|
||||||
|
intVal, err := strconv.Atoi(strVal)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("invalid theme option value: name %v, type %v, value %v", option.Name, option.Type, strVal)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
typedVal = intVal
|
||||||
|
default:
|
||||||
|
log.Printf("unknown theme option type: name %v, type %v", option.Name, option.Type)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
themeOptions[option.Name] = typedVal
|
||||||
|
}
|
||||||
|
|
||||||
|
return map[string]any{
|
||||||
|
"theme_options": themeOptions,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -7,9 +7,19 @@ import (
|
||||||
|
|
||||||
var themes = []models.ThemeMeta{
|
var themes = []models.ThemeMeta{
|
||||||
{
|
{
|
||||||
ID: "bear",
|
ID: "bear",
|
||||||
Name: "Bear",
|
Name: "Bear",
|
||||||
URL: "https://github.com/janraasch/hugo-bearblog",
|
URL: "https://github.com/janraasch/hugo-bearblog",
|
||||||
|
Options: []models.ThemeOption{
|
||||||
|
{
|
||||||
|
Name: "homepage_post_limit",
|
||||||
|
Label: "Homepage Post Limit",
|
||||||
|
Description: "Number of links to post on homepage",
|
||||||
|
Type: models.ThemeOptionTypeInt,
|
||||||
|
DefaultValue: "30",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
OverlayFS: overlays.HugoBearblogFS,
|
OverlayFS: overlays.HugoBearblogFS,
|
||||||
Overlays: map[string]string{
|
Overlays: map[string]string{
|
||||||
"hugo-bearblog/archetype-posts.md": "archetypes/posts.md",
|
"hugo-bearblog/archetype-posts.md": "archetypes/posts.md",
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
<h3>Recent Posts</h3>
|
<h3>Recent Posts</h3>
|
||||||
|
|
||||||
<ul class="blog-posts">
|
<ul class="blog-posts">
|
||||||
{{ range site.RegularPages }}
|
{{ range site.RegularPages.Limit .Site.Params.theme_options.homepage_post_limit }}
|
||||||
<li>
|
<li>
|
||||||
<span>
|
<span>
|
||||||
<i>
|
<i>
|
||||||
|
|
Loading…
Reference in a new issue