File tree 1 file changed +21
-4
lines changed
packages/web/src/app/[domain]/search/components/codePreviewPanel
1 file changed +21
-4
lines changed Original file line number Diff line number Diff line change @@ -43,12 +43,29 @@ export const CodePreviewPanel = ({
43
43
. then ( ( { source } ) => {
44
44
const link = ( ( ) => {
45
45
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 ( / ^ { { U R L J o i n P a t h \s .* } } ( \? .+ ) ? $ / ) ) {
47
52
return undefined ;
48
53
}
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 ;
52
69
} ) ( ) ;
53
70
54
71
const decodedSource = base64Decode ( source ) ;
You can’t perform that action at this time.
0 commit comments