diff --git a/assets/js/shell/commands.ts b/assets/js/shell/commands.ts index fc02b31..85fcc66 100644 --- a/assets/js/shell/commands.ts +++ b/assets/js/shell/commands.ts @@ -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()]; diff --git a/assets/js/shell/commands/List.ts b/assets/js/shell/commands/List.ts new file mode 100644 index 0000000..a5912d0 --- /dev/null +++ b/assets/js/shell/commands/List.ts @@ -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); + }); + } +} diff --git a/assets/js/types/hugo.d.ts b/assets/js/types/hugo.d.ts new file mode 100644 index 0000000..3a5730c --- /dev/null +++ b/assets/js/types/hugo.d.ts @@ -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; +}; diff --git a/layouts/partials/head.html b/layouts/partials/head.html index ca9bd9c..92b5e8c 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -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 }}