Skip to content

Support for reusable content across spaces. #3173

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
6 changes: 6 additions & 0 deletions .changeset/polite-falcons-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"gitbook-v2": minor
"gitbook": minor
---

Add support for reusable content across spaces.
4 changes: 2 additions & 2 deletions bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@
},
"overrides": {
"@codemirror/state": "6.4.1",
"@gitbook/api": "0.111.0",
"@gitbook/api": "0.113.0",
"react": "^19.0.0",
"react-dom": "^19.0.0",
},
Expand Down Expand Up @@ -624,7 +624,7 @@

"@fortawesome/fontawesome-svg-core": ["@fortawesome/fontawesome-svg-core@6.6.0", "", { "dependencies": { "@fortawesome/fontawesome-common-types": "6.6.0" } }, "sha512-KHwPkCk6oRT4HADE7smhfsKudt9N/9lm6EJ5BVg0tD1yPA5hht837fB87F8pn15D8JfTqQOjhKTktwmLMiD7Kg=="],

"@gitbook/api": ["@gitbook/api@0.111.0", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-E5Pk28kPD4p6XNWdwFM9pgDijdByseIZQqcFK+/hoW5tEZa5Yw/plRKJyN1hmwfPL6SKq6Maf0fbIzTQiVXyQQ=="],
"@gitbook/api": ["@gitbook/api@0.113.0", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-PWMeAkdm4bHSl3b5OmtcmskZ6qRkkDhauCPybo8sGnjS03O14YAUtubAQiNCKX/uwbs+yiQ8KRPyeIwn+g42yw=="],

"@gitbook/cache-do": ["@gitbook/cache-do@workspace:packages/cache-do"],

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"packageManager": "bun@1.2.8",
"overrides": {
"@codemirror/state": "6.4.1",
"@gitbook/api": "0.111.0",
"@gitbook/api": "0.113.0",
"react": "^19.0.0",
"react-dom": "^19.0.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export const contentKitServerContext: ContentKitServerContext = {
'link-external': (props) => <Icon icon="arrow-up-right-from-square" {...props} />,
eye: (props) => <Icon icon="eye" {...props} />,
lock: (props) => <Icon icon="lock" {...props} />,
check: (props) => <Icon icon="check" {...props} />,
'check-circle': (props) => <Icon icon="check-circle" {...props} />,
},
codeBlock: (props) => {
return <PlainCodeBlock code={props.code} syntax={props.syntax} />;
Expand Down
23 changes: 18 additions & 5 deletions packages/gitbook/src/components/DocumentView/ReusableContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,28 @@ export async function ReusableContent(props: BlockProps<DocumentBlockReusableCon
throw new Error('Expected a content context to render a reusable content block');
}

const resolved = await resolveContentRef(block.data.ref, context.contentContext);
if (!resolved?.reusableContent?.document) {
const dataFetcher = block.meta?.token
? context.contentContext.dataFetcher.withToken({ apiToken: block.meta.token })
: context.contentContext.dataFetcher;

const resolved = await resolveContentRef(block.data.ref, {
...context.contentContext,
dataFetcher,
});

if (!resolved?.reusableContent) {
return null;
}

const reusableContent = resolved.reusableContent.revisionReusableContent;
if (!reusableContent.document) {
return null;
}

const document = await getDataOrNull(
context.contentContext.dataFetcher.getDocument({
spaceId: context.contentContext.space.id,
documentId: resolved.reusableContent.document,
dataFetcher.getDocument({
spaceId: resolved.reusableContent.space,
documentId: reusableContent.document,
})
);

Expand Down
24 changes: 24 additions & 0 deletions packages/gitbook/src/fonts/custom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const TEST_FONTS: { [key in string]: CustomizationFontDefinition } = {
],
},
],
permissions: {
edit: false,
},
},

multiWeight: {
Expand Down Expand Up @@ -81,6 +84,9 @@ const TEST_FONTS: { [key in string]: CustomizationFontDefinition } = {
],
},
],
permissions: {
edit: false,
},
},

multiSource: {
Expand All @@ -99,6 +105,9 @@ const TEST_FONTS: { [key in string]: CustomizationFontDefinition } = {
],
},
],
permissions: {
edit: false,
},
},

missingFormat: {
Expand All @@ -117,13 +126,19 @@ const TEST_FONTS: { [key in string]: CustomizationFontDefinition } = {
],
},
],
permissions: {
edit: false,
},
},

