Skip to content

Commit c1a50ad

Browse files
committed
fix: relative links not embedded correctly
1 parent 6efb66e commit c1a50ad

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

mkdocs_embed_file_plugins/plugin.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,17 @@ def mini_ez_links(urlo, base, end, url_whitespace, url_case):
5656
base, url_blog = base
5757
url_blog_path = [x for x in url_blog.split('/') if len(x) > 0]
5858
url_blog_path = url_blog_path[len(url_blog_path) - 1]
59+
file_name = urlo[2].replace('index', '')
60+
file_name = file_name.replace('../', '')
61+
file_name = file_name.replace('./', '')
62+
5963
all_docs = [
6064
re.sub(rf"(.*)({url_blog_path})?/docs/*", '', x.replace('\\', '/')).replace(
6165
'.md', ''
6266
)
6367
for x in iglob(str(base) + os.sep + '**', recursive=True)
6468
if os.path.isfile(x)
6569
]
66-
file_name = urlo[2].replace('index', '')
6770
file_found = [
6871
'/' + x for x in all_docs if os.path.basename(x) == file_name or x == file_name
6972
]
@@ -167,6 +170,11 @@ def search_doc(md_link_path, all_docs):
167170
return file[0]
168171
return 0
169172

173+
def create_link(link):
174+
if link.endswith('/'):
175+
return link[:-1] + '.md'
176+
else:
177+
return link + '.md'
170178

171179
class EmbedFile(BasePlugin):
172180
config_scheme = (('param', config_options.Type(str, default='')),)
@@ -192,35 +200,26 @@ def on_post_page(self, output_content, page, config):
192200
):
193201
if len(link['src']) > 0:
194202

195-
if link['src'][0] == '.':
196-
md_src_path = link['src'][3:-1] + '.md'
197-
md_src_path = md_src_path.replace('.m.md', '.md')
198-
md_link_path = os.path.join(
199-
os.path.dirname(page.file.abs_src_path), md_src_path
200-
)
201-
md_link_path = Path(unquote(md_link_path)).resolve()
203+
if link['src'][0] == '.': #relative links
204+
md_src=create_link(unquote(link['src']))
205+
md_link_path=Path(
206+
os.path.dirname(page.file.abs_src_path), md_src).resolve()
202207

203208
elif link['src'][0] == '/':
204-
if link['src'].endswith('/'):
205-
md_src_path = link['src'][:-1] + '.md'
206-
else:
207-
md_src_path = link['src'] + '.md'
209+
md_src_path=create_link(unquote(link['src']))
208210
md_link_path = os.path.join(
209211
config['docs_dir'], md_src_path)
210212
md_link_path = Path(unquote(md_link_path)).resolve()
211213

212214
elif link['src'][0] != '#':
213-
if link['src'].endswith('/'):
214-
md_src_path = link['src'][:-1] + '.md'
215-
else:
216-
md_src_path = link['src'] + '.md'
215+
md_src_path=create_link(unquote(link['src']))
217216

218217
md_link_path = os.path.join(
219218
os.path.dirname(page.file.abs_src_path), md_src_path
220219
)
221220
md_link_path = Path(unquote(md_link_path)).resolve()
222221
else:
223-
md_src_path = link['src'] + '.md'
222+
md_src_path = create_link(unquote(link['src']))
224223
md_link_path = os.path.join(
225224
os.path.dirname(page.file.abs_src_path), md_src_path
226225
)

0 commit comments

Comments
 (0)