@@ -42,27 +42,57 @@ def absolute_url(cls, base_url, path):
42
42
43
43
44
44
class RemoveMdExtensionTreeprocessor (Treeprocessor ):
45
+
46
+ def __init__ (self , md , base_url , dir_path ):
47
+ super ().__init__ (md )
48
+ self .base_url = urllib .parse .urlparse (base_url )
49
+ self .dir_path = dir_path
50
+
45
51
def run (self , root ):
46
- """Iterate over all <a> tags in the HTML tree and remove `.md` from their href."""
52
+ """Iterate over all <a> tags in the HTML tree and remove `.md` from
53
+ their href. If the link is in a relative directory, resolve the full
54
+ link using base URL"""
47
55
for element in root .iter ("a" ):
48
56
href = element .get ("href" )
49
57
if href and href .endswith (".md" ) and not href .startswith ("http" ):
50
- # Strip off the last 3 chars (".md")
51
- element .set ("href" , href [:- 3 ])
58
+ full_path = os .path .normpath (
59
+ os .path .join (self .base_url .path , self .dir_path , href )
60
+ )
61
+ print ("href, full_path:" , href , full_path )
62
+ target = urllib .parse .urlunparse (
63
+ (
64
+ self .base_url .scheme or "http" ,
65
+ self .base_url .netloc ,
66
+ full_path ,
67
+ self .base_url .params ,
68
+ self .base_url .query ,
69
+ self .base_url .fragment ,
70
+ )
71
+ )
72
+ if target .endswith ("/index.md" ):
73
+ target = target [:- 9 ]
74
+ else :
75
+ # Strip off the last 3 chars (".md")
76
+ target = target [:- 3 ]
77
+ element .set ("href" , target )
52
78
53
79
54
80
class RemoveMdExtension (Extension ):
81
+
82
+ def __init__ (self , base_url , dir_path ):
83
+ super ().__init__ ()
84
+ self .base_url = base_url
85
+ self .dir_path = dir_path
86
+
55
87
def extendMarkdown (self , md ):
88
+ print ("base_url, dir_path" , self .base_url , self .dir_path )
56
89
md .treeprocessors .register (
57
- RemoveMdExtensionTreeprocessor (md ), "remove_md_extension" , priority = 15
90
+ RemoveMdExtensionTreeprocessor (md , self .base_url , self .dir_path ),
91
+ "remove_md_extension" ,
92
+ priority = 15 ,
58
93
)
59
94
60
95
61
- def make_extension (** kwargs ):
62
- """Entry point for the extension."""
63
- return RemoveMdExtension (** kwargs )
64
-
65
-
66
96
def replace_extension (filename , old , new ):
67
97
parts = filename .rsplit (
68
98
f".{ old } " , 1
@@ -100,7 +130,14 @@ def render_file(input_path, output_path, env, page_context={}):
100
130
# Convert Markdown to HTML
101
131
html_content = markdown .markdown (
102
132
md_content ,
103
- extensions = ["tables" , "toc" , "attr_list" , RemoveMdExtension ()],
133
+ extensions = [
134
+ "tables" ,
135
+ "toc" ,
136
+ "attr_list" ,
137
+ RemoveMdExtension (
138
+ page_context .get ("base_url" ), page_context .get ("dir_path" )
139
+ ),
140
+ ],
104
141
)
105
142
106
143
# Wrap with a template
@@ -169,7 +206,9 @@ def render_markdown(input_dir, output_dir, env, page_context={}) -> ConversionRe
169
206
if should_render :
170
207
# Ensure the parent directory (or in the case of .md, the subfolder) exists.
171
208
os .makedirs (os .path .dirname (output_path ), exist_ok = True )
172
- render_file (input_path , output_path , env , page_context = page_context )
209
+ context = page_context .copy ()
210
+ context ["dir_path" ] = relative_path
211
+ render_file (input_path , output_path , env , page_context = context )
173
212
converted += 1
174
213
else :
175
214
os .makedirs (os .path .dirname (output_path ), exist_ok = True )
0 commit comments