summaryrefslogtreecommitdiff
path: root/internal/links/links.go
blob: be0f051a0b5d5088fbd87a6087a15d4d119cea5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package links

import (
	"bytes"
	"net/url"
	"path"
	"strings"

	"golang.org/x/net/html"

	"mokhan.ca/xlgmokha/gitmal/internal/git"
)

type Set map[string]struct{}

func BuildDirSet(files []git.Blob) Set {
	dirs := make(Set)
	for _, f := range files {
		dir := path.Dir(f.Path)
		for dir != "." && dir != "/" {
			if _, ok := dirs[dir]; ok {
				break
			}
			dirs[dir] = struct{}{}
			if i := strings.LastIndex(dir, "/"); i != -1 {
				dir = dir[:i]
			} else {
				break
			}
		}
	}
	return dirs
}

func BuildFileSet(files []git.Blob) Set {
	filesSet := make(Set)
	for _, f := range files {
		filesSet[f.Path] = struct{}{}
	}
	return filesSet
}

func Resolve(content, currentPath, rootHref, ref string, dirs, files Set) string {
	doc, err := html.Parse(strings.NewReader(content))
	if err != nil {
		return content
	}

	baseDir := path.Dir(currentPath)

	var walk func(*html.Node)
	walk = func(n *html.Node) {
		if n.Type == html.ElementNode {
			switch n.Data {
			case "a":
				for i, attr := range n.Attr {
					if attr.Key == "href" {
						newHref := transformHref(attr.Val, baseDir, rootHref, ref, files, dirs)
						n.Attr[i].Val = newHref
						break
					}
				}
			case "img":
				for i, attr := range n.Attr {
					if attr.Key == "src" {
						newSrc := transformImgSrc(attr.Val, baseDir, rootHref, ref)
						n.Attr[i].Val = newSrc
						break
					}
				}
			}
		}

		for c := n.FirstChild; c != nil; c = c.NextSibling {
			walk(c)
		}
	}
	walk(doc)

	var buf bytes.Buffer
	if err := html.Render(&buf, doc); err != nil {
		return content
	}

	return buf.String()
}

func transformHref(href, baseDir, rootHref, ref string, files, dirs Set) string {
	if href == "" {
		return href
	}
	if strings.HasPrefix(href, "#") {
		return href
	}

	u, err := url.Parse(href)
	if err != nil {
		return href
	}

	// Absolute URLs are left untouched
	if u.IsAbs() {
		return href
	}

	// Skip mailto:, javascript:, data: etc. (url.Parse sets Scheme)
	if u.Scheme != "" {
		return href
	}

	// Resolve against the directory of the current file
	relPath := u.Path
	if relPath == "" {
		return href
	}

	var repoPath string

	if strings.HasPrefix(relPath, "/") {
		// Root-relative repo path
		relPath = strings.TrimPrefix(relPath, "/")
		repoPath = path.Clean(relPath)
	} else {
		// Relative to current file directory
		repoPath = path.Clean(path.Join(baseDir, relPath))
	}

	// Decide if this is a file or a directory in the repo
	var newPath string

	// 1) Exact file match
	if _, ok := files[repoPath]; ok {
		newPath = repoPath + ".html"
	} else if _, ok := files[repoPath+".md"]; ok {
		// 2) Maybe the link omitted ".md" but the repo has it
		newPath = repoPath + ".md.html"
	} else if _, ok := dirs[repoPath]; ok {
		// 3) Directory: add /index.html
		newPath = path.Join(repoPath, "index.html")
	} else {
		// Unknown target, leave as-is
		return href
	}

	// Link from the root href
	newPath = path.Join(rootHref, "blob", ref, newPath)

	// Preserve any query/fragment if they existed
	u.Path = newPath
	return u.String()
}

func transformImgSrc(src, baseDir, rootHref, ref string) string {
	u, err := url.Parse(src)
	if err != nil {
		return src
	}

	if u.IsAbs() {
		return src
	}

	relPath := u.Path

	var repoPath string
	if strings.HasPrefix(relPath, "/") {
		// Root-relative: drop leading slash
		repoPath = strings.TrimPrefix(relPath, "/")
	} else {
		// Resolve against current file directory
		repoPath = path.Clean(path.Join(baseDir, relPath))
	}

	final := path.Join(rootHref, "raw", ref, repoPath)

	// Preserve any query/fragment if they existed
	u.Path = final
	return u.String()
}