2
2
import os
3
3
from random import choice
4
4
from argparse import ArgumentParser
5
+ from urllib .parse import urlparse
5
6
6
7
from notion .client import NotionClient
7
8
from notion .block import Block , PageBlock , CollectionViewBlock
@@ -194,35 +195,50 @@ def sync_markdown_blocks_to_block(markdown_blocks, block):
194
195
c .remove ()
195
196
196
197
197
- def sync_file_to_block (filename , block ):
198
+ def sync_file_to_block (filename , block , links : dict = {} ):
198
199
logger .info (f"Syncing { filename } to block { block .id } " )
199
200
200
201
with open (filename ) as markdown_fd :
201
202
contents = markdown_fd .read ()
202
203
203
204
post = frontmatter .loads (contents )
204
205
205
- markdown_blocks = convert (str (post ))
206
+ def resolve_link (target ):
207
+ try :
208
+ parsed = urlparse (target )
206
209
207
- sync_markdown_blocks_to_block (markdown_blocks , block )
210
+ if parsed .scheme :
211
+ return target
212
+ except :
213
+ pass
208
214
215
+ target_path = os .path .realpath (os .path .join (os .path .dirname (filename ), target ))
209
216
210
- def sync_directory_to_block (directory , root_block ):
211
- if not root_block .get (['format' , 'block_locked' ], default = False ):
212
- root_block .set (['format' , 'block_locked' ], True )
217
+ block = links .get (target_path )
218
+
219
+ if not block :
220
+ return target
221
+
222
+ return block .get_browseable_url ()
223
+
224
+ markdown_blocks = convert (str (post ), link_resolver = resolve_link )
213
225
226
+ sync_markdown_blocks_to_block (markdown_blocks , block )
227
+
228
+
229
+ def create_page_structure (directory , root_block ):
214
230
touched_pages = set ()
215
231
216
- index_path = os .path .join (directory , "index.md" )
217
- readme_path = os .path .join (directory , "README.md" )
232
+ files_to_pages = dict ()
233
+
234
+ index_path = os .path .realpath (os .path .join (directory , "index.md" ))
235
+ readme_path = os .path .realpath (os .path .join (directory , "README.md" ))
218
236
219
237
# Do the index/readme first to ensure the correct sort order.
220
238
if os .path .isfile (index_path ):
221
- touched_pages .add (root_block .id )
222
- sync_file_to_block (index_path , root_block )
239
+ files_to_pages [index_path ] = root_block
223
240
elif os .path .isfile (readme_path ):
224
- touched_pages .add (root_block .id )
225
- sync_file_to_block (readme_path , root_block )
241
+ files_to_pages [readme_path ] = root_block
226
242
227
243
for path in os .listdir (directory ):
228
244
if path .startswith ('.' ):
@@ -238,29 +254,48 @@ def sync_directory_to_block(directory, root_block):
238
254
if not block :
239
255
continue
240
256
241
- if not block .get (['format' , 'block_locked' ], default = False ):
242
- block .set (['format' , 'block_locked' ], True )
257
+ full_path = os .path .realpath (os .path .join (directory , path ))
243
258
244
259
touched_pages .add (block .id )
245
260
246
- full_path = os .path .join (directory , path )
261
+ if os .path .isdir (full_path ):
262
+ files_to_pages .update (create_page_structure (full_path , block ))
263
+ else :
264
+ files_to_pages [full_path ] = block
265
+
266
+ return files_to_pages
267
+
268
+
269
+ def sync_directory_to_block (directory , root_block ):
270
+ # Do Two Passes: First, create blocks for all files that need them
271
+ # Keep track of absolute file path -> block
272
+ logger .info ("Creating page structure.." )
273
+ files_to_pages = create_page_structure (os .path .realpath (directory ), root_block )
274
+
275
+ touched_pages = set (block .id for block in files_to_pages .values ())
276
+
277
+ # Then, for iterate through every single page block created and:
278
+ for full_path , block in files_to_pages .items ():
279
+ # Lock it
280
+ if not block .get (['format' , 'block_locked' ], default = False ):
281
+ block .set (['format' , 'block_locked' ], True )
247
282
248
283
if block .icon is None :
249
284
block .icon = random_emoji ()
250
285
251
- if os .path .isdir (full_path ):
252
- sync_directory_to_block (full_path , block )
253
- else :
254
- sync_file_to_block (full_path , block )
286
+ # Sync it.
287
+ sync_file_to_block (full_path , block , links = files_to_pages )
288
+
289
+ # Sort it.
290
+ move_pages_to_end (block )
255
291
256
- # Any children that are pages under root_block but aren't in touched_pages should be pruned
257
- # And the pages linked within them should be moved to the tail.
258
- move_pages_to_end (root_block )
259
- for child in root_block .children :
260
- move_pages_to_end (child )
261
- if child .type == 'page' and child .id not in touched_pages :
262
- child .remove ()
292
+ # Clean it.
293
+ for child in block .children :
294
+ # Any children that are pages under block but aren't in touched_pages should be pruned
295
+ if child .type == 'page' and child .id not in touched_pages :
296
+ child .remove ()
263
297
298
+ # Technologic.
264
299
265
300
def main ():
266
301
import sys
0 commit comments