diff options
Diffstat (limited to 'markup')
-rw-r--r-- | markup/converter/converter.go | 2 | ||||
-rw-r--r-- | markup/converter/hooks/hooks.go | 54 | ||||
-rw-r--r-- | markup/goldmark/render_hooks.go | 12 |
3 files changed, 54 insertions, 14 deletions
diff --git a/markup/converter/converter.go b/markup/converter/converter.go index d7e3d4639..3fa3bea39 100644 --- a/markup/converter/converter.go +++ b/markup/converter/converter.go @@ -127,7 +127,7 @@ type DocumentContext struct { type RenderContext struct { Src []byte RenderTOC bool - RenderHooks *hooks.Renderers + RenderHooks hooks.Renderers } var FeatureRenderHooks = identity.NewPathIdentity("markup", "renderingHooks") diff --git a/markup/converter/hooks/hooks.go b/markup/converter/hooks/hooks.go index 6f08a2161..d36dad288 100644 --- a/markup/converter/hooks/hooks.go +++ b/markup/converter/hooks/hooks.go @@ -14,7 +14,9 @@ package hooks import ( + "fmt" "io" + "strings" "github.com/gohugoio/hugo/identity" ) @@ -67,26 +69,64 @@ type Renderers struct { HeadingRenderer HeadingRenderer } -func (r *Renderers) Eq(other interface{}) bool { - ro, ok := other.(*Renderers) +func (r Renderers) Eq(other interface{}) bool { + ro, ok := other.(Renderers) if !ok { return false } - if r == nil || ro == nil { - return r == nil + + if r.IsZero() || ro.IsZero() { + return r.IsZero() && ro.IsZero() } - if r.ImageRenderer.GetIdentity() != ro.ImageRenderer.GetIdentity() { + var b1, b2 bool + b1, b2 = r.ImageRenderer == nil, ro.ImageRenderer == nil + if (b1 || b2) && (b1 != b2) { + return false + } + if !b1 && r.ImageRenderer.GetIdentity() != ro.ImageRenderer.GetIdentity() { return false } - if r.LinkRenderer.GetIdentity() != ro.LinkRenderer.GetIdentity() { + b1, b2 = r.LinkRenderer == nil, ro.LinkRenderer == nil + if (b1 || b2) && (b1 != b2) { + return false + } + if !b1 && r.LinkRenderer.GetIdentity() != ro.LinkRenderer.GetIdentity() { return false } - if r.HeadingRenderer.GetIdentity() != ro.HeadingRenderer.GetIdentity() { + b1, b2 = r.HeadingRenderer == nil, ro.HeadingRenderer == nil + if (b1 || b2) && (b1 != b2) { + return false + } + if !b1 && r.HeadingRenderer.GetIdentity() != ro.HeadingRenderer.GetIdentity() { return false } return true } + +func (r Renderers) IsZero() bool { + return r.HeadingRenderer == nil && r.LinkRenderer == nil && r.ImageRenderer == nil +} + +func (r Renderers) String() string { + if r.IsZero() { + return "<zero>" + } + + var sb strings.Builder + + if r.LinkRenderer != nil { + sb.WriteString(fmt.Sprintf("LinkRenderer<%s>|", r.LinkRenderer.GetIdentity())) + } + if r.HeadingRenderer != nil { + sb.WriteString(fmt.Sprintf("HeadingRenderer<%s>|", r.HeadingRenderer.GetIdentity())) + } + if r.ImageRenderer != nil { + sb.WriteString(fmt.Sprintf("ImageRenderer<%s>|", r.ImageRenderer.GetIdentity())) + } + + return sb.String() +} diff --git a/markup/goldmark/render_hooks.go b/markup/goldmark/render_hooks.go index 41db4011b..5e0865a70 100644 --- a/markup/goldmark/render_hooks.go +++ b/markup/goldmark/render_hooks.go @@ -206,12 +206,12 @@ func (r *hookedRenderer) renderDefaultImage(w util.BufWriter, source []byte, nod func (r *hookedRenderer) renderImage(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { n := node.(*ast.Image) - var h *hooks.Renderers + var h hooks.Renderers ctx, ok := w.(*renderContext) if ok { h = ctx.RenderContext().RenderHooks - ok = h != nil && h.ImageRenderer != nil + ok = h.ImageRenderer != nil } if !ok { @@ -267,12 +267,12 @@ func (r *hookedRenderer) renderDefaultLink(w util.BufWriter, source []byte, node func (r *hookedRenderer) renderLink(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { n := node.(*ast.Link) - var h *hooks.Renderers + var h hooks.Renderers ctx, ok := w.(*renderContext) if ok { h = ctx.RenderContext().RenderHooks - ok = h != nil && h.LinkRenderer != nil + ok = h.LinkRenderer != nil } if !ok { @@ -326,12 +326,12 @@ func (r *hookedRenderer) renderDefaultHeading(w util.BufWriter, source []byte, n func (r *hookedRenderer) renderHeading(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { n := node.(*ast.Heading) - var h *hooks.Renderers + var h hooks.Renderers ctx, ok := w.(*renderContext) if ok { h = ctx.RenderContext().RenderHooks - ok = h != nil && h.HeadingRenderer != nil + ok = h.HeadingRenderer != nil } if !ok { |