Skip to content

Commit b7972fb

Browse files
committed
fixes
1 parent 1d26263 commit b7972fb

File tree

1 file changed

+110
-85
lines changed

1 file changed

+110
-85
lines changed

src/index.ts

Lines changed: 110 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,26 @@ function composeSignature(pageId: string): string {
5656
return `<!-- ${pageId} -->`;
5757
}
5858

59-
function extractPageTitle(page: Page): string {
60-
return ((page as any).properties?.title || (page as any).properties?.Title)
61-
?.title[0].plain_text;
59+
function extractPageTitle(page: Page): string | null {
60+
const pageTitleProps =
61+
(page as any).properties?.title || (page as any).properties?.Title;
62+
63+
if (!pageTitleProps) {
64+
return null;
65+
}
66+
67+
try {
68+
const titleRecord = pageTitleProps.title?.[0];
69+
if (!titleRecord) {
70+
return null;
71+
}
72+
73+
return titleRecord.plain_text || null;
74+
} catch (e) {
75+
console.error("failed on pageTitleProps", page);
76+
77+
return null;
78+
}
6279
}
6380

6481
type DiscussionsSearchResult = NonNullable<
@@ -287,95 +304,103 @@ async function buildUpdatePlan(
287304
delete: [],
288305
};
289306

290-
for (const page of pages) {
291-
const pageTitle = extractPageTitle(page);
292-
console.info(`Building plan for page: `, pageTitle, page);
293-
const mdBlocks = await n2m.pageToMarkdown(page.id, 2);
294-
const pageAttributes = distinguishPage(mdBlocks[0]);
295-
const notionPageSignature = composeSignature(page.id);
296-
const existingDiscussion = discussions.find((v) =>
297-
v.body.startsWith(notionPageSignature)
298-
);
299-
const existingIssue = issues.find((v) =>
300-
v.body.startsWith(notionPageSignature)
301-
);
302-
303-
if (pageAttributes === null) {
304-
if (existingDiscussion) {
305-
outputDiscussions.delete.push({
306-
repoId: existingDiscussion.repository.id,
307-
discussion: existingDiscussion,
308-
});
309-
}
307+
const modified = await Promise.all(
308+
pages.map(async (page) => {
309+
const pageTitle = extractPageTitle(page);
310310

311-
if (existingIssue) {
312-
outputIssues.delete.push({
313-
repoId: existingIssue.repository.id,
314-
issue: existingIssue,
315-
});
311+
if (!pageTitle) {
312+
console.warn(`Skipped page due to missing title info:`, page);
313+
return;
316314
}
317-
} else if (pageAttributes.type === "discussion") {
318-
const [owner, name] = pageAttributes.repo.split("/");
319-
const repoInfo = await getRepoInfo(octokit, name, owner);
320-
const category = (repoInfo.discussionCategories || []).find(
321-
(d: any) =>
322-
d.name.toLowerCase() === pageAttributes.categoryName.toLowerCase()
315+
316+
console.info(`Building plan for page: `, pageTitle, page);
317+
const mdBlocks = await n2m.pageToMarkdown(page.id, 2);
318+
const pageAttributes = distinguishPage(mdBlocks[0]);
319+
const notionPageSignature = composeSignature(page.id);
320+
const existingDiscussion = discussions.find((v) =>
321+
v.body.startsWith(notionPageSignature)
322+
);
323+
const existingIssue = issues.find((v) =>
324+
v.body.startsWith(notionPageSignature)
323325
);
324326

325-
if (!category) {
326-
throw new Error(
327-
`Category ${pageAttributes.categoryName} not found in repo ${pageAttributes.repo}`
328-
);
329-
}
327+
if (pageAttributes === null) {
328+
if (existingDiscussion) {
329+
outputDiscussions.delete.push({
330+
repoId: existingDiscussion.repository.id,
331+
discussion: existingDiscussion,
332+
});
333+
}
330334

331-
if (existingDiscussion) {
332-
outputDiscussions.update.push({
333-
page,
334-
body: `${notionPageSignature}\n${HEADER_NOTE}\n${composeLink(
335-
page
336-
)}\n${n2m.toMarkdownString(mdBlocks.slice(1))}`,
337-
title: pageTitle,
338-
repoId: existingDiscussion.repository.id,
339-
discussion: existingDiscussion,
340-
categoryId: category.id,
341-
});
342-
} else {
343-
outputDiscussions.create.push({
344-
page,
345-
body: `${notionPageSignature}\n${HEADER_NOTE}\n${composeLink(
346-
page
347-
)}\n${n2m.toMarkdownString(mdBlocks.slice(1))}`,
348-
title: pageTitle,
349-
categoryId: category.id,
350-
repoId: repoInfo.id,
351-
});
352-
}
353-
} else if (pageAttributes.type === "issue") {
354-
if (existingIssue) {
355-
outputIssues.update.push({
356-
page,
357-
body: `${notionPageSignature}\n${HEADER_NOTE}\n${composeLink(
358-
page
359-
)}\n${n2m.toMarkdownString(mdBlocks.slice(1))}`,
360-
title: pageTitle,
361-
repoId: existingIssue.repository.id,
362-
issue: existingIssue,
363-
});
364-
} else {
335+
if (existingIssue) {
336+
outputIssues.delete.push({
337+
repoId: existingIssue.repository.id,
338+
issue: existingIssue,
339+
});
340+
}
341+
} else if (pageAttributes.type === "discussion") {
365342
const [owner, name] = pageAttributes.repo.split("/");
366343
const repoInfo = await getRepoInfo(octokit, name, owner);
344+
const category = (repoInfo.discussionCategories || []).find(
345+
(d: any) =>
346+
d.name.toLowerCase() === pageAttributes.categoryName.toLowerCase()
347+
);
348+
349+
if (!category) {
350+
throw new Error(
351+
`Category ${pageAttributes.categoryName} not found in repo ${pageAttributes.repo}`
352+
);
353+
}
367354

368-
outputIssues.create.push({
369-
page,
370-
body: `${notionPageSignature}\n${HEADER_NOTE}\n${composeLink(
371-
page
372-
)}\n${n2m.toMarkdownString(mdBlocks.slice(1))}`,
373-
title: pageTitle,
374-
repoId: repoInfo.id,
375-
});
355+
if (existingDiscussion) {
356+
outputDiscussions.update.push({
357+
page,
358+
body: `${notionPageSignature}\n${HEADER_NOTE}\n${composeLink(
359+
page
360+
)}\n${n2m.toMarkdownString(mdBlocks.slice(1))}`,
361+
title: pageTitle,
362+
repoId: existingDiscussion.repository.id,
363+
discussion: existingDiscussion,
364+
categoryId: category.id,
365+
});
366+
} else {
367+
outputDiscussions.create.push({
368+
page,
369+
body: `${notionPageSignature}\n${HEADER_NOTE}\n${composeLink(
370+
page
371+
)}\n${n2m.toMarkdownString(mdBlocks.slice(1))}`,
372+
title: pageTitle,
373+
categoryId: category.id,
374+
repoId: repoInfo.id,
375+
});
376+
}
377+
} else if (pageAttributes.type === "issue") {
378+
if (existingIssue) {
379+
outputIssues.update.push({
380+
page,
381+
body: `${notionPageSignature}\n${HEADER_NOTE}\n${composeLink(
382+
page
383+
)}\n${n2m.toMarkdownString(mdBlocks.slice(1))}`,
384+
title: pageTitle,
385+
repoId: existingIssue.repository.id,
386+
issue: existingIssue,
387+
});
388+
} else {
389+
const [owner, name] = pageAttributes.repo.split("/");
390+
const repoInfo = await getRepoInfo(octokit, name, owner);
391+
392+
outputIssues.create.push({
393+
page,
394+
body: `${notionPageSignature}\n${HEADER_NOTE}\n${composeLink(
395+
page
396+
)}\n${n2m.toMarkdownString(mdBlocks.slice(1))}`,
397+
title: pageTitle,
398+
repoId: repoInfo.id,
399+
});
400+
}
376401
}
377-
}
378-
}
402+
})
403+
);
379404

380405
return {
381406
issues: outputIssues,
@@ -647,7 +672,7 @@ export default {
647672
): Promise<Response> {
648673
try {
649674
const plan = await run(env);
650-
675+
console.info(`Sync result:`, plan);
651676
return new Response(JSON.stringify({ plan }), { status: 200 });
652677
} catch (e) {
653678
console.error(e);
@@ -666,6 +691,6 @@ export default {
666691
env: Env,
667692
ctx: ExecutionContext
668693
): Promise<void> {
669-
await run(env);
694+
console.info(`Scheduled sync result: `, await run(env));
670695
},
671696
};

0 commit comments

Comments
 (0)