1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
# Copyright (c) 2016-2025 Martin Donath <martin.donath@squidfunk.com>
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
from __future__ import annotations
import posixpath
import re
from mkdocs.config.defaults import MkDocsConfig
from mkdocs.structure.files import File, Files
from mkdocs.structure.pages import Page
from re import Match
# -----------------------------------------------------------------------------
# Hooks
# -----------------------------------------------------------------------------
# @todo
def on_page_markdown(
markdown: str, *, page: Page, config: MkDocsConfig, files: Files
):
# Replace callback
def replace(match: Match):
type, args = match.groups()
args = args.strip()
if type == "version":
if args.startswith("insiders-"):
return _badge_for_version_insiders(args, page, files)
else:
return _badge_for_version(args, page, files)
elif type == "sponsors": return _badge_for_sponsors(page, files)
elif type == "flag": return flag(args, page, files)
elif type == "option": return option(args)
elif type == "setting": return setting(args)
elif type == "feature": return _badge_for_feature(args, page, files)
elif type == "plugin": return _badge_for_plugin(args, page, files)
elif type == "extension": return _badge_for_extension(args, page, files)
elif type == "utility": return _badge_for_utility(args, page, files)
elif type == "example": return _badge_for_example(args, page, files)
elif type == "demo": return _badge_for_demo(args, page, files)
elif type == "default":
if args == "none": return _badge_for_default_none(page, files)
elif args == "computed": return _badge_for_default_computed(page, files)
else: return _badge_for_default(args, page, files)
# Otherwise, raise an error
raise RuntimeError(f"Unknown shortcode: {type}")
# Find and replace all external asset URLs in current page
return re.sub(
r"<!-- md:(\w+)(.*?) -->",
replace, markdown, flags = re.I | re.M
)
# -----------------------------------------------------------------------------
# Helper functions
# -----------------------------------------------------------------------------
# Create a flag of a specific type
def flag(args: str, page: Page, files: Files):
type, *_ = args.split(" ", 1)
if type == "experimental": return _badge_for_experimental(page, files)
elif type == "required": return _badge_for_required(page, files)
elif type == "customization": return _badge_for_customization(page, files)
elif type == "metadata": return _badge_for_metadata(page, files)
elif type == "multiple": return _badge_for_multiple(page, files)
raise RuntimeError(f"Unknown type: {type}")
# Create a linkable option
def option(type: str):
_, *_, name = re.split(r"[.:]", type)
return f"[`{name}`](#+{type}){{ #+{type} }}\n\n"
# Create a linkable setting - @todo append them to the bottom of the page
def setting(type: str):
_, *_, name = re.split(r"[.*]", type)
return f"`{name}` {{ #{type} }}\n\n[{type}]: #{type}\n\n"
# -----------------------------------------------------------------------------
# Resolve path of file relative to given page - the posixpath always includes
# one additional level of `..` which we need to remove
def _resolve_path(path: str, page: Page, files: Files):
path, anchor, *_ = f"{path}#".split("#")
path = _resolve(files.get_file_from_path(path), page)
return "#".join([path, anchor]) if anchor else path
# Resolve path of file relative to given page - the posixpath always includes
# one additional level of `..` which we need to remove
def _resolve(file: File, page: Page):
path = posixpath.relpath(file.src_uri, page.file.src_uri)
return posixpath.sep.join(path.split(posixpath.sep)[1:])
# -----------------------------------------------------------------------------
# Create badge
def _badge(icon: str, text: str = "", type: str = ""):
classes = f"mdx-badge mdx-badge--{type}" if type else "mdx-badge"
return "".join([
f"<span class=\"{classes}\">",
*([f"<span class=\"mdx-badge__icon\">{icon}</span>"] if icon else []),
*([f"<span class=\"mdx-badge__text\">{text}</span>"] if text else []),
f"</span>",
])
# Create sponsors badge
def _badge_for_sponsors(page: Page, files: Files):
icon = "material-heart"
href = _resolve_path("insiders/index.md", page, files)
return _badge(
icon = f"[:{icon}:]({href} 'Sponsors only')",
type = "heart"
)
# Create badge for version
def _badge_for_version(text: str, page: Page, files: Files):
spec = text
path = f"changelog/index.md#{spec}"
# Return badge
icon = "material-tag-outline"
href = _resolve_path("conventions.md#version", page, files)
return _badge(
icon = f"[:{icon}:]({href} 'Minimum version')",
text = f"[{text}]({_resolve_path(path, page, files)})" if spec else ""
)
# Create badge for version of Insiders
def _badge_for_version_insiders(text: str, page: Page, files: Files):
spec = text.replace("insiders-", "")
path = f"insiders/changelog/index.md#{spec}"
# Return badge
icon = "material-tag-heart-outline"
href = _resolve_path("conventions.md#version-insiders", page, files)
return _badge(
icon = f"[:{icon}:]({href} 'Minimum version')",
text = f"[{text}]({_resolve_path(path, page, files)})" if spec else ""
)
# Create badge for feature
def _badge_for_feature(text: str, page: Page, files: Files):
icon = "material-toggle-switch"
href = _resolve_path("conventions.md#feature", page, files)
return _badge(
icon = f"[:{icon}:]({href} 'Optional feature')",
text = text
)
# Create badge for plugin
def _badge_for_plugin(text: str, page: Page, files: Files):
icon = "material-floppy"
href = _resolve_path("conventions.md#plugin", page, files)
return _badge(
icon = f"[:{icon}:]({href} 'Plugin')",
text = text
)
# Create badge for extension
def _badge_for_extension(text: str, page: Page, files: Files):
icon = "material-language-markdown"
href = _resolve_path("conventions.md#extension", page, files)
return _badge(
icon = f"[:{icon}:]({href} 'Markdown extension')",
text = text
)
# Create badge for utility
def _badge_for_utility(text: str, page: Page, files: Files):
icon = "material-package-variant"
href = _resolve_path("conventions.md#utility", page, files)
return _badge(
icon = f"[:{icon}:]({href} 'Third-party utility')",
text = text
)
# Create badge for example
def _badge_for_example(text: str, page: Page, files: Files):
return "\n".join([
_badge_for_example_download(text, page, files),
_badge_for_example_view(text, page, files)
])
# Create badge for example view
def _badge_for_example_view(text: str, page: Page, files: Files):
icon = "material-folder-eye"
href = f"https://mkdocs-material.github.io/examples/{text}/"
return _badge(
icon = f"[:{icon}:]({href} 'View example')",
type = "right"
)
# Create badge for example download
def _badge_for_example_download(text: str, page: Page, files: Files):
icon = "material-folder-download"
href = f"https://mkdocs-material.github.io/examples/{text}.zip"
return _badge(
icon = f"[:{icon}:]({href} 'Download example files')",
text = f"[`.zip`]({href})",
type = "right"
)
# Create badge for demo repository
def _badge_for_demo(text: str, page: Page, files: Files):
icon = "material-github"
href = f"https://github.com/mkdocs-material/{text}"
return _badge(
icon = f"[:{icon}:]({href} 'Demo repository')",
text = text,
type = "right"
)
# Create badge for default value
def _badge_for_default(text: str, page: Page, files: Files):
icon = "material-water"
href = _resolve_path("conventions.md#default", page, files)
return _badge(
icon = f"[:{icon}:]({href} 'Default value')",
text = text
)
# Create badge for empty default value
def _badge_for_default_none(page: Page, files: Files):
icon = "material-water-outline"
href = _resolve_path("conventions.md#default", page, files)
return _badge(
icon = f"[:{icon}:]({href} 'Default value is empty')"
)
# Create badge for computed default value
def _badge_for_default_computed(page: Page, files: Files):
icon = "material-water-check"
href = _resolve_path("conventions.md#default", page, files)
return _badge(
icon = f"[:{icon}:]({href} 'Default value is computed')"
)
# Create badge for metadata property flag
def _badge_for_metadata(page: Page, files: Files):
icon = "material-list-box-outline"
href = _resolve_path("conventions.md#metadata", page, files)
return _badge(
icon = f"[:{icon}:]({href} 'Metadata property')"
)
# Create badge for required value flag
def _badge_for_required(page: Page, files: Files):
icon = "material-alert"
href = _resolve_path("conventions.md#required", page, files)
return _badge(
icon = f"[:{icon}:]({href} 'Required value')"
)
# Create badge for customization flag
def _badge_for_customization(page: Page, files: Files):
icon = "material-brush-variant"
href = _resolve_path("conventions.md#customization", page, files)
return _badge(
icon = f"[:{icon}:]({href} 'Customization')"
)
# Create badge for multiple instance flag
def _badge_for_multiple(page: Page, files: Files):
icon = "material-inbox-multiple"
href = _resolve_path("conventions.md#multiple-instances", page, files)
return _badge(
icon = f"[:{icon}:]({href} 'Multiple instances')"
)
# Create badge for experimental flag
def _badge_for_experimental(page: Page, files: Files):
icon = "material-flask-outline"
href = _resolve_path("conventions.md#experimental", page, files)
return _badge(
icon = f"[:{icon}:]({href} 'Experimental')"
)
|