Skip to content

Commit

Permalink
Merge pull request #49 from TAServers/LEST-84-docs-table-of-contents
Browse files Browse the repository at this point in the history
LEST-84 - Manually generate table of contents
  • Loading branch information
Derpius authored Jul 1, 2023
2 parents 75bf9c4 + 0fabc02 commit b1fc8fe
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/site/docs/api/expect.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import toMatchObject from "@lest/docs/matchers/symmetric/toMatchObject.json";
import toMatch from "@lest/docs/matchers/symmetric/toMatch.json";
import toThrow from "@lest/docs/matchers/symmetric/toThrow.json";

<FunctionRenderer {...expect}>
<FunctionRenderer {...expect} headingLevel="h2">

```lua
test("returns hello world", function()
Expand Down
28 changes: 13 additions & 15 deletions packages/site/src/components/FunctionRenderer/FunctionRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@ export const FunctionRenderer: React.FC<FunctionRendererProps> = ({
returns = [],
headingLevel = "h3",
children,
}) => {
return (
<section>
<Heading as={headingLevel} id={name}>
<code>
{name}({parameters.length > 0 && renderParameterSignature(parameters)})
{returns.length > 0 && `: ${renderReturnSignature(returns)}`}
</code>
</Heading>
<Aliases aliases={aliases} />
<Description text={description} />
{children}
</section>
);
};
}) => (
<section>
<Heading as={headingLevel} id={name}>
<code>
{name}({parameters.length > 0 && renderParameterSignature(parameters)})
{returns.length > 0 && `: ${renderReturnSignature(returns)}`}
</code>
</Heading>
<Aliases aliases={aliases} />
<Description text={description} />
{children}
</section>
);
48 changes: 48 additions & 0 deletions packages/site/src/theme/DocItem/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React, { useEffect, useState } from "react";
import { HtmlClassNameProvider } from "@docusaurus/theme-common";
import { DocProvider } from "@docusaurus/theme-common/internal";
import DocItemMetadata from "@theme/DocItem/Metadata";
import DocItemLayout from "@theme/DocItem/Layout";
import type { Props } from "@theme/DocItem";
import type { TOCItem } from "@docusaurus/mdx-loader";

const getLevelFromTag = (tag: string): number | undefined => {
const match = tag.match(/^H(?<level>\d)$/);

if (match?.groups) {
return Number(match.groups.level);
}
};

const transformHeading = (heading: Element): TOCItem => {
const isCode = heading.getElementsByTagName("code").length > 0;
const value = isCode ? `<code>${heading.textContent}</code>` : heading.textContent ?? "";

return {
value,
id: heading.id,
level: getLevelFromTag(heading.tagName) ?? 2,
};
};

export default function DocItem({ content: DocContent }: Props) {
const docHtmlClassName = `docs-doc-id-${DocContent.metadata.unversionedId}`;
const [toc, setToc] = useState<TOCItem[]>([]);

useEffect(() => {
const headings = document.querySelectorAll("h2, h3, h4, h5, h6");

setToc(Array.from(headings).map(transformHeading));
}, [DocContent]);

return (
<DocProvider content={{ ...DocContent, toc } as never}>
<HtmlClassNameProvider className={docHtmlClassName}>
<DocItemMetadata />
<DocItemLayout>
<DocContent />
</DocItemLayout>
</HtmlClassNameProvider>
</DocProvider>
);
}

0 comments on commit b1fc8fe

Please sign in to comment.