Skip to content

Commit

Permalink
Fix expandable block anchor resolution (#2625)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge authored Dec 12, 2024
1 parent bb208ab commit 076dc48
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-falcons-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'gitbook': patch
---

Fix expandable block anchore resolution
75 changes: 74 additions & 1 deletion packages/gitbook/src/lib/document.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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');
});
});
2 changes: 1 addition & 1 deletion packages/gitbook/src/lib/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 076dc48

Please sign in to comment.