Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/playground/blueprints/src/lib/v1/resources.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ describe('UrlResource', () => {
'https://raw.githubusercontent.com/WordPress/wordpress-develop/trunk/src/wp-includes/version.php'
);
});

it('should translate github.com raw URLs into raw.githubusercontent.com URLs', () => {
const resource = new UrlResource({
resource: 'url',
url: 'https://github.com/adamziel/blueprints/raw/f49382e89099806a8eede4feba41a9a7ab89bcfe/blueprints%2Fbeta-rc%2Fblueprint.json',
caption: 'Example',
});
expect(resource.getURL()).toBe(
'https://raw.githubusercontent.com/adamziel/blueprints/f49382e89099806a8eede4feba41a9a7ab89bcfe/blueprints%2Fbeta-rc%2Fblueprint.json'
);
});
});

describe('GitDirectoryResource', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/blueprints/src/lib/v1/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ export class UrlResource extends FetchResource {
*/
if (this.resource.url.startsWith('https://github.com/')) {
const match = this.resource.url.match(
/^https:\/\/github\.com\/(?<owner>[^/]+)\/(?<repo>[^/]+)\/blob\/(?<branch>[^/]+)\/(?<path>.+[^/])$/
/^https:\/\/github\.com\/(?<owner>[^/]+)\/(?<repo>[^/]+)\/(?:blob|raw)\/(?<branch>[^/]+)\/(?<path>.+[^/])$/
);
if (match?.groups) {
this.resource = {
Expand Down
9 changes: 6 additions & 3 deletions packages/playground/website/builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -877,11 +877,14 @@ async function initializeBlueprint() {
try {
const blueprintUrl = new URL(urlParams.get('blueprint-url'));
if (blueprintUrl.hostname === 'github.com') {
blueprintUrl.pathname = blueprintUrl.pathname.replace(
/^\/([^/]+)\/([^/]+)\/blob/,
const rewrittenPath = blueprintUrl.pathname.replace(
/^\/([^/]+)\/([^/]+)\/(?:blob|raw)\//,
'/$1/$2/'
);
blueprintUrl.hostname = 'raw.githubusercontent.com';
if (rewrittenPath !== blueprintUrl.pathname) {
blueprintUrl.pathname = rewrittenPath;
blueprintUrl.hostname = 'raw.githubusercontent.com';
}
}
console.log('blueprintUrl.toString()', blueprintUrl.toString());
const response = await fetch(blueprintUrl.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ export type ResolvedBlueprint = {
source: BlueprintSource;
};

const githubBlobOrRawPathPattern = /^\/([^/]+)\/([^/]+)\/(?:blob|raw)\//;

function normalizeBlueprintUrl(remoteUrl: string): string {
try {
const parsedUrl = new URL(remoteUrl);
if (parsedUrl.hostname !== 'github.com') {
return remoteUrl;
}
const rewrittenPath = parsedUrl.pathname.replace(
githubBlobOrRawPathPattern,
'/$1/$2/'
);
if (rewrittenPath === parsedUrl.pathname) {
return remoteUrl;
}
parsedUrl.pathname = rewrittenPath;
parsedUrl.hostname = 'raw.githubusercontent.com';
return parsedUrl.toString();
} catch {
return remoteUrl;
}
}

export async function resolveBlueprintFromURL(
url: URL,
defaultBlueprint?: string
Expand Down Expand Up @@ -59,13 +82,12 @@ export async function resolveBlueprintFromURL(
* Support passing blueprints via query parameter, e.g.:
* ?blueprint-url=https://example.com/blueprint.json
*/
const blueprintUrl = normalizeBlueprintUrl(query.get('blueprint-url')!);
return {
blueprint: await resolveRemoteBlueprint(
query.get('blueprint-url')!
),
blueprint: await resolveRemoteBlueprint(blueprintUrl),
source: {
type: 'remote-url',
url: query.get('blueprint-url')!,
url: blueprintUrl,
},
};
} else if (fragment.length) {
Expand Down
Loading