MkDocs config file wrapper.
Source code in mkdocs_mknodes/mkdocsconfig.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237 | class Config:
"""MkDocs config file wrapper."""
def __init__(self, config: Mapping | str | os.PathLike[str] | None = None):
"""Constructor.
Args:
config: MkDocs config
"""
match config:
case MkNodesConfig():
self._config: MkNodesConfig = config
case Mapping():
self._config = load_config(config)
case str() | os.PathLike() as path:
self._config = load_config(str(path))
case None:
if file := pathhelpers.find_cfg_for_folder("mkdocs.yml"):
self._config = load_config(str(file))
else:
msg = "Could not find config file"
raise FileNotFoundError(msg)
case _:
raise TypeError(config)
self.plugin = self._config.plugins["mknodes"]
def __getattr__(self, name):
return getattr(self._config, name)
def __repr__(self):
return reprhelpers.get_repr(self, self._config)
@property
def site_url(self) -> str:
url = self._config.site_url
if url is None:
return ""
return url if url.endswith("/") else f"{url}/"
@property
def docs_dir(self) -> pathlib.Path:
return pathlib.Path(self._config.docs_dir)
@property
def site_dir(self) -> pathlib.Path:
return pathlib.Path(self._config.site_dir)
def update_from_context(self, context: contexts.ProjectContext):
if not self._config.extra.get("social"):
self._config.extra["social"] = context.metadata.social_info
self._config.repo_url = context.metadata.repository_url
self._config.site_description = context.metadata.summary
self._config.site_name = context.metadata.distribution_name
self._config.site_author = context.metadata.author_name
text = f"Copyright © {datetime.now().year} {context.metadata.author_name}"
self._config.copyright = text
def get_markdown_instance(
self,
additional_extensions: list[str] | None = None,
config_override: dict[str, Any] | None = None,
) -> mdconverter.MdConverter:
"""Return a markdown instance based on given config.
Args:
additional_extensions: Additional extensions to use
config_override: Dict with extension settings. Overrides config settings.
"""
extensions = self._config.markdown_extensions
if additional_extensions:
extensions = list(set(additional_extensions + extensions))
configs = self._config.mdx_configs | (config_override or {})
return mdconverter.MdConverter(extensions=extensions, extension_configs=configs)
def get_edit_url(self, edit_path: str | None) -> str | None:
"""Return edit url.
If no explicit edit path is given, this will return the path
to the builder function.
Args:
edit_path: Edit path
"""
repo_url = self.repo_url
if not repo_url:
return None
edit_uri = self.edit_uri or "edit/main/"
if not edit_uri.startswith(("?", "#")) and not repo_url.endswith("/"):
repo_url += "/"
rel_path = self.build_fn.split(":")[0]
if not rel_path.endswith(".py"):
rel_path = rel_path.replace(".", "/")
rel_path += ".py"
base_url = parse.urljoin(repo_url, edit_uri)
if repo_url and edit_path:
# root_path = pathlib.Path(config["docs_dir"]).parent
# edit_path = str(edit_path.relative_to(root_path))
rel_path = edit_path
return parse.urljoin(base_url, rel_path)
def add_js(
self,
path: str,
defer: bool = False,
async_: bool = False,
typ: str = "",
):
"""Add javascript to the config.
Args:
path: Path / URL to the javascript file
defer: Add defer attribute to <script> tag
async_: Add async attribute to <script> tag
typ: Add given type attribute to <script> tag
"""
from mkdocs.config import config_options
val = config_options.ExtraScriptValue(str(path))
val.async_ = async_
val.defer = defer
val.type = typ
self.extra_javascript.append(val)
|
__init__
Constructor.
Parameters:
Source code in mkdocs_mknodes/mkdocsconfig.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 | def __init__(self, config: Mapping | str | os.PathLike[str] | None = None):
"""Constructor.
Args:
config: MkDocs config
"""
match config:
case MkNodesConfig():
self._config: MkNodesConfig = config
case Mapping():
self._config = load_config(config)
case str() | os.PathLike() as path:
self._config = load_config(str(path))
case None:
if file := pathhelpers.find_cfg_for_folder("mkdocs.yml"):
self._config = load_config(str(file))
else:
msg = "Could not find config file"
raise FileNotFoundError(msg)
case _:
raise TypeError(config)
self.plugin = self._config.plugins["mknodes"]
|
add_js
add_js(path: str, defer: bool = False, async_: bool = False, typ: str = '')
Add javascript to the config.
Parameters:
Name |
Type |
Description |
Default |
path
|
str
|
Path / URL to the javascript file
|
required
|
defer
|
bool
|
|