Skip to content

Commit 4f9abfb

Browse files
authored
Display headings from reusable content in TOC (#3708)
1 parent 252afce commit 4f9abfb

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

packages/gitbook/src/lib/document-sections.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import type { GitBookAnyContext } from '@/lib/context';
22
import type { DocumentBlock, JSONDocument } from '@gitbook/api';
33

4+
import { getDataOrNull } from './data';
45
import { getNodeText } from './document';
56
import { resolveOpenAPIOperationBlock } from './openapi/resolveOpenAPIOperationBlock';
67
import { resolveOpenAPISchemasBlock } from './openapi/resolveOpenAPISchemasBlock';
8+
import { resolveContentRef } from './references';
79

810
export interface DocumentSection {
911
id: string;
@@ -28,10 +30,11 @@ export async function getDocumentSections(
2830
*/
2931
async function getSectionsFromNodes(
3032
nodes: DocumentBlock[],
31-
context: GitBookAnyContext
33+
context: GitBookAnyContext,
34+
initialDepth = 0
3235
): Promise<DocumentSection[]> {
3336
const sections: DocumentSection[] = [];
34-
let depth = 0;
37+
let depth = initialDepth;
3538

3639
for (const block of nodes) {
3740
switch (block.type) {
@@ -64,7 +67,9 @@ async function getSectionsFromNodes(
6467
}
6568
case 'stepper': {
6669
const stepNodes = await Promise.all(
67-
block.nodes.map(async (step) => getSectionsFromNodes(step.nodes, context))
70+
block.nodes.map(async (step) =>
71+
getSectionsFromNodes(step.nodes, context, depth)
72+
)
6873
);
6974
for (const stepSections of stepNodes) {
7075
sections.push(...stepSections);
@@ -116,6 +121,39 @@ async function getSectionsFromNodes(
116121
}
117122
continue;
118123
}
124+
case 'reusable-content': {
125+
const dataFetcher = block.meta?.token
126+
? context.dataFetcher.withToken({ apiToken: block.meta.token })
127+
: context.dataFetcher;
128+
129+
const resolved = await resolveContentRef(block.data.ref, {
130+
...context,
131+
dataFetcher,
132+
});
133+
if (!resolved) {
134+
continue;
135+
}
136+
const { reusableContent } = resolved;
137+
if (!reusableContent) {
138+
continue;
139+
}
140+
const document = await getDataOrNull(
141+
dataFetcher.getRevisionReusableContentDocument({
142+
spaceId: reusableContent.context.space.id,
143+
revisionId: reusableContent.context.revisionId,
144+
reusableContentId: reusableContent.revisionReusableContent.id,
145+
})
146+
);
147+
if (!document) {
148+
continue;
149+
}
150+
const reusableContentSections = await getSectionsFromNodes(
151+
document.nodes,
152+
reusableContent.context,
153+
depth
154+
);
155+
sections.push(...reusableContentSections);
156+
}
119157
}
120158
}
121159

0 commit comments

Comments
 (0)