Skip to content

Commit 25516f6

Browse files
committed
more refactoring to plugins and unit tests
wip wip, compiles wip processing sample site wip
1 parent 02d9cac commit 25516f6

28 files changed

+1399
-716
lines changed

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"//chalk@4": "also ESM related problem",
5151
"//notion-client@4": "also ESM related problem",
5252
"dependencies": {
53-
"@notionhq/client": "1",
53+
"@notionhq/client": "2.2.3",
5454
"chalk": "^4.1.2",
5555
"commander": "^9.2.0",
5656
"cosmiconfig": "^8.0.0",
@@ -62,7 +62,6 @@
6262
"notion-client": "^4",
6363
"notion-to-md": "^2.5.5",
6464
"path": "^0.12.7",
65-
"postinstall-postinstall": "^2.1.0",
6665
"sanitize-filename": "^1.6.3"
6766
},
6867
"devDependencies": {
@@ -80,7 +79,6 @@
8079
"eslint-plugin-prettier": "^3.4.0",
8180
"jest": "^28.1.3",
8281
"lint-staged": "^10.5.4",
83-
"patch-package": "^6.5.1",
8482
"prettier": "^2.2.1",
8583
"semantic-release": "^19.0.2",
8684
"ts-jest": "^28.0.7",

patches/@notionhq+client+1.0.4.patch

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/DocusaurusTweaks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function notionEmbedsToMDX(input: string): {
102102
return { body, imports: [...imports].join("\n") };
103103
}
104104

105-
export const imgur: IPlugin = {
105+
export const imgurGifTweak: IPlugin = {
106106
name: "imgur",
107107
regexMarkdownModifications: [
108108
{

src/HierarchicalNamedLayoutStrategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class HierarchicalNamedLayoutStrategy extends LayoutStrategy {
3636
.replaceAll("'", "")
3737
.replaceAll("?", "-");
3838

39-
const context = ("/" + page.context + "/").replaceAll("//", "/");
39+
const context = ("/" + page.layoutContext + "/").replaceAll("//", "/");
4040
const path =
4141
this.rootDirectory + context + sanitizedName + extensionWithDot;
4242

src/NotionPage.ts

Lines changed: 44 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,11 @@ import {
44
GetPageResponse,
55
ListBlockChildrenResponse,
66
} from "@notionhq/client/build/src/api-endpoints";
7-
import { RateLimiter } from "limiter";
87
import { Client } from "@notionhq/client";
98
import { logDebug } from "./log";
10-
import { parseLinkId } from "./links";
9+
import { parseLinkId } from "./plugins/internalLinks";
1110
import { info } from "console";
12-
13-
const notionLimiter = new RateLimiter({
14-
tokensPerInterval: 3,
15-
interval: "second",
16-
});
17-
18-
let notionClient: Client;
11+
import { ListBlockChildrenResponseResults } from "notion-to-md/build/types";
1912

2013
// Notion has 2 kinds of pages: a normal one which is just content, and what I'm calling a "database page", which has whatever properties you put on it.
2114
// docu-notion supports the later via links from outline pages. That is, you put the database pages in a database, then separately, in the outline, you
@@ -25,55 +18,50 @@ export enum PageType {
2518
DatabasePage,
2619
Simple,
2720
}
28-
export function initNotionClient(notionToken: string): Client {
29-
notionClient = new Client({
30-
auth: notionToken,
31-
});
32-
return notionClient;
33-
}
3421

3522
export class NotionPage {
36-
private metadata: GetPageResponse;
37-
public readonly pageId: string;
38-
public readonly order: number;
39-
public context: string; // where we found it in the hierarchy of the outline
23+
public metadata: GetPageResponse;
24+
public pageId: string;
25+
public order: number;
26+
public layoutContext: string; // where we found it in the hierarchy of the outline
4027
public foundDirectlyInOutline: boolean; // the page was found as a descendent of /outline instead of being linked to
4128

42-
private constructor(
43-
context: string,
44-
pageId: string,
45-
order: number,
46-
metadata: GetPageResponse,
47-
foundDirectlyInOutline: boolean
48-
) {
49-
this.context = context;
50-
this.pageId = pageId;
51-
this.order = order;
52-
this.metadata = metadata;
53-
this.foundDirectlyInOutline = foundDirectlyInOutline;
29+
// public constructor(
30+
// layoutContext: string,
31+
// pageId: string,
32+
// order: number,
33+
// metadata: GetPageResponse,
34+
// foundDirectlyInOutline: boolean
35+
// ) {
36+
// this.layoutContext = layoutContext;
37+
// this.pageId = pageId;
38+
// this.order = order;
39+
// this.metadata = metadata;
40+
// this.foundDirectlyInOutline = foundDirectlyInOutline;
41+
42+
// // review: this is expensive to learn as it takes another api call... I
43+
// // think? We can tell if it's a database because it has a "Name" instead of a
44+
// // "tile" and "parent": "type": "database_id". But do we need to differentiate
45+
// //this.type = PageType.Unknown;
46+
// }
47+
public constructor(args: {
48+
layoutContext: string;
49+
pageId: string;
50+
order: number;
51+
metadata: GetPageResponse;
52+
foundDirectlyInOutline: boolean;
53+
}) {
54+
this.layoutContext = args.layoutContext;
55+
this.pageId = args.pageId;
56+
this.order = args.order;
57+
this.metadata = args.metadata;
58+
this.foundDirectlyInOutline = args.foundDirectlyInOutline;
5459

5560
// review: this is expensive to learn as it takes another api call... I
5661
// think? We can tell if it's a database because it has a "Name" instead of a
5762
// "tile" and "parent": "type": "database_id". But do we need to differentiate
5863
//this.type = PageType.Unknown;
5964
}
60-
public static async fromPageId(
61-
context: string,
62-
pageId: string,
63-
order: number,
64-
foundDirectlyInOutline: boolean
65-
): Promise<NotionPage> {
66-
const metadata = await getPageMetadata(pageId);
67-
68-
//logDebug("notion metadata", JSON.stringify(metadata));
69-
return new NotionPage(
70-
context,
71-
pageId,
72-
order,
73-
metadata,
74-
foundDirectlyInOutline
75-
);
76-
}
7765

7866
public matchesLinkId(id: string): boolean {
7967
const { baseLinkId } = parseLinkId(id);
@@ -172,14 +160,6 @@ export class NotionPage {
172160
return this.getSelectProperty("Status");
173161
}
174162

175-
private async getChildren(): Promise<ListBlockChildrenResponse> {
176-
const children = await notionClient.blocks.children.list({
177-
block_id: this.pageId,
178-
page_size: 100, // max hundred links in a page
179-
});
180-
return children;
181-
}
182-
183163
public getPlainTextProperty(
184164
property: string,
185165
defaultIfEmpty: string
@@ -244,68 +224,28 @@ export class NotionPage {
244224
return p.select?.name || undefined;
245225
}
246226

247-
public async getBlockChildren(): Promise<ListBlockChildrenResponse> {
248-
// we can only get so many responses per call, so we set this to
249-
// the first response we get, then keep adding to its array of blocks
250-
// with each subsequent response
251-
let overallResult: ListBlockChildrenResponse | undefined = undefined;
252-
let start_cursor = undefined;
253-
254-
do {
255-
await rateLimit();
256-
257-
const response: ListBlockChildrenResponse =
258-
await notionClient.blocks.children.list({
259-
start_cursor: start_cursor,
260-
block_id: this.pageId,
261-
});
262-
if (!overallResult) {
263-
overallResult = response;
264-
} else {
265-
overallResult.results.push(...response.results);
266-
}
267-
268-
start_cursor = response?.next_cursor;
269-
} while (start_cursor != null);
270-
return overallResult;
271-
}
272-
273-
public async getContentInfo(): Promise<{
227+
public async getContentInfo(
228+
children: ListBlockChildrenResponseResults
229+
): Promise<{
274230
childPageIdsAndOrder: { id: string; order: number }[];
275231
linksPageIdsAndOrder: { id: string; order: number }[];
276232
hasParagraphs: boolean;
277233
}> {
278-
const children = await this.getChildren();
279-
for (let i = 0; i < children.results.length; i++) {
280-
(children.results[i] as any).order = i;
234+
for (let i = 0; i < children.length; i++) {
235+
(children[i] as any).order = i;
281236
}
282237
return {
283-
childPageIdsAndOrder: children.results
238+
childPageIdsAndOrder: children
284239
.filter((b: any) => b.type === "child_page")
285240
.map((b: any) => ({ id: b.id, order: b.order })),
286-
linksPageIdsAndOrder: children.results
241+
linksPageIdsAndOrder: children
287242
.filter((b: any) => b.type === "link_to_page")
288243
.map((b: any) => ({ id: b.link_to_page.page_id, order: b.order })),
289-
hasParagraphs: children.results.some(
244+
hasParagraphs: children.some(
290245
b =>
291246
(b as any).type === "paragraph" &&
292247
(b as any).paragraph.rich_text.length > 0
293248
),
294249
};
295250
}
296251
}
297-
298-
async function getPageMetadata(id: string): Promise<GetPageResponse> {
299-
await rateLimit();
300-
301-
return await notionClient.pages.retrieve({
302-
page_id: id,
303-
});
304-
}
305-
306-
async function rateLimit() {
307-
if (notionLimiter.getTokensRemaining() < 1) {
308-
logDebug("", "*** delaying for rate limit");
309-
}
310-
await notionLimiter.removeTokens(1);
311-
}

0 commit comments

Comments
 (0)