From 494e88abf6007c48e51e5e065936ba88b3b75a87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Tue, 18 Feb 2025 09:30:47 +0100 Subject: [PATCH] markup/goldmark: Fix panic on empty Markdown header Fixes #13416 --- markup/goldmark/render_hooks.go | 8 ++++---- markup/goldmark/toc_integration_test.go | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/markup/goldmark/render_hooks.go b/markup/goldmark/render_hooks.go index 12cf00455..1e91f7ab1 100644 --- a/markup/goldmark/render_hooks.go +++ b/markup/goldmark/render_hooks.go @@ -499,10 +499,10 @@ func (r *hookedRenderer) renderHeading(w util.BufWriter, source []byte, node ast text := ctx.PopRenderedString() - // 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) + var anchor []byte + if anchori, ok := n.AttributeString("id"); ok { + anchor, _ = anchori.([]byte) + } page, pageInner := render.GetPageAndPageInner(ctx) diff --git a/markup/goldmark/toc_integration_test.go b/markup/goldmark/toc_integration_test.go index 7ce2e8664..814ae199b 100644 --- a/markup/goldmark/toc_integration_test.go +++ b/markup/goldmark/toc_integration_test.go @@ -258,7 +258,29 @@ title: p7 (emoji) `) // emoji + b.AssertFileContent("public/p7/index.html", `
  • A 🐍 emoji
  • `) } + +func TestIssue13416(t *testing.T) { + t.Parallel() + + files := ` +-- hugo.toml -- +disableKinds = ['page','rss','section','sitemap','taxonomy','term'] +-- layouts/index.html -- +Content:{{ .Content }}| +-- layouts/_default/_markup/render-heading.html -- +-- content/_index.md -- +--- +title: home +--- +# +` + + b := hugolib.Test(t, files) + + b.AssertFileExists("public/index.html", true) +}