Skip to content

Commit

Permalink
enable default taxonomy
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwei committed Nov 24, 2024
1 parent aa00fd0 commit 6f26cee
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 34 deletions.
4 changes: 4 additions & 0 deletions internal/domain/contenthub/entity/pagefields.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func (p *Page) Pages(langIndex int) contenthub.Pages {
return nil
}

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

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
32 changes: 32 additions & 0 deletions internal/domain/contenthub/entity/pagemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/gohugonet/hugoverse/pkg/loggers"
"github.com/gohugonet/hugoverse/pkg/paths"
"path"
"strings"
)

type PageMap struct {
Expand Down Expand Up @@ -348,3 +349,34 @@ func (m *PageMap) getPagesWithTerm(q pageMapQueryPagesBelowPath) contenthub.Page

return v
}

func (m *PageMap) getTermsForPageInTaxonomy(base, taxonomy string) contenthub.Pages {
prefix := paths.AddLeadingSlash(taxonomy)

v, err := m.Cache.CachePages1.GetOrCreate(prefix+base, func(string) (contenthub.Pages, error) {
var pas contenthub.Pages

err := m.TreeTaxonomyEntries.WalkPrefix(
doctree.LockTypeNone,
paths.AddTrailingSlash(prefix),
func(s string, n *WeightedTermTreeNode) (bool, error) {
if strings.HasSuffix(s, base) {
pas = append(pas, n.term)
}
return false, nil
},
)
if err != nil {
return nil, err
}

valueobject.SortByDefault(pas)

return pas, nil
})
if err != nil {
panic(err)
}

return v
}
14 changes: 8 additions & 6 deletions internal/domain/contenthub/entity/term.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ func (t *Term) Assemble(pages *doctree.NodeShiftTree[*PageTreesNode],
}

viewTermKey := "/" + viewName.Plural() + "/" + v
term := pages.Get(viewTermKey)

