Skip to content

Commit 3dd1e39

Browse files
committed
point View source button to specific template folder
1 parent 73552e5 commit 3dd1e39

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

src/lib/helpers/templateSource.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type { Models } from '@appwrite.io/console';
22

33
/**
4-
* Build VCS repo URL from the template response model.
5-
* Example (GitHub): https://github.com/appwrite/templates-for-sites
4+
* Example (GitHub): https://github.com/appwrite/templates-for-sites/tree/main/sveltekit/starter
65
*/
76
export function getTemplateSourceUrl(
87
t: Models.TemplateSite | Models.TemplateFunction
@@ -20,7 +19,40 @@ export function getTemplateSourceUrl(
2019
bitbucket: 'bitbucket.org'
2120
};
2221

23-
const host = hostMap[provider.toLowerCase()] ?? provider; // fallback
22+
const host = hostMap[provider.toLowerCase()];
23+
if (!host) return null;
2424

25-
return `https://${host}/${owner}/${repo}`;
25+
let folderPath: string | undefined;
26+
if (
27+
'providerRootDirectory' in t &&
28+
t.providerRootDirectory &&
29+
typeof t.providerRootDirectory === 'string'
30+
) {
31+
folderPath = t.providerRootDirectory;
32+
} else if (
33+
'frameworks' in t &&
34+
t.frameworks?.length > 0 &&
35+
t.frameworks[0]?.providerRootDirectory &&
36+
typeof t.frameworks[0].providerRootDirectory === 'string'
37+
) {
38+
folderPath = t.frameworks[0].providerRootDirectory;
39+
}
40+
41+
let url = `https://${host}/${owner}/${repo}`;
42+
43+
if (folderPath) {
44+
const normalizedPath = folderPath.replace(/^\/+|\/+$/g, '');
45+
if (normalizedPath) {
46+
const providerLower = provider.toLowerCase();
47+
if (providerLower === 'github') {
48+
url = `${url}/tree/main/${normalizedPath}`;
49+
} else if (providerLower === 'gitlab') {
50+
url = `${url}/-/tree/main/${normalizedPath}`;
51+
} else if (providerLower === 'bitbucket') {
52+
url = `${url}/src/main/${normalizedPath}`;
53+
}
54+
}
55+
}
56+
57+
return url;
2658
}

0 commit comments

Comments
 (0)