From fb02bd8c4250cfaf427886bc7999b65438fd0ad2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Thu, 12 Dec 2024 15:24:56 +0100 Subject: [PATCH] Fix expandable block anchor resolution --- .changeset/beige-falcons-enjoy.md | 5 ++ packages/gitbook/src/lib/document.test.ts | 75 ++++++++++++++++++++++- packages/gitbook/src/lib/document.ts | 2 +- 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 .changeset/beige-falcons-enjoy.md diff --git a/.changeset/beige-falcons-enjoy.md b/.changeset/beige-falcons-enjoy.md new file mode 100644 index 0000000000..76e2df793e --- /dev/null +++ b/.changeset/beige-falcons-enjoy.md @@ -0,0 +1,5 @@ +--- +'gitbook': patch +--- + +Fix expandable block anchore resolution diff --git a/packages/gitbook/src/lib/document.test.ts b/packages/gitbook/src/lib/document.test.ts index 98199dc1a2..1e034d084e 100644 --- a/packages/gitbook/src/lib/document.test.ts +++ b/packages/gitbook/src/lib/document.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'bun:test'; -import { isNodeEmpty } from './document'; +import { getBlockTitle, isNodeEmpty } from './document'; describe('isNodeEmpty', () => { it('should return true for a document with an empty paragraph', () => { @@ -54,3 +54,76 @@ describe('isNodeEmpty', () => { ).toEqual(false); }); }); + +describe('#getBlockTitle', () => { + it('should return the title of an expandable block', () => { + expect( + getBlockTitle({ + object: 'block', + type: 'expandable', + isVoid: true, + data: {}, + key: 'OX8znB9VmbgK', + fragments: [ + { + object: 'fragment', + nodes: [ + { + object: 'block', + type: 'paragraph', + isVoid: false, + data: {}, + nodes: [ + { + object: 'text', + leaves: [ + { + object: 'leaf', + text: 'Title of expandable block', + marks: [], + }, + ], + key: '7sZdCBHTw6Si', + }, + ], + key: 'msYtjdwNmiAB', + }, + ], + key: 'cNhmBygbrP8N', + fragment: 'expandable-title', + type: 'expandable-title', + }, + { + object: 'fragment', + nodes: [ + { + object: 'block', + type: 'paragraph', + isVoid: false, + data: {}, + nodes: [ + { + object: 'text', + leaves: [ + { + object: 'leaf', + text: 'And content of the expandable', + marks: [], + }, + ], + key: '0GEghVKyWRBt', + }, + ], + key: '9iEwdHdZ5y0S', + }, + ], + key: 'newg71i9Ujjl', + fragment: 'expandable-body', + type: 'expandable-body', + }, + ], + meta: { id: 'expandable-block' }, + }), + ).toEqual('Title of expandable block'); + }); +}); diff --git a/packages/gitbook/src/lib/document.ts b/packages/gitbook/src/lib/document.ts index aef7010a33..8cf5be47bf 100644 --- a/packages/gitbook/src/lib/document.ts +++ b/packages/gitbook/src/lib/document.ts @@ -154,7 +154,7 @@ export function isNodeEmpty( export function getBlockTitle(block: DocumentBlock): string { switch (block.type) { case 'expandable': { - const titleFragment = getNodeFragmentByType(block, 'title'); + const titleFragment = getNodeFragmentByType(block, 'expandable-title'); if (titleFragment) { return getNodeText(titleFragment); }