Skip to content

Commit 076dc48

Browse files
authored
Fix expandable block anchor resolution (#2625)
1 parent bb208ab commit 076dc48

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

.changeset/beige-falcons-enjoy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'gitbook': patch
3+
---
4+
5+
Fix expandable block anchore resolution

packages/gitbook/src/lib/document.test.ts

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'bun:test';
22

3-
import { isNodeEmpty } from './document';
3+
import { getBlockTitle, isNodeEmpty } from './document';
44

55
describe('isNodeEmpty', () => {
66
it('should return true for a document with an empty paragraph', () => {
@@ -54,3 +54,76 @@ describe('isNodeEmpty', () => {
5454
).toEqual(false);
5555
});
5656
});
57+
58+
describe('#getBlockTitle', () => {
59+
it('should return the title of an expandable block', () => {
60+
expect(
61+
getBlockTitle({
62+
object: 'block',
63+
type: 'expandable',
64+
isVoid: true,
65+
data: {},
66+
key: 'OX8znB9VmbgK',
67+
fragments: [
68+
{
69+
object: 'fragment',
70+
nodes: [
71+
{
72+
object: 'block',
73+
type: 'paragraph',
74+
isVoid: false,
75+
data: {},
76+
nodes: [
77+
{
78+
object: 'text',
79+
leaves: [
80+
{
81+
object: 'leaf',
82+
text: 'Title of expandable block',
83+
marks: [],
84+
},
85+
],
86+
key: '7sZdCBHTw6Si',
87+
},
88+
],
89+
key: 'msYtjdwNmiAB',
90+
},
91+
],
92+
key: 'cNhmBygbrP8N',
93+
fragment: 'expandable-title',
94+
type: 'expandable-title',
95+
},
96+
{
97+
object: 'fragment',
98+
nodes: [
99+
{
100+
object: 'block',
101+
type: 'paragraph',
102+
isVoid: false,
103+
data: {},
104+
nodes: [
105+
{
106+
object: 'text',
107+
leaves: [
108+
{
109+
object: 'leaf',
110+
text: 'And content of the expandable',
111+
marks: [],
112+
},
113+
],
114+
key: '0GEghVKyWRBt',
115+
},
116+
],
117+
key: '9iEwdHdZ5y0S',
118+
},
119+
],
120+
key: 'newg71i9Ujjl',
121+
fragment: 'expandable-body',
122+
type: 'expandable-body',
123+
},
124+
],
125+
meta: { id: 'expandable-block' },
126+
}),
127+
).toEqual('Title of expandable block');
128+
});
129+
});

packages/gitbook/src/lib/document.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export function isNodeEmpty(
154154
export function getBlockTitle(block: DocumentBlock): string {
155155
switch (block.type) {
156156
case 'expandable': {
157-
const titleFragment = getNodeFragmentByType(block, 'title');
157+
const titleFragment = getNodeFragmentByType(block, 'expandable-title');
158158
if (titleFragment) {
159159
return getNodeText(titleFragment);
160160
}

0 commit comments

Comments
 (0)