generated from actions/hello-world-javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
317 lines (263 loc) · 9.81 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
const fs = require('fs')
const crypto = require('crypto')
const { globSync } = require('glob')
const { markdownToBlocks } = require('@tryfabric/martian')
const { Client } = require('@notionhq/client')
const { title } = require('process')
const REQUIRED_ENV_VARS = ['FOLDER', 'NOTION_TOKEN', 'NOTION_ROOT_PAGE_ID', 'RELATIVE_URLS_ROOT']
const DEBUG = !!process.env.DEBUG
const IGNORE_CREATE_ERRORS = process.env.IGNORE_CREATE_ERRORS !== undefined ? !!process.env.IGNORE_CREATE_ERRORS : true
const DOCUMENT_HASH_TAG_REGEXP = /^md5:/
const notion = new Client({
auth: process.env.NOTION_TOKEN
})
// TODO: delete page instead of many blocks for updates? (optionable)
// TODO: append github link to footer for each doc?
// FIX: fixing relative url -> mailto:[email protected]
// TODO: NEXT: add folders list support ?
// TODO: how to import images?
const validateRequiredEnvVariables = () => {
REQUIRED_ENV_VARS.forEach((varName) => {
if (!process.env[varName]) {
console.log(`${varName} not provided`)
process.exit(1)
}
})
}
const getNotionRootPageId = () => {
const notionUrlMatch = process.env.NOTION_ROOT_PAGE_ID.match(/[^-]*$/)
if (notionUrlMatch == null) {
throw new SyntaxError('Provided page was not in a valid format, url must end with "-<page-id>"')
}
return notionUrlMatch[0]
}
const getFilesToProcess = () => {
let files = globSync(`${process.env.FOLDER}/**/*.md`, { ignore: 'node_modules/**' })
// pop readme to top
const readmePath = `${process.env.FOLDER}/README.md`
if (files.includes(readmePath)) {
files = files.filter((path) => path !== readmePath)
files = [readmePath, ...files]
}
return files
}
const deleteBlocksSequentially = function (idToDelete, allIdsToDelete) {
if (!idToDelete) return new Promise((resolve, _reject) => resolve())
const deleteOne = (id, ids, resolve, reject) => {
notion.blocks.delete({
block_id: id
}).then(function () {
console.log('Block deleted:', id)
if (id !== ids[ids.length - 1]) {
const nextDeleteId = ids[ids.indexOf(id) + 1]
deleteOne(nextDeleteId, ids, resolve, reject)
} else {
console.log('Block deletion complete')
resolve()
}
}).catch((error) => {
reject(error)
})
}
const resultPromise = new Promise((resolve, reject) => {
deleteOne(idToDelete, allIdsToDelete, resolve, reject)
})
return resultPromise
}
const deepReplaceValue = (target, lookupKey, newValueFn) => {
if (Array.isArray(target)) {
target.forEach((obj) => {
deepReplaceValue(obj, lookupKey, newValueFn)
})
} else if (typeof target === 'object') {
for (const key in target) {
if (typeof target[key] === 'object') {
deepReplaceValue(target[key], lookupKey, newValueFn)
} else {
if (key === lookupKey) {
target[key] = newValueFn(target[key])
}
}
}
}
return target
}
const titleFromFilePath = (filePath) => {
let title = filePath.split(process.env.FOLDER).splice(-1)[0]
title = title.replace(/^\//, '')
return title.replace('.md', '')
}
const titleToFilePath = (filePath) => {
return `${process.env.FOLDER}/${filePath}.md`
}
const fileToNotionBlocks = (filePath) => {
const mdContent = fs.readFileSync(filePath, 'utf8')
let newBlocks = markdownToBlocks(mdContent)
const fileHash = crypto.createHash('md5').update(mdContent).digest('hex')
const hashBlock = markdownToBlocks(`md5:${fileHash}`)
newBlocks.push(hashBlock[0])
// fix relative urls
newBlocks = deepReplaceValue(JSON.parse(JSON.stringify(newBlocks)), 'url', (url) => {
if (url.match(/^http/)) {
return url
} else if (url.match(/^#/)) {
DEBUG && console.log('fixing #-url -> ', url)
// FIXME: don't know what to do with this problem
// url likes this:
// #1.-сделки-и-договоры-сделки-post
return process.env.RELATIVE_URLS_ROOT
// } else if (url.match(/\.png$|\.jpg$|\.jpeg$|\.webp/)) {
// DEBUG && console.log('fixing img url -> ', url)
// return `${process.env.RELATIVE_URLS_ROOT}/blob/master/${url}`
} else {
DEBUG && console.log('fixing relative url -> ', url)
return `${process.env.RELATIVE_URLS_ROOT}/tree/master/${url}`
}
})
return newBlocks
}
const createPagesSequentially = (fileToCreate, allFilesToCreate, rootPage) => {
if (!fileToCreate) return new Promise((resolve, _reject) => resolve())
const createOne = (file, files, resolve, reject) => {
const newBlocks = fileToNotionBlocks(file)
const title = titleFromFilePath(file)
notion.pages.create({
parent: {
type: 'page_id',
page_id: rootPage.id
},
properties: {
title: {
title: [{ text: { content: title } }], type: 'title'
}
}
}).then((pageResponse) => {
console.log('Page created', title)
notion.blocks.children.append({ block_id: pageResponse.id, children: newBlocks }).then(() => {
// process next page
if (file !== files[files.length - 1]) {
createOne(files[files.indexOf(file) + 1], files, resolve, reject)
} else {
resolve()
}
}).catch((error) => {
if (IGNORE_CREATE_ERRORS) {
console.log('Blocks appending failed, but error ignored ', error)
if (file !== files[files.length - 1]) {
createOne(files[files.indexOf(file) + 1], files, resolve, reject)
} else {
resolve()
}
} else {
reject(error)
}
})
}).catch((error) => {
reject(error)
})
}
const resultPromise = new Promise((resolve, reject) => {
createOne(fileToCreate, allFilesToCreate, resolve, reject)
})
return resultPromise
}
const updatePagesSequentially = (fileToUpdate, filesToUpdate, blocksWithChildPages) => {
if (!fileToUpdate) return new Promise((resolve, _reject) => resolve())
const updateOne = (file, files, resolve, reject) => {
const finalize = () => {
if (files.slice(-1)[0] === file) {
resolve()
} else {
updateOne(files[files.indexOf(file) + 1], files, resolve, reject)
}
}
const blockWithChildPage = blocksWithChildPages.filter((r) => {
return r.child_page?.title === titleFromFilePath(file)
})[0]
if (!blockWithChildPage) {
console.log('block not found on readme, skip ... (this is error)', file)
return finalize()
} // or error?
notion.blocks.children.list({ block_id: blockWithChildPage.id }).then((pageBlocksResponse) => {
const updatedNotionBlocks = fileToNotionBlocks(file)
// change detection
let isChanged = false
const fileContent = fs.readFileSync(file, 'utf8')
const fileMD5 = crypto.createHash('md5').update(fileContent).digest('hex')
const md5Block = pageBlocksResponse.results.slice(-1)[0]
const md5RichText = md5Block?.paragraph?.rich_text[0]
if (md5RichText?.text?.content?.match(DOCUMENT_HASH_TAG_REGEXP)) {
const md5 = md5RichText.text.content.split(DOCUMENT_HASH_TAG_REGEXP).slice(-1)[0]
if (md5 !== fileMD5) isChanged = true
} else {
isChanged = true
}
DEBUG && console.log('is changed ->', file, isChanged)
const idsToRemove = pageBlocksResponse.results.map((e) => e.id)
if (isChanged) {
deleteBlocksSequentially(idsToRemove[0], idsToRemove).then(() => {
// update page with new content
notion.blocks.children.append({
block_id: blockWithChildPage.id,
children: updatedNotionBlocks
}).then(() => {
finalize()
}).catch((error) => {
if (IGNORE_CREATE_ERRORS) {
console.log('Blocks appending failed, error ignored', error)
console.log('Try append error on page')
const errorBlocks = markdownToBlocks(`Blocks appending failed with error: ${error}`)
notion.blocks.children.append({
block_id: blockWithChildPage.id,
children: errorBlocks
}).then(() => {
finalize()
})
finalize()
} else {
reject(error)
}
})
})
} else {
finalize()
}
})
}
const resultPromise = new Promise((resolve, reject) => {
updateOne(fileToUpdate, filesToUpdate, resolve, reject)
})
return resultPromise
}
const run = function () {
DEBUG && console.log('Running inside folder: ', process.env.FOLDER)
notion.pages.retrieve({ page_id: getNotionRootPageId() }).then((rootPage) => {
// DEBUG && console.log('Files to sync ->', filesToCreate)
// const toCreate = filesToCreate.map((e) => titleFromFilePath(e))
notion.blocks.children.list({ block_id: getNotionRootPageId() }).then((blocksResponse) => {
const current = blocksResponse.results.map((e) => titleToFilePath(e.child_page.title))
// console.log('created titles ->', current)
const toCreate = getFilesToProcess()
const updateList = toCreate.filter((e) => current.includes(e))
const createList = toCreate.filter((e) => !current.includes(e))
const deleteList = current.filter((e) => !toCreate.includes(e))
console.log('createList ->', createList)
console.log('updateList ->', updateList)
console.log('deleteList ->', deleteList)
updatePagesSequentially(updateList[0], updateList, blocksResponse.results).then(() => {
console.log('--- all pages updated')
createPagesSequentially(createList[0], createList, rootPage).then(() => {
console.log('--- new pages created')
deleteBlocksSequentially(deleteList[0], deleteList).then(() => {
console.log('--- sync complete')
})
})
})
})
}).catch((error) => {
console.log('Root page not found', error)
process.exit(1)
})
}
validateRequiredEnvVariables()
run()