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 markdown links to forge URLs.
During mkdocs build, ](../path) links in each page's markdown are replaced with GitHub
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.
Source code in src/mkdocs_source_links/plugin.py
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 | |
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 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
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 | |
on_page_markdown ¶
on_page_markdown(markdown, /, *, page, config, files)
Rewrite ](../…) links in page markdown to forge view 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 parent-directory links rewritten, or the original |
Source code in src/mkdocs_source_links/plugin.py
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 | |
Link rewriting¶
mkdocs_source_links.rewrite ¶
Rewrite ../ markdown links to forge view URLs.
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.
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/rewrite.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
rewrite_repo_parent_links ¶
rewrite_repo_parent_links(
markdown,
*,
page_abs_path,
repo_root,
repo_url,
view_ref,
forge=None,
report_missing=None,
)
Replace ](../…) markdown links with git-forge view URLs.
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 or commit SHA and its kind for blob/tree URLs. |
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
|
Returns:
| Type | Description |
|---|---|
str
|
Markdown with matching parent-directory 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 (](<../x>)) are supported. Links inside fenced code blocks
and inline code spans are left unchanged.
Source code in src/mkdocs_source_links/rewrite.py
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 | |
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
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 | |
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 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 |
Source code in src/mkdocs_source_links/urls.py
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 | |
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
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 | |
Git ref resolution¶
mkdocs_source_links.ref ¶
Resolve git ref and ref kind for forge view URLs.
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. If git is
unavailable, times out, or repo_root is not a repository, the resolved branch is used
instead.
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 |
|---|---|
ViewRef
|
Ref string and kind ( |
Source code in src/mkdocs_source_links/ref.py
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 | |
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 | |