Skip to content

Commit 6ed9c32

Browse files
committed
fix: in some condition, internal links were broke
1 parent b22a80b commit 6ed9c32

File tree

1 file changed

+40
-44
lines changed

1 file changed

+40
-44
lines changed

mkdocs_embed_file_plugins/plugin.py

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,27 @@ def mini_ez_links(urlo, base, end, url_whitespace, url_case):
6262
if os.path.isfile(internal_link):
6363
internal_link = str(internal_link).replace(base, '')
6464
else: # fallback to searching
65-
file_name = urlo[2].replace('index', '')
66-
file_name = file_name.replace('../', '')
67-
file_name = file_name.replace('./', '')
65+
if urlo[2].endswith('.md'):
66+
internal_link = str(search_file_in_documentation(Path(urlo[2]).resolve(), Path(md_link_path).parent))
67+
if not os.path.isfile(internal_link):
68+
file_name = urlo[2].replace('index', '')
69+
file_name = file_name.replace('../', '')
70+
file_name = file_name.replace('./', '')
6871

69-
all_docs = [
70-
re.sub(rf"(.*)({url_blog_path})?/docs/*", '', x.replace('\\', '/')).replace(
71-
'.md', ''
72-
)
73-
for x in iglob(str(base) + os.sep + '**', recursive=True)
74-
if os.path.isfile(x)
75-
]
76-
file_found = [
77-
'/' + x for x in all_docs if os.path.basename(x) == file_name or x == file_name
78-
]
79-
if file_found:
80-
internal_link = file_found[0]
81-
else:
82-
return file_name
72+
all_docs = [
73+
re.sub(rf"(.*)({url_blog_path})?/docs/*", '', x.replace('\\', '/')).replace(
74+
'.md', ''
75+
)
76+
for x in iglob(str(base) + os.sep + '**', recursive=True)
77+
if os.path.isfile(x)
78+
]
79+
file_found = [
80+
'/' + x for x in all_docs if os.path.basename(x) == file_name or x == file_name
81+
]
82+
if file_found:
83+
internal_link = file_found[0]
84+
else:
85+
return file_name
8386
file_path = internal_link.replace(base, '')
8487
url = file_path.replace('\\', '/').replace('.md', '')
8588
url = url.replace('//', '/')
@@ -122,6 +125,10 @@ def cite(md_link_path, link, soup, citation_part, config, callouts, custom_attr)
122125

123126
contents = frontmatter.loads(text).content
124127
quote = search_in_file(citation_part, contents)
128+
tooltip_template = (
129+
"<div class='not_found'>" +
130+
str(link['src'].replace('/', '')) + '</div>'
131+
)
125132
if len(quote) > 0:
126133
if callouts:
127134
quote = CalloutsPlugin().on_page_markdown(quote, None, None, None)
@@ -167,32 +174,23 @@ def cite(md_link_path, link, soup, citation_part, config, callouts, custom_attr)
167174
return soup
168175

169176

170-
def search_doc(md_link_path, all_docs):
171-
"""
172-
Search a file in the docs
173-
Args:
174-
md_link_path: Path to check
175-
all_docs: a list containing all path to the file
176-
Returns: Path to link found or 0 otherwise
177-
178-
"""
179-
if os.path.basename(md_link_path) == '.md':
180-
md_link_path = str(md_link_path).replace(
181-
f'{os.sep}.md', f'{os.sep}index.md')
182-
else:
183-
md_link_path = str(md_link_path).replace(f'{os.sep}.md', '')
184-
file = [x for x in all_docs if Path(x) == Path(md_link_path)]
185-
if len(file) > 0:
186-
return file[0]
187-
return 0
188-
189-
190177
def create_link(link):
191178
if link.endswith('/'):
192179
return link[:-1] + '.md'
193180
else:
194181
return link + '.md'
195182

183+
def search_file_in_documentation(link: Path|str, config_dir: Path):
184+
file_name = os.path.basename(link)
185+
if not file_name.endswith('.md'):
186+
file_name = file_name + '.md'
187+
for p in config_dir.rglob(f"*{file_name}"):
188+
print(p)
189+
return p
190+
return 0
191+
192+
193+
196194

197195
class EmbedFile(BasePlugin):
198196
config_scheme = (
@@ -215,14 +213,17 @@ def on_post_page(self, output_content, page, config):
215213
for link in soup.findAll(
216214
'img',
217215
src=lambda src: src is not None and 'favicon' not in src and not src.endswith(
218-
('png', 'jpg', 'jpeg', 'gif')),
216+
('png', 'jpg', 'jpeg', 'gif', 'svg')),
219217
):
220218
if len(link['src']) > 0:
221219

222220
if link['src'][0] == '.': # relative links
223221
md_src = create_link(unquote(link['src']))
224222
md_link_path = Path(
225223
os.path.dirname(page.file.abs_src_path), md_src).resolve()
224+
if not os.path.isfile(md_link_path):
225+
md_link_path = search_file_in_documentation(md_link_path, docs)
226+
226227

227228
elif link['src'][0] == '/':
228229
md_src_path = create_link(unquote(link['src']))
@@ -261,12 +262,7 @@ def on_post_page(self, output_content, page, config):
261262
soup = cite(md_link_path, link, soup,
262263
citation_part, config, callout, self.config['custom-attributes'])
263264
else:
264-
all_docs = [
265-
x
266-
for x in iglob(str(docs) + os.sep + '**', recursive=True)
267-
if x.endswith('.md')
268-
]
269-
link_found = search_doc(md_link_path, all_docs)
265+
link_found=search_file_in_documentation(md_link_path, docs)
270266
if link_found != 0:
271267
soup = cite(link_found, link, soup,
272268
citation_part, config, callout, self.config['custom-attributes'])

0 commit comments

Comments
 (0)