11import codecs
2- import os
2+ import logging
33import re
44from pathlib import Path
55from urllib .parse import unquote
6+
67import frontmatter
78import markdown
89from bs4 import BeautifulSoup
10+ from custom_attributes .plugin import convert_text_attributes
911from mkdocs .config import config_options
1012from mkdocs .plugins import BasePlugin
1113from mkdocs_callouts .plugin import CalloutsPlugin
12- from custom_attributes .plugin import convert_text_attributes
13- import logging
1414
1515from mkdocs_embed_file_plugins .src .links_correction import (
1616 MULTIMEDIA_EXTENSIONS ,
2727 strip_comments ,
2828)
2929
30+ DEFAULT_MARKDOWN_EXTENSIONS_CONFIG = {
31+ "pymdownx.arithmatex" : {"generic" : True },
32+ "pymdownx.highlight" : {
33+ "use_pygments" : True ,
34+ "linenums" : True ,
35+ },
36+ "pymdownx.tasklist" : {"custom_checkbox" : True },
37+ }
38+
3039
3140def cite (
32- md_link_path , link , soup , citation_part , config , callouts , custom_attr , msg
41+ md_link_path ,
42+ link ,
43+ soup ,
44+ citation_part ,
45+ config ,
46+ callouts ,
47+ custom_attr ,
48+ msg ,
49+ md_config ,
3350) -> BeautifulSoup :
3451 """Append the content of the founded file to the original file.
3552
3653 Args:
37- md_link_path (str): Path of the file to be modified.
54+ md_link_path (str|Path ): Path of the file to be modified.
3855 link (str): Link to the file to be included.
3956 soup (BeautifulSoup): BeautifulSoup object of the file to be modified.
4057 citation_part (str): Part of the link to be included.
41- config (dict): Configuration of the plugin.
58+ config (dict|MkdocsConfig ): Configuration of the plugin.
4259 callouts (CalloutsPlugin): Callouts plugin.
4360 custom_attr (CustomAttributesPlugin): Custom attributes plugin.
4461 msg (str): Message to be displayed if the file is not found.
62+ md_config (dict): Configuration of the markdown extensions.
4563 Returns: updated HTML
4664 """
65+ log = logging .getLogger ("mkdocs.plugins." + __name__ )
4766 docs = config ["docs_dir" ]
4867 url = config ["site_url" ]
49-
50- md_config = {
68+ mdx_wikilink_plus = {
5169 "mdx_wikilink_plus" : {
5270 "base_url" : (docs , url , md_link_path ),
5371 "build_url" : mini_ez_links ,
5472 "image_class" : "wikilink" ,
55- }
73+ },
5674 }
75+ md_config .update (mdx_wikilink_plus )
5776 new_uri = str (md_link_path ).replace (str (docs ), str (url ))
5877 new_uri = new_uri .replace ("\\ " , "/" )
5978 new_uri = new_uri .replace (".md" , "/" )
@@ -83,9 +102,13 @@ def cite(
83102 quote = strip_comments (quote )
84103 md_extensions = config ["markdown_extensions" ]
85104 md_extensions .append ("mdx_wikilink_plus" )
105+ md_extensions = list (dict .fromkeys (md_extensions ))
106+ # remove arithmatex from the list of extensions
107+
86108 html = markdown .markdown (
87109 quote , extensions = md_extensions , extension_configs = md_config
88110 )
111+
89112 link_soup = BeautifulSoup (html , "html.parser" )
90113 if link_soup :
91114 tooltip_template = (
@@ -101,8 +124,7 @@ def cite(
101124 soup = BeautifulSoup (new_soup , "html.parser" )
102125 return soup
103126 else :
104- log = logging .getLogger ("mkdocs.plugins." + __name__ )
105- log .info (
127+ log .warning (
106128 "[EMBED FILE PLUGIN] CITATION NOT FOUND : "
107129 + unquote (citation_part )
108130 + "for : "
@@ -141,9 +163,20 @@ def __init__(self):
141163 self .enabled = True
142164 self .total_time = 0
143165
166+ def create_config (self , config ):
167+ md_config = {}
168+ mdx_config = config .get ("mdx_configs" )
169+ if mdx_config :
170+ for key , value in mdx_config .items ():
171+ md_config [key ] = value
172+ else :
173+ md_config .update (DEFAULT_MARKDOWN_EXTENSIONS_CONFIG )
174+ return md_config
175+
144176 def on_post_page (self , output_content , page , config ) -> str :
145177 soup = BeautifulSoup (output_content , "html.parser" )
146178 docs = Path (config ["docs_dir" ])
179+ md_config = self .create_config (config )
147180 md_link_path = ""
148181 callout = self .config ["callouts" ]
149182 language_message = self .config ["language_message" ]
@@ -162,32 +195,30 @@ def on_post_page(self, output_content, page, config) -> str:
162195 elif link ["src" ][0 ] == "." : # relative links
163196 md_src = create_link (unquote (link ["src" ]))
164197 md_link_path = Path (
165- os . path . dirname (page .file .abs_src_path ), md_src
198+ Path (page .file .abs_src_path ). parent , md_src
166199 ).resolve ()
167200 md_link_path = re .sub (r"[\/\\]?#(.*)$" , "" , str (md_link_path ))
168- if not os . path . isfile (md_link_path ):
201+ if not Path (md_link_path ). is_file ( ):
169202 md_link_path = search_file_in_documentation (
170203 md_link_path , docs , docs
171204 )
172205
173206 elif link ["src" ][0 ] == "/" :
174207 md_src_path = create_link (unquote (link ["src" ]))
175- md_link_path = os . path . join (config ["docs_dir" ], md_src_path )
208+ md_link_path = Path (config ["docs_dir" ]) / md_src_path
176209 md_link_path = Path (unquote (md_link_path )).resolve ()
177210
178211 elif link ["src" ][0 ] != "#" :
179212 md_src_path = create_link (unquote (link ["src" ]))
180- md_link_path = os .path .join (
181- os .path .dirname (page .file .abs_src_path ), md_src_path
182- )
213+ md_link_path = Path (page .file .abs_src_path ).parent / md_src_path
214+
183215 md_link_path = re .sub (r"/#(.*).md$" , ".md" , str (md_link_path ))
184216 md_link_path = Path (unquote (md_link_path )).resolve ()
185217
186218 else :
187219 md_src_path = create_link (unquote (link ["src" ]))
188- md_link_path = os .path .join (
189- os .path .dirname (page .file .abs_src_path ), md_src_path
190- )
220+ md_link_path = Path (page .file .abs_src_path ).parent / md_src_path
221+
191222 md_link_path = Path (unquote (md_link_path )).resolve ()
192223 if md_link_path == 0 :
193224 soup = tooltip_not_found (link , soup , language_message )
@@ -202,7 +233,7 @@ def on_post_page(self, output_content, page, config) -> str:
202233 if citation_part :
203234 md_link_path = Path (str (md_link_path ))
204235
205- if os . path . isfile (md_link_path ):
236+ if Path (md_link_path ). is_file ( ):
206237 soup = cite (
207238 md_link_path ,
208239 link ,
@@ -212,6 +243,7 @@ def on_post_page(self, output_content, page, config) -> str:
212243 callout ,
213244 self .config ["custom-attributes" ],
214245 language_message ,
246+ md_config ,
215247 )
216248 else :
217249 link_found = search_file_in_documentation (
@@ -227,5 +259,6 @@ def on_post_page(self, output_content, page, config) -> str:
227259 callout ,
228260 self .config ["custom-attributes" ],
229261 language_message ,
262+ md_config ,
230263 )
231264 return add_not_found_class (str (soup ))
0 commit comments