Skip to content

Commit

Permalink
support reserved structure
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwei committed Jan 11, 2025
1 parent 814c50d commit 7e73f95
Show file tree
Hide file tree
Showing 47 changed files with 565 additions and 64 deletions.
2 changes: 2 additions & 0 deletions ADRs/02.ContentDirectoryStructure.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ We will simplify the directory structure as follows:
│ ├── 📄 index.md # About Home | 关于主页
│ ├── 📄 resume.md # Resume | 简历
│ ├── 📄 company.md # Company | 公司信息
│ ├── 📄 contact.md # Contact | 联系方式
│ ├── 📄 social.md # Social Media | 社交媒体
├── 📁 portfolio # 🏆 Portfolio | 案例展示
│ ├── 📄 index.md # Portfolio Home | 案例主页
Expand Down
1 change: 1 addition & 0 deletions internal/domain/config/entity/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Config struct {
OutputFormats

MinifyC
Sitemap

*Taxonomy
}
Expand Down
15 changes: 15 additions & 0 deletions internal/domain/config/entity/sitemap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package entity

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

type Sitemap struct {
Conf valueobject.SitemapConfig
}

func (s Sitemap) ChangeFreq() string {
return s.Conf.ChangeFreq
}

func (s Sitemap) Priority() float64 {
return s.Conf.Priority
}
6 changes: 6 additions & 0 deletions internal/domain/config/factory/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ func (cl *ConfigLoader) decodeConfig(p config.Provider, target *entity.Config) e
}
target.Module.ModuleConfig = m

sitemap, err := valueobject.DecodeSitemap(valueobject.SitemapConfig{Priority: -1, Filename: "sitemap.xml"}, p.GetStringMap("sitemap"))
if err != nil {
return err
}
target.Sitemap.Conf = sitemap

languages, err := valueobject.DecodeLanguageConfig(p)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions internal/domain/config/factory/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func LoadConfig() (*entity.Config, error) {
Language: &entity.Language{},
Imaging: entity.Imaging{},
MediaType: entity.MediaType{},
Sitemap: entity.Sitemap{},

Taxonomy: &entity.Taxonomy{},
}
Expand Down
19 changes: 19 additions & 0 deletions internal/domain/config/valueobject/sitemap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package valueobject

import "github.com/mitchellh/mapstructure"

type SitemapConfig struct {
// The page change frequency.
ChangeFreq string
// The priority of the page.
Priority float64
// The sitemap filename.
Filename string
// Whether to disable page inclusion.
Disable bool
}

