@@ -81,9 +81,11 @@ def _extract_text_and_format_from_ast(item):
81
81
if item ["type" ] == "html_inline" :
82
82
if item .get ("literal" , "" ) == "<s>" :
83
83
return "" , ("s" ,)
84
- if item .get ("literal" , "" ).startswith ('<latex' ):
85
- elem = minidom .parseString (item .get ("literal" , "" ) + '</latex>' ).documentElement
86
- equation = elem .attributes ['equation' ].value
84
+ if item .get ("literal" , "" ).startswith ("<latex" ):
85
+ elem = minidom .parseString (
86
+ item .get ("literal" , "" ) + "</latex>"
87
+ ).documentElement
88
+ equation = elem .attributes ["equation" ].value
87
89
return "" , ("e" , equation )
88
90
89
91
if item ["type" ] == "emph" :
@@ -126,8 +128,15 @@ def markdown_to_notion(markdown):
126
128
127
129
# commonmark doesn't support latex blocks, so we need to handle it ourselves
128
130
def handle_latex (match ):
129
- return f'<latex equation="{ html .escape (match .group (0 )[2 :- 2 ])} ">\u204d </latex>'
130
- markdown = re .sub (r'(?<!\\\\|\$\$)(?:\\\\)*((\$\$)+)(?!(\$\$))(.+?)(?<!(\$\$))\1(?!(\$\$))' , handle_latex , markdown )
131
+ return '<latex equation="{}">\u204d </latex>' .format (
132
+ html .escape (match .group (0 )[2 :- 2 ])
133
+ )
134
+
135
+ markdown = re .sub (
136
+ r"(?<!\\\\|\$\$)(?:\\\\)*((\$\$)+)(?!(\$\$))(.+?)(?<!(\$\$))\1(?!(\$\$))" ,
137
+ handle_latex ,
138
+ markdown ,
139
+ )
131
140
132
141
# we don't want to touch dashes, so temporarily replace them here
133
142
markdown = markdown .replace ("-" , "⸻" )
@@ -160,7 +169,7 @@ def handle_latex(match):
160
169
literal = ""
161
170
162
171
if item ["type" ] == "html_inline" and literal == "</latex>" :
163
- for f in filter (lambda f : f [0 ] == 'e' , format ):
172
+ for f in filter (lambda f : f [0 ] == "e" , format ):
164
173
format .remove (f )
165
174
break
166
175
literal = ""
@@ -195,16 +204,18 @@ def handle_latex(match):
195
204
196
205
return cleanup_dashes (consolidated )
197
206
207
+
198
208
def cleanup_dashes (thing ):
199
- regex_pattern = re .compile (' ⸻|%E2%B8%BB' )
209
+ regex_pattern = re .compile (" ⸻|%E2%B8%BB" )
200
210
if type (thing ) is list :
201
211
for counter , value in enumerate (thing ):
202
212
thing [counter ] = cleanup_dashes (value )
203
213
elif type (thing ) is str :
204
- return regex_pattern .sub ('-' , thing )
214
+ return regex_pattern .sub ("-" , thing )
205
215
206
216
return thing
207
217
218
+
208
219
def notion_to_markdown (notion ):
209
220
210
221
markdown_chunks = []
@@ -247,7 +258,7 @@ def notion_to_markdown(notion):
247
258
# Check wheter a format modifies the content
248
259
content_changed = False
249
260
for f in sorted_format :
250
- if f [0 ] == 'e' :
261
+ if f [0 ] == "e" :
251
262
markdown += f [1 ]
252
263
content_changed = True
253
264
0 commit comments