This repository has been archived by the owner on Jul 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload.py
62 lines (49 loc) · 2.17 KB
/
upload.py
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
from notion.client import NotionClient
from notion.block import PageBlock
from md2notion.upload import uploadBlock, convert
from md2notion.upload import addLatexExtension, NotionPyRenderer # latex support
from md2notion.NotionPyRenderer import EquationBlock
from preprocess import preprocess_list
from config import *
import os
# fix latex import error
class ModifiedNotionPyRenderer(NotionPyRenderer):
def __init__(self, *extraExtensions):
super(ModifiedNotionPyRenderer, self).__init__(*extraExtensions)
def render_block_equation(self, token):
def blockFunc(blockStr):
return {
'type': EquationBlock,
'title_plaintext': blockStr.replace('\\', '\\') # \\\\ not work correctly on my system
}
return self.renderMultipleToStringAndCombine(token.children, blockFunc)
def get_md(root_dir):
f_list = []
for r, d, f in os.walk(root_dir):
for file in f:
if file.endswith(".md"):
# print(os.path.join(r, file))
f_list.append(os.path.join(r, file))
return f_list
# default
def upload2notion(local_md, token_v2, block_url, title):
# Follow the instructions at https://github.com/jamalex/notion-py#quickstart to setup Notion.py
client = NotionClient(token_v2=token_v2)
page = client.get_block(block_url)
with open(local_md, "r", encoding="UTF-8") as mdFile:
newPage = page.children.add_new(PageBlock, title=title)
# upload(mdFile, newPage, notionPyRendererCls=addLatexExtension(
# NotionPyRenderer)) # Appends the converted contents of TestMarkdown.md to newPage
lines = mdFile.readlines()
rendered = convert(lines, addLatexExtension(ModifiedNotionPyRenderer))
for blockDesc in rendered:
uploadBlock(blockDesc, newPage, mdFile.name)
if __name__ == "__main__":
md_files = get_md(md_dir)
for md_file in md_files:
for action in preprocess_list:
md_file = action(md_file)
upload2notion(local_md=md_file,
token_v2=token,
block_url=block,
title=md_file.split("\\")[-1].split(".")[-2])