From 27bb5549e276c1382ef8c983504038d798acfdb7 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Thu, 30 Apr 2026 12:08:43 +0100 Subject: [PATCH 1/2] fix: Add tests for LinkForType Signed-off-by: Stephen Finucane --- renderer/functions_test.go | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/renderer/functions_test.go b/renderer/functions_test.go index c312c1c..7104023 100644 --- a/renderer/functions_test.go +++ b/renderer/functions_test.go @@ -24,6 +24,60 @@ import ( "github.com/stretchr/testify/require" ) +func TestLinkForType(t *testing.T) { + conf := config.Config{ + Render: config.RenderConfig{ + KubernetesVersion: "1.29", + KnownTypes: []*config.KnownType{ + // Register our own package to verify it is ignored for local types. + {Name: "Foo", Package: "example.com/pkg", Link: "https://example.com/docs#foo"}, + // Also register a kube type to verify IsKubeType takes precedence over knownTypes. + {Name: "ObjectMeta", Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Link: "https://example.com/docs#objectmeta"}, + }, + }, + } + + f, err := NewFunctions(&conf) + require.NoError(t, err) + + cases := []struct { + name string + typ *types.Type + wantLink string + wantLocal bool + }{ + { + name: "imported type gets external link from knownTypes", + typ: &types.Type{Name: "Foo", Package: "example.com/pkg", Imported: true}, + wantLink: "https://example.com/docs#foo", + wantLocal: false, + }, + { + name: "local type ignores knownTypes and gets local link", + typ: &types.Type{Name: "Foo", Package: "example.com/pkg", Imported: false}, + // FIXME: This is incorrect and should be a relative link + // wantLink: "example-com-pkg-foo", + // wantLocal: true, + wantLink: "https://example.com/docs#foo", + wantLocal: false, + }, + { + name: "kube type gets kubernetes.io link even when also in knownTypes", + typ: &types.Type{Package: "k8s.io/apimachinery/pkg/apis/meta/v1", Name: "ObjectMeta"}, + wantLink: "https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.29/#objectmeta-v1-meta", + wantLocal: false, + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + link, local := f.LinkForType(tc.typ) + require.Equal(t, tc.wantLink, link) + require.Equal(t, tc.wantLocal, local) + }) + } +} + func TestKubernetesHelper(t *testing.T) { conf := config.Config{ Render: config.RenderConfig{ From eaea7f363c7b9fdff2fa02490c24af3c775f53c8 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Thu, 30 Apr 2026 12:08:48 +0100 Subject: [PATCH 2/2] fix: knownTypes should only apply to imported types knownTypes entries were being applied to all types regardless of whether they were local to the source package or imported from another package. This caused local types that appeared in knownTypes to be rendered with external links rather than local anchor links, breaking in-page navigation when rendered locally. Move the lookup inside the IsBasic/Imported branch so that it only applies to types that cannot resolve to a local anchor. Signed-off-by: Stephen Finucane --- renderer/functions.go | 7 +++---- renderer/functions_test.go | 7 ++----- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/renderer/functions.go b/renderer/functions.go index ceb16a3..194a439 100644 --- a/renderer/functions.go +++ b/renderer/functions.go @@ -75,11 +75,10 @@ func (f *Functions) LinkForType(t *types.Type) (link string, local bool) { return f.LinkForKubeType(t), false } - if kt, ok := f.IsKnownType(t); ok { - return f.LinkForKnownType(kt), false - } - if t.IsBasic() || t.Imported { + if kt, ok := f.IsKnownType(t); ok { + return f.LinkForKnownType(kt), false + } return "", false } diff --git a/renderer/functions_test.go b/renderer/functions_test.go index 7104023..40e906b 100644 --- a/renderer/functions_test.go +++ b/renderer/functions_test.go @@ -55,11 +55,8 @@ func TestLinkForType(t *testing.T) { { name: "local type ignores knownTypes and gets local link", typ: &types.Type{Name: "Foo", Package: "example.com/pkg", Imported: false}, - // FIXME: This is incorrect and should be a relative link - // wantLink: "example-com-pkg-foo", - // wantLocal: true, - wantLink: "https://example.com/docs#foo", - wantLocal: false, + wantLink: "example-com-pkg-foo", + wantLocal: true, }, { name: "kube type gets kubernetes.io link even when also in knownTypes",