-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathimport-notion.js
136 lines (119 loc) · 3.93 KB
/
import-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
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
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable no-console */
const path = require("path")
const axios = require('axios')
const fs = require('fs')
const crc32 = require('js-crc').crc32
const DEFAULT_NOTION_ID = '6b3a4ffc3bb146a7873e654f1209d979'
const POTION_API = 'https://potion.banklessacademy.com'
const args = process.argv
const NOTION_ID =
args[2] && args[2].length === 32
? args[2]
: DEFAULT_NOTION_ID
console.log('NOTION_ID', NOTION_ID)
const KEY_MATCHING = {
'Name': 'name',
'Page': 'page',
'Category': 'category',
'Description': 'description',
'Image': 'image',
'Link': 'link',
}
const slugify = (text) =>
text
.toLowerCase()
.replace(/<[^>]*>?/gm, '') // remove tags
.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-') // collapse dashes
const download_image = (url, image_path) =>
axios({
url,
responseType: 'stream',
}).then(function (response) {
response.data.pipe(fs.createWriteStream(image_path))
})
const get_img = (imageLink, slug, image_name) => {
const [file_name] = imageLink.split('?')
const file_extension = file_name
.match(/\.(png|svg|jpg|jpeg|webp|webm|mp4|gif)/)[1]
.replace('jpeg', 'jpg')
// console.log(file_extension)
// create "unique" hash based on Notion imageLink (different when re-uploaded)
const hash = crc32(file_name)
const image_dir = `/images/${slug}/`
const image_path = `${image_dir}${slugify(
image_name
)}-${hash}.${file_extension}`
// console.log('image_path', image_path)
const local_image_path = `public${image_path}`
if (!fs.existsSync(local_image_path)) {
// remove eventual previous image
const dirname = path.dirname(local_image_path) + '/'
fs.readdirSync(dirname)
.filter(f => f.startsWith(slugify(image_name)))
.map(f => {
console.log('delete previous image:', dirname + f)
fs.unlinkSync(dirname + f)
})
// download new image
download_image(imageLink, local_image_path)
console.log('downloading image: ', local_image_path)
}
return image_path
}
const pages = []
axios
.get(`${POTION_API}/table?id=${NOTION_ID}`)
.then((response) => {
// console.log(response.data)
response.data.map((notion) => {
let page = {}
// console.log(notion)
page = Object.keys(KEY_MATCHING).reduce(
(obj, k) =>
Object.assign(obj, {
[KEY_MATCHING[k]]: notion.fields[k],
}),
{}
)
if (page.page.length > 0 && page.image !== undefined) {
page.page = page.page[0]
if (page.link === null) {
delete page.link
}
page.image = get_img(page.image, page.page, slugify(page.name))
pages.push(page)
} else {
console.log('pb missing field', page)
}
})
// console.log(pages)
const departments = pages.filter(page => page.page === 'department')
console.log('departments', departments)
const guilds = pages.filter(page => page.page === 'guild')
console.log('guilds', guilds)
const projects = pages.filter(page => page.page === 'project')
console.log('projects', projects)
const workWithUs = pages.filter(page => page.page === 'work-with-us')
console.log('work-with-us', workWithUs)
const fileContent = `import { ProjectType } from 'entities/project'
export const DEPARTMENTS: ProjectType[] = ${JSON.stringify(departments, null, 2)}
export const GUILDS: ProjectType[] = ${JSON.stringify(guilds, null, 2)}
export const PROJECTS: ProjectType[] = ${JSON.stringify(projects, null, 2)}
export const WORK_WITH_US: ProjectType[] = ${JSON.stringify(workWithUs, null, 2)}
`
const filePath = `constants/data.ts`
fs.writeFile(
filePath,
fileContent,
(error) => {
if (error) throw error
}
)
console.log(`export done -> check ${filePath}`)
})
.catch((error) => {
console.log(error)
})