empty: {
id: 'empty-font',
custom: true,
fontFamily: 'Empty Font',
fontFaces: [],
permissions: {
edit: false,
},
},

specialChars: {
Expand All @@ -136,6 +151,9 @@ const TEST_FONTS: { [key in string]: CustomizationFontDefinition } = {
sources: [{ url: 'https://example.com/fonts/special.woff2', format: 'woff2' }],
},
],
permissions: {
edit: false,
},
},

complex: {
Expand All @@ -158,6 +176,9 @@ const TEST_FONTS: { [key in string]: CustomizationFontDefinition } = {
],
},
],
permissions: {
edit: false,
},
},

variousURLs: {
Expand All @@ -174,6 +195,9 @@ const TEST_FONTS: { [key in string]: CustomizationFontDefinition } = {
],
},
],
permissions: {
edit: false,
},
},
};

Expand Down
6 changes: 3 additions & 3 deletions packages/gitbook/src/fonts/custom.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { CustomizationFontDefinition } from '@gitbook/api';
import type { CustomizationFontDefinitionInput } from '@gitbook/api';

/**
* Define the custom font faces and set the --font-custom to the custom font name
*/
export function generateFontFacesCSS(customFont: CustomizationFontDefinition): string {
export function generateFontFacesCSS(customFont: CustomizationFontDefinitionInput): string {
const { fontFaces } = customFont;

// Generate font face declarations for all weights
Expand Down Expand Up @@ -45,7 +45,7 @@ export function generateFontFacesCSS(customFont: CustomizationFontDefinition): s
/**
* Get a list of font sources to preload (only 400 and 700 weights)
*/
export function getFontSourcesToPreload(customFont: CustomizationFontDefinition) {
export function getFontSourcesToPreload(customFont: CustomizationFontDefinitionInput) {
return customFont.fontFaces.filter(
(face): face is typeof face & { weight: 400 | 700 } =>
face.weight === 400 || face.weight === 700
Expand Down
1 change: 1 addition & 0 deletions packages/gitbook/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ export const getPublishedContentByUrl = cache({

const parsed = parseCacheResponse(response);

// biome-ignore lint/suspicious/noConsole: log the ttl of the token
console.log(
`Parsed ttl: ${parsed.ttl} at ${Date.now()}, for ${'apiToken' in response.data ? response.data.apiToken : '<no-token>'}`
);
Expand Down
47 changes: 41 additions & 6 deletions packages/gitbook/src/lib/references.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ export interface ResolvedContentRef {
file?: RevisionFile;
/** Page document resolved from the content ref */
page?: RevisionPageDocument;
/** Resolved reusable content, if the ref points to reusable content on a revision. */
reusableContent?: RevisionReusableContent;
/** Resolved reusable content, if the ref points to reusable content on a revision. Also contains the space and revision used for resolution. */
reusableContent?: {
revisionReusableContent: RevisionReusableContent;
space: string;
revision: string;
};
/** Resolve OpenAPI spec filesystem. */
openAPIFilesystem?: Filesystem;
}
Expand Down Expand Up @@ -231,21 +235,52 @@ export async function resolveContentRef(
}

case 'reusable-content': {
// Figure out which space and revision the reusable content is in.
const container: { space: string; revision: string } | null = await (async () => {
// without a space on the content ref, or if the space is the same as the current one, we can use the current revision.
if (!contentRef.space || contentRef.space === context.space.id) {
return { space: context.space.id, revision: revisionId };
}

const space = await getDataOrNull(
dataFetcher.getSpace({
spaceId: contentRef.space,
shareKey: undefined,
})
);

if (!space) {
return null;
}

return { space: space.id, revision: space.revision };
})();

if (!container) {
return null;
}

const reusableContent = await getDataOrNull(
dataFetcher.getReusableContent({
spaceId: space.id,
revisionId,
spaceId: container.space,
revisionId: container.revision,
reusableContentId: contentRef.reusableContent,
})
);

if (!reusableContent) {
return null;
}

return {
href: getGitBookAppHref(`/s/${space.id}`),
href: getGitBookAppHref(`/s/${container.space}/~/reusable/${reusableContent.id}`),
text: reusableContent.title,
active: false,
reusableContent,
reusableContent: {
revisionReusableContent: reusableContent,
space: container.space,
revision: container.revision,
},
};
}

Expand Down
Loading
Loading