Skip to content

Commit

Permalink
Treat GitHub URLs to non-schematic/board blobs as tree links to the c…
Browse files Browse the repository at this point in the history
…ontaining directory. Closes #37.
  • Loading branch information
theacodes committed Oct 29, 2023
1 parent 5d5fd68 commit 8822187
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/base/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@
Full text available at: https://opensource.org/licenses/MIT
*/

export function dirname(path: string | URL) {
if (path instanceof URL) {
path = path.pathname;
}
return path.split("/").slice(0, -1).join("/");
}

export function basename(path: string | URL) {
return new URL(path).pathname.split("/").at(-1)!;
if (path instanceof URL) {
path = path.pathname;
}
return path.split("/").at(-1)!;
}

export function extension(path: string) {
Expand Down
19 changes: 14 additions & 5 deletions src/kicanvas/services/github-vfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { initiate_download } from "../../base/dom/download";
import { basename, extension } from "../../base/paths";
import { basename, dirname, extension } from "../../base/paths";
import { GitHub, GitHubUserContent } from "./github";
import { VirtualFileSystem } from "./vfs";

Expand Down Expand Up @@ -42,13 +42,22 @@ export class GitHubFileSystem extends VirtualFileSystem {

// Link to a single file.
if (info.type == "blob") {
const guc_url = gh_user_content.convert_url(url);
const name = basename(guc_url);
files_to_urls.set(name, guc_url);
if (
["kicad_sch", "kicad_pcb"].includes(extension(info.path!))
) {
const guc_url = gh_user_content.convert_url(url);
const name = basename(guc_url);
files_to_urls.set(name, guc_url);
} else {
// Link to non-kicad file, try using the containing directory.
info.type = "tree";
info.path = dirname(info.path!);
console.log(info);
}
}

// Link to a directory.
else if (info.type == "tree") {
if (info.type == "tree") {
// Get a list of files in the directory.
const gh_file_list = (await gh.repos_contents(
info.owner,
Expand Down

0 comments on commit 8822187

Please sign in to comment.