mirror of
https://github.com/kovetskiy/mark.git
synced 2026-01-22 10:47:36 +08:00
feat: add normalizeConfluenceWebUIPath function and tests for URL rewriting
Signed-off-by: Nikolai Emil Damm <ndam@tv2.dk>
This commit is contained in:
parent
4c2a3c9b96
commit
b4370c09c6
26
page/link.go
26
page/link.go
@ -221,6 +221,30 @@ func getConfluenceLink(
|
||||
}
|
||||
// Confluence supports relative links to reference other pages:
|
||||
// https://confluence.atlassian.com/doc/links-776656293.html
|
||||
linkPath := linkUrl.Path
|
||||
linkPath := normalizeConfluenceWebUIPath(linkUrl.Path)
|
||||
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
|
||||
}
|
||||
|
||||
@ -51,3 +51,21 @@ func TestParseLinks(t *testing.T) {
|
||||
assert.Equal(t, "example.md", links[7].full)
|
||||
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))
|
||||
})
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user