Skip to content

Fix expandable block anchor resolution #2625

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
Show file tree
Hide file tree
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/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
Loading