Skip to content

Commit

Permalink
feat: add support for cover and icon
Browse files Browse the repository at this point in the history
  • Loading branch information
alvis committed Jun 2, 2022
1 parent 979b057 commit 3e0c3e7
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 2 deletions.
31 changes: 30 additions & 1 deletion source/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* -------------------------------------------------------------------------
*/

import { getPropertyContentFromFile } from '#property';

import type { Metadata, NotionAPIDatabase, NotionAPIPage } from '#types';

/**
Expand All @@ -28,6 +30,33 @@ export function getMetadata<E extends NotionAPIPage | NotionAPIDatabase>(

const variable = { url, lastEditedTime };
const invariant = { createdTime };
const visual = getCommonVisualMetadata(entity);

return { ...variable, ...invariant, ...visual };
}

/**
* get common visual properties such as cover and icon from a page or database
* @param entity the page or database object returned from Notion API
* @returns common properties
*/
export function getCommonVisualMetadata<
E extends NotionAPIPage | NotionAPIDatabase,
>(
entity: E,
): {
coverImage: string | null;
iconEmoji: string | null;
iconImage: string | null;
} {
const { cover, icon } = entity;

return { ...variable, ...invariant };
return {
coverImage: cover ? getPropertyContentFromFile(cover) : null,
iconEmoji: icon?.type === 'emoji' ? icon.emoji : null,
iconImage:
icon?.type === 'external' || icon?.type === 'file'
? getPropertyContentFromFile(icon)
: null,
};
}
3 changes: 3 additions & 0 deletions source/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ export type Metadata = {
url: string;
createdTime: string;
lastEditedTime: string;
coverImage: string | null;
iconEmoji: string | null;
iconImage: string | null;
};

export type Page = {
Expand Down
6 changes: 5 additions & 1 deletion spec/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@

import assert from 'assert';

import { Notion } from '#client';
import { Notion, getCommonMetadata } from '#client';
import { mockDatabase, mockPage } from './mock';
import { NotionAPIPage } from '#types';

describe('cl:Notion', () => {
const client = new Notion({ token: 'token' });
Expand Down Expand Up @@ -71,6 +72,9 @@ title: 'Text'
url: 'https://www.notion.so/workspace/page'
lastEditedTime: '2020-01-01T00:00:00Z'
createdTime: '2020-01-01T00:00:00Z'
coverImage: 'https://www.notion.so/cover.png'
iconEmoji: '📚'
iconImage: null
---
page-block0
Expand Down
85 changes: 85 additions & 0 deletions spec/metadata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,91 @@ describe('fn:getMetadata', () => {
createdTime: '2020-01-01T00:00:00Z',
lastEditedTime: '2020-01-01T00:00:00Z',
url: 'https://www.notion.so/workspace/page',
coverImage: 'https://www.notion.so/cover.png',
iconEmoji: '📚',
iconImage: null,
});
});

it('return null if no cover is set', () => {
expect(getMetadata({ ...examples.page, cover: null })).toEqual(
expect.objectContaining({ coverImage: null }),
);
});

it('return the file url if the cover is embedded', () => {
expect(
getMetadata({
...examples.page,
cover: {
type: 'file',
file: {
url: 'url',
expiry_time: '2020-01-01T00:00:00Z',
},
},
}),
).toEqual(expect.objectContaining({ coverImage: 'url' }));
});

it('return the file url if the cover is external', () => {
expect(
getMetadata({
...examples.page,
cover: {
type: 'external',
external: {
url: 'url',
},
},
}),
).toEqual(expect.objectContaining({ coverImage: 'url' }));
});

it('return null if no icon is set', () => {
expect(getMetadata({ ...examples.page, icon: null })).toEqual(
expect.objectContaining({ iconEmoji: null, iconImage: null }),
);
});

it('return the emoji string if the icon is set as an emoji', () => {
expect(
getMetadata({
...examples.page,
icon: {
type: 'emoji',
emoji: '☀️',
},
}),
).toEqual(expect.objectContaining({ iconEmoji: '☀️', iconImage: null }));
});

it('return the file url if the icon is embedded', () => {
expect(
getMetadata({
...examples.page,
icon: {
type: 'file',
file: {
url: 'url',
expiry_time: '2020-01-01T00:00:00Z',
},
},
}),
).toEqual(expect.objectContaining({ iconEmoji: null, iconImage: 'url' }));
});

it('return the file url if the icon is external', () => {
expect(
getMetadata({
...examples.page,
icon: {
type: 'external',
external: {
url: 'url',
},
},
}),
).toEqual(expect.objectContaining({ iconEmoji: null, iconImage: 'url' }));
});
});
6 changes: 6 additions & 0 deletions spec/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ function generateDatabase({
url: `https://www.notion.so/${title.replace(' ', '-')}-${databaseID}`,
createdTime,
lastEditedTime,
coverImage: null,
iconEmoji: null,
iconImage: null,
},
pages,
};
Expand Down Expand Up @@ -80,6 +83,9 @@ function generatePage({
url: `https://www.notion.so/${title.replace(' ', '-')}-${pageID}`,
createdTime,
lastEditedTime,
coverImage: null,
iconEmoji: null,
iconImage: null,
},
markdown: '',
blocks: [],
Expand Down

0 comments on commit 3e0c3e7

Please sign in to comment.