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

feat(guide): parse and serialize markdown headings #9598

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions apps/guide/contentlayer.config.ts
@@ -1,5 +1,6 @@
import { remarkCodeHike } from '@code-hike/mdx';
import { defineDocumentType, makeSource } from 'contentlayer/source-files';
import GithubSlugger from 'github-slugger';
import { type Node, toString } from 'hast-util-to-string';
import { h } from 'hastscript';
import { escape } from 'html-escaper';
Expand Down Expand Up @@ -33,6 +34,23 @@ export const Content = defineDocumentType(() => ({
// eslint-disable-next-line unicorn/prefer-string-replace-all
resolve: (doc) => `/guide/${doc._raw.flattenedPath.replace(/\d+-/g, '')}`,
},
headings: {
type: 'json',
resolve: async (doc) => {
const regXHeader = /\n(?<flag>#{1,6})\s+(?<content>.+)/g;
const slugger = new GithubSlugger();
// @ts-expect-error TypeScript can't infer
return Array.from(doc.body.raw.matchAll(regXHeader)).map(({ groups }) => {
const flag = groups?.flag;
const content = groups?.content;
return {
level: flag?.length,
text: content,
slug: content ? slugger.slug(content) : undefined,
suneettipirneni marked this conversation as resolved.
Show resolved Hide resolved
};
});
},
},
},
}));

Expand Down
1 change: 1 addition & 0 deletions apps/guide/package.json
Expand Up @@ -53,6 +53,7 @@
"ariakit": "^2.0.0-next.44",
"cmdk": "^0.2.0",
"contentlayer": "^0.3.2",
"github-slugger": "^2.0.0",
"next": "^13.4.3",
"next-contentlayer": "^0.3.2",
"next-themes": "^0.2.1",
Expand Down
4 changes: 3 additions & 1 deletion apps/guide/src/app/guide/[...slug]/page.tsx
Expand Up @@ -8,11 +8,13 @@ export async function generateStaticParams() {

export default function Page({ params }: { params: { slug: string[] } }) {
const content = allContents.find((content) => content.slug === params.slug?.join('/'));

if (!content) {
notFound();
}

// TODO Render headings in table of contents
// const headings = serializeHeadings(content.headings);

return (
<article className="max-w-none px-5 prose">
<Mdx code={content?.body.code ?? ''} />
Expand Down
42 changes: 42 additions & 0 deletions apps/guide/src/util/heading-node.ts
@@ -0,0 +1,42 @@
export interface HeadingData {
level: number;
slug: string;
text: string;
}

export interface HeadingNode extends HeadingData {
children: HeadingNode[];
}

/**
* Serialize heading data into a hierarchial tree structure where lower level headings are children of higher level headings.
suneettipirneni marked this conversation as resolved.
Show resolved Hide resolved
*
* @param headings - An array of heading data
*/
export function serializeHeadings(headings: HeadingData[]): HeadingNode[] {
const tree: HeadingNode[] = [];
const stack: HeadingNode[] = [];

for (const heading of headings) {
const node: HeadingNode = {
level: heading.level,
text: heading.text,
slug: heading.slug,
children: [],
};

while (stack.length > 0 && stack.at(-1)!.level >= node.level) {
stack.pop();
}

if (stack.length === 0) {
tree.push(node);
} else {
stack.at(-1)!.children.push(node);
}

stack.push(node);
}

return tree;
}
1 change: 1 addition & 0 deletions yarn.lock
Expand Up @@ -2208,6 +2208,7 @@ __metadata:
eslint: ^8.41.0
eslint-config-neon: ^0.1.47
eslint-formatter-pretty: ^5.0.0
github-slugger: ^2.0.0
happy-dom: ^9.20.1
hast-util-to-string: ^2.0.0
hastscript: ^7.2.0
Expand Down