func DecodeSitemap(prototype SitemapConfig, input map[string]any) (SitemapConfig, error) {
err := mapstructure.WeakDecode(input, &prototype)
return prototype, err
}
8 changes: 3 additions & 5 deletions internal/domain/content/entity/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,13 @@ func (c *Content) BuildTarget(contentType, id, status string) (string, error) {

func (c *Content) writeSitePosts(siteId int, dir string, writerFiles chan *valueobject.File) error {
q := fmt.Sprintf(`site%d`, siteId)
encodedQ := url.QueryEscape(q)

sitePosts, err := c.search("SitePost", fmt.Sprintf("slug:%s", encodedQ))
sitePosts, err := c.Repo.ContentByPrefix(GetNamespace("SitePost", ""), q)
if err != nil {
return err
}

c.Log.Printf("sitePosts len: %d", len(sitePosts))
c.Log.Debugf("sitePosts len: %d", len(sitePosts))

for _, data := range sitePosts {
var sp valueobject.SitePost
Expand Down Expand Up @@ -117,9 +116,8 @@ func (c *Content) writeSitePosts(siteId int, dir string, writerFiles chan *value

func (c *Content) writeSiteResource(siteId int, dir string) error {
q := fmt.Sprintf(`site%d`, siteId)
encodedQ := url.QueryEscape(q)

siteResources, err := c.search("SiteResource", fmt.Sprintf("slug:%s", encodedQ))
siteResources, err := c.Repo.ContentByPrefix(GetNamespace("SiteResource", ""), q)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/domain/content/entity/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (c *Content) UpdateContentObject(ci any) error {
GetNamespace(cii.ItemName(), string(cis.ItemStatus())),
fmt.Sprintf("%d", cii.ItemID()), b); err != nil {

log.Println("[search] UpdateIndex Error:", err)
c.Log.Errorln("[search] UpdateIndex Error:", err)
}
}()
}
Expand All @@ -147,7 +147,7 @@ func (c *Content) UpdateContentObject(ci any) error {
go func() {
err := c.SortContent(cii.ItemName())
if err != nil {
log.Println("sort content err: ", err)
c.Log.Errorln("sort content err: ", err)
}
}()
}
Expand Down
1 change: 1 addition & 0 deletions internal/domain/content/repository/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type Repository interface {
NewContent(ci any, data []byte) error

AllContent(namespace string) [][]byte
ContentByPrefix(namespace, prefix string) ([][]byte, error)
GetContent(namespace string, id string) ([]byte, error)
DeleteContent(namespace string, id string, slug string) error

Expand Down
14 changes: 9 additions & 5 deletions internal/domain/content/valueobject/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ type Site struct {
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"`
WorkingDir string `json:"working_dir,omitempty"`
GoogleAnalytics string `json:"google_analytics,omitempty"`
DefaultContentLanguage string `json:"default_content_language,omitempty"`
Languages []string `json:"languages,omitempty"`
Menus []string `json:"menus,omitempty"`
}

// MarshalEditor writes a buffer of html to edit a Song within the CMS
Expand Down Expand Up @@ -217,8 +217,12 @@ description = "{{.Description}}"
baseURL = "{{.BaseURL}}"
owner = "{{.Owner}}"
{{ if .DefaultContentLanguage }}
defaultContentLanguage = "{{.DefaultContentLanguage}}"
{{ end }}
{{ if .GoogleAnalytics }}
googleAnalytics = "{{.GoogleAnalytics}}"
{{ end }}
[module]
[[module.imports]]
Expand Down
4 changes: 2 additions & 2 deletions internal/domain/contenthub/entity/contenthub.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ func (ch *ContentHub) GetPageSources(page contenthub.Page) ([]contenthub.PageSou
return v, nil
}

func (ch *ContentHub) GlobalPages() contenthub.Pages {
func (ch *ContentHub) GlobalPages(langIndex int) contenthub.Pages {
return ch.PageMap.getPagesInSection(
0,
langIndex,
pageMapQueryPagesInSection{
pageMapQueryPagesBelowPath: pageMapQueryPagesBelowPath{
Path: "",
Expand Down
9 changes: 9 additions & 0 deletions internal/domain/contenthub/entity/pagefields.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,19 @@ func (p *Page) RegularPages() contenthub.Pages {
return nil
}

func (p *Page) Sections(langIndex int) contenthub.Pages {
prefix := paths.AddTrailingSlash(p.Paths().Base())
return p.pageMap.getSections(langIndex, prefix)
}

func (p *Page) Terms(langIndex int, taxonomy string) contenthub.Pages {
return p.pageMap.getTermsForPageInTaxonomy(p.Paths().Base(), taxonomy)
}

func (p *Page) IsTranslated() bool {
return len(p.Translations()) > 0
}

func (p *Page) Translations() contenthub.Pages {
key := p.Path() + "/" + p.PageLanguage() + "/" + "translations"
pages, err := p.pageMap.getOrCreatePagesFromCache(nil, key, func(string) (contenthub.Pages, error) {
Expand Down
39 changes: 39 additions & 0 deletions internal/domain/contenthub/entity/pagemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,42 @@ func (m *PageMap) getTermsForPageInTaxonomy(base, taxonomy string) contenthub.Pa

return v
}

func (m *PageMap) getSections(langIndex int, prefix string) contenthub.Pages {
var (
pages contenthub.Pages
currentBranchPrefix string
tree = m.TreePages.Shape(0, langIndex)
)

w := &doctree.NodeShiftTreeWalker[*PageTreesNode]{
Tree: tree,
Prefix: prefix,
}
w.Handle = func(ss string, n *PageTreesNode, match doctree.DimensionFlag) (bool, error) {
p, found := n.getPage()
if !found {
return false, nil
}

if p.IsPage() {
return false, nil
}
if currentBranchPrefix == "" || !strings.HasPrefix(ss, currentBranchPrefix) {
if p.IsSection() && p.ShouldList(false) && p.Parent() == p {
pages = append(pages, p)
} else {
w.SkipPrefix(ss + "/")
}
}
currentBranchPrefix = ss + "/"
return false, nil
}

if err := w.Walk(context.Background()); err != nil {
panic(err)
}

valueobject.SortByDefault(pages)
return pages
}
9 changes: 9 additions & 0 deletions internal/domain/contenthub/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,11 @@ type Page interface {

Parent() Page
Pages(langIndex int) Pages
Sections(langIndex int) Pages
RegularPages() Pages
Terms(langIndex int, taxonomy string) Pages

IsTranslated() bool
Translations() Pages
}

Expand Down Expand Up @@ -317,12 +320,18 @@ type PagerManager interface {
Paginate(groups PageGroups) (Pager, error)
}

type Pagers []Pager

type Pager interface {
PageNumber() int
TotalPages() int

URL() string
Pages() Pages

Pagers() Pagers
First() Pager
Last() Pager
HasPrev() bool
Prev() Pager
HasNext() bool
Expand Down
10 changes: 10 additions & 0 deletions internal/domain/contenthub/valueobject/pagenop.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ var (
// PageNop implements Page, but does nothing.
type nopPage int

func (p *nopPage) Sections(langIndex int) contenthub.Pages {
//TODO implement me
panic("implement me")
}

func (p *nopPage) IsTranslated() bool {
//TODO implement me
panic("implement me")
}

func (p *nopPage) PageDate() time.Time {
//TODO implement me
panic("implement me")
Expand Down
8 changes: 8 additions & 0 deletions internal/domain/contenthub/valueobject/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ func (p *Pager) Next() contenthub.Pager {
return p.pagers[p.PageNumber()]
}

func (p *Pager) First() contenthub.Pager {
return p.pagers[0]
}

func (p *Pager) Last() contenthub.Pager {
return p.pagers[len(p.pagers)-1]
}

func (p *Pager) URL() string {
pageNumber := p.PageNumber()
if pageNumber > 1 {
Expand Down
10 changes: 8 additions & 2 deletions internal/domain/contenthub/valueobject/paginator.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,14 @@ func splitPageGroups(pageGroups contenthub.PageGroups, size int) []paginatedElem
return split
}

func (p *Paginator) Pagers() pagers {
return p.pagers
func (p *Paginator) Pagers() contenthub.Pagers {
var ps contenthub.Pagers

for _, pager := range p.pagers {
ps = append(ps, pager)
}

return ps
}

func (p *Paginator) TotalPages() int {
Expand Down
5 changes: 5 additions & 0 deletions internal/domain/markdown/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,14 @@ type Header interface {
Level() int

Links() []Link
Paragraphs() []Paragraph
}

type Link interface {
Text() string
URL() string
}

type Paragraph interface {
Text() string
}
21 changes: 21 additions & 0 deletions internal/domain/markdown/valueobject/nodeheader.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,24 @@ func (h *HeaderNode) Links() []markdown.Link {

return links
}

func (h *HeaderNode) Paragraphs() []markdown.Paragraph {
var paragraphs []markdown.Paragraph

for sibling := h.node.NextSibling(); sibling != nil; sibling = sibling.NextSibling() {
// 如果遇到下一个 Header,停止收集
if heading, ok := sibling.(*ast.Heading); ok {
if heading.Level <= h.Level() {
break
}
}

// 检查段落节点
if paragraph, ok := sibling.(*ast.Paragraph); ok {
text := extractAllTextFromNode(paragraph, h.src)
paragraphs = append(paragraphs, &ParagraphNode{text: text})
}
}

return paragraphs
}
9 changes: 9 additions & 0 deletions internal/domain/markdown/valueobject/nodeparagraph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package valueobject

type ParagraphNode struct {
text string
}

func (p *ParagraphNode) Text() string {
return p.text
}
Loading

0 comments on commit 7e73f95

Please sign in to comment.