gugebing commited on
Commit
6a4344c
1 Parent(s): 5974fc9

Upload 4 files

Browse files
Files changed (4) hide show
  1. cookiePath/cookies.json +462 -0
  2. requirements.txt +4 -0
  3. static/main.css +33 -0
  4. utils.py +110 -0
cookiePath/cookies.json ADDED
@@ -0,0 +1,462 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "domain": ".bing.com",
4
+ "expirationDate": 1711551090.085507,
5
+ "hostOnly": false,
6
+ "httpOnly": false,
7
+ "name": "_ITAB",
8
+ "path": "/",
9
+ "sameSite": "no_restriction",
10
+ "secure": true,
11
+ "session": false,
12
+ "storeId": null,
13
+ "value": "STAB=TR"
14
+ },
15
+ {
16
+ "domain": ".bing.com",
17
+ "expirationDate": 1712921027.037775,
18
+ "hostOnly": false,
19
+ "httpOnly": false,
20
+ "name": "SnrOvr",
21
+ "path": "/",
22
+ "sameSite": "no_restriction",
23
+ "secure": true,
24
+ "session": false,
25
+ "storeId": null,
26
+ "value": "X=rebateson"
27
+ },
28
+ {
29
+ "domain": ".bing.com",
30
+ "expirationDate": 1715856453.135928,
31
+ "hostOnly": false,
32
+ "httpOnly": false,
33
+ "name": "SRCHUSR",
34
+ "path": "/",
35
+ "sameSite": "no_restriction",
36
+ "secure": true,
37
+ "session": false,
38
+ "storeId": null,
39
+ "value": "DOB=20230320&T=1681296449000"
40
+ },
41
+ {
42
+ "domain": "www.bing.com",
43
+ "expirationDate": 1681299302,
44
+ "hostOnly": true,
45
+ "httpOnly": false,
46
+ "name": "ai_session",
47
+ "path": "/",
48
+ "sameSite": "no_restriction",
49
+ "secure": true,
50
+ "session": false,
51
+ "storeId": null,
52
+ "value": "Mt6d5tTxQmyayDikeQAELC|1681296653177|1681297502888"
53
+ },
54
+ {
55
+ "domain": ".bing.com",
56
+ "expirationDate": 1681319735.074255,
57
+ "hostOnly": false,
58
+ "httpOnly": true,
59
+ "name": "SUID",
60
+ "path": "/",
61
+ "sameSite": "no_restriction",
62
+ "secure": true,
63
+ "session": false,
64
+ "storeId": null,
65
+ "value": "A"
66
+ },
67
+ {
68
+ "domain": ".bing.com",
69
+ "expirationDate": 1715858933.743547,
70
+ "hostOnly": false,
71
+ "httpOnly": false,
72
+ "name": "SRCHHPGUSR",
73
+ "path": "/",
74
+ "sameSite": "no_restriction",
75
+ "secure": true,
76
+ "session": false,
77
+ "storeId": null,
78
+ "value": "CW=1262&CH=623&SCW=1247&SCH=2312&BRW=M&BRH=S&SRCHLANG=zh-Hans&PV=15.0.0&BZA=0&PRVCW=586&PRVCH=2885&DPR=1.5&UTC=480&DM=0&EXLTT=31&HV=1681298045&cdxtone=Creative&cdxtoneopts=h3imaginative,clgalileo,gencontentv3,cachewriteext,e2ecachewrite,nodlcpcwrite,nointernalsugg"
79
+ },
80
+ {
81
+ "domain": ".bing.com",
82
+ "expirationDate": 1715857850.467479,
83
+ "hostOnly": false,
84
+ "httpOnly": false,
85
+ "name": "ANON",
86
+ "path": "/",
87
+ "sameSite": "no_restriction",
88
+ "secure": true,
89
+ "session": false,
90
+ "storeId": null,
91
+ "value": "A=8951135C4A61751579AD7F36FFFFFFFF&E=1bfd&W=1"
92
+ },
93
+ {
94
+ "domain": ".bing.com",
95
+ "expirationDate": 1681385038,
96
+ "hostOnly": false,
97
+ "httpOnly": false,
98
+ "name": "_clsk",
99
+ "path": "/",
100
+ "sameSite": null,
101
+ "secure": false,
102
+ "session": false,
103
+ "storeId": null,
104
+ "value": "1smjqa5|1681298638204|3|1|p.clarity.ms/collect"
105
+ },
106
+ {
107
+ "domain": ".bing.com",
108
+ "expirationDate": 1708247278,
109
+ "hostOnly": false,
110
+ "httpOnly": false,
111
+ "name": "BCP",
112
+ "path": "/",
113
+ "sameSite": "no_restriction",
114
+ "secure": true,
115
+ "session": false,
116
+ "storeId": null,
117
+ "value": "AD=1&AL=1&SM=1"
118
+ },
119
+ {
120
+ "domain": ".bing.com",
121
+ "expirationDate": 1714894848.328181,
122
+ "hostOnly": false,
123
+ "httpOnly": false,
124
+ "name": "EDGSRVCUSR",
125
+ "path": "/",
126
+ "sameSite": "no_restriction",
127
+ "secure": true,
128
+ "session": false,
129
+ "storeId": null,
130
+ "value": "udscdxtone=Precise"
131
+ },
132
+ {
133
+ "domain": "www.bing.com",
134
+ "expirationDate": 1712833502.880629,
135
+ "hostOnly": true,
136
+ "httpOnly": false,
137
+ "name": "MicrosoftApplicationsTelemetryDeviceId",
138
+ "path": "/",
139
+ "sameSite": "no_restriction",
140
+ "secure": true,
141
+ "session": false,
142
+ "storeId": null,
143
+ "value": "4376440f-af9b-4afe-a7a6-b799142da412"
144
+ },
145
+ {
146
+ "domain": ".bing.com",
147
+ "hostOnly": false,
148
+ "httpOnly": false,
149
+ "name": "_SS",
150
+ "path": "/",
151
+ "sameSite": "no_restriction",
152
+ "secure": true,
153
+ "session": true,
154
+ "storeId": null,
155
+ "value": "SID=020A3DE8A4A9603A3CD22F19A5EA614C&R=5630&RB=5630&GB=0&RG=0&RP=5630&PC=EDGEDBB"
156
+ },
157
+ {
158
+ "domain": ".bing.com",
159
+ "hostOnly": false,
160
+ "httpOnly": false,
161
+ "name": "ipv6",
162
+ "path": "/",
163
+ "sameSite": "no_restriction",
164
+ "secure": true,
165
+ "session": true,
166
+ "storeId": null,
167
+ "value": "hit=1681300057272&t=4"
168
+ },
169
+ {
170
+ "domain": ".bing.com",
171
+ "hostOnly": false,
172
+ "httpOnly": false,
173
+ "name": "dsc",
174
+ "path": "/",
175
+ "sameSite": "no_restriction",
176
+ "secure": true,
177
+ "session": true,
178
+ "storeId": null,
179
+ "value": "order=News"
180
+ },
181
+ {
182
+ "domain": ".bing.com",
183
+ "expirationDate": 1682507450.467457,
184
+ "hostOnly": false,
185
+ "httpOnly": false,
186
+ "name": "_U",
187
+ "path": "/",
188
+ "sameSite": "no_restriction",
189
+ "secure": true,
190
+ "session": false,
191
+ "storeId": null,
192
+ "value": "1bhWU4RnZG3CJ4NQ2kLYw8stShmBfTvGqufph_YA8qPQZxYGhz9owwgb767dnkknBAMHKOV67KU4rA7AFZ-B000K6PYIKcn0senIgjLdtC34R5yHGQfWhGmV39zR-MLU13o773946CCNsngzHkI6DzEtv5eBBqr5bgObGEM1PHZvSlLrFCEWEobVaiLa3AsEGl6mKj04y_1o-ch8ABuU4Kw"
193
+ },
194
+ {
195
+ "domain": ".bing.com",
196
+ "expirationDate": 1710385726.625049,
197
+ "hostOnly": false,
198
+ "httpOnly": false,
199
+ "name": "SRCHD",
200
+ "path": "/",
201
+ "sameSite": "no_restriction",
202
+ "secure": true,
203
+ "session": false,
204
+ "storeId": null,
205
+ "value": "AF=MD1A0E"
206
+ },
207
+ {
208
+ "domain": ".bing.com",
209
+ "expirationDate": 1683687874,
210
+ "hostOnly": false,
211
+ "httpOnly": false,
212
+ "name": "ANIMIA",
213
+ "path": "/",
214
+ "sameSite": "no_restriction",
215
+ "secure": true,
216
+ "session": false,
217
+ "storeId": null,
218
+ "value": "FRE=1"
219
+ },
220
+ {
221
+ "domain": ".bing.com",
222
+ "expirationDate": 1710128122.873179,
223
+ "hostOnly": false,
224
+ "httpOnly": true,
225
+ "name": "_EDGE_V",
226
+ "path": "/",
227
+ "sameSite": null,
228
+ "secure": false,
229
+ "session": false,
230
+ "storeId": null,
231
+ "value": "1"
232
+ },
233
+ {
234
+ "domain": ".bing.com",
235
+ "expirationDate": 1712921027.037736,
236
+ "hostOnly": false,
237
+ "httpOnly": false,
238
+ "name": "_RwBf",
239
+ "path": "/",
240
+ "sameSite": "no_restriction",
241
+ "secure": true,
242
+ "session": false,
243
+ "storeId": null,
244
+ "value": "r=1&mta=0&rc=5630&rb=5630&gb=0&rg=0&pc=5630&mtu=0&rbb=0.0&g=0&cid=&clo=0&v=10&l=2023-04-12T07:00:00.0000000Z&lft=0001-01-01T00:00:00.0000000&aof=0&o=16&p=Xpay&c=ML1UAM&t=4817&s=2023-01-14T08:25:40.3240734+00:00&ts=2023-04-12T11:23:45.3716052+00:00&rwred=0&wls=2&lka=0&lkt=0&TH=&e=5L9X8XiVkXD5DGObVNxddSH6-LnXbK_qyZfq2jwXqQSIEqPH038wOm6saTvRc02YYg94PY5L-pdQbi891tBxqg&A=&mte=0"
245
+ },
246
+ {
247
+ "domain": ".bing.com",
248
+ "expirationDate": 1715857509.765812,
249
+ "hostOnly": false,
250
+ "httpOnly": false,
251
+ "name": "_UR",
252
+ "path": "/",
253
+ "sameSite": "no_restriction",
254
+ "secure": true,
255
+ "session": false,
256
+ "storeId": null,
257
+ "value": "QS=0&TQS=0"
258
+ },
259
+ {
260
+ "domain": ".bing.com",
261
+ "hostOnly": false,
262
+ "httpOnly": true,
263
+ "name": "_EDGE_S",
264
+ "path": "/",
265
+ "sameSite": null,
266
+ "secure": false,
267
+ "session": true,
268
+ "storeId": null,
269
+ "value": "SID=020A3DE8A4A9603A3CD22F19A5EA614C&ui=zh-cn&mkt=zh-sg"
270
+ },
271
+ {
272
+ "domain": "www.bing.com",
273
+ "expirationDate": 1710072027.848176,
274
+ "hostOnly": true,
275
+ "httpOnly": false,
276
+ "name": "ai_user",
277
+ "path": "/",
278
+ "sameSite": "no_restriction",
279
+ "secure": true,
280
+ "session": false,
281
+ "storeId": null,
282
+ "value": "j+NP37bcLf3NviBmzFvqes|2023-03-11T12:00:27.847Z"
283
+ },
284
+ {
285
+ "domain": "www.bing.com",
286
+ "expirationDate": 1714994631.697242,
287
+ "hostOnly": true,
288
+ "httpOnly": true,
289
+ "name": "MUIDB",
290
+ "path": "/",
291
+ "sameSite": null,
292
+ "secure": false,
293
+ "session": false,
294
+ "storeId": null,
295
+ "value": "2493B87BBFD5614F34D5AAC2BE07606C"
296
+ },
297
+ {
298
+ "domain": ".bing.com",
299
+ "expirationDate": 1710992123.001399,
300
+ "hostOnly": false,
301
+ "httpOnly": true,
302
+ "name": "USRLOC",
303
+ "path": "/",
304
+ "sameSite": "no_restriction",
305
+ "secure": true,
306
+ "session": false,
307
+ "storeId": null,
308
+ "value": "HS=1&CLOC=LAT=22.923892200720825|LON=113.86205492100088|A=733.4464586120832|TS=230412112607|SRC=W"
309
+ },
310
+ {
311
+ "domain": ".bing.com",
312
+ "expirationDate": 1715600324.489507,
313
+ "hostOnly": false,
314
+ "httpOnly": false,
315
+ "name": "_BINGNEWS",
316
+ "path": "/",
317
+ "sameSite": "no_restriction",
318
+ "secure": true,
319
+ "session": false,
320
+ "storeId": null,
321
+ "value": "SW=1247&SH=608"
322
+ },
323
+ {
324
+ "domain": ".bing.com",
325
+ "expirationDate": 1712813484,
326
+ "hostOnly": false,
327
+ "httpOnly": false,
328
+ "name": "_clck",
329
+ "path": "/",
330
+ "sameSite": null,
331
+ "secure": false,
332
+ "session": false,
333
+ "storeId": null,
334
+ "value": "5yqu1f|1|fap|0"
335
+ },
336
+ {
337
+ "domain": ".bing.com",
338
+ "expirationDate": 1715857509.766073,
339
+ "hostOnly": false,
340
+ "httpOnly": false,
341
+ "name": "_HPVN",
342
+ "path": "/",
343
+ "sameSite": "no_restriction",
344
+ "secure": true,
345
+ "session": false,
346
+ "storeId": null,
347
+ "value": "CS=eyJQbiI6eyJDbiI6MTMsIlN0IjoyLCJRcyI6MCwiUHJvZCI6IlAifSwiU2MiOnsiQ24iOjEzLCJTdCI6MCwiUXMiOjAsIlByb2QiOiJIIn0sIlF6Ijp7IkNuIjoxMywiU3QiOjEsIlFzIjowLCJQcm9kIjoiVCJ9LCJBcCI6dHJ1ZSwiTXV0ZSI6dHJ1ZSwiTGFkIjoiMjAyMy0wNC0xMlQwMDowMDowMFoiLCJJb3RkIjowLCJHd2IiOjAsIkRmdCI6bnVsbCwiTXZzIjowLCJGbHQiOjAsIkltcCI6OTN9"
348
+ },
349
+ {
350
+ "domain": ".bing.com",
351
+ "expirationDate": 1712563394.919742,
352
+ "hostOnly": false,
353
+ "httpOnly": false,
354
+ "name": "_tarLang",
355
+ "path": "/",
356
+ "sameSite": "no_restriction",
357
+ "secure": true,
358
+ "session": false,
359
+ "storeId": null,
360
+ "value": "default=zh-Hans"
361
+ },
362
+ {
363
+ "domain": ".bing.com",
364
+ "expirationDate": 1712563394.920876,
365
+ "hostOnly": false,
366
+ "httpOnly": false,
367
+ "name": "_TTSS_IN",
368
+ "path": "/",
369
+ "sameSite": "no_restriction",
370
+ "secure": true,
371
+ "session": false,
372
+ "storeId": null,
373
+ "value": "hist=WyJlbiIsImF1dG8tZGV0ZWN0Il0="
374
+ },
375
+ {
376
+ "domain": ".bing.com",
377
+ "expirationDate": 1711036592.362162,
378
+ "hostOnly": false,
379
+ "httpOnly": false,
380
+ "name": "MMCASM",
381
+ "path": "/",
382
+ "sameSite": "no_restriction",
383
+ "secure": true,
384
+ "session": false,
385
+ "storeId": null,
386
+ "value": "ID=AAD619F527FA45E7B0E99AEAC00AEE4A"
387
+ },
388
+ {
389
+ "domain": ".bing.com",
390
+ "hostOnly": false,
391
+ "httpOnly": false,
392
+ "name": "msau",
393
+ "path": "/",
394
+ "sameSite": "no_restriction",
395
+ "secure": true,
396
+ "session": true,
397
+ "storeId": null,
398
+ "value": "id=8951135C4A61751579AD7F36FFFFFFFF&msa=1&aad=0"
399
+ },
400
+ {
401
+ "domain": ".bing.com",
402
+ "expirationDate": 1714623736.424564,
403
+ "hostOnly": false,
404
+ "httpOnly": false,
405
+ "name": "MUID",
406
+ "path": "/",
407
+ "sameSite": "no_restriction",
408
+ "secure": true,
409
+ "session": false,
410
+ "storeId": null,
411
+ "value": "2493B87BBFD5614F34D5AAC2BE07606C"
412
+ },
413
+ {
414
+ "domain": ".bing.com",
415
+ "hostOnly": false,
416
+ "httpOnly": false,
417
+ "name": "SNRHOP",
418
+ "path": "/",
419
+ "sameSite": "no_restriction",
420
+ "secure": true,
421
+ "session": true,
422
+ "storeId": null,
423
+ "value": "I=&TS="
424
+ },
425
+ {
426
+ "domain": ".bing.com",
427
+ "hostOnly": false,
428
+ "httpOnly": false,
429
+ "name": "SRCHS",
430
+ "path": "/",
431
+ "sameSite": "no_restriction",
432
+ "secure": true,
433
+ "session": true,
434
+ "storeId": null,
435
+ "value": "PC=EDGEDBB"
436
+ },
437
+ {
438
+ "domain": ".bing.com",
439
+ "expirationDate": 1710385726.625058,
440
+ "hostOnly": false,
441
+ "httpOnly": false,
442
+ "name": "SRCHUID",
443
+ "path": "/",
444
+ "sameSite": "no_restriction",
445
+ "secure": true,
446
+ "session": false,
447
+ "storeId": null,
448
+ "value": "V=2&GUID=7DEEF64AA11741BA86416317E5A9DF69&dmnchg=1"
449
+ },
450
+ {
451
+ "domain": ".bing.com",
452
+ "hostOnly": false,
453
+ "httpOnly": false,
454
+ "name": "WLS",
455
+ "path": "/",
456
+ "sameSite": "no_restriction",
457
+ "secure": true,
458
+ "session": true,
459
+ "storeId": null,
460
+ "value": "C=a825d204f6af0b0b&N=wangyifeng"
461
+ }
462
+ ]
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ EdgeGPT
3
+ Markdown
4
+ mdtex2html
static/main.css ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* 屏幕宽度大于等于500px的设备 */
2
+ @media screen and (min-width: 500px) {
3
+ #chat_window {
4
+ height: calc(80vh - 150px);
5
+ }
6
+ #chat_window .wrap {
7
+ max-height: calc(80vh - 150px - var(--line-sm)*1rem - 2*var(--block-label-margin) );
8
+ }
9
+ }
10
+ /* 屏幕宽度小于500px的设备 */
11
+ @media screen and (max-width: 499px) {
12
+ #chat_window {
13
+ height: calc(70vh - 180px);
14
+ }
15
+ #chat_window .wrap {
16
+ max-height: calc(70vh - 180px - var(--line-sm)*1rem - 2*var(--block-label-margin) );
17
+ }
18
+ }
19
+
20
+ #chat_window .user {
21
+ text-align: right
22
+ }
23
+
24
+ #user_input {
25
+ height: 100%;
26
+ display: flex;
27
+ flex-direction: column-reverse;
28
+ }
29
+
30
+ footer.svelte-1lyswbr.svelte-1lyswbr.svelte-1lyswbr {
31
+ display: none;
32
+ }
33
+
utils.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import html
2
+ import re
3
+ from pygments import highlight
4
+ from pygments.lexers import get_lexer_by_name
5
+ from pygments.formatters import HtmlFormatter
6
+ from typing import List, Tuple
7
+
8
+ from markdown import markdown
9
+ import mdtex2html
10
+
11
+
12
+ ALREADY_CONVERTED_MARK = "<!-- 已转换. -->"
13
+
14
+
15
+ def postprocess(
16
+ self, y: List[Tuple[str | None, str | None]]
17
+ ) -> List[Tuple[str | None, str | None]]:
18
+ if y is None or y == []:
19
+ return []
20
+ user, bot = y[-1]
21
+ if user:
22
+ if not detect_converted_mark(user):
23
+ user = convert_asis(user)
24
+ if bot:
25
+ if not detect_converted_mark(bot):
26
+ bot = convert_mdtext(bot)
27
+ y[-1] = (user, bot)
28
+ return y
29
+
30
+ def detect_converted_mark(userinput):
31
+ if userinput.endswith(ALREADY_CONVERTED_MARK):
32
+ return True
33
+ else:
34
+ return False
35
+
36
+ def convert_asis(userinput):
37
+ return (
38
+ f'<p style="white-space:pre-wrap;">{html.escape(userinput)}</p>'
39
+ + ALREADY_CONVERTED_MARK
40
+ )
41
+
42
+
43
+ def convert_mdtext(md_text):
44
+ code_block_pattern = re.compile(r"```(.*?)(?:```|$)", re.DOTALL)
45
+ inline_code_pattern = re.compile(r"`(.*?)`", re.DOTALL)
46
+ code_blocks = code_block_pattern.findall(md_text)
47
+ non_code_parts = code_block_pattern.split(md_text)[::2]
48
+
49
+ result = []
50
+ for non_code, code in zip(non_code_parts, code_blocks + [""]):
51
+ if non_code.strip():
52
+ non_code = normalize_markdown(non_code)
53
+ if inline_code_pattern.search(non_code):
54
+ result.append(markdown(non_code, extensions=["tables"]))
55
+ else:
56
+ result.append(mdtex2html.convert(non_code, extensions=["tables"]))
57
+ if code.strip():
58
+ # _, code = detect_language(code) # 暂时去除代码高亮功能,因为在大段代码的情况下会出现问题
59
+ # code = code.replace("\n\n", "\n") # 暂时去除代码中的空行,因为在大段代码的情况下会出现问题
60
+ code = f"\n```{code}\n\n```"
61
+ code = markdown_to_html_with_syntax_highlight(code)
62
+ result.append(code)
63
+ result = "".join(result)
64
+ result += ALREADY_CONVERTED_MARK
65
+ return result
66
+
67
+ def normalize_markdown(md_text: str) -> str:
68
+ lines = md_text.split("\n")
69
+ normalized_lines = []
70
+ inside_list = False
71
+
72
+ for i, line in enumerate(lines):
73
+ if re.match(r"^(\d+\.|-|\*|\+)\s", line.strip()):
74
+ if not inside_list and i > 0 and lines[i - 1].strip() != "":
75
+ normalized_lines.append("")
76
+ inside_list = True
77
+ normalized_lines.append(line)
78
+ elif inside_list and line.strip() == "":
79
+ if i < len(lines) - 1 and not re.match(
80
+ r"^(\d+\.|-|\*|\+)\s", lines[i + 1].strip()
81
+ ):
82
+ normalized_lines.append(line)
83
+ continue
84
+ else:
85
+ inside_list = False
86
+ normalized_lines.append(line)
87
+
88
+ return "\n".join(normalized_lines)
89
+
90
+
91
+ def markdown_to_html_with_syntax_highlight(md_str):
92
+ def replacer(match):
93
+ lang = match.group(1) or "text"
94
+ code = match.group(2)
95
+
96
+ try:
97
+ lexer = get_lexer_by_name(lang, stripall=True)
98
+ except ValueError:
99
+ lexer = get_lexer_by_name("text", stripall=True)
100
+
101
+ formatter = HtmlFormatter()
102
+ highlighted_code = highlight(code, lexer, formatter)
103
+
104
+ return f'<pre><code class="{lang}">{highlighted_code}</code></pre>'
105
+
106
+ code_block_pattern = r"```(\w+)?\n([\s\S]+?)\n```"
107
+ md_str = re.sub(code_block_pattern, replacer, md_str, flags=re.MULTILINE)
108
+
109
+ html_str = markdown(md_str)
110
+ return html_str