-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (63 loc) · 1.78 KB
/
index.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
const { default: NotionExporter } = require("notion-exporter");
const { Client: NotionClient } = require('@notionhq/client');
module.exports = class NotionSource {
static defaultOptions() {
return {
baseUrl: '',
notionKey: '',
notionToken: '',
databaseId: '',
perPage: 100,
typeName: 'Notion'
}
}
constructor (api, options) {
this.api = api
this.options = options
this.notionApi = new NotionClient({ auth: this.options.notionKey });
api.loadSource(async actions => {
const collection = actions.addCollection({
typeName: this.options.typeName,
});
await this.loadPagesToCollection(collection)
})
}
async loadPagesToCollection(collection) {
const response = await this.notionApi.databases.query({
database_id: this.options.databaseId,
});
let count = 1;
for (const block of response.results) {
//console.log(block)
let content = await new NotionExporter(this.options.notionToken).getMdString(block.id);
console.log(content)
content = this.removeProperties(content, block.properties);
console.log(block.properties);
console.log(content)
collection.addNode({
id: block.id,
path: `/${count++}/`,
properties: {
...block.properties
},
internal: {
mimeType: 'text/x-markdown',
content,
origin: block.url
},
})
}
}
removeProperties(content, properties) {
let lines = content.split('\n');
lines.splice(0, 2);
for (let p of Object.keys(properties)) {
const pindex = lines.findIndex((l) => l.startsWith(p));
if (pindex !== -1) {
lines.splice(pindex, 1);
}
}
lines.splice(0, 1);
return lines.join('\n');
}
}