Skip to content

Commit 7feb7b2

Browse files
FranciscoMorettihatton
authored andcommitted
Added getDateProperty method to NotionPage
1 parent 60141a4 commit 7feb7b2

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

src/NotionPage.spec.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ describe("NotionPage", () => {
6969
},
7070
],
7171
},
72+
date_property: {
73+
id: "a%3Cql",
74+
type: "date",
75+
date: {
76+
start: "2021-10-24",
77+
end: "2021-10-28",
78+
time_zone: null,
79+
},
80+
},
7281
},
7382
url: "https://www.notion.so/Site-docu-notion-PAGEID",
7483
};
@@ -102,4 +111,48 @@ describe("NotionPage", () => {
102111
expect(result).toBe("Default Value");
103112
});
104113
});
114+
115+
describe("getDateProperty", () => {
116+
it("should return the start date property by default", () => {
117+
const page = new NotionPage({
118+
layoutContext: "Test Context",
119+
pageId: "123",
120+
order: 1,
121+
metadata: mockMetadata,
122+
foundDirectlyInOutline: true,
123+
});
124+
125+
const result = page.getDateProperty("date_property", "");
126+
127+
expect(result).toBe("2021-10-24");
128+
});
129+
130+
it("should return the end date if start is false", () => {
131+
const page = new NotionPage({
132+
layoutContext: "Test Context",
133+
pageId: "123",
134+
order: 1,
135+
metadata: mockMetadata,
136+
foundDirectlyInOutline: true,
137+
});
138+
139+
const result = page.getDateProperty("date_property", "", false);
140+
141+
expect(result).toBe("2021-10-28");
142+
});
143+
144+
it("should return the default value if the property is not found", () => {
145+
const page = new NotionPage({
146+
layoutContext: "Test Context",
147+
pageId: "123",
148+
order: 1,
149+
metadata: mockMetadata,
150+
foundDirectlyInOutline: true,
151+
});
152+
153+
const result = page.getPlainTextProperty("nonexistent", "Default Value");
154+
155+
expect(result).toBe("Default Value");
156+
});
157+
});
105158
});

src/NotionPage.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,38 @@ export class NotionPage {
207207
return p.select?.name || undefined;
208208
}
209209

210+
public getDateProperty(
211+
property: string,
212+
defaultIfEmpty: string,
213+
start = true
214+
): string {
215+
/* Notion dates look like this
216+
"properties": {
217+
"published_date":
218+
{
219+
"id":"a%3Cql",
220+
"type":"date",
221+
"date":{
222+
"start":"2021-10-24",
223+
"end":null,
224+
"time_zone":null
225+
}
226+
}
227+
}
228+
*/
229+
230+
// console.log("metadata:\n" + JSON.stringify(this.metadata, null, 2));
231+
const p = (this.metadata as any).properties?.[property];
232+
233+
// console.log(`prop ${property} = ${JSON.stringify(p)}`);
234+
if (!p) return defaultIfEmpty;
235+
if (start) {
236+
return p?.date?.start ? (p.date.start as string) : defaultIfEmpty;
237+
} else {
238+
return p?.date?.end ? (p.date.end as string) : defaultIfEmpty;
239+
}
240+
}
241+
210242
public async getContentInfo(
211243
children: ListBlockChildrenResponseResults
212244
): Promise<{

0 commit comments

Comments
 (0)