Skip to content

Support external images in cards cover #3399

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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/stupid-geese-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'gitbook': minor
---

Support external images in cards cover
17 changes: 15 additions & 2 deletions packages/gitbook/src/components/DocumentView/Table/RecordCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ export async function RecordCard(
const { view, record, context, block, isOffscreen } = props;

const coverFile = view.coverDefinition
? getRecordValue<string[]>(record[1], view.coverDefinition)?.[0]
? getRecordValue<ContentRef[] | string[]>(record[1], view.coverDefinition)?.[0]
: null;
const targetRef = view.targetDefinition
? (record[1].values[view.targetDefinition] as ContentRef)
: null;

const [cover, target] = await Promise.all([
coverFile && context.contentContext
? resolveContentRef({ kind: 'file', file: coverFile }, context.contentContext)
? resolveContentRef(getCoverFileContentRef(coverFile), context.contentContext)
: null,
targetRef && context.contentContext
? resolveContentRef(targetRef, context.contentContext)
Expand Down Expand Up @@ -167,3 +167,16 @@ export async function RecordCard(

return <div className={tcls(RecordCardStyles)}>{body}</div>;
}

/**
* Resolve the cover file content-ref.
* If the value is a string, it means it's a file ID (legacy).
* Otherwise, it's a content-ref (File|URL).
*/
function getCoverFileContentRef(value: ContentRef | string): ContentRef {
if (typeof value === 'string') {
return { kind: 'file', file: value };
}

return value;
}
7 changes: 3 additions & 4 deletions packages/gitbook/src/components/DocumentView/Table/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import assertNever from 'assert-never';
/**
* Get the value for a column in a record.
*/
export function getRecordValue<T extends number | string | boolean | string[] | ContentRef>(
record: DocumentTableRecord,
definitionId: string
): T {
export function getRecordValue<
T extends number | string | boolean | string[] | ContentRef | ContentRef[],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can it be an array of ContentRef?

Why would a cover column referenes multiples content refs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's to handle images type, similar to files, but ContentRef instead of string to get the difference between file and url content-ref

>(record: DocumentTableRecord, definitionId: string): T {
// @ts-ignore
return record.values[definitionId];
}
Expand Down