Skip to content

Commit

Permalink
feat: add pages' flattened properties in the frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
alvis committed Jul 8, 2021
1 parent 0a954af commit 0be8b1b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 28 deletions.
6 changes: 6 additions & 0 deletions source/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ export class Notion {
title,
createdTime: page.created_time,
lastEditedTime: page.last_edited_time,
...Object.fromEntries(
Object.entries(page.properties)
// omit the already transformed title
.filter(([key]) => !['title', 'Name'].includes(key))
.map(([key, property]) => [key, getPropertyContent(property)]),
),
},
{ forceQuotes: true },
).trim(),
Expand Down
32 changes: 31 additions & 1 deletion spec/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,36 @@ describe('cl:Notion', () => {

describe('fn:getPage', () => {
const BLOCKS_PER_PAGE = 2;
mockPage('page', BLOCKS_PER_PAGE);
mockPage('page', BLOCKS_PER_PAGE, {
title: {
id: 'title',
type: 'title',
title: [
{
annotations: {
bold: false,
code: false,
color: 'default',
italic: false,
strikethrough: false,
underline: false,
},
href: null,
plain_text: 'Title',
text: {
content: 'Title',
link: null,
},
type: 'text',
},
],
},
extra: {
id: 'extra',
type: 'number',
number: 0,
},
});

it('return a page in detail', async () => {
const page = await client.getPage('page');
Expand All @@ -57,6 +86,7 @@ id: 'page'
title: 'Title'
createdTime: '2020-01-01T00:00:00Z'
lastEditedTime: '2020-01-01T00:00:00Z'
extra: 0
---
block 0 for block page
Expand Down
58 changes: 31 additions & 27 deletions spec/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import nock from 'nock';
import { URL } from 'url';

import type { Block, Database, List, Page } from '#types';
import type { Block, Database, List, Page, PropertyValueMap } from '#types';

export function mockBlockList(
blockID: string,
Expand Down Expand Up @@ -193,7 +193,35 @@ export function mockDatabasePageList(
.persist();
}

export function mockPage(pageID: string, blocks: number = 1) {
export function mockPage(
pageID: string,
blocks: number = 1,
properties: PropertyValueMap = {
title: {
id: 'title',
type: 'title',
title: [
{
annotations: {
bold: false,
code: false,
color: 'default',
italic: false,
strikethrough: false,
underline: false,
},
href: null,
plain_text: 'Title',
text: {
content: 'Title',
link: null,
},
type: 'text',
},
],
},
},
) {
mockBlockList(pageID, blocks);

const body: Page = {
Expand All @@ -207,31 +235,7 @@ export function mockPage(pageID: string, blocks: number = 1) {
},
archived: false,
url: `https://www.notion.so/${pageID}`,
properties: {
title: {
id: 'title',
title: [
{
annotations: {
bold: false,
code: false,
color: 'default',
italic: false,
strikethrough: false,
underline: false,
},
href: null,
plain_text: 'Title',
text: {
content: 'Title',
link: null,
},
type: 'text',
},
],
type: 'title',
},
},
properties,
};

nock('https://api.notion.com')
Expand Down

0 comments on commit 0be8b1b

Please sign in to comment.