API reference¶
Auto-generated from the package's docstrings.
Plugin¶
mkdocs_source_links.plugin ¶
MkDocs plugin entry point.
SourceLinksPlugin ¶
Bases: BasePlugin
MkDocs plugin that rewrites parent-directory inline and reference markdown links to forge URLs.
During mkdocs build, complete inline [text](../path) links and [ref]: ../path
reference definitions in each page's markdown are replaced with git-forge blob/tree/view URLs.
Source files on disk are not modified.
Attributes:
| Name | Type | Description |
|---|---|---|
config_scheme |
PlainConfigSchema
|
Plugin configuration schema. Supports |
Notes
Requires repo_url in mkdocs.yml. Pages without a backing file (virtual pages) are left
unchanged. The git view ref is resolved once per build in on_config, which MkDocs always
runs before on_page_markdown. _view_ref holds a placeholder until on_config runs.
Source code in src/mkdocs_source_links/plugin.py
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 | |
on_config ¶
on_config(config)
Resolve the git view ref once per build and cache it.
Resolving here (rather than per page) avoids running git for every page when
pin: commit or pin: tag is set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
MkDocsConfig
|
MkDocs site configuration. |
required |
Returns:
| Type | Description |
|---|---|
MkDocsConfig
|
The unmodified configuration. |
Source code in src/mkdocs_source_links/plugin.py
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 | |
on_page_markdown ¶
on_page_markdown(markdown, /, *, page, config, files)
Rewrite inline [text](../…) links and [ref]: ../… definitions to forge URLs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
markdown
|
str
|
Markdown source for the page after metadata has been stripped. |
required |
page
|
Page
|
MkDocs page whose |
required |
config
|
MkDocsConfig
|
MkDocs site configuration; |
required |
files
|
Files
|
MkDocs file collection (required by the hook signature; unused). |
required |
Returns:
| Type | Description |
|---|---|
str
|
Markdown with matching inline and reference links rewritten, or the original
|
Source code in src/mkdocs_source_links/plugin.py
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 | |
on_post_build ¶
on_post_build(*, config, **kwargs)
Log rewrite statistics when log_rewrites is enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
MkDocsConfig
|
MkDocs site configuration; |
required |
**kwargs
|
object
|
Additional keyword arguments passed by MkDocs (unused; reserved for hook compatibility). |
{}
|
Source code in src/mkdocs_source_links/plugin.py
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 | |
Link rewriting¶
mkdocs_source_links.rewrite ¶
Rewrite [text](../path) inline links and [ref]: ../path definitions to forge view URLs.
:func:rewrite_repo_parent_links collects image-reference labels, rewrites inline links, then
reference definitions. The markdown parsing primitives live in the internal _scan and
_fences modules, and ../ path resolution lives in the internal _paths module.
repo_relative_path ¶
repo_relative_path(*, page_abs_path, href, repo_root)
Resolve a ../ link from a doc page to a repo-root-relative POSIX path.
The returned path reflects the link text (lexical path). Symlink targets are not followed
when building the forge URL path segment, but resolve() is still used to verify the
target lies inside repo_root.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
page_abs_path
|
Path
|
Absolute path to the page's markdown file on disk. |
required |
href
|
str
|
Link target as written in the markdown; only |
required |
repo_root
|
Path
|
Repository root used to resolve the target and build the relative path. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Repo-root-relative POSIX path, or |
Source code in src/mkdocs_source_links/_paths.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
rewrite_repo_parent_links ¶
rewrite_repo_parent_links(
markdown,
*,
page_abs_path,
repo_root,
repo_url,
view_ref,
forge=None,
report_missing=None,
report_rewrite=None,
report_unknown_forge=None,
report_skipped_shared_label=None,
)
Replace complete inline [text](../…) links and [ref]: ../… definitions with
forge URLs.
Inline rewrites require a bracket-balanced [label](../path) pair; lonely ](../path)
suffixes in prose are left unchanged. Only links whose target resolves to an existing file or
directory inside repo_root are rewritten. Unsupported hosts, paths outside the repo, and
missing targets are left unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
markdown
|
str
|
Markdown source text for a single documentation page. |
required |
page_abs_path
|
Path
|
Absolute path to the page's markdown file on disk. |
required |
repo_root
|
Path
|
Repository root used to resolve |
required |
repo_url
|
str
|
Forge repository URL from |
required |
view_ref
|
ViewRef
|
Git branch name, tag name, or commit SHA and its kind ( |
required |
forge
|
str | None
|
Explicit forge name; when |
None
|
report_missing
|
Callable[[str], None] | None
|
Optional callback invoked with the link target (as written in the markdown) for each
|
None
|
report_rewrite
|
Callable[[], None] | None
|
Optional callback invoked once for each successfully rewritten inline link or reference definition. |
None
|
report_unknown_forge
|
Callable[[], None] | None
|
Optional callback invoked when a |
None
|
report_skipped_shared_label
|
Callable[[str], None] | None
|
Optional callback invoked when a |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Markdown with matching inline and reference links rewritten to forge URLs. |
Notes
Directory targets use tree URLs; file targets use blob URLs (on forges that
distinguish them). URL fragments (#anchor) and link titles are preserved, and
angle-bracket destinations ([text](<../x>)) are supported. Inline images
(), including alt text with nested or escaped ] characters, and image
reference definitions are left unchanged because forge blob URLs are HTML pages, not raw image
assets. Links inside fenced code blocks and inline code spans are left unchanged.
Reference-style definitions ([ref]: ../path) are rewritten when not inside a fenced code
block and not used as an image reference label.
The report_* callbacks are opt-in: when omitted, the corresponding events (missing targets,
rewrites, undetectable forge, skipped shared labels) are silently ignored and only affect the
returned markdown. The plugin (:class:mkdocs_source_links.plugin.SourceLinksPlugin) wires
these callbacks to MkDocs logging — for example it passes report_unknown_forge only when no
explicit forge is configured and warns once per build. Direct API callers that want the
same warnings must pass their own callbacks.
Source code in src/mkdocs_source_links/rewrite.py
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 | |
Forge URLs¶
mkdocs_source_links.urls ¶
Forge view URL builders with host autodetection.
Supports GitHub, GitLab, Bitbucket Cloud, Gitea/Forgejo (incl. Codeberg), and Azure DevOps. Public forge hosts are detected automatically; self-hosted instances on non-obvious domains can be selected with an explicit forge name.
detect_forge ¶
detect_forge(repo_url)
Identify the git forge that hosts a repository URL.
Detection first matches well-known public hosts exactly, then falls back to hostname-label
hints so self-hosted instances (for example GitHub Enterprise at github.example.com) are
recognized. Ambiguous custom domains return None and should be configured explicitly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_url
|
str
|
Repository URL from |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
A forge name from :data: |
Source code in src/mkdocs_source_links/urls.py
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 | |
repo_view_url ¶
repo_view_url(
*,
repo_url,
ref,
ref_kind,
repo_path,
is_dir,
forge=None,
)
Build a git-forge view URL for a path inside the repository.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_url
|
str
|
Forge repository URL from |
required |
ref
|
str
|
Git branch name, tag name, or commit SHA to embed in the URL. |
required |
ref_kind
|
RefKind
|
Whether |
required |
repo_path
|
str
|
Repo-root-relative POSIX path to the target file or directory. |
required |
is_dir
|
bool
|
Whether |
required |
forge
|
str | None
|
Explicit forge name from :data: |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
Forge view URL, or |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/mkdocs_source_links/urls.py
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 | |
Branch resolution¶
mkdocs_source_links.branch ¶
Resolve git branch for forge URLs from MkDocs config.
resolve_branch ¶
resolve_branch(*, plugin_branch, extra, edit_uri)
Resolve the git branch name used in forge view URLs.
Branch resolution follows MkDocs and plugin configuration in priority order: explicit plugin
branch, then extra.git_branch, then the branch parsed from edit_uri, then main.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plugin_branch
|
str | None
|
Value of the plugin's |
required |
extra
|
Mapping[str, Any]
|
MkDocs |
required |
edit_uri
|
str | None
|
MkDocs |
required |
Returns:
| Type | Description |
|---|---|
str
|
Branch name for forge URLs. |
Source code in src/mkdocs_source_links/branch.py
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 | |
Git ref resolution¶
mkdocs_source_links.ref ¶
Resolve git ref and ref kind for forge view URLs.
ResolvedViewRef ¶
Bases: NamedTuple
Result of resolving pin to a :class:ViewRef, with fallback diagnostics.
Source code in src/mkdocs_source_links/ref.py
22 23 24 25 26 27 | |
ViewRef ¶
Bases: NamedTuple
Git ref and kind for forge view URLs.
Source code in src/mkdocs_source_links/ref.py
15 16 17 18 19 | |
resolve_view_ref ¶
resolve_view_ref(*, pin, repo_root, branch)
Resolve the git ref and kind used in forge view URLs.
When pin is commit, the current HEAD SHA is resolved via git rev-parse. When
pin is tag, an exact tag at HEAD is resolved via git describe --tags
--exact-match. If git is unavailable, times out, or lookup fails, the resolved branch is
used instead (for commit and tag).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pin
|
str
|
Pin mode: |
required |
repo_root
|
Path
|
Repository root passed to |
required |
branch
|
str
|
Resolved branch name used when |
required |
Returns:
| Type | Description |
|---|---|
ResolvedViewRef
|
Resolved ref and kind for URL building, plus whether a |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/mkdocs_source_links/ref.py
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 | |
Line anchors¶
mkdocs_source_links.anchors ¶
Translate canonical line-number URL fragments to forge-specific syntax.
translate_line_fragment ¶
translate_line_fragment(fragment, *, forge)
Translate a canonical #L line fragment to forge-specific syntax.
Canonical input uses #L10 for a single line and #L10-L20 for a range. Non-line
fragments (for example #section) are returned unchanged. Azure DevOps does not support
hash-based line anchors in view URLs, so line fragments are dropped for that forge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fragment
|
str
|
URL fragment from the markdown link, including the leading |
required |
forge
|
str
|
Forge name from :data: |
required |
Returns:
| Type | Description |
|---|---|
str
|
Forge-specific fragment, the original fragment when it is not a line anchor, or an empty string for Azure line anchors. |
Source code in src/mkdocs_source_links/anchors.py
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 | |