Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge dev into production #1631

Merged
merged 4 commits into from
Nov 13, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import remarkParse from "remark-parse";
import { unified } from "unified";
import { Index } from "unist-util-index";

import { TopLevelBlock } from "@starknet-io/cms-data/src/pages";
import { HeadingData } from "./TableOfContents";

/**
* `Node`type.
*/

type Node = {
children: Node[];
depth: number;
type: string;
value: string;
}

export function blocksToTOC(blocks: readonly TopLevelBlock[] = [], level: number, tableOfContents: HeadingData[] = []): readonly HeadingData[] {
blocks.forEach((block) => {
if(block.type === 'page_header'){
Expand Down Expand Up @@ -50,18 +60,21 @@ export function blocksToTOC(blocks: readonly TopLevelBlock[] = [], level: number
.use(() => {
return (tree: any) => {
const typeIndex = new Index("type", tree);
const headings = typeIndex.get("heading");

const headingItems: HeadingData[] = headings.map((node: any) => {
const textNode = node.children.find((n: any) => {
return n.type === "text";
const headings = typeIndex.get("heading") as Node[];
const headingItems = headings.map(node => {
const textNode = node?.children?.find(child => {
return (child.type === "text" || child.type === "strong") && node.depth < 4;
});

if (!textNode) {
return null;
}

return {
title: textNode?.value ?? "",
level
title: textNode?.value ?? textNode?.children[0].value ?? "",
level: node.depth - 1
};
});
}).filter(heading => !!heading) as HeadingData[];

tableOfContents.push(...headingItems);
};
Expand Down
Loading