1
1
import re
2
2
3
+
3
4
def non_breaking_space (markdown ):
4
- return re .sub ('[\u00A0 \u1680 \u180E \u2000 -\u200B \u202F \u205F \u3000 \uFEFF ]' , ' ' , markdown )
5
+ return re .sub (
6
+ "[\u00A0 \u1680 \u180E \u2000 -\u200B \u202F \u205F \u3000 \uFEFF ]" , " " , markdown
7
+ )
8
+
5
9
6
10
def update_heading (markdown ):
7
- file_content = markdown .split (' \n ' )
8
- markdown = ''
11
+ file_content = markdown .split (" \n " )
12
+ markdown = ""
9
13
code = False
10
14
for line in file_content :
11
15
if not code :
12
- if line .startswith (' ```' ):
16
+ if line .startswith (" ```" ):
13
17
code = True
14
- elif line .startswith ('#' ) and line .count ('#' ) <= 5 :
15
- heading_number = line .count ('#' ) + 1
16
- line = '#' * heading_number + ' ' + line .replace ('#' , '' )
17
- elif line .startswith (' ```' ) and code :
18
+ elif line .startswith ("#" ) and line .count ("#" ) <= 5 :
19
+ heading_number = line .count ("#" ) + 1
20
+ line = "#" * heading_number + " " + line .replace ("#" , "" )
21
+ elif line .startswith (" ```" ) and code :
18
22
code = True
19
- markdown += line + ' \n '
23
+ markdown += line + " \n "
20
24
return markdown
21
25
26
+
22
27
def strip_comments (markdown ):
23
- file_content = markdown .split ('\n ' )
24
- markdown = ''
25
- for line in file_content :
26
- if not re .search (r'%%(.*)%%' , line ) or not line .startswith ('%%' ) or not line .endswith ('%%' ):
27
- markdown += line + '\n '
28
- markdown = re .sub (r'%%(.*)%%' , '' , markdown , flags = re .DOTALL )
29
- return markdown
28
+ return re .sub (r"%%.*?%%" , "" , markdown , flags = re .DOTALL )
29
+
30
30
31
31
def fix_tags (metadata ):
32
- tags = metadata .get (' tags' , None ) or metadata .get (' tag' , None )
32
+ tags = metadata .get (" tags" , None ) or metadata .get (" tag" , None )
33
33
if tags and isinstance (tags , str ):
34
- tags = tags .split ('/' )
34
+ tags = tags .split ("/" )
35
35
tags = [tag .strip () for tag in tags ]
36
- metadata [' tags' ] = tags
36
+ metadata [" tags" ] = tags
37
37
return metadata
38
38
39
+
39
40
def on_page_markdown (markdown , files , page , config , ** kwargs ):
40
- config_hooks = config ['extra' ].get ('hooks' , {'strip_comments' : True , 'fix_heading' : False })
41
- if config_hooks ['strip_comments' ]:
41
+ config_hooks = config .get (
42
+ "extra" , {"hooks" : {"strip_comments" : True , "fix_heading" : False }}
43
+ ).get ("hooks" , {"strip_comments" : True , "fix_heading" : False })
44
+ if config_hooks .get ("strip_comments" , True ):
42
45
markdown = strip_comments (markdown )
43
- if config_hooks [ ' fix_heading' ] :
46
+ if config_hooks . get ( " fix_heading" , False ) :
44
47
markdown = update_heading (markdown )
45
48
metadata = fix_tags (page .meta )
46
49
page .meta = metadata
47
50
markdown = non_breaking_space (markdown )
48
- return markdown
51
+ return markdown
0 commit comments