Made some improvements to how Hugo is configured
This commit is contained in:
parent
b09a8bd8d8
commit
39611070f8
22
providers/hugo/config.go
Normal file
22
providers/hugo/config.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package hugo
|
||||
|
||||
type hugoConfig struct {
|
||||
BaseURL string `yaml:"baseURL,omitempty"`
|
||||
LanguageCode string `yaml:"languageCode"`
|
||||
Title string `yaml:"title"`
|
||||
Theme string `yaml:"theme"`
|
||||
|
||||
Markup hugoConfigMarkup `yaml:"markup"`
|
||||
}
|
||||
|
||||
type hugoConfigMarkup struct {
|
||||
Goldmark hugoGoldmarkConfig `yaml:"goldmark"`
|
||||
}
|
||||
|
||||
type hugoGoldmarkConfig struct {
|
||||
Renderer hugoGoldmarkRendererConfig `yaml:"renderer"`
|
||||
}
|
||||
|
||||
type hugoGoldmarkRendererConfig struct {
|
||||
Unsafe bool `yaml:"unsafe"`
|
||||
}
|
|
@ -1,33 +1,24 @@
|
|||
package hugo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"gopkg.in/yaml.v3"
|
||||
"lmika.dev/lmika/hugo-cms/models"
|
||||
"lmika.dev/lmika/hugo-cms/providers/hugo/tmpls"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
type Provider struct {
|
||||
stagingDir string
|
||||
scratchDir string
|
||||
tmpls *template.Template
|
||||
}
|
||||
|
||||
func New(stagingDir, scratchDir string) (*Provider, error) {
|
||||
ts, err := template.ParseFS(tmpls.FS, "*.tmpl")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Provider{
|
||||
stagingDir: stagingDir,
|
||||
scratchDir: scratchDir,
|
||||
tmpls: ts,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@ -48,7 +39,9 @@ func (p *Provider) NewSite(ctx context.Context, site models.Site) error {
|
|||
}
|
||||
|
||||
// Create the new site
|
||||
if err := exec.CommandContext(ctx, "hugo", "new", "site", p.SiteStagingDir(site, BaseSiteDir)).Run(); err != nil {
|
||||
if err := exec.CommandContext(ctx, "hugo", "new", "site",
|
||||
"--config=hugo.yaml",
|
||||
p.SiteStagingDir(site, BaseSiteDir)).Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
@ -86,18 +79,33 @@ func (p *Provider) PublishSite(ctx context.Context, site models.Site, target mod
|
|||
}
|
||||
|
||||
func (p *Provider) ReconfigureSite(ctx context.Context, site models.Site) error {
|
||||
// Reconfigure the site
|
||||
var hugoCfg bytes.Buffer
|
||||
if err := p.tmpls.ExecuteTemplate(&hugoCfg, "config.toml.tmpl", struct {
|
||||
Site models.Site
|
||||
}{
|
||||
Site: site,
|
||||
}); err != nil {
|
||||
hugoCfg := hugoConfig{
|
||||
Title: site.Name,
|
||||
LanguageCode: "en",
|
||||
Theme: site.Theme,
|
||||
Markup: hugoConfigMarkup{
|
||||
Goldmark: hugoGoldmarkConfig{
|
||||
Renderer: hugoGoldmarkRendererConfig{
|
||||
Unsafe: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
ymlBytes, err := yaml.Marshal(hugoCfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.WriteFile(filepath.Join(p.SiteStagingDir(site, BaseSiteDir), "hugo.toml"), hugoCfg.Bytes(), 0644); err != nil {
|
||||
if err := os.WriteFile(filepath.Join(p.SiteStagingDir(site, BaseSiteDir), "hugo.yaml"), ymlBytes, 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(p.SiteStagingDir(site, BaseSiteDir), "hugo.toml")); err == nil {
|
||||
if err := os.Remove(filepath.Join(p.SiteStagingDir(site, BaseSiteDir), "hugo.toml")); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
baseURL = 'https://example.com/'
|
||||
languageCode = 'en-us'
|
||||
title = {{.Site.Title | printf "%q"}}
|
||||
|
||||
theme = {{.Site.Theme | printf "%q"}}
|
|
@ -1,6 +0,0 @@
|
|||
package tmpls
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed *.tmpl
|
||||
var FS embed.FS
|
|
@ -72,7 +72,7 @@ func (s *Service) fullRebuildNecessary(ctx context.Context, site models.Site) (b
|
|||
}
|
||||
|
||||
filesMustExists := []string{
|
||||
filepath.Join(s.hugo.SiteStagingDir(site, hugo.BaseSiteDir), "hugo.toml"),
|
||||
filepath.Join(s.hugo.SiteStagingDir(site, hugo.BaseSiteDir), "hugo.yaml"),
|
||||
}
|
||||
|
||||
for _, dir := range dirsMustExists {
|
||||
|
|
|
@ -63,15 +63,17 @@ func (s *Service) CreateSite(ctx context.Context, user models.User, name string)
|
|||
}
|
||||
|
||||
// TEMP
|
||||
if err := s.db.InsertPublishTarget(ctx, &models.PublishTarget{
|
||||
SiteID: newSite.ID,
|
||||
Role: models.TargetRoleProduction,
|
||||
Type: models.TargetTypeNetlify,
|
||||
URL: "https://meek-meringue-060cfc.netlify.app/",
|
||||
TargetRef: "e628dc6e-e6e1-45a9-847a-982adef940a8",
|
||||
}); err != nil {
|
||||
return models.Site{}, err
|
||||
}
|
||||
/*
|
||||
if err := s.db.InsertPublishTarget(ctx, &models.PublishTarget{
|
||||
SiteID: newSite.ID,
|
||||
Role: models.TargetRoleProduction,
|
||||
Type: models.TargetTypeNetlify,
|
||||
URL: "https://meek-meringue-060cfc.netlify.app/",
|
||||
TargetRef: "e628dc6e-e6e1-45a9-847a-982adef940a8",
|
||||
}); err != nil {
|
||||
return models.Site{}, err
|
||||
}
|
||||
*/
|
||||
|
||||
return newSite, s.jobs.Queue(ctx, s.sb.CreateNewSite(newSite))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue