Skip to content

Commit

Permalink
add gitlab folder support
Browse files Browse the repository at this point in the history
  • Loading branch information
SirajChokshi committed Apr 10, 2023
1 parent e628324 commit bd42511
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
15 changes: 15 additions & 0 deletions docs/URL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# URL Processing

## Gitlab

### UI URL Patterns

| View | URL |
| ----------- | ------------------------------------------------------------------------------------------ |
| Project | `https://gitlab.com/<USER>/projects/<REPO_NAME>` |
| Folder View | `https://gitlab.com/<USER>/projects/<REPO_NAME>/-/tree/<BRANCH_NAME>/<FOLDER>` |
| File View | `https://gitlab.com/<USER>/projects/<REPO_NAME>/-/blob/<BRANCH_NAME>/<FOLDER>/<FILE>.html` |

### Raw File Pattern

- `https://gitlab.com/<USER>/projects/<REPO_NAME>/-/raw/<BRANCH_NAME>/<FOLDER>/<FILE>.md`
2 changes: 2 additions & 0 deletions src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export const proxyFetch = (url: string) =>
fetch(`https://api.codetabs.com/v1/proxy/?quest=${url}`, undefined).then(
(res) => {
if (!res.ok) throw new Error(`Could not load ${url}`)
// TODO - handle 404s/pageful errors
// if (res.status === 404) return null
return res.text()
},
)
1 change: 1 addition & 0 deletions src/utils/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export class Preview {

// otherwise, fetch it and store it
const data = await proxyFetch(url)

this.resources[url] = data

return data
Expand Down
16 changes: 9 additions & 7 deletions src/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ function processGithubURL(urlStr: string): string {
function processGitlabURL(urlStr: string): string {
const url = new URL(urlStr)

if (url.pathname.includes('/blob/')) {
// the first instance of /blob/ is the branch name
// unless the project or user's name includes /blob/
// TODO: handle arbitrary `blob` in project name
url.pathname = url.pathname.replace('/blob/', '/raw/')
if (url.pathname.includes('/-/blob/')) {
url.pathname = url.pathname.replace('/-/blob/', '/-/raw/')
}

if (url.pathname.includes('/-/tree/')) {
url.pathname = url.pathname.replace('/-/tree/', '/-/raw/')
}

return url.href
Expand Down Expand Up @@ -76,7 +77,8 @@ export function getPossibleUrls(url: string): string[] {

return [
// TODO - first check if the user is attempting to load without a file path
`${baseUrl}/main/index.html`,
`${baseUrl}/master/index.html`,
`${baseUrl}/index.html`,
`${baseUrl}main/index.html`,
`${baseUrl}master/index.html`,
]
}

0 comments on commit bd42511

Please sign in to comment.