Skip to content

Commit

Permalink
fix: basic symbols missing from examples #1
Browse files Browse the repository at this point in the history
  • Loading branch information
urish committed Mar 11, 2024
1 parent a0c8bc5 commit 2469631
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/LibraryLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
// Copyright 2024 Tiny Tapeout LTD
// Author: Uri Shaked

import { githubURLToRaw } from './util/github';

export interface ILibraryDefinition {
path: string;
url: string;
}

export class LibraryLoader {
cache = new Map<string, string>();
readonly cache = new Map<string, string>();

baseURL?: string;

constructor(readonly libraries: ILibraryDefinition[]) {}

Expand All @@ -35,29 +39,28 @@ export class LibraryLoader {
return content;
}

private async fetchContent(path: string) {
private async fetchContent(path: string): Promise<Response> {
if (path.toLowerCase().startsWith('https://')) {
const url = new URL(path);
console.log(url, url.hostname);
if (url.hostname === 'github.com') {
url.hostname = 'raw.githubusercontent.com';
const pathParts = url.pathname.substring(1).split('/');
if (pathParts[2] === 'blob') {
pathParts.splice(2, 1);
url.pathname = pathParts.join('/');
}
console.log(url);
return await fetch(url.toString());
} else {
return await fetch(url);
}
const url = githubURLToRaw(path);
return await fetch(url);
}
for (const library of this.libraries) {
if (path.startsWith(library.path)) {
const url = library.url + path;
return await fetch(url);
}
}
if (this.baseURL != null) {
const url = new URL(path, githubURLToRaw(this.baseURL));
const result = await fetch(url);
if (result.ok) {
return result;
}
}
if (!path.includes('/')) {
// as a fallback, look under devices/
return await this.fetchContent(`devices/${path}`);
}
throw new Error(`File not found: ${path}`);
}
}
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ target.appendChild(svgRoot);
async function render(path: string) {
loadingText.textContent = `Loading... ${path}`;
svgRoot.style.visibility = 'hidden';

if (path.startsWith('https://')) {
libraryLoader.baseURL = path.substring(0, path.lastIndexOf('/') + 1);
}

try {
pz.pause();
await renderer.render(path, svgRoot);
Expand Down
19 changes: 19 additions & 0 deletions src/util/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2024 Tiny Tapeout LTD
// Author: Uri Shaked

/** Convert a GitHub URL to a raw.githubusercontent.com URL */
export function githubURLToRaw(path: string) {
const parsedURL = new URL(path);
if (parsedURL.hostname !== 'github.com') {
return path;
}

parsedURL.hostname = 'raw.githubusercontent.com';
const pathParts = parsedURL.pathname.substring(1).split('/');
if (pathParts[2] === 'blob') {
pathParts.splice(2, 1);
parsedURL.pathname = pathParts.join('/');
}
return parsedURL.toString();
}

0 comments on commit 2469631

Please sign in to comment.