if term == nil {
fmi := t.FsSvc.NewFileMetaInfo(viewTermKey + "/_index.md")
f := valueobject.NewFileInfo(fmi)
fmi := t.FsSvc.NewFileMetaInfo(viewTermKey + "/_index.md")
f := valueobject.NewFileInfo(fmi)
pi := f.Paths()

term := pages.Get(pi.Base())

if term == nil {
ps, err := newPageSource(f, t.Cache)
if err != nil {
return false, err
Expand All @@ -67,7 +69,7 @@ func (t *Term) Assemble(pages *doctree.NodeShiftTree[*PageTreesNode],
}

pages.InsertIntoValuesDimension(viewTermKey, newPageTreesNode(p))
term = pages.Get(viewTermKey)
term = pages.Get(pi.Base())
} else {
tp, found := term.getPage()
if !found {
Expand All @@ -84,7 +86,7 @@ func (t *Term) Assemble(pages *doctree.NodeShiftTree[*PageTreesNode],
s = "/"
}

key := viewTermKey + s
key := pi.Base() + s

tp, found := term.getPage()
if !found {
Expand Down
1 change: 1 addition & 0 deletions internal/domain/contenthub/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ type Page interface {
PageOutputs() ([]PageOutput, error)

Pages(langIndex int) Pages
Terms(langIndex int, taxonomy string) Pages
Translations() Pages
}

Expand Down
5 changes: 5 additions & 0 deletions internal/domain/contenthub/valueobject/pagenop.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ var (
// PageNop implements Page, but does nothing.
type nopPage int

func (p *nopPage) Terms(langIndex int, taxonomy string) contenthub.Pages {
//TODO implement me
panic("implement me")
}

func (p *nopPage) ShouldList(global bool) bool {
//TODO implement me
panic("implement me")
Expand Down
9 changes: 6 additions & 3 deletions internal/domain/markdown/factory/gmbuilder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package factory

import (
"github.com/gohugonet/hugoverse/internal/domain/markdown"
"github.com/gohugonet/hugoverse/internal/domain/markdown/valueobject"
"github.com/yuin/goldmark"
emoji "github.com/yuin/goldmark-emoji"
Expand All @@ -13,16 +14,18 @@ import (

type Builder struct {
cfg valueobject.GoldMarkConfig
markdown.Highlighter

rendererOptions []renderer.Option
tocRendererOptions []renderer.Option
parserOptions []parser.Option
extensions []goldmark.Extender
}

func NewGoldMarkBuilder(cfg valueobject.GoldMarkConfig) *Builder {
func NewGoldMarkBuilder(cfg valueobject.GoldMarkConfig, highlighter markdown.Highlighter) *Builder {
return &Builder{
cfg: cfg,
cfg: cfg,
Highlighter: highlighter,

rendererOptions: []renderer.Option{},
tocRendererOptions: nil,
Expand Down Expand Up @@ -87,7 +90,7 @@ func (b *Builder) WithImage() {

func (b *Builder) WithCodeFences() {
if b.cfg.Extensions.Highlight.CodeFences {
b.extensions = append(b.extensions, valueobject.NewCodeBlocksExt())
b.extensions = append(b.extensions, valueobject.NewCodeBlocksExt(b.Highlighter))
}
}

Expand Down
6 changes: 4 additions & 2 deletions internal/domain/markdown/factory/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ var once sync.Once

func NewMarkdown() markdown.Markdown {
once.Do(func() {
builder := NewGoldMarkBuilder(valueobject.DefaultGoldMarkConf)
hl := valueobject.NewDefaultHighlighter()

builder := NewGoldMarkBuilder(valueobject.DefaultGoldMarkConf, hl)
md := builder.Build()

instance = &entity.Markdown{
GoldMark: md,
Highlighter: valueobject.NewHighlighter(valueobject.DefaultHighlightConfig),
Highlighter: hl,
}
})

Expand Down
2 changes: 2 additions & 0 deletions internal/domain/markdown/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ type ElementPositionResolver interface {
// CodeblockContext is the context passed to a code block render hook.
type CodeblockContext interface {
AttributesProvider
AttributesOptionsSliceProvider

text.Positioner

// Chroma highlighting processing options. This will only be filled if Type is a known Chroma Lexer.
Expand Down
36 changes: 32 additions & 4 deletions internal/domain/markdown/valueobject/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ func (a Attribute) ValueString() string {
return cast.ToString(a.Value)
}

type attribute struct {
Attribute
}

func (a attribute) Name() string {
return a.Attribute.Name
}

func (a attribute) Value() any {
return a.Attribute.Value
}

func (a attribute) ValueString() string {
return a.Attribute.ValueString()
}

// EmptyAttr holds no attributes.
var EmptyAttr = &AttributesHolder{}

Expand Down Expand Up @@ -144,12 +160,24 @@ func (a *AttributesHolder) Options() map[string]any {
return a.optionsMap
}

func (a *AttributesHolder) AttributesSlice() []Attribute {
return a.attributes
func (a *AttributesHolder) AttributesSlice() []markdown.Attribute {
attrs := make([]markdown.Attribute, len(a.attributes))

for i, v := range a.attributes {
attrs[i] = attribute{Attribute: v}
}

return attrs
}

func (a *AttributesHolder) OptionsSlice() []Attribute {
return a.options
func (a *AttributesHolder) OptionsSlice() []markdown.Attribute {
attrs := make([]markdown.Attribute, len(a.options))

for i, v := range a.options {
attrs[i] = attribute{Attribute: v}
}

return attrs
}

// RenderASTAttributes writes the AST attributes to the given as attributes to an HTML element.
Expand Down
36 changes: 22 additions & 14 deletions internal/domain/markdown/valueobject/extcodeblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package valueobject
import (
"bytes"
"errors"
"fmt"
"github.com/gohugonet/hugoverse/internal/domain/markdown"
"strings"
"sync"
Expand All @@ -32,12 +31,18 @@ import (
)

type (
codeBlocksExtension struct{}
htmlRenderer struct{}
codeBlocksExtension struct {
markdown.Highlighter
}
htmlRenderer struct {
markdown.Highlighter
}
)

func NewCodeBlocksExt() goldmark.Extender {
return &codeBlocksExtension{}
func NewCodeBlocksExt(highlighter markdown.Highlighter) goldmark.Extender {
return &codeBlocksExtension{
highlighter,
}
}

func (e *codeBlocksExtension) Extend(m goldmark.Markdown) {
Expand All @@ -47,12 +52,14 @@ func (e *codeBlocksExtension) Extend(m goldmark.Markdown) {
),
)
m.Renderer().AddOptions(renderer.WithNodeRenderers(
util.Prioritized(newHTMLRenderer(), 100),
util.Prioritized(newHTMLRenderer(e.Highlighter), 100),
))
}

func newHTMLRenderer() renderer.NodeRenderer {
r := &htmlRenderer{}
func newHTMLRenderer(highlighter markdown.Highlighter) renderer.NodeRenderer {
r := &htmlRenderer{
highlighter,
}
return r
}

Expand All @@ -69,9 +76,10 @@ func (r *htmlRenderer) renderCodeBlock(w util.BufWriter, src []byte, node ast.No

n := node.(*codeBlock)
lang := getLang(n.b, src)
renderer := ctx.RenderContext().GetRenderer(markdown.CodeBlockRendererType, lang)
if renderer == nil {
return ast.WalkStop, fmt.Errorf("no code renderer found for %q", lang)
cbRenderer := ctx.RenderContext().GetRenderer(markdown.CodeBlockRendererType, lang)
if cbRenderer == nil {
cbRenderer = r.Highlighter // use default highlighter code block renderer
//return ast.WalkStop, fmt.Errorf("no code renderer found for %q", lang)
}

ordinal := n.ordinal
Expand All @@ -92,7 +100,7 @@ func (r *htmlRenderer) renderCodeBlock(w util.BufWriter, src []byte, node ast.No
}

attrtp := AttributesOwnerCodeBlockCustom
if isd, ok := renderer.(markdown.IsDefaultCodeBlockRendererProvider); (ok && isd.IsDefaultCodeBlockRenderer()) || GetChromaLexer(lang) != nil {
if isd, ok := cbRenderer.(markdown.IsDefaultCodeBlockRendererProvider); (ok && isd.IsDefaultCodeBlockRenderer()) || GetChromaLexer(lang) != nil {
// We say that this is a Chroma code block if it's the default code block renderer
// or if the language is supported by Chroma.
attrtp = AttributesOwnerCodeBlockChroma
Expand All @@ -112,7 +120,7 @@ func (r *htmlRenderer) renderCodeBlock(w util.BufWriter, src []byte, node ast.No
}

cbctx.createPos = func() htext.Position {
if resolver, ok := renderer.(markdown.ElementPositionResolver); ok {
if resolver, ok := cbRenderer.(markdown.ElementPositionResolver); ok {
return resolver.ResolvePosition(cbctx)
}
return htext.Position{
Expand All @@ -122,7 +130,7 @@ func (r *htmlRenderer) renderCodeBlock(w util.BufWriter, src []byte, node ast.No
}
}

cr := renderer.(markdown.CodeBlockRenderer)
cr := cbRenderer.(markdown.CodeBlockRenderer)

err = cr.RenderCodeblock(
ctx.RenderContext().Ctx,
Expand Down
4 changes: 4 additions & 0 deletions internal/domain/markdown/valueobject/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func init() {
}
}

func NewDefaultHighlighter() markdown.Highlighter {
return NewHighlighter(DefaultHighlightConfig)
}

func NewHighlighter(cfg HighlightConfig) markdown.Highlighter {
return chromaHighlighter{
cfg: cfg,
Expand Down
4 changes: 2 additions & 2 deletions internal/domain/resources/valueobject/resourcepaths.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func (d ResourcePaths) join(p ...string) string {
s += pp

}
if !strings.HasPrefix(s, "/") {
s = "/" + s
if strings.HasPrefix(s, "/") {
s = paths.ToSlashTrimLeading(s)
}
return s
}
Expand Down
9 changes: 9 additions & 0 deletions internal/domain/site/entity/pagefields.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ func (p *Page) Pages() []*Page {
return p.sitePages(ps)
}

func (p *Page) GetTerms(taxonomy string) []*Page {
ps := p.Page.Terms(p.Site.Language.CurrentLanguageIndex(), taxonomy)
if ps == nil {
return make([]*Page, 0)
}

return p.sitePages(ps)
}

func (p *Page) Translations() []*Page {
return p.sitePages(p.Page.Translations())
}
Expand Down
3 changes: 2 additions & 1 deletion internal/domain/site/entity/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func (r PageResources) ByType(typ any) PageResources {
var filtered PageResources

for _, resource := range r {
if resource.ResourceType() == tpstr {
rt := resource.ResourceType()
if rt == tpstr || strings.HasPrefix(rt, tpstr) {
filtered = append(filtered, resource)
}
}
Expand Down
15 changes: 13 additions & 2 deletions internal/domain/site/entity/sitefields.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (s *Site) sitePage(p contenthub.Page) (*Page, error) {
}
po := pos[0] // TODO, check for multiple outputs

return &Page{
sp := &Page{
resSvc: s.ResourcesSvc,
tmplSvc: s.Template,
langSvc: s.LanguageSvc,
Expand All @@ -92,5 +92,16 @@ func (s *Site) sitePage(p contenthub.Page) (*Page, error) {
Page: p,
PageOutput: po,
Site: s,
}, nil
}

sources, err := s.ContentSvc.GetPageSources(sp.Page)
if err != nil {
return nil, err
}

if err := sp.processResources(sources); err != nil {
return nil, err
}

return sp, nil
}
Loading

0 comments on commit 6f26cee

Please sign in to comment.