url.go

v0.1.0
Doc Versions Source
1
package api
2
3
import (
4
	"fmt"
5
	"net/url"
6
	"strings"
7
)
8
9
// PageRef holds parsed Confluence page reference from a URL.
10
type PageRef struct {
11
	BaseURL  string // e.g., "https://confluence.example.com"
12
	PageID   string // numeric ID if available
13
	SpaceKey string // from /display/SPACE/... format
14
	Title    string // URL-decoded page title
15
}
16
17
// ParsePageURL parses a Confluence Server/Data Center page URL into a PageRef.
18
//
19
// Supported formats:
20
//   - https://confluence.example.com/pages/viewpage.action?pageId=12345
21
//   - https://confluence.example.com/display/SPACE/Page+Title
22
//   - https://confluence.example.com/display/SPACE/Page+Title/Sub+Page
23
func ParsePageURL(rawURL string) (*PageRef, error) {
24
	u, err := url.Parse(rawURL)
25
	if err != nil {
26
		return nil, fmt.Errorf("parsing URL: %w", err)
27
	}
28
29
	if u.Scheme == "" || u.Host == "" {
30
		return nil, fmt.Errorf("URL must include scheme and host: %s", rawURL)
31
	}
32
33
	baseURL := u.Scheme + "://" + u.Host
34
	if u.Port() != "" && !strings.Contains(u.Host, ":") {
35
		baseURL = u.Scheme + "://" + u.Host + ":" + u.Port()
36
	}
37
38
	path := strings.TrimRight(u.Path, "/")
39
40
	// Format 1: /pages/viewpage.action?pageId=12345
41
	if strings.HasSuffix(path, "/pages/viewpage.action") || path == "/pages/viewpage.action" {
42
		pageID := u.Query().Get("pageId")
43
		if pageID == "" {
44
			return nil, fmt.Errorf("URL has viewpage.action but no pageId parameter: %s", rawURL)
45
		}
46
		return &PageRef{
47
			BaseURL: baseURL,
48
			PageID:  pageID,
49
		}, nil
50
	}
51
52
	// Format 2: /display/SPACE/Page+Title
53
	if idx := strings.Index(path, "/display/"); idx != -1 {
54
		rest := path[idx+len("/display/"):]
55
		parts := strings.SplitN(rest, "/", 2)
56
		if len(parts) < 2 || parts[0] == "" || parts[1] == "" {
57
			return nil, fmt.Errorf("URL display format requires /display/SPACE/Title: %s", rawURL)
58
		}
59
		spaceKey := parts[0]
60
		// The title may contain slashes for sub-pages; take the last segment
61
		titleEncoded := parts[1]
62
		// Confluence uses + for spaces in URL paths
63
		title := strings.ReplaceAll(titleEncoded, "+", " ")
64
		title, err = url.PathUnescape(title)
65
		if err != nil {
66
			return nil, fmt.Errorf("decoding page title: %w", err)
67
		}
68
		return &PageRef{
69
			BaseURL:  baseURL,
70
			SpaceKey: spaceKey,
71
			Title:    title,
72
		}, nil
73
	}
74
75
	return nil, fmt.Errorf("unrecognized Confluence URL format: %s", rawURL)
76
}
77

Source Files