Skip to content

Make heading from stepper visible in outline #3301

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

Merged
Changes from all 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
128 changes: 86 additions & 42 deletions packages/gitbook/src/lib/document-sections.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { JSONDocument } from '@gitbook/api';
import type { DocumentBlock, JSONDocument } from '@gitbook/api';
import type { GitBookAnyContext } from '@v2/lib/context';

import { getNodeText } from './document';
Expand All @@ -19,58 +19,102 @@ export interface DocumentSection {
export async function getDocumentSections(
context: GitBookAnyContext,
document: JSONDocument
): Promise<DocumentSection[]> {
return getSectionsFromNodes(document.nodes, context);
}

/**
* Extract a list of sections from a list of nodes.
*/
async function getSectionsFromNodes(
nodes: DocumentBlock[],
context: GitBookAnyContext
): Promise<DocumentSection[]> {
const sections: DocumentSection[] = [];
let depth = 0;

for (const block of document.nodes) {
if ((block.type === 'heading-1' || block.type === 'heading-2') && block.meta?.id) {
if (block.type === 'heading-1') {
for (const block of nodes) {
switch (block.type) {
case 'heading-1': {
const id = block.meta?.id;
if (!id) {
continue;
}
depth = 1;
}
const title = getNodeText(block);
const id = block.meta.id;

sections.push({
id,
title,
depth: block.type === 'heading-1' ? 1 : depth > 0 ? 2 : 1,
});
}

if ((block.type === 'swagger' || block.type === 'openapi-operation') && block.meta?.id) {
const { data: operation } = await resolveOpenAPIOperationBlock({
block,
context,
});
if (operation) {
const title = getNodeText(block);
sections.push({
id: block.meta.id,
tag: operation.method.toUpperCase(),
title: operation.operation.summary || operation.path,
id,
title,
depth: 1,
deprecated: operation.operation.deprecated,
});
continue;
}
}

if (
block.type === 'openapi-schemas' &&
!block.data.grouped &&
block.meta?.id &&
block.data.schemas.length === 1
) {
const { data } = await resolveOpenAPISchemasBlock({
block,
context,
});
const schema = data?.schemas[0];
if (schema) {
case 'heading-2': {
const id = block.meta?.id;
if (!id) {
continue;
}
const title = getNodeText(block);
sections.push({
id: block.meta.id,
title: `The ${schema.name} object`,
depth: 1,
id,
title,
depth: depth > 0 ? 2 : 1,
});
continue;
}
case 'stepper': {
const stepNodes = await Promise.all(
block.nodes.map(async (step) => getSectionsFromNodes(step.nodes, context))
);
for (const stepSections of stepNodes) {
sections.push(...stepSections);
}
continue;
}
case 'swagger':
case 'openapi-operation': {
const id = block.meta?.id;
if (!id) {
continue;
}
const { data: operation } = await resolveOpenAPIOperationBlock({
block,
context,
});
if (operation) {
sections.push({
id,
tag: operation.method.toUpperCase(),
title: operation.operation.summary || operation.path,
depth: 1,
deprecated: operation.operation.deprecated,
});
}
continue;
}
case 'openapi-schemas': {
const id = block.meta?.id;
if (!id) {
continue;
}
if (block.data.grouped || block.data.schemas.length !== 1) {
// Skip grouped schemas, they are not sections
continue;
}

const { data } = await resolveOpenAPISchemasBlock({
block,
context,
});
const schema = data?.schemas[0];
if (schema) {
sections.push({
id,
title: `The ${schema.name} object`,
depth: 1,
});
}
continue;
}
}
}
Expand Down