add tree weight

pull/13/head
mrshmllow 2 years ago committed by marshmallow
parent fbf0c1c25f
commit 8157ca19ff

8
additional.d.ts vendored

@ -1,10 +1,4 @@
type node = {
value: string;
pretty?: string | null;
children: node[];
current: boolean;
};
type FrontMatter = {
title?: string
weight?: number
}

@ -2,8 +2,9 @@ import { faCaretDown, faCaretRight } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import Link from "next/link";
import { FC, useState } from "react";
import { TreeItem } from "../lib/tree";
const TreeNode: FC<{ node: node; path: string }> = ({
const TreeNode: FC<{ node: TreeItem; path: string }> = ({
node: { children, value, current, pretty },
path,
}) => {

@ -0,0 +1,42 @@
export interface TreeItem {
value: string,
current: boolean,
children: TreeItem[],
weight: number,
pretty: string | null
}
export class TreeItemConstructor {
value: string;
current: boolean;
children: TreeItemConstructor[] = [];
weight: number;
pretty: string | null;
constructor(
value = "root",
current = false,
pretty: string | null = null,
weight = 0
) {
this.value = value;
this.current = current;
this.pretty = pretty
this.weight = weight;
}
addChild(child: TreeItemConstructor) {
this.children.push(child)
this.children.sort((a, b) => b.weight - a.weight);
}
plain(): TreeItem {
return {
value: this.value,
current: this.current,
children: this.children.map(child => child.plain()),
weight: this.weight,
pretty: this.pretty
}
}
}

@ -0,0 +1,14 @@
import { useRouter } from "next/router";
import { FC } from "react";
const DocPage: FC<{}> = () => {
const route = useRouter()
console.log(route.query)
return (
<>
</>
);
};
export default DocPage;

@ -4,12 +4,14 @@ import { FC } from "react";
import { resolve } from "path";
import { GetStaticPaths, GetStaticProps, Redirect } from "next";
import { removeExt, walkFiles } from "../../lib/files";
import { lstat, readFile } from "fs/promises";
import { readFile } from "fs/promises";
import { readdir } from "fs/promises";
import remarkGfm from "remark-gfm";
import TreeNode from "../../components/TreeNode";
import TreeNode from "../../components/TreeItem";
import fm from "front-matter";
import DocWrapper from "../../components/docs/Wrapper";
import { TreeItem, TreeItemConstructor } from "../../lib/tree";
import { inspect } from "util";
export const getStaticPaths: GetStaticPaths = async () => {
const paths: {
@ -47,54 +49,34 @@ export const getStaticProps: GetStaticProps = async (context) => {
? []
: (context.params!.slug as string[]);
let path = ["_docs", ...slug].join("/");
// try {
// if ((await lstat(path)).isDirectory()) {
// const dirents = await readdir(resolve(process.cwd(), path));
//
// return {
// props: {},
// redirect: {
// destination: `/docs/${slug.join("/")}/${removeExt(dirents.shift()!)}`,
// permanent: false,
// },
// };
// }
// } catch {}
path = path + ".mdx";
let path = ["_docs", ...slug].join("/") + ".mdx";
let redirect: Redirect | null = null;
const walk = async (node: node, dir: string, i = 0) => {
const walk = async (node: TreeItemConstructor, dir: string, i = 0) => {
const dirents = await readdir(resolve(process.cwd(), dir), {
withFileTypes: true,
});
for (const dirent of dirents) {
if (dirent.name.startsWith(".")) {
continue;
}
for (const dirent of dirents.filter(
(dirent) => !dirent.name.startsWith(".")
)) {
const current = slug[i] === removeExt(dirent.name) && node.current;
if (dirent.isDirectory()) {
const child = await walk(
{
value: removeExt(dirent.name),
children: [],
current,
},
resolve(dir, dirent.name),
i + 1
node.addChild(
await walk(
new TreeItemConstructor(removeExt(dirent.name), current, null, 0),
resolve(dir, dirent.name),
i + 1
)
);
node.children.push(child);
if (current && i + 1 == slug.length && node.children.length > 0) {
// We are a directory AND the path
redirect = {
permanent: false,
destination: `/docs/${slug.join("/")}/${
node.children[0].children.shift()!.value
node.children[
node.children.findIndex(a => a.value === slug[i])
].children.shift()!.value
}`,
};
}
@ -105,28 +87,23 @@ export const getStaticProps: GetStaticProps = async (context) => {
).toString();
const frontmatter = fm<FrontMatter>(contents);
node.children.push({
value: removeExt(dirent.name),
pretty: frontmatter.attributes.title
? frontmatter.attributes.title
: null,
children: [],
current,
});
node.addChild(
new TreeItemConstructor(
removeExt(dirent.name),
current,
frontmatter.attributes.title
? frontmatter.attributes.title
: null,
frontmatter.attributes.weight ? frontmatter.attributes.weight : 0
)
);
} catch (e) {}
}
}
return node;
};
const tree = await walk(
{
value: "root",
children: [],
current: true,
},
"_docs/"
);
const tree = await walk(new TreeItemConstructor("root", true), "_docs/");
if (redirect) {
return { props: { tree }, redirect };
@ -142,10 +119,10 @@ export const getStaticProps: GetStaticProps = async (context) => {
}
);
return { props: { source: mdxSource, tree } };
return { props: { source: mdxSource, tree: tree.plain() } };
};
const DocPage: FC<{ source: MDXRemoteSerializeResult; tree: node }> = ({
const DocPage: FC<{ source: MDXRemoteSerializeResult; tree: TreeItem }> = ({
source,
tree,
}) => {

Loading…
Cancel
Save