/
notion.js
53 lines (44 loc) · 1.26 KB
/
notion.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
const { Client } = require("@notionhq/client");
const { NotionToMarkdown } = require('notion-to-md');
module.exports = class Notion {
notion;
n2m;
constructor (token) {
this.notion = new Client({
auth: token,
});
this.n2m = new NotionToMarkdown({
notionClient: this.notion
})
}
stripURLTags(url) {
let parsedUrl = new URL(url)
parsedUrl.search = ""
// remove trailing slash
if (parsedUrl.pathname.endsWith('/')) {
parsedUrl.pathname = url.pathname.slice(0, -1);
}
return parsedUrl.toString()
}
getPageIdFromURL(url) {
url = this.stripURLTags(url)
const urlArr = url.split('-')
let pageId = urlArr[urlArr.length - 1]
return pageId
}
async getBlocks (url) {
const pageId = this.getPageIdFromURL(url)
const blocks = await this.n2m.pageToMarkdown(pageId);
return blocks
}
async getMarkdown (source){
let mdblocks = []
if (typeof source === 'string') {
const pageId = this.getPageIdFromURL(source)
mdblocks = await this.n2m.pageToMarkdown(pageId);
} else {
mdblocks = source
}
return this.n2m.toMarkdownString(mdblocks);
}
}