Skip to content

Commit c035a32

Browse files
alexdlndanielroe
andauthored
fix: support tree files in image path resolver (#866)
Co-authored-by: Daniel Roe <daniel@roe.dev>
1 parent 517d021 commit c035a32

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

app/components/VersionSelector.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ watch(
546546
<span
547547
v-else
548548
class="w-3 h-3 transition-transform duration-200 rtl-flip"
549-
:class="group.isExpanded ? 'i:carbon:chevron-down' : 'i-carbon:chevron-right'"
549+
:class="group.isExpanded ? 'i-carbon:chevron-down' : 'i-carbon:chevron-right'"
550550
aria-hidden="true"
551551
/>
552552
</button>

server/utils/readme.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { marked, type Tokens } from 'marked'
22
import sanitizeHtml from 'sanitize-html'
33
import { hasProtocol } from 'ufo'
44
import type { ReadmeResponse, TocItem } from '#shared/types/readme'
5-
import { convertBlobToRawUrl, type RepositoryInfo } from '#shared/utils/git-providers'
5+
import { convertBlobOrFileToRawUrl, type RepositoryInfo } from '#shared/utils/git-providers'
66
import { highlightCodeSync } from './shiki'
77
import { convertToEmoji } from '#shared/utils/emoji'
88

@@ -258,7 +258,7 @@ function resolveUrl(url: string, packageName: string, repoInfo?: RepositoryInfo)
258258
function resolveImageUrl(url: string, packageName: string, repoInfo?: RepositoryInfo): string {
259259
const resolved = resolveUrl(url, packageName, repoInfo)
260260
if (repoInfo?.provider) {
261-
return convertBlobToRawUrl(resolved, repoInfo.provider)
261+
return convertBlobOrFileToRawUrl(resolved, repoInfo.provider)
262262
}
263263
return resolved
264264
}

shared/utils/git-providers.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ interface ProviderConfig {
4848
getRawBaseUrl(ref: RepoRef, branch?: string): string
4949
/** Get blob/rendered URL base for markdown files */
5050
getBlobBaseUrl(ref: RepoRef, branch?: string): string
51+
/** Convert file URLs to blob URLs (for images) */
52+
fileToRaw?(url: string): string
5153
/** Convert blob URLs to raw URLs (for images) */
5254
blobToRaw?(url: string): string
5355
}
@@ -69,6 +71,7 @@ const providers: ProviderConfig[] = [
6971
`https://raw.githubusercontent.com/${ref.owner}/${ref.repo}/${branch}`,
7072
getBlobBaseUrl: (ref, branch = 'HEAD') =>
7173
`https://github.com/${ref.owner}/${ref.repo}/blob/${branch}`,
74+
fileToRaw: url => url.replace('/tree/', '/raw/'),
7275
blobToRaw: url => url.replace('/blob/', '/raw/'),
7376
},
7477
{
@@ -386,12 +389,16 @@ export function getProviderConfig(providerId: ProviderId): ProviderConfig | unde
386389
return providers.find(p => p.id === providerId)
387390
}
388391

389-
export function convertBlobToRawUrl(url: string, providerId: ProviderId): string {
392+
export function convertBlobOrFileToRawUrl(url: string, providerId: ProviderId): string {
390393
const provider = providers.find(p => p.id === providerId)
394+
let rawUrl = url
395+
if (provider?.fileToRaw) {
396+
rawUrl = provider.fileToRaw(url)
397+
}
391398
if (provider?.blobToRaw) {
392-
return provider.blobToRaw(url)
399+
rawUrl = provider.blobToRaw(rawUrl)
393400
}
394-
return url
401+
return rawUrl
395402
}
396403

397404
export function isKnownGitProvider(url: string): boolean {

0 commit comments

Comments
 (0)