Skip to content

Commit

Permalink
Add list command
Browse files Browse the repository at this point in the history
  • Loading branch information
pindab0ter committed Apr 8, 2024
1 parent 5efd3f9 commit ed2b35b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
3 changes: 2 additions & 1 deletion assets/js/shell/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import { Help } from "./commands/Help";
import { Command } from "./Command";
import { Clear } from "./commands/Clear";
import { Kitties } from "./commands/Kitties";
import { List } from "./commands/List";

export const commands: Command[] = [new Clear(), new Help(), new Kitties()];
export const commands: Command[] = [new Clear(), new Help(), new Kitties(), new List()];
36 changes: 36 additions & 0 deletions assets/js/shell/commands/List.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Command } from "../Command";

// @ts-ignore
import params from "@params";
import { HugoPage } from "../../types/hugo";

export class List implements Command {
public readonly name: string = "ls";

public execute(consoleElement: HTMLDivElement): void {
let pages = JSON.parse(params.pages) as HugoPage[];
pages.sort((a: HugoPage, b: HugoPage) => b.Path.localeCompare(a.Path));

const currentPath = window.location.pathname;

if (currentPath === "/") {
pages = pages
.filter((page: HugoPage) => page.Path.split("/").length === 2)
.filter((page: HugoPage) => page.Path !== "/");
} else {
pages = pages.filter((page: HugoPage) => page.Path.startsWith(currentPath));
}

pages.forEach((page: HugoPage) => {
const outputElement = document.createElement("pre");

if (page.Slug) {
outputElement.textContent = page.Slug.concat("/");
} else {
outputElement.textContent = page.Path.replace(currentPath, "").concat("/");
}

consoleElement.appendChild(outputElement);
});
}
}
30 changes: 30 additions & 0 deletions assets/js/types/hugo.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export type HugoPage = {
Date: string;
Lastmod: string;
PublishDate: string;
ExpiryDate: string;
Aliases: string[];
BundleType: string;
Description: string;
Draft: boolean;
IsHome: boolean;
Keywords: null;
Kind: string;
Layout: string;
LinkTitle: string;
IsNode: boolean;
IsPage: boolean;
Path: string;
Pathc: string;
Slug: string;
Lang: string;
IsSection: boolean;
Section: string;
Sitemap: {
ChangeFreq: string;
Priority: number;
Filename: string;
};
Type: string;
Weight: number;
};
3 changes: 2 additions & 1 deletion layouts/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
/>
{{ end -}}

{{ $js := resources.Get "js/main.ts" | js.Build (dict "format" "esm") }}
{{ $opts := dict "params" (dict "pages" (.Site.AllPages | jsonify) "test" "test") "format" "esm" }}
{{ $js := resources.Get "js/main.ts" | js.Build $opts }}
{{ if .Site.IsServer }}
{{ $script := $js | fingerprint }}
<script data-precache src="{{ $script.RelPermalink }}" defer></script>
Expand Down

0 comments on commit ed2b35b

Please sign in to comment.