import { NextRouter } from "next/router";
import { slugify } from "queries/helpers";
import { preprocessMarkDawn } from "./Markdawn";
type TOCProps = {
text: string;
title?: string;
router: NextRouter;
};
export default function TOC(props: TOCProps): JSX.Element {
const { router, text, title } = props;
const toc = getTocFromMarkdawn(preprocessMarkDawn(text), title);
return (
<>
Table of content
>
);
}
type TOCLevelProps = {
tocchildren: TOC[];
parentNumbering: string;
router: NextRouter;
};
function TOCLevel(props: TOCLevelProps): JSX.Element {
const { tocchildren, parentNumbering, router } = props;
return (
{tocchildren.map((child, childIndex) => (
<>
-
{`${parentNumbering}${
childIndex + 1
}.`}{" "}
router.replace(`#${child.slug}`)}>
{{child.title}}
>
))}
);
}
export type TOC = {
title: string;
slug: string;
children: TOC[];
};
export function getTocFromMarkdawn(text: string, title?: string): TOC {
if (!title) title = "Return to top";
let toc: TOC = { title: title, slug: slugify(title) || "", children: [] };
let h2 = -1;
let h3 = -1;
let h4 = -1;
let h5 = -1;
let scenebreak = 0;
let scenebreakIndex = 0;
function getTitle(line: string): string {
return line.slice(line.indexOf(`">`) + 2, line.indexOf(""));
}
function getSlug(line: string): string {
return line.slice(line.indexOf(`id="`) + 4, line.indexOf(`">`));
}
text.split("\n").map((line) => {
if (line.startsWith("