66
77from mkdocs_embed_file_plugins .src .search_quote import search_file_in_documentation
88
9+ import re
10+ from pathlib import Path
11+ MULTIMEDIA_EXTENSIONS = (
12+ ".png" , ".jpg" , ".jpeg" , ".gif" , ".webp" , ".svg" , # Images
13+ ".mp4" , ".avi" , ".mov" , ".mkv" , # Vidéos
14+ ".mp3" , ".wav" , ".flac" , # Audio
15+ ".pdf" , ".doc" , ".docx" , ".xls" , ".xlsx" , ".ppt" , ".pptx" , # Documents
16+ )
917
1018def mini_ez_links (link , base , end , url_whitespace , url_case ):
1119 base_data , url_blog , md_link_path = base
12- url_blog_path = [x for x in url_blog .split ("/" ) if len (x ) > 0 ]
13- url_blog_path = url_blog_path [len (url_blog_path ) - 1 ]
20+ url_blog_path = [x for x in url_blog .split ("/" ) if x ]
21+ url_blog_path = url_blog_path [- 1 ]
22+
23+ # Vérifie si c'est une image (ne pas ajouter notfound:: pour les images)
24+ if any (link [2 ].lower ().endswith (ext ) for ext in MULTIMEDIA_EXTENSIONS ) :
25+ internal_link = Path (md_link_path , link [2 ]).resolve ()
26+ if internal_link .is_file () :
27+ return create_url (internal_link , link [2 ], base , url_blog_path , True )
28+ else :
29+ # Retourne simplement le chemin brut pour les fichiers multimédias non trouvés
30+ return link [2 ]
31+
32+ # Résout le chemin interne pour les fichiers Markdown
1433 internal_link = Path (md_link_path , link [2 ]).resolve ()
15- return create_url (internal_link , link [2 ], base , url_blog_path , True )
34+ if internal_link .is_file ():
35+ return create_url (internal_link , link [2 ], base , url_blog_path , True )
1636
37+ # Si le fichier Markdown n'est pas trouvé, marque avec "notfound::"
38+ return f"notfound::{ create_url (internal_link , link [2 ], base , url_blog_path , True )} "
1739
1840def convert_links_if_markdown (quote_str , base ):
1941 """Convert links if the file is a markdown file."""
20- # search for links
42+ # Search for links
2143 links = re .findall (r"\[([^\]]*)\]\(([^\)]*)\)" , quote_str )
2244 base_data , url_blog , md_link_path = base
2345 if not url_blog :
24- # generate a fake url for the links
2546 raise Exception ("site_url is not defined in mkdocs.yml" )
2647
27- url_blog_path = [x for x in url_blog .split ("/" ) if len ( x ) > 0 ]
28- url_blog_path = url_blog_path [len ( url_blog_path ) - 1 ]
48+ url_blog_path = [x for x in url_blog .split ("/" ) if x ]
49+ url_blog_path = url_blog_path [- 1 ]
2950 for link in links :
3051 if not link [1 ].startswith ("http" ):
3152 internal_link = Path (md_link_path , link [1 ]).resolve ()
@@ -34,48 +55,40 @@ def convert_links_if_markdown(quote_str, base):
3455 return quote_str
3556
3657
37- def create_url (internal_link , link , base , url_blog_path , wikilinks = False ):
58+ def create_url (internal_link , link , base , url_blog_path , wikilinks = False ) :
3859 base , url_blog , md_link_path = base
39- if os .path .isfile (internal_link ):
40- internal_link = str (internal_link ).replace (base , "" )
41- else :
42- if link .endswith (".md" ):
43- if wikilinks :
44- internal_link = str (
45- search_file_in_documentation (
46- Path (link ).resolve (), md_link_path .parent , base
47- )
48- )
49- else :
50- internal_link = str (
51- search_file_in_documentation (link , md_link_path .parent , base )
52- )
53- if not os .path .isfile (internal_link ):
54- file_name = link .replace ("index" , "" )
55- file_name = file_name .replace ("../" , "" )
56- file_name = file_name .replace ("./" , "" )
57- file_name = file_name .replace (".md" , "" )
58- all_docs = [
59- re .sub (
60- rf"(.*)({ url_blog_path } )?/docs/*" , "" , x .replace ("\\ " , "/" )
61- ).replace (".md" , "" )
62- for x in iglob (str (base ) + os .sep + "**" , recursive = True )
63- if os .path .isfile (x )
64- ]
65- file_found = [
66- "/" + x
67- for x in all_docs
68- if os .path .basename (x ) == file_name or x == file_name
69- ]
70- if file_found :
71- internal_link = file_found [0 ]
72- else :
73- internal_link = file_name
74- filepath = internal_link .replace (base , "" )
75- url = filepath .replace ("\\ " , "/" ).replace (".md" , "" )
76- url = re .sub (r"\/$" , "" , str (url_blog )) + "/" + quote (url )
77- if not url .startswith ("http" ):
60+ internal_path = Path (internal_link )
61+ # Vérifie si le lien est une image ou un fichier multimédia
62+ if any (link .lower ().endswith (ext ) for ext in MULTIMEDIA_EXTENSIONS ) :
63+ # Normalise le chemin des images sans les transformer en URLs Markdown
64+ image_path = Path (url_blog ) / link .replace ("\\ " , "/" )
65+ final_url = str (image_path ).replace ("\\ " , "/" )
66+ return final_url
67+
68+ # Vérifie si le chemin est un fichier Markdown valide
69+ if internal_path .is_file () :
70+ internal_link = str (internal_path ).replace (str (base ), "" )
71+ else :
72+ resolved = search_file_in_documentation (link , md_link_path .parent , base )
73+
74+ # Fallback explicite pour `/index.md` via dossier parent
75+ if resolved == 0 and not link .endswith ("index.md" ) :
76+ folder_name = os .path .splitext (link )[0 ]
77+ resolved = search_file_in_documentation (f"{ folder_name } /index.md" , md_link_path .parent , base )
78+
79+ if resolved == 0 :
80+ internal_link = str (link ).replace ("../" , "" ).replace ("./" , "" ).replace (".md" , "" )
81+ else :
82+ internal_link = str (resolved ).replace (str (base ), "" )
83+
84+ # Normalisation du chemin final pour les fichiers Markdown
85+ filepath = internal_link .replace ("\\ " , "/" ).replace (".md" , "" )
86+ url = re .sub (r"/+$" , "" , str (url_blog )) + "/" + quote (filepath )
87+
88+ # Ajout du protocole si manquant
89+ if not url .startswith ("http" ) :
7890 url = "https://" + url
79- if not url .endswith ("/" ) and not re .search (r"\.(.*)$" , url ):
80- url = url + "/"
91+ if not url .endswith ("/" ) and not re .search (r"\\.(.*)$" , url ) :
92+ url += "/"
93+
8194 return url
0 commit comments