Skip to content

Commit

Permalink
support default content language and google analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwei committed Jan 7, 2025
1 parent 48f7a64 commit ea51537
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 33 deletions.
12 changes: 12 additions & 0 deletions internal/domain/config/entity/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ func (l *Language) LanguageKeys() []string {
return l.indices
}

func (l *Language) LanguageIndexes() []int {
var indexes []int
for i, _ := range l.indices {
indexes = append(indexes, i)
}
return indexes
}

func (l *Language) GetLanguageIndex(lang string) (int, error) {
for i, v := range l.indices {
if v == lang {
Expand All @@ -94,6 +102,10 @@ func (l *Language) GetLanguageIndex(lang string) (int, error) {
return -1, errors.New("language not found in indices")
}

func (l *Language) GetLanguageByIndex(idx int) string {
return l.indices[idx]
}

func (l *Language) GetLanguageName(lang string) string {
for c, v := range l.Configs {
if c == lang {
Expand Down
4 changes: 3 additions & 1 deletion internal/domain/config/entity/service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package entity

import "github.com/gohugonet/hugoverse/internal/domain/config/valueobject"
import (
"github.com/gohugonet/hugoverse/internal/domain/config/valueobject"
)

type Service struct {
valueobject.ServiceConfig
Expand Down
37 changes: 28 additions & 9 deletions internal/domain/content/valueobject/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ import (
type Site struct {
Item

Title string `json:"title"`
Description string `json:"description"`
BaseURL string `json:"base_url"`
Theme string `json:"theme"`
Params string `json:"params"`
Owner string `json:"owner"`
WorkingDir string `json:"working_dir"`
Languages []string `json:"languages"`
Menus []string `json:"menus"`
Title string `json:"title"`
Description string `json:"description"`
BaseURL string `json:"base_url"`
Theme string `json:"theme"`
Params string `json:"params"`
Owner string `json:"owner"`
WorkingDir string `json:"working_dir"`
GoogleAnalytics string `json:"google_analytics"`
DefaultContentLanguage string `json:"default_content_language"`
Languages []string `json:"languages"`
Menus []string `json:"menus"`
}

// MarshalEditor writes a buffer of html to edit a Song within the CMS
Expand Down Expand Up @@ -78,6 +80,20 @@ func (s *Site) MarshalEditor() ([]byte, error) {
"placeholder": "Enter the project file system dir here",
}),
},
editor.Field{
View: editor.Input("GoogleAnalytics", s, map[string]string{
"label": "GoogleAnalytics",
"type": "text",
"placeholder": "Enter the GoogleAnalytics here",
}),
},
editor.Field{
View: editor.Input("DefaultLanguage", s, map[string]string{
"label": "DefaultLanguage",
"type": "text",
"placeholder": "Enter the DefaultLanguage here",
}),
},
editor.Field{
View: editor.Input("Languages", s, map[string]string{
"label": "Languages",
Expand Down Expand Up @@ -201,6 +217,9 @@ description = "{{.Description}}"
baseURL = "{{.BaseURL}}"
owner = "{{.Owner}}"
defaultContentLanguage = "{{.DefaultContentLanguage}}"
googleAnalytics = "{{.GoogleAnalytics}}"
[module]
[[module.imports]]
path = "{{.Theme}}"
Expand Down
26 changes: 24 additions & 2 deletions internal/domain/contenthub/entity/pagebuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type PageBuilder struct {
kind string
singular string
term string
langIdx int

fm *valueobject.FrontMatter
fmParser *valueobject.FrontMatterParser
Expand All @@ -50,9 +51,16 @@ func (b *PageBuilder) WithSource(source *Source) *PageBuilder {
return &cloneBuilder
}

func (b *PageBuilder) WithLangIdx(idx int) *PageBuilder {
b.langIdx = idx

return b
}

func (b *PageBuilder) reset() {
b.c = nil
b.kind = ""
b.langIdx = -1
}

func (b *PageBuilder) Build() (contenthub.Page, error) {
Expand Down Expand Up @@ -82,8 +90,14 @@ func (b *PageBuilder) KindBuild() (contenthub.Page, error) {
return nil, err
}

if err := b.parseLanguageByDefault(); err != nil {
return nil, err
if b.langIdx == -1 {
if err := b.parseLanguageByDefault(); err != nil {
return nil, err
}
} else {
if err := b.parseLanguageByIdx(b.langIdx); err != nil {
return nil, err
}
}

b.fm = &valueobject.FrontMatter{}
Expand Down Expand Up @@ -392,6 +406,14 @@ func (b *PageBuilder) parseLanguageByDefault() error {
return nil
}

func (b *PageBuilder) parseLanguageByIdx(langIdx int) error {
dl := b.LangSvc.GetLanguageByIndex(langIdx)

b.source.Identity.Lang = dl
b.source.Identity.LangIdx = langIdx
return nil
}

func (b *PageBuilder) parseLanguage() error {
l, ok := b.LangSvc.GetSourceLang(b.source.File.Root())
if ok {
Expand Down
7 changes: 5 additions & 2 deletions internal/domain/contenthub/entity/pagemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,11 @@ func (m *PageMap) assembleStructurePages() error {
return err
}

if err := m.PageBuilder.Section.Assemble(m.TreePages, m.PageBuilder); err != nil {
return err
for _, idx := range m.PageBuilder.LangSvc.LanguageIndexes() {
tree := m.TreePages.Shape(0, idx)
if err := m.PageBuilder.Section.Assemble(tree, m.PageBuilder, idx); err != nil {
return err
}
}

if err := m.addMissingStandalone(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/domain/contenthub/entity/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (s *Section) isSectionExist(section string) bool {
return false
}

func (s *Section) Assemble(pages *doctree.NodeShiftTree[*PageTreesNode], pb *PageBuilder) error {
func (s *Section) Assemble(pages *doctree.NodeShiftTree[*PageTreesNode], pb *PageBuilder, langIdx int) error {
s.seen = make(map[string]bool)

var w *doctree.NodeShiftTreeWalker[*PageTreesNode]
Expand Down Expand Up @@ -85,7 +85,7 @@ func (s *Section) Assemble(pages *doctree.NodeShiftTree[*PageTreesNode], pb *Pag
nn := w.Tree.Get(sectionBase)

if nn == nil {
sectionPage, err := pb.WithSource(sectionSource).KindBuild()
sectionPage, err := pb.WithSource(sectionSource).WithLangIdx(langIdx).KindBuild()
if err != nil {
return false, err
}
Expand Down
2 changes: 2 additions & 0 deletions internal/domain/contenthub/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ type LangService interface {
IsLanguageValid(lang string) bool
GetSourceLang(source string) (string, bool)
GetLanguageIndex(lang string) (int, error)
GetLanguageByIndex(idx int) string
DefaultLanguage() string
LanguageIndexes() []int
}

type Taxonomy interface {
Expand Down
14 changes: 13 additions & 1 deletion internal/domain/site/entity/navigation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@ package entity

import (
"fmt"
"github.com/gohugonet/hugoverse/internal/domain/site"
"github.com/gohugonet/hugoverse/internal/domain/site/valueobject"
"github.com/gohugonet/hugoverse/pkg/compare"
"sort"
)

type Navigation struct {
taxonomies TaxonomyList
menus valueobject.Menus
menus map[string]valueobject.Menus
}

func NewNavigation(langSvc site.LanguageService) *Navigation {
n := &Navigation{
taxonomies: make(TaxonomyList),
menus: make(map[string]valueobject.Menus),
}
for _, l := range langSvc.LanguageKeys() {
n.menus[l] = valueobject.NewEmptyMenus()
}
return n
}

// The TaxonomyList is a list of all taxonomies and their values
Expand Down
37 changes: 24 additions & 13 deletions internal/domain/site/entity/siteinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,21 @@ import (
type siteInit struct {
prevNext *lazy.Init
prevNextInSection *lazy.Init
menus *lazy.Init
menus map[string]*lazy.Init
taxonomies *lazy.Init
}

func (init *siteInit) Reset() {
init.prevNext.Reset()
init.prevNextInSection.Reset()
init.menus.Reset()
for k := range init.menus {
init.menus[k].Reset()
}
init.taxonomies.Reset()
}

func (s *Site) PrepareLazyLoads() {
s.lazy = &siteInit{}

var init lazy.Init

s.lazy.menus = init.Branch(func() (any, error) {
initMenu := func() (any, error) {
menus := valueobject.NewEmptyMenus()

menusConf := s.ConfigSvc.Menus()
Expand Down Expand Up @@ -69,10 +67,18 @@ func (s *Site) PrepareLazyLoads() {
s.Log.Errorf("Reserved links Menus: %v", err)
}

s.Navigation.menus = menus

s.Navigation.menus[s.Language.currentLanguage] = menus
return nil, nil
})
}

s.lazy = &siteInit{
menus: map[string]*lazy.Init{},
}

var init lazy.Init
for _, lang := range s.LanguageSvc.LanguageKeys() {
s.lazy.menus[lang] = init.Branch(initMenu)
}

s.lazy.taxonomies = init.Branch(func() (any, error) {
s.Navigation.taxonomies = make(TaxonomyList)
Expand Down Expand Up @@ -115,9 +121,14 @@ func (s *Site) Taxonomies() TaxonomyList {
}

func (s *Site) Menus() valueobject.Menus {
if _, err := s.lazy.menus.Do(); err != nil {
s.Log.Errorf("Menus: %v", err)
init, ok := s.lazy.menus[s.Language.currentLanguage]
if ok {
if _, err := init.Do(); err != nil {
s.Log.Errorf("Menus: %v", err)
}
} else {
s.Log.Errorf("Menus: no init for %s", s.Language.currentLanguage)
}

return s.Navigation.menus
return s.Navigation.menus[s.Language.currentLanguage]
}
2 changes: 1 addition & 1 deletion internal/domain/site/factory/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func New(services site.Services) *entity.Site {
Language: &entity.Language{
LangSvc: services,
},
Navigation: &entity.Navigation{},
Navigation: entity.NewNavigation(services),

Log: loggers.NewDefault(),
}
Expand Down
2 changes: 1 addition & 1 deletion internal/interfaces/cli/vercurr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package cli
var CurrentVersion = Version{
Major: 0,
Minor: 1,
PatchLevel: 3,
PatchLevel: 4,
Suffix: "",
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.3",
"version": "0.1.4",
"name": "Hugoverse",
"description": "Headless CMS for Hugo",
"author": "sunwei",
Expand Down

0 comments on commit ea51537

Please sign in to comment.