-
Notifications
You must be signed in to change notification settings - Fork 0
/
sitemap.js
189 lines (139 loc) · 5.52 KB
/
sitemap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const notion = require('./notion');
// Whether to log when unsupported blocks are encountered
const logUnsupported = false;
// The maximum number of database pages to continue recursing through
const maximumDatabasePagesToRetrieve = 5;
// Whether to ignore all database pages if more than the above count are encountered
const ignoreAllPagesIfMoreThanMaximum = true;
// The maximum number of levels to recurse through, or null for no maximum
const maximumNestLevel = null;
function resolvePageTitle(page) {
return page?.child_page?.title ?? page?.child_database?.title ?? page.id;
}
async function findChildPages(pageId, type) {
let pageBlock;
if (type === 'child_page') pageBlock = await notion.retrieveBlock(pageId);
else if (type === 'child_database') pageBlock = await notion.queryDatabase(pageId);
if (!pageBlock) return;
const childPages = [];
if (pageBlock?.has_children ?? pageBlock?.results?.length) {
if (type === 'child_page') {
const blockChildren = await notion.retrieveBlockChildren({ block_id: pageId, page_size: 100 });
const unsupportedBlocks = blockChildren.results.filter(childBlock => childBlock.type === 'unsupported');
// Log if an unsupported block has been found
if (unsupportedBlocks.length && logUnsupported) {
console.log(`Unsupported block(s) in '${pageBlock.child_page.title}'`);
console.log(unsupportedBlocks);
}
// Filter for <BlockObject>[] of child pages, databases and unsupported blocks
childPages.push(...blockChildren.results
.filter(childBlock => ['child_page', 'child_database', 'unsupported'].includes(childBlock.type)),
);
}
else if (type === 'child_database') {
const databasePages = pageBlock.results
.map(childPage => {
childPage.type = 'child_page';
return childPage;
});
if ((ignoreAllPagesIfMoreThanMaximum && databasePages.length <= maximumDatabasePagesToRetrieve) || !ignoreAllPagesIfMoreThanMaximum) {
childPages.push(...databasePages.slice(0, maximumDatabasePagesToRetrieve - 1));
}
}
}
return {
parentTitle: resolvePageTitle(pageBlock),
parentId: pageBlock.id,
childPages,
};
}
function recordChildPages(pageChildren) {
return pageChildren.childPages
.map(page => ({
type: page.type,
pageId: page.id,
pageOrder: page.pageOrder,
}));
}
function resolvePagePlainText(page) {
try {
const _pageTitle = page?.properties?.title?.title || Object.values(page?.properties || {}).find(({ id }) => id === 'title')?.title || [];
const databaseTitle = page?.title || [];
const pageTitle = (_pageTitle?.length) ? _pageTitle : [];
// Last line of defence against {}
const nameArray = [...pageTitle, ...databaseTitle];
return (nameArray.length)
? nameArray.map(({ plain_text }) => plain_text).join('')
: null;
}
catch (error) {
console.error({ error, page });
}
}
function formatPageURL(page) {
const pageIcon = page?.icon?.emoji;
const pageTitle = resolvePagePlainText(page) ?? page.id;
return (page.url)
? `[${(pageIcon) ? `${pageIcon} ` : ''}${pageTitle}](${page.url})`
: pageTitle;
}
const tabs = count => '\t'.repeat(count);
function generateOutput(pageTree) {
return pageTree.reduce((output, page) => {
const nestLevel = page.pageOrder.length - 1;
return output + `${tabs(nestLevel)}- ${formatPageURL(page)}\n`;
}, '');
}
function sortPageOrder(pageOne, pageTwo) {
const shallowestNest = Math.min(pageOne.pageOrder.length, pageTwo.pageOrder.length);
for (let i = 0; i < shallowestNest; i++) {
if (pageOne.pageOrder[i] === pageTwo.pageOrder[i]) {
if (i === shallowestNest - 1) return pageOne.pageOrder.length - pageTwo.pageOrder.length;
else continue;
}
return pageOne.pageOrder[i] - pageTwo.pageOrder[i];
}
}
(async () => {
const foundPages = new Set();
const scriptId = process.env.SCRIPT_ID;
if (!scriptId) return console.error('Invalid scriptId!');
let pages = [{ type: 'child_page', pageId: scriptId, pageOrder: [1] }];
console.log('\n\n\nStarting execution...');
let oldNestLevel = pages[0].pageOrder.length;
while (pages.length) {
const { type, pageId, pageOrder } = pages.shift();
if (maximumNestLevel && pageOrder.length > maximumNestLevel) break;
if (pageOrder.length > oldNestLevel) console.log(`Now at nest level ${pageOrder.length} (${pages.length} pages)...`);
if (type === 'child_page' || type === 'child_database') {
const pageChildren = await findChildPages(pageId, type);
// Record found child pages in Set
pageChildren.childPages.forEach((child, index) => {
child.pageOrder = [...pageOrder, index];
// Store full child page object as JSON in the foundPages Set
foundPages.add(JSON.stringify(child));
});
// Add found child pages to pages array to be searched
pages = [...pages, ...recordChildPages(pageChildren)];
}
oldNestLevel = pageOrder.length;
}
console.log('Generating page tree...');
const pageTree = await Promise.all([...foundPages.values()]
.map(async page => {
page = JSON.parse(page);
let retrievedObject = page;
if (page.type === 'child_page') retrievedObject = await notion.retrievePage(page.id);
else if (page.type === 'child_database') retrievedObject = await notion.retrieveDatabase(page.id);
// Properties from while loop to transfer over
retrievedObject.pageOrder = page.pageOrder;
return retrievedObject;
}),
);
const scriptPage = await notion.retrievePage(scriptId);
scriptPage.pageOrder = [1];
console.log('Generating sitemap...');
const output = generateOutput([scriptPage, ...pageTree.sort(sortPageOrder)]);
console.log('Generated sitemap!\n\n\n');
console.log(output);
})();