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

Resolve sections in reusable content #2870

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/khaki-ravens-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'gitbook': patch
---

Resolve sections in reusable content blocks
4 changes: 3 additions & 1 deletion packages/gitbook/src/components/PageAside/PageAside.tsx
Original file line number Diff line number Diff line change
@@ -252,7 +252,9 @@ export async function PageAside(props: {
async function PageAsideSections(props: { document: JSONDocument; context: ContentRefContext }) {
const { document, context } = props;

const sections = await getDocumentSections(document, (ref) => resolveContentRef(ref, context));
const sections = await getDocumentSections(context.space, document, (ref) =>
resolveContentRef(ref, context),
);

return sections.length > 1 ? <ScrollSectionsList sections={sections} /> : null;
}
29 changes: 28 additions & 1 deletion packages/gitbook/src/lib/document-sections.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { JSONDocument, ContentRef } from '@gitbook/api';
import { JSONDocument, ContentRef, Space } from '@gitbook/api';

import { getDocument } from './api';
import { getNodeText } from './document';
import { resolveOpenAPIBlock } from './openapi/fetch';
import { ResolvedContentRef } from './references';
@@ -16,6 +17,7 @@ export interface DocumentSection {
* Extract a list of sections from a document.
*/
export async function getDocumentSections(
space: Space,
document: JSONDocument,
resolveContentRef: (ref: ContentRef) => Promise<ResolvedContentRef | null>,
): Promise<DocumentSection[]> {
@@ -52,6 +54,31 @@ export async function getDocumentSections(
});
}
}

if (block.type === 'reusable-content') {
const resolved = await resolveContentRef(block.data.ref);
const documentId = resolved?.reusableContent?.document;
if (!documentId) {
throw new Error(`Expected a document ID for reusable content block`);
}

const document = await getDocument(space.id, documentId);
if (!document) {
throw new Error(`Document not found for reusable content block`);
}

const resuableContentSections = await getDocumentSections(
space,
document,
resolveContentRef,
);
sections.push(
...resuableContentSections.map((section) => ({
...section,
depth: section.depth + depth,
})),
);
}
}

return sections;
Loading
Oops, something went wrong.