Skip to content

Commit 7d516b1

Browse files
Fix broken file links (#161)
1 parent a013298 commit 7d516b1

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Fixed
1111

1212
- Fixed issue where we crash on startup if the install / upgrade PostHog event fails to send. ([#159](https://github.com/sourcebot-dev/sourcebot/pull/159))
13+
- Fixed issue with broken file links. ([#161](https://github.com/sourcebot-dev/sourcebot/pull/161))
1314

1415
## [2.7.0] - 2025-01-10
1516

packages/web/src/app/search/components/codePreviewPanel/index.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,30 @@ export const CodePreviewPanel = ({
4141
.then(({ source }) => {
4242
const link = (() => {
4343
const template = repoUrlTemplates[fileMatch.Repository];
44-
if (!template) {
44+
45+
// This is a hacky parser for templates generated by
46+
// the go text/template package. Example template:
47+
// {{URLJoinPath "https://github.com/sourcebot-dev/sourcebot" "blob" .Version .Path}}
48+
// @see: https://pkg.go.dev/text/template
49+
if (!template || !template.match(/^{{URLJoinPath\s.*}}(\?.+)?$/)) {
4550
return undefined;
4651
}
47-
return template
48-
.replace("{{.Version}}", branch ?? "HEAD")
49-
.replace("{{.Path}}", fileMatch.FileName);
52+
53+
const url =
54+
template.substring("{{URLJoinPath ".length,template.indexOf("}}"))
55+
.replace(".Version", branch ?? "HEAD")
56+
.replace(".Path", fileMatch.FileName)
57+
.split(" ")
58+
.map((part) => {
59+
// remove wrapping quotes
60+
if (part.startsWith("\"")) part = part.substring(1);
61+
if (part.endsWith("\"")) part = part.substring(0, part.length - 1);
62+
return part;
63+
})
64+
.join("/");
65+
66+
const optionalQueryParams = template.substring(template.indexOf("}}") + 2);
67+
return url + optionalQueryParams;
5068
})();
5169

5270
const decodedSource = base64Decode(source);

0 commit comments

Comments
 (0)