feat: add normalizeConfluenceWebUIPath function and tests for URL rewriting

Signed-off-by: Nikolai Emil Damm <ndam@tv2.dk>
This commit is contained in:
Nikolai Emil Damm 2025-12-18 15:55:39 +01:00 committed by Manuel Rüger
parent 4c2a3c9b96
commit b4370c09c6
2 changed files with 43 additions and 1 deletions

View File

@ -221,6 +221,30 @@ func getConfluenceLink(
} }
// Confluence supports relative links to reference other pages: // Confluence supports relative links to reference other pages:
// https://confluence.atlassian.com/doc/links-776656293.html // https://confluence.atlassian.com/doc/links-776656293.html
linkPath := linkUrl.Path linkPath := normalizeConfluenceWebUIPath(linkUrl.Path)
return linkPath, nil return linkPath, nil
} }
// normalizeConfluenceWebUIPath rewrites Confluence Cloud "experience" URLs
// ("/ex/confluence/<cloudId>/wiki/..."), to canonical wiki paths ("/wiki/...").
//
// This function is intentionally conservative and only touches the exact
// experience prefix, so that local relative paths like "./img/foo.png" are not
// impacted.
func normalizeConfluenceWebUIPath(path string) string {
if path == "" {
return path
}
// Example:
// /ex/confluence/05594958-6d5d-4e00-9017-90926d8b82d5/wiki/spaces/DVT/pages/5645697027/DX
// ->
// /wiki/spaces/DVT/pages/5645697027/DX
re := regexp.MustCompile(`^/ex/confluence/[^/]+(/wiki/.*)$`)
match := re.FindStringSubmatch(path)
if len(match) == 2 {
return match[1]
}
return path
}

View File

@ -51,3 +51,21 @@ func TestParseLinks(t *testing.T) {
assert.Equal(t, "example.md", links[7].full) assert.Equal(t, "example.md", links[7].full)
assert.Equal(t, len(links), 8) assert.Equal(t, len(links), 8)
} }
func TestNormalizeConfluenceWebUIPath(t *testing.T) {
t.Run("confluence-cloud-experience-prefix", func(t *testing.T) {
input := "/ex/confluence/05594958-6d5d-4e00-9017-90926d8b82d5/wiki/spaces/DVT/pages/5645697027/DX"
expected := "/wiki/spaces/DVT/pages/5645697027/DX"
assert.Equal(t, expected, normalizeConfluenceWebUIPath(input))
})
t.Run("already-canonical-wiki", func(t *testing.T) {
input := "/wiki/spaces/DVT/pages/5645697027/DX"
assert.Equal(t, input, normalizeConfluenceWebUIPath(input))
})
t.Run("local-relative-path-unchanged", func(t *testing.T) {
input := "./img/some-nice-image.png"
assert.Equal(t, input, normalizeConfluenceWebUIPath(input))
})
}