- the resources
+ Welcome to the TPH resources.
+
+ This is meant as a small knowledge base for commonly answered questions
+ on our Discord community,{" "}
+ The Programmer's Hangout.
+
+
+ All of it is open source, and you can contribute to it on{" "}
+ GitHub.
+
+
)
}
diff --git a/src/types/index.d.ts b/src/types/index.d.ts
new file mode 100644
index 0000000..5d1a69c
--- /dev/null
+++ b/src/types/index.d.ts
@@ -0,0 +1,32 @@
+export interface IFile {
+ title: string
+ type: "file"
+ path: string
+}
+
+export interface IFolder {
+ title: string
+ type: "folder"
+ path: string
+ children: IFileOrFolder[]
+}
+
+export type IFileOrFolder = IFile | IFolder
+
+export interface IFileQuery {
+ node: {
+ relativePath: string
+ childMarkdownRemark: {
+ frontmatter: {
+ author: string
+ date: string
+ }
+ }
+ }
+}
+
+export interface IAllResourcesQuery {
+ allFile: {
+ edges: IFileQuery[]
+ }
+}
diff --git a/src/utils/index.ts b/src/utils/index.ts
index a7a3fd2..0f12043 100644
--- a/src/utils/index.ts
+++ b/src/utils/index.ts
@@ -1,4 +1,7 @@
-import { IFileOrFolder } from "../components/ResourcesSidebar/index"
+import chain from "ramda/es/chain"
+import partition from "ramda/es/partition"
+
+import { IFile, IFileOrFolder, IFolder } from "../types"
export function traversePaths(
[head, ...tail]: string[],
@@ -22,3 +25,85 @@ export function traversePaths(
children: [traversePaths(tail, path)],
}
}
+
+export function traversePathsToFiles(
+ [head, ...tail]: string[],
+ basePath: string = "/resources",
+ depth: number = 0
+): IFileOrFolder {
+ const path = basePath + "/" + head + "/" + tail.join("/")
+
+ if (depth !== 0) {
+ // probably not more than one dot
+ const [name] = basePath.split(".")
+ return {
+ title: name,
+ type: "file",
+ path: basePath,
+ }
+ }
+
+ return {
+ title: head,
+ type: "folder",
+ path,
+ children: [traversePathsToFiles(tail, path, 1)],
+ }
+}
+
+function generateFolder({
+ title,
+ path,
+ targets,
+}: {
+ title: IFolder["title"]
+ path: IFile["path"]
+ targets: IFolder[]
+}): IFolder {
+ const children = join(chain(target => target.children, targets))
+
+ return {
+ title,
+ type: "folder",
+ path,
+ children: children.sort((a, b) =>
+ a.title.split("/").length > b.title.split("/").length
+ ? -1
+ : a.title.localeCompare(b.title)
+ ),
+ }
+}
+
+function generateFile({
+ title,
+ path,
+}: {
+ title: IFile["title"]
+ path: IFile["path"]
+}): IFile {
+ return {
+ title,
+ type: "file",
+ path,
+ }
+}
+
+export function join([head, ...tail]: IFileOrFolder[]): IFileOrFolder[] {
+ if (!head) {
+ return []
+ }
+
+ const [similarFs, remaining] = partition(
+ obj => obj.title === head.title && obj.type === head.type,
+ tail
+ )
+ const targets = [head, ...similarFs]
+ const { title, path } = head
+
+ const current =
+ head.type === "folder"
+ ? generateFolder({ title, path, targets: targets as IFolder[] })
+ : generateFile({ title, path })
+
+ return [current, ...join(remaining)]
+}
diff --git a/tsconfig.json b/tsconfig.json
index 3565c9a..8249c01 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -46,7 +46,7 @@
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
- "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
+ "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
@@ -59,5 +59,6 @@
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
+ "resolveJsonModule": true
}
}
diff --git a/typings.d.ts b/typings.d.ts
index 3a47d73..04494d2 100644
--- a/typings.d.ts
+++ b/typings.d.ts
@@ -3,6 +3,7 @@ import "styled-components"
declare module "*.svg"
declare module "*.png"
+declare module "*.json"
type ComponentQuery