mirror of
https://github.com/gohugoio/hugo.git
synced 2024-12-01 04:11:35 -05:00
7285e74090
There are some breaking changes in this commit, see #11455. Closes #11455 Closes #11549 This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build. The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate. A list of the notable new features: * A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server. * A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently. * You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs. We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections). Memory Limit * Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory. New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively. This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive): Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers. Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds. Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role). Fixes #10169 Fixes #10364 Fixes #10482 Fixes #10630 Fixes #10656 Fixes #10694 Fixes #10918 Fixes #11262 Fixes #11439 Fixes #11453 Fixes #11457 Fixes #11466 Fixes #11540 Fixes #11551 Fixes #11556 Fixes #11654 Fixes #11661 Fixes #11663 Fixes #11664 Fixes #11669 Fixes #11671 Fixes #11807 Fixes #11808 Fixes #11809 Fixes #11815 Fixes #11840 Fixes #11853 Fixes #11860 Fixes #11883 Fixes #11904 Fixes #7388 Fixes #7425 Fixes #7436 Fixes #7544 Fixes #7882 Fixes #7960 Fixes #8255 Fixes #8307 Fixes #8863 Fixes #8927 Fixes #9192 Fixes #9324
481 lines
13 KiB
Go
481 lines
13 KiB
Go
// Copyright 2019 The Hugo Authors. All rights reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package goldmark
|
|
|
|
import (
|
|
"bytes"
|
|
"strings"
|
|
|
|
"github.com/gohugoio/hugo/common/types/hstring"
|
|
"github.com/gohugoio/hugo/markup/converter/hooks"
|
|
"github.com/gohugoio/hugo/markup/goldmark/goldmark_config"
|
|
"github.com/gohugoio/hugo/markup/goldmark/images"
|
|
"github.com/gohugoio/hugo/markup/goldmark/internal/render"
|
|
"github.com/gohugoio/hugo/markup/internal/attributes"
|
|
|
|
"github.com/yuin/goldmark"
|
|
"github.com/yuin/goldmark/ast"
|
|
"github.com/yuin/goldmark/renderer"
|
|
"github.com/yuin/goldmark/renderer/html"
|
|
"github.com/yuin/goldmark/util"
|
|
)
|
|
|
|
var _ renderer.SetOptioner = (*hookedRenderer)(nil)
|
|
|
|
func newLinkRenderer(cfg goldmark_config.Config) renderer.NodeRenderer {
|
|
r := &hookedRenderer{
|
|
linkifyProtocol: []byte(cfg.Extensions.LinkifyProtocol),
|
|
Config: html.Config{
|
|
Writer: html.DefaultWriter,
|
|
},
|
|
}
|
|
return r
|
|
}
|
|
|
|
func newLinks(cfg goldmark_config.Config) goldmark.Extender {
|
|
return &links{cfg: cfg}
|
|
}
|
|
|
|
type linkContext struct {
|
|
page any
|
|
destination string
|
|
title string
|
|
text hstring.RenderedString
|
|
plainText string
|
|
*attributes.AttributesHolder
|
|
}
|
|
|
|
func (ctx linkContext) Destination() string {
|
|
return ctx.destination
|
|
}
|
|
|
|
func (ctx linkContext) Page() any {
|
|
return ctx.page
|
|
}
|
|
|
|
func (ctx linkContext) Text() hstring.RenderedString {
|
|
return ctx.text
|
|
}
|
|
|
|
func (ctx linkContext) PlainText() string {
|
|
return ctx.plainText
|
|
}
|
|
|
|
func (ctx linkContext) Title() string {
|
|
return ctx.title
|
|
}
|
|
|
|
type imageLinkContext struct {
|
|
linkContext
|
|
ordinal int
|
|
isBlock bool
|
|
}
|
|
|
|
func (ctx imageLinkContext) IsBlock() bool {
|
|
return ctx.isBlock
|
|
}
|
|
|
|
func (ctx imageLinkContext) Ordinal() int {
|
|
return ctx.ordinal
|
|
}
|
|
|
|
type headingContext struct {
|
|
page any
|
|
level int
|
|
anchor string
|
|
text hstring.RenderedString
|
|
plainText string
|
|
*attributes.AttributesHolder
|
|
}
|
|
|
|
func (ctx headingContext) Page() any {
|
|
return ctx.page
|
|
}
|
|
|
|
func (ctx headingContext) Level() int {
|
|
return ctx.level
|
|
}
|
|
|
|
func (ctx headingContext) Anchor() string {
|
|
return ctx.anchor
|
|
}
|
|
|
|
func (ctx headingContext) Text() hstring.RenderedString {
|
|
return ctx.text
|
|
}
|
|
|
|
func (ctx headingContext) PlainText() string {
|
|
return ctx.plainText
|
|
}
|
|
|
|
type hookedRenderer struct {
|
|
linkifyProtocol []byte
|
|
html.Config
|
|
}
|
|
|
|
func (r *hookedRenderer) SetOption(name renderer.OptionName, value any) {
|
|
r.Config.SetOption(name, value)
|
|
}
|
|
|
|
// RegisterFuncs implements NodeRenderer.RegisterFuncs.
|
|
func (r *hookedRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
|
|
reg.Register(ast.KindLink, r.renderLink)
|
|
reg.Register(ast.KindAutoLink, r.renderAutoLink)
|
|
reg.Register(ast.KindImage, r.renderImage)
|
|
reg.Register(ast.KindHeading, r.renderHeading)
|
|
}
|
|
|
|
func (r *hookedRenderer) renderImage(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
n := node.(*ast.Image)
|
|
var lr hooks.LinkRenderer
|
|
|
|
ctx, ok := w.(*render.Context)
|
|
if ok {
|
|
h := ctx.RenderContext().GetRenderer(hooks.ImageRendererType, nil)
|
|
ok = h != nil
|
|
if ok {
|
|
lr = h.(hooks.LinkRenderer)
|
|
}
|
|
}
|
|
|
|
if !ok {
|
|
return r.renderImageDefault(w, source, node, entering)
|
|
}
|
|
|
|
if entering {
|
|
// Store the current pos so we can capture the rendered text.
|
|
ctx.PushPos(ctx.Buffer.Len())
|
|
return ast.WalkContinue, nil
|
|
}
|
|
|
|
pos := ctx.PopPos()
|
|
text := ctx.Buffer.Bytes()[pos:]
|
|
ctx.Buffer.Truncate(pos)
|
|
|
|
var (
|
|
isBlock bool
|
|
ordinal int
|
|
)
|
|
if b, ok := n.AttributeString(images.AttrIsBlock); ok && b.(bool) {
|
|
isBlock = true
|
|
}
|
|
if n, ok := n.AttributeString(images.AttrOrdinal); ok {
|
|
ordinal = n.(int)
|
|
}
|
|
|
|
// We use the attributes to signal from the parser whether the image is in
|
|
// a block context or not.
|
|
// We may find a better way to do that, but for now, we'll need to remove any
|
|
// internal attributes before rendering.
|
|
attrs := r.filterInternalAttributes(n.Attributes())
|
|
|
|
err := lr.RenderLink(
|
|
ctx.RenderContext().Ctx,
|
|
w,
|
|
imageLinkContext{
|
|
linkContext: linkContext{
|
|
page: ctx.DocumentContext().Document,
|
|
destination: string(n.Destination),
|
|
title: string(n.Title),
|
|
text: hstring.RenderedString(text),
|
|
plainText: string(n.Text(source)),
|
|
AttributesHolder: attributes.New(attrs, attributes.AttributesOwnerGeneral),
|
|
},
|
|
ordinal: ordinal,
|
|
isBlock: isBlock,
|
|
},
|
|
)
|
|
|
|
return ast.WalkContinue, err
|
|
}
|
|
|
|
func (r *hookedRenderer) filterInternalAttributes(attrs []ast.Attribute) []ast.Attribute {
|
|
n := 0
|
|
for _, x := range attrs {
|
|
if !bytes.HasPrefix(x.Name, []byte(internalAttrPrefix)) {
|
|
attrs[n] = x
|
|
n++
|
|
}
|
|
}
|
|
return attrs[:n]
|
|
}
|
|
|
|
// Fall back to the default Goldmark render funcs. Method below borrowed from:
|
|
// https://github.com/yuin/goldmark/blob/b611cd333a492416b56aa8d94b04a67bf0096ab2/renderer/html/html.go#L404
|
|
func (r *hookedRenderer) renderImageDefault(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
if !entering {
|
|
return ast.WalkContinue, nil
|
|
}
|
|
n := node.(*ast.Image)
|
|
_, _ = w.WriteString("<img src=\"")
|
|
if r.Unsafe || !html.IsDangerousURL(n.Destination) {
|
|
_, _ = w.Write(util.EscapeHTML(util.URLEscape(n.Destination, true)))
|
|
}
|
|
_, _ = w.WriteString(`" alt="`)
|
|
_, _ = w.Write(nodeToHTMLText(n, source))
|
|
_ = w.WriteByte('"')
|
|
if n.Title != nil {
|
|
_, _ = w.WriteString(` title="`)
|
|
r.Writer.Write(w, n.Title)
|
|
_ = w.WriteByte('"')
|
|
}
|
|
if n.Attributes() != nil {
|
|
attrs := r.filterInternalAttributes(n.Attributes())
|
|
attributes.RenderASTAttributes(w, attrs...)
|
|
}
|
|
if r.XHTML {
|
|
_, _ = w.WriteString(" />")
|
|
} else {
|
|
_, _ = w.WriteString(">")
|
|
}
|
|
return ast.WalkSkipChildren, nil
|
|
}
|
|
|
|
func (r *hookedRenderer) renderLink(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
n := node.(*ast.Link)
|
|
var lr hooks.LinkRenderer
|
|
|
|
ctx, ok := w.(*render.Context)
|
|
if ok {
|
|
h := ctx.RenderContext().GetRenderer(hooks.LinkRendererType, nil)
|
|
ok = h != nil
|
|
if ok {
|
|
lr = h.(hooks.LinkRenderer)
|
|
}
|
|
}
|
|
|
|
if !ok {
|
|
return r.renderLinkDefault(w, source, node, entering)
|
|
}
|
|
|
|
if entering {
|
|
// Store the current pos so we can capture the rendered text.
|
|
ctx.PushPos(ctx.Buffer.Len())
|
|
return ast.WalkContinue, nil
|
|
}
|
|
|
|
pos := ctx.PopPos()
|
|
text := ctx.Buffer.Bytes()[pos:]
|
|
ctx.Buffer.Truncate(pos)
|
|
|
|
err := lr.RenderLink(
|
|
ctx.RenderContext().Ctx,
|
|
w,
|
|
linkContext{
|
|
page: ctx.DocumentContext().Document,
|
|
destination: string(n.Destination),
|
|
title: string(n.Title),
|
|
text: hstring.RenderedString(text),
|
|
plainText: string(n.Text(source)),
|
|
AttributesHolder: attributes.Empty,
|
|
},
|
|
)
|
|
|
|
return ast.WalkContinue, err
|
|
}
|
|
|
|
// Fall back to the default Goldmark render funcs. Method below borrowed from:
|
|
// https://github.com/yuin/goldmark/blob/b611cd333a492416b56aa8d94b04a67bf0096ab2/renderer/html/html.go#L404
|
|
func (r *hookedRenderer) renderLinkDefault(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
n := node.(*ast.Link)
|
|
if entering {
|
|
_, _ = w.WriteString("<a href=\"")
|
|
if r.Unsafe || !html.IsDangerousURL(n.Destination) {
|
|
_, _ = w.Write(util.EscapeHTML(util.URLEscape(n.Destination, true)))
|
|
}
|
|
_ = w.WriteByte('"')
|
|
if n.Title != nil {
|
|
_, _ = w.WriteString(` title="`)
|
|
r.Writer.Write(w, n.Title)
|
|
_ = w.WriteByte('"')
|
|
}
|
|
_ = w.WriteByte('>')
|
|
} else {
|
|
_, _ = w.WriteString("</a>")
|
|
}
|
|
return ast.WalkContinue, nil
|
|
}
|
|
|
|
func (r *hookedRenderer) renderAutoLink(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
if !entering {
|
|
return ast.WalkContinue, nil
|
|
}
|
|
|
|
n := node.(*ast.AutoLink)
|
|
var lr hooks.LinkRenderer
|
|
|
|
ctx, ok := w.(*render.Context)
|
|
if ok {
|
|
h := ctx.RenderContext().GetRenderer(hooks.LinkRendererType, nil)
|
|
ok = h != nil
|
|
if ok {
|
|
lr = h.(hooks.LinkRenderer)
|
|
}
|
|
}
|
|
|
|
if !ok {
|
|
return r.renderAutoLinkDefault(w, source, node, entering)
|
|
}
|
|
|
|
url := string(r.autoLinkURL(n, source))
|
|
label := string(n.Label(source))
|
|
if n.AutoLinkType == ast.AutoLinkEmail && !strings.HasPrefix(strings.ToLower(url), "mailto:") {
|
|
url = "mailto:" + url
|
|
}
|
|
|
|
err := lr.RenderLink(
|
|
ctx.RenderContext().Ctx,
|
|
w,
|
|
linkContext{
|
|
page: ctx.DocumentContext().Document,
|
|
destination: url,
|
|
text: hstring.RenderedString(label),
|
|
plainText: label,
|
|
AttributesHolder: attributes.Empty,
|
|
},
|
|
)
|
|
|
|
return ast.WalkContinue, err
|
|
}
|
|
|
|
// Fall back to the default Goldmark render funcs. Method below borrowed from:
|
|
// https://github.com/yuin/goldmark/blob/5588d92a56fe1642791cf4aa8e9eae8227cfeecd/renderer/html/html.go#L439
|
|
func (r *hookedRenderer) renderAutoLinkDefault(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
n := node.(*ast.AutoLink)
|
|
if !entering {
|
|
return ast.WalkContinue, nil
|
|
}
|
|
|
|
_, _ = w.WriteString(`<a href="`)
|
|
url := r.autoLinkURL(n, source)
|
|
label := n.Label(source)
|
|
if n.AutoLinkType == ast.AutoLinkEmail && !bytes.HasPrefix(bytes.ToLower(url), []byte("mailto:")) {
|
|
_, _ = w.WriteString("mailto:")
|
|
}
|
|
_, _ = w.Write(util.EscapeHTML(util.URLEscape(url, false)))
|
|
if n.Attributes() != nil {
|
|
_ = w.WriteByte('"')
|
|
html.RenderAttributes(w, n, html.LinkAttributeFilter)
|
|
_ = w.WriteByte('>')
|
|
} else {
|
|
_, _ = w.WriteString(`">`)
|
|
}
|
|
_, _ = w.Write(util.EscapeHTML(label))
|
|
_, _ = w.WriteString(`</a>`)
|
|
return ast.WalkContinue, nil
|
|
}
|
|
|
|
func (r *hookedRenderer) autoLinkURL(n *ast.AutoLink, source []byte) []byte {
|
|
url := n.URL(source)
|
|
if len(n.Protocol) > 0 && !bytes.Equal(n.Protocol, r.linkifyProtocol) {
|
|
// The CommonMark spec says "http" is the correct protocol for links,
|
|
// but this doesn't make much sense (the fact that they should care about the rendered output).
|
|
// Note that n.Protocol is not set if protocol is provided by user.
|
|
url = append(r.linkifyProtocol, url[len(n.Protocol):]...)
|
|
}
|
|
return url
|
|
}
|
|
|
|
func (r *hookedRenderer) renderHeading(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
n := node.(*ast.Heading)
|
|
var hr hooks.HeadingRenderer
|
|
|
|
ctx, ok := w.(*render.Context)
|
|
if ok {
|
|
h := ctx.RenderContext().GetRenderer(hooks.HeadingRendererType, nil)
|
|
ok = h != nil
|
|
if ok {
|
|
hr = h.(hooks.HeadingRenderer)
|
|
}
|
|
}
|
|
|
|
if !ok {
|
|
return r.renderHeadingDefault(w, source, node, entering)
|
|
}
|
|
|
|
if entering {
|
|
// Store the current pos so we can capture the rendered text.
|
|
ctx.PushPos(ctx.Buffer.Len())
|
|
return ast.WalkContinue, nil
|
|
}
|
|
|
|
pos := ctx.PopPos()
|
|
text := ctx.Buffer.Bytes()[pos:]
|
|
ctx.Buffer.Truncate(pos)
|
|
// All ast.Heading nodes are guaranteed to have an attribute called "id"
|
|
// that is an array of bytes that encode a valid string.
|
|
anchori, _ := n.AttributeString("id")
|
|
anchor := anchori.([]byte)
|
|
|
|
err := hr.RenderHeading(
|
|
ctx.RenderContext().Ctx,
|
|
w,
|
|
headingContext{
|
|
page: ctx.DocumentContext().Document,
|
|
level: n.Level,
|
|
anchor: string(anchor),
|
|
text: hstring.RenderedString(text),
|
|
plainText: string(n.Text(source)),
|
|
AttributesHolder: attributes.New(n.Attributes(), attributes.AttributesOwnerGeneral),
|
|
},
|
|
)
|
|
|
|
return ast.WalkContinue, err
|
|
}
|
|
|
|
func (r *hookedRenderer) renderHeadingDefault(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
n := node.(*ast.Heading)
|
|
if entering {
|
|
_, _ = w.WriteString("<h")
|
|
_ = w.WriteByte("0123456"[n.Level])
|
|
if n.Attributes() != nil {
|
|
attributes.RenderASTAttributes(w, node.Attributes()...)
|
|
}
|
|
_ = w.WriteByte('>')
|
|
} else {
|
|
_, _ = w.WriteString("</h")
|
|
_ = w.WriteByte("0123456"[n.Level])
|
|
_, _ = w.WriteString(">\n")
|
|
}
|
|
return ast.WalkContinue, nil
|
|
}
|
|
|
|
type links struct {
|
|
cfg goldmark_config.Config
|
|
}
|
|
|
|
// Extend implements goldmark.Extender.
|
|
func (e *links) Extend(m goldmark.Markdown) {
|
|
m.Renderer().AddOptions(renderer.WithNodeRenderers(
|
|
util.Prioritized(newLinkRenderer(e.cfg), 100),
|
|
))
|
|
}
|
|
|
|
// Borrowed from Goldmark.
|
|
func nodeToHTMLText(n ast.Node, source []byte) []byte {
|
|
var buf bytes.Buffer
|
|
for c := n.FirstChild(); c != nil; c = c.NextSibling() {
|
|
if s, ok := c.(*ast.String); ok && s.IsCode() {
|
|
buf.Write(s.Text(source))
|
|
} else if !c.HasChildren() {
|
|
buf.Write(util.EscapeHTML(c.Text(source)))
|
|
if t, ok := c.(*ast.Text); ok && t.SoftLineBreak() {
|
|
buf.WriteByte('\n')
|
|
}
|
|
} else {
|
|
buf.Write(nodeToHTMLText(c, source))
|
|
}
|
|
}
|
|
return buf.Bytes()
|
|
}
|