Skip to content

Commit 1bdb65c

Browse files
committed
cherry pick fix for file links
1 parent b935234 commit 1bdb65c

File tree

1 file changed

+21
-4
lines changed
  • packages/web/src/app/[domain]/search/components/codePreviewPanel

1 file changed

+21
-4
lines changed

packages/web/src/app/[domain]/search/components/codePreviewPanel/index.tsx

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

5471
const decodedSource = base64Decode(source);

0 commit comments

Comments
 (0)