-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e26e30
commit 670bf03
Showing
4 changed files
with
127 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { Command } from "../Command"; | ||
|
||
import { getAllPages, getPagesInPath } from "../helpers"; | ||
import { HugoPage } from "../../types/hugo"; | ||
|
||
export class ChangeDirectory implements Command { | ||
public readonly name: string = "cd"; | ||
|
||
public execute(consoleElement: HTMLDivElement, args: string[] = []): void { | ||
if (args.length === 0) { | ||
const outputElement = document.createElement("pre"); | ||
outputElement.textContent = "cd: missing argument"; | ||
consoleElement.appendChild(outputElement); | ||
return; | ||
} | ||
|
||
if (args.length > 1) { | ||
const outputElement = document.createElement("pre"); | ||
outputElement.textContent = "cd: too many arguments"; | ||
consoleElement.appendChild(outputElement); | ||
return; | ||
} | ||
|
||
const allPages = getAllPages(); | ||
const pagesInPath = getPagesInPath(window.location.pathname); | ||
|
||
// Change to root directory | ||
if (!args.length || ["/", "~"].includes(args[0])) { | ||
window.location.pathname = "/"; | ||
return; | ||
} | ||
|
||
const inputPath = args[0].replace(/\/$/, "").replace(/^~\//, "/").toLowerCase(); | ||
|
||
// Change to current directory | ||
if (inputPath === ".") { | ||
// noinspection SillyAssignmentJS | ||
window.location.pathname = window.location.pathname; | ||
return; | ||
} | ||
|
||
// Change to parent directory | ||
if (inputPath === "..") { | ||
if (window.location.pathname === "/") { | ||
return; | ||
} | ||
|
||
window.location.pathname = window.location.pathname | ||
.split("/") | ||
.filter((s) => s !== "") | ||
.slice(0, -1) | ||
.join("/") | ||
.concat("/"); | ||
} | ||
|
||
// Change to an absolute path | ||
if (inputPath.startsWith("/") || inputPath.startsWith("~/")) { | ||
const page = allPages.find( | ||
(p: HugoPage) => | ||
p.Path.toLowerCase() === inputPath || | ||
"/" + p.Section.toLowerCase() + "/" + p.Slug === inputPath, | ||
); | ||
|
||
if (page !== undefined) { | ||
window.location.pathname = "/" + page.Section.toLowerCase() + "/" + page.Slug; | ||
return; | ||
} | ||
return; | ||
} | ||
|
||
// Change to a relative path | ||
console.log(inputPath, pagesInPath); | ||
if ( | ||
pagesInPath.find( | ||
(p: HugoPage) => | ||
p.Path.replace(window.location.pathname, "").toLowerCase() === inputPath || | ||
p.Slug === inputPath, | ||
) | ||
) { | ||
window.location.pathname = window.location.pathname.concat(inputPath).concat("/"); | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// @ts-ignore | ||
import params from "@params"; | ||
import { HugoPage } from "../types/hugo"; | ||
|
||
export function getAllPages(): HugoPage[] { | ||
const pages = JSON.parse(params.pages) as HugoPage[]; | ||
|
||
return pages.sort((a: HugoPage, b: HugoPage) => b.Path.localeCompare(a.Path)); | ||
} | ||
|
||
export function getPagesInPath(path: string): HugoPage[] { | ||
const pages = getAllPages(); | ||
|
||
// Filter pages based on current path | ||
if (path === "/") { | ||
return pages | ||
.filter((page: HugoPage) => page.Path.split("/").length === 2) | ||
.filter((page: HugoPage) => page.Path !== "/"); | ||
} else { | ||
return pages.filter((page: HugoPage) => page.Path.startsWith(path)); | ||
} | ||
} |