Debian Codegraph @

docs/src/markdown/extensions/magiclink.md

raw
1 ---
2 icon: lucide/link
3 ---
4 [:octicons-file-code-24:][_magiclink]{: .source-link }
5
6 # MagicLink
7
8 /// note | Icons
9 This documentation implements additional styling with CSS that inserts icons before special links, such as GitHub,
10 logos, bug icons, etc. MagicLink does not inject icons or CSS to insert icons, but it is left to the user to
11 implement (if desired) via the provided [classes](#css). User's are free to reference this
12 [documentation's source][magiclink-icons] to learn how.
13 ///
14
15 ## Overview
16
17 MagicLink is an extension that provides a number of useful link related features. MagicLink can auto-link HTML, FTP, and
18 email links. It can auto-convert repository links (GitHub, GitLab, and Bitbucket) and display them in a more concise,
19 shorthand format. MagicLink can also be configured to directly auto-link the aforementioned shorthand format.
20
21 If you happen to have some conflicts with syntax for a specific case, you can always revert to the old auto-link format
22 as well: `#!md <https://www.link.com>`. If enabled, repository link shortening will be applied to the angle bracketed
23 auto-link format as well.
24
25 /// tip | SaneHeaders
26 It is also recommended to use [SaneHeaders](./saneheaders.md) when using MagicLink to avoid problems when specifying
27 a repository bug (using the syntax `#1`) at the start of a line. Python Markdown, by default, will treat `#1` as a
28 header if detected at the start of a line without SaneHeaders.
29 ///
30
31 The MagicLink extension can be included in Python Markdown by using the following:
32
33 ```py3
34 import markdown
35 md = markdown.Markdown(extensions=['pymdownx.magiclink'])
36 ```
37
38 ## Auto-Linking
39
40 MagicLink supports auto-linking HTTP, FTP and email links. You can specify these links in raw text and they should get
41 auto-linked. There are some limitations placed on MagicLink to keep it from aggressively auto-linking text that is not
42 part of links. If you have a link that cannot be detected, you can always use the old style angle bracketed link format:
43 `#!md <https://www.link.com>`.
44
45 ```text title="Auto-Linking"
46 - Just paste links directly in the document like this: https://google.com.
47 - Or even an email address: [email protected].
48 ```
49
50 /// html | div.result
51 ```md-render
52 ---
53 extensions:
54 - pymdownx.magiclink
55 - pymdownx.saneheaders
56 - markdown.extensions.attr_list
57
58 extension_configs:
59 pymdownx.magiclink:
60 repo_url_shortener: true
61 repo_url_shorthand: true
62 social_url_shorthand: true
63 social_url_shortener: true
64 user: facelessuser
65 repo: pymdown-extensions
66 ---
67 - Just paste links directly in the document like this: https://google.com.
68 - Or even an email address: [email protected]{.magiclink-ignore}.
69 ```
70 ///
71
72 ## Shorthand Links
73
74
75 ```md-render
76 ---
77 extensions:
78 - pymdownx.magiclink
79 - pymdownx.saneheaders
80 - markdown.extensions.attr_list
81
82 extension_configs:
83 pymdownx.magiclink:
84 repo_url_shortener: true
85 repo_url_shorthand: true
86 social_url_shorthand: true
87 social_url_shortener: true
88 user: facelessuser
89 repo: pymdown-extensions
90 ---
91 MagicLink supports shorthand references for GitHub, GitLab, and Bitbucket issues (#1{.magiclink-ignore}), pull/merge
92 requests (!13{.magiclink-ignore}), GitHub Discussion (?1173{.magiclink-ignore}), commits
93 (7d1b1902ea7fe00043a249564ed5032f08dd7152{.magiclink-ignore}), and compares
94 (e2ed7e0b3973f3f9eb7a26b8ef7ae514eebfe0d2...90b6fb8711e75732f987982cc024e9bb0111beac{.magiclink-ignore}). You can also
95 reference repositories (@facelessuser/pymdown-extensions{.magiclink-ignore}) and users
96 (@facelessuser{.magiclink-ignore}). Mentions also works for social media (only X is supported at this time).
97 ```
98
99 The syntax used is actually very similar to GitLab's syntax. GitLab was chosen as its syntax bridges the gaps between
100 the three providers. GitLab and Bitbucket require a pull specific syntax while GitHub does not have one. Also, while
101 GitHub uses `#<num>` for issues, pulls, and discussions, the idea of using a different symbol for issue
102 types gives us the context we need to generate the correct links for issues vs pulls, etc.
103
104 ### Configuring
105
106 To enable shorthand syntax for code repository providers and social media providers, enable `repo_url_shorthand` and
107 `social_url_shorthand` respectively in the [options](#options).
108
109 To use this feature you must first specify in the [options](#options) your `provider` to which all shorthand references
110 are relative to, but you will still be able to reference outside of your defaults. And if you are specifying a code
111 repository provider as your default, you must also specify the `user` and `repo` (repository) which adds more context
112 and allows you to shorten the syntax even more for issues relating to your default `user` and/or `repo`. This is useful
113 if you are using MagicLink to write documentation for a project that is hosted on one of the supported code repository
114 providers.
115
116 If you are using this extension more generally, it may make more sense to set a social media provider as the default
117 `provider`. There is no need to set a `user` or `repo` for a social media provider as the context is not useful for
118 mentions. You will still be able to reference repository links with shorthand if it enabled, albeit in a longer format.
119
120 /// warning
121 Links are not verified, so make sure you are specifying valid issues, repositories, and users as they will be
122 auto-linked even if they are not valid.
123 ///
124
125 ### Mentions
126
127 Mentions of other users are performed with the following syntax: `@{user}`. To reference a provider other than your
128 default, use the format `@{provider}:{user}`
129
130 ```text title="Mentions"
131 @facelessuser
132
133 @x:x
134 ```
135
136 /// html | div.result
137 ```md-render
138 ---
139 extensions:
140 - pymdownx.magiclink
141 - pymdownx.saneheaders
142 - markdown.extensions.attr_list
143
144 extension_configs:
145 pymdownx.magiclink:
146 repo_url_shortener: true
147 repo_url_shorthand: true
148 social_url_shorthand: true
149 social_url_shortener: true
150 user: facelessuser
151 repo: pymdown-extensions
152 ---
153 @facelessuser
154
155 @x:x
156 ```
157 ///
158
159 For code repository providers, you can also mention repositories. This feature is actually inspired by the GitHub only
160 extension @Python-Markdown/github-links, which performs *similar* auto-linking. The syntax to auto-link a repository
161 with mentioning is very similar to auto-linking a user, except you append the repository to the user name like so:
162 `@{user}/{repo}`. If specifying a non-default provider, the form would look like: `@{provider}:{user}/{repo}`. The
163 output for repository mentions omits the `@` symbol and will just show the user and repository, but you are free to
164 style it with CSS to make it stand out more like has been done in this document.
165
166 ```text title="Repository Mentions"
167 @facelessuser/pymdown-extensions
168
169 @gitlab:pycqa/flake8-engine
170 ```
171
172 /// html | div.result
173 ```md-render
174 ---
175 extensions:
176 - pymdownx.magiclink
177 - pymdownx.saneheaders
178 - markdown.extensions.attr_list
179
180 extension_configs:
181 pymdownx.magiclink:
182 repo_url_shortener: true
183 repo_url_shorthand: true
184 social_url_shorthand: true
185 social_url_shortener: true
186 user: facelessuser
187 repo: pymdown-extensions
188 ---
189 @facelessuser/pymdown-extensions{.magiclink-ignore}
190
191 @gitlab:pycqa/flake8-engine{.magiclink-ignore}
192 ```
193 ///
194
195 ### Issues, Pull Requests, and Discussions
196
197 Issues, pull requests, and GitHub discussions are specified with `#{num}`, `!{num}`, and `?{num}` respectively. When
198 specified in this manner, the links will assume the default repository and default user for generated links.
199
200 To specify an issue for a non-default repository under the default user, prefix the repository: `{repo}#{num}`.
201
202 To specify a repository under a non-default user, prefix both the user and repository: `{user}/{repo}#{num}`.
203
204 Lastly, to reference an external provider, use the format `{provider}:{user}/{repo}#{num}`.
205
206 The syntax was borrowed and adapted from GitLab as they use `!{num}` for pulls and `#{num}` for issues, `?{num}` is our
207 own take for discussions. This syntax was
208 mainly used to ensure we could provide context to the link generator. When rendering the actual links in your documents,
209 it will use the syntax associated with the specified provider. If this is unsatisfactory, you can override this behavior
210 with the `icons` option and all links will use the same specified convention on output regardless of the provider.
211
212 ```text title="Issues"
213 #1
214
215 backrefs#1
216
217 Python-Markdown/markdown#1
218
219 gitlab:pycqa/flake8-engine#21
220
221 !13
222
223 backrefs!4
224
225 Python-Markdown/markdown!598
226
227 gitlab:pycqa/infrastructure!1
228 ```
229
230 /// html | div.result
231 ```md-render
232 ---
233 extensions:
234 - pymdownx.magiclink
235 - pymdownx.saneheaders
236 - markdown.extensions.attr_list
237
238 extension_configs:
239 pymdownx.magiclink:
240 repo_url_shortener: true
241 repo_url_shorthand: true
242 social_url_shorthand: true
243 social_url_shortener: true
244 user: facelessuser
245 repo: pymdown-extensions
246 ---
247 #1{.magiclink-ignore}
248
249 backrefs#1{.magiclink-ignore}
250
251 Python-Markdown/markdown#1{.magiclink-ignore}
252
253 gitlab:pycqa/flake8-engine#21{.magiclink-ignore}
254
255 !13{.magiclink-ignore}
256
257 backrefs!4{.magiclink-ignore}
258
259 Python-Markdown/markdown!598{.magiclink-ignore}
260
261 gitlab:pycqa/infrastructure!1{.magiclink-ignore}
262 ```
263 ///
264
265
266 /// note | Note
267 GitHub actually gives pull requests and issues unique values while GitLab and Bitbucket can have pulls with the same
268 ID as an issue. So with GitHub, you can use `#{num}` format for both issues and pulls, and GitHub will redirect you
269 to the appropriate issue or pull.
270
271 GitLab and Bitbucket **must** specify pulls different from issues, hence the `!13` format. Though GitHub doesn't
272 *need* to use the pull format, you can if you like. This format was actually borrowed from GitLab.
273 ///
274
275 ### Commits
276
277 Commit shorthand syntax is simply the 40 character commit hash value: `{hash}`. And much like issues and pull requests,
278 you can denote a repository under the default user with `{repo}@{hash}` and a repository under a non-default user with
279 `{user}/{repo}@{hash}`. Lastly, to reference an external provider, use the format `{provider}:{user}/{repo}@{hash}`. If
280 the default provider is a social media provider, then only the latter syntax can be used.
281
282 ```text title="Commits"
283 181c06d1f11fa29961b334e90606ed1f1ec7a7cc
284
285 backrefs@cb4ecc5e7d8f7cdff0bb4482174f2ff0dcc35c61
286
287 Python-Markdown/markdown@de5c696f94e8dde242c29d4be50b7bbf3c17fedb
288
289 gitlab:pycqa/flake8-engine@ee18ac981e3dbaf3365a817663834c7b547f83cb
290 ```
291
292 /// html | div.result
293 ```md-render
294 ---
295 extensions:
296 - pymdownx.magiclink
297 - pymdownx.saneheaders
298 - markdown.extensions.attr_list
299
300 extension_configs:
301 pymdownx.magiclink:
302 repo_url_shortener: true
303 repo_url_shorthand: true
304 social_url_shorthand: true
305 social_url_shortener: true
306 user: facelessuser
307 repo: pymdown-extensions
308 ---
309 181c06d1f11fa29961b334e90606ed1f1ec7a7cc{.magiclink-ignore}
310
311 backrefs@cb4ecc5e7d8f7cdff0bb4482174f2ff0dcc35c61{.magiclink-ignore}
312
313 Python-Markdown/markdown@de5c696f94e8dde242c29d4be50b7bbf3c17fedb{.magiclink-ignore}
314
315 gitlab:pycqa/flake8-engine@ee18ac981e3dbaf3365a817663834c7b547f83cb{.magiclink-ignore}
316 ```
317 ///
318
319 ### Diff/Compare
320
321 GitLab offers a useful shorthand to specify links to compare differences between two commits. This has been adopted for
322 all the supported repository providers.
323
324 Specifying a compare link is very similar to specifying a commit link except you must specify the older hash and the
325 newer hash separated by `...`: `{hash1}...{hash2}`. To specify a compare for a non-default repository under the default
326 user, prefix the repository: `{repo}@{hash1}...{hash2}`. And to specify a compare under a non-default user, prefix both
327 the user and repository: `{user}/{repo}@{hash1}...{hash2}`. Lastly, to reference an external provider, use the format
328 `{provider}:{user}/{repo}@{hash1}...{hash2}`.
329
330 ```text title="Comparisons"
331 e2ed7e0b3973f3f9eb7a26b8ef7ae514eebfe0d2...90b6fb8711e75732f987982cc024e9bb0111beac
332
333 backrefs@88c6238a1c2cf71a96eb9abb4b0213f79d6ca81f...cb4ecc5e7d8f7cdff0bb4482174f2ff0dcc35c61
334
335 Python-Markdown/markdown@007bd2aa4c184b28f710d041a0abe78bffc0ec2e...de5c696f94e8dde242c29d4be50b7bbf3c17fedb
336
337 gitlab:pycqa/flake8-engine@0b063a10249558ede919bcd7f67c6aa563ba74ab...ee18ac981e3dbaf3365a817663834c7b547f83cb
338 ```
339
340 /// html | div.result
341 ```md-render
342 ---
343 extensions:
344 - pymdownx.magiclink
345 - pymdownx.saneheaders
346 - markdown.extensions.attr_list
347
348 extension_configs:
349 pymdownx.magiclink:
350 repo_url_shortener: true
351 repo_url_shorthand: true
352 social_url_shorthand: true
353 social_url_shortener: true
354 user: facelessuser
355 repo: pymdown-extensions
356 ---
357 e2ed7e0b3973f3f9eb7a26b8ef7ae514eebfe0d2...90b6fb8711e75732f987982cc024e9bb0111beac{.magiclink-ignore}
358
359 backrefs@88c6238a1c2cf71a96eb9abb4b0213f79d6ca81f...cb4ecc5e7d8f7cdff0bb4482174f2ff0dcc35c61{.magiclink-ignore}
360
361 Python-Markdown/markdown@007bd2aa4c184b28f710d041a0abe78bffc0ec2e...de5c696f94e8dde242c29d4be50b7bbf3c17fedb{.magiclink-ignore}
362
363 gitlab:pycqa/flake8-engine@0b063a10249558ede919bcd7f67c6aa563ba74ab...ee18ac981e3dbaf3365a817663834c7b547f83cb{.magiclink-ignore}
364 ```
365 ///
366
367 ## Repository Link Shortener
368
369 MagicLink can also recognize issue, pull request, commit, and compare links, and render them in the same output format
370 as the [repository shortcut links](#shorthand-links) feature.
371
372 If we specify long form URLs from external providers, they will be shortened appropriately.
373
374 ```text title="External Provider"
375 - https://github.com/facelessuser
376 - https://github.com/facelessuser/pymdown-extensions
377 - https://gitlab.com/pycqa/flake8-engine/-/issues/21
378 - https://bitbucket.org/mrabarnett/mrab-regex/issues/260/extremely-slow-matching-using-ignorecase
379 ```
380
381 /// html | div.result
382 ```md-render
383 ---
384 extensions:
385 - pymdownx.magiclink
386 - pymdownx.saneheaders
387 - markdown.extensions.attr_list
388
389 extension_configs:
390 pymdownx.magiclink:
391 repo_url_shortener: true
392 repo_url_shorthand: true
393 social_url_shorthand: true
394 social_url_shortener: true
395 user: facelessuser
396 repo: pymdown-extensions
397 ---
398 - <https://github.com/facelessuser>{.magiclink-ignore}
399 - <https://github.com/facelessuser/pymdown-extensions>{.magiclink-ignore}
400 - <https://gitlab.com/pycqa/flake8-engine/-/issues/21>{.magiclink-ignore}
401 - <https://bitbucket.org/mrabarnett/mrab-regex/issues/260/extremely-slow-matching-using-ignorecase>{.magiclink-ignore}
402 ```
403 ///
404
405 When specifying links that reference the configured `provider`, `user`, and `repo`, some links will be shortened
406 differently in light of that context.
407
408 ```text title="Internal Provider"
409 - https://github.com/facelessuser/pymdown-extensions/issues/1
410 - https://github.com/facelessuser/pymdown-extensions/pull/13
411 - https://github.com/facelessuser/pymdown-extensions/commit/3f6b07a8eeaa9d606115758d90f55fec565d4e2a
412 - https://github.com/facelessuser/pymdown-extensions/compare/e2ed7e0b3973f3f9eb7a26b8ef7ae514eebfe0d2...90b6fb8711e75732f987982cc024e9bb0111beac
413 - https://github.com/facelessuser/Rummage/commit/181c06d1f11fa29961b334e90606ed1f1ec7a7cc
414 ```
415
416 /// html | div.result
417 ```md-render
418 ---
419 extensions:
420 - pymdownx.magiclink
421 - pymdownx.saneheaders
422 - markdown.extensions.attr_list
423
424 extension_configs:
425 pymdownx.magiclink:
426 repo_url_shortener: true
427 repo_url_shorthand: true
428 social_url_shorthand: true
429 social_url_shortener: true
430 user: facelessuser
431 repo: pymdown-extensions
432 ---
433 - <https://github.com/facelessuser/pymdown-extensions/issues/1>{.magiclink-ignore}
434 - <https://github.com/facelessuser/pymdown-extensions/pull/13>{.magiclink-ignore}
435 - <https://github.com/facelessuser/pymdown-extensions/commit/3f6b07a8eeaa9d606115758d90f55fec565d4e2a>{.magiclink-ignore}
436 - <https://github.com/facelessuser/pymdown-extensions/compare/e2ed7e0b3973f3f9eb7a26b8ef7ae514eebfe0d2...90b6fb8711e75732f987982cc024e9bb0111beac>{.magiclink-ignore}
437 - <https://github.com/facelessuser/Rummage/commit/181c06d1f11fa29961b334e90606ed1f1ec7a7cc>{.magiclink-ignore}
438 ```
439 ///
440
441 MagicLink will shorten user name and repository name links, but every site has some links that will conflict, or better
442 stated, will have links that follow the pattern of user name and repository name links, but are not actually either.
443 For example, `https://github.com/support` is not a user name, nor are any links under `support` repository names. By
444 default, MagicLink has provided a list of exclusions for each provider to avoid treating such links as a user name or
445 repository name. You can override them and add more via the option [`shortener_user_exclude`](#user-excludes).
446
447 /// new | New 7.0
448 MagicLink added user name and repository name link shortening along.
449 ///
450
451 ## Custom Repository Hosts
452
453 It is possible that someone may be running their own private GitHub, GitLab, or Bitbucket repository. MagicLink allows
454 for the creating variants of either of these repository providers with a custom host. MagicLink provides no additional
455 control over specifics, so if this is sufficient for your needs, then it give it a try!
456
457 To specify a custom provider, you simply need to specify them via the `custom` option.
458
459 /// html | div.steps
460 1. Simply specify the name to identify the provider (must only contain alphanumeric characters). Provider name is used
461 when manually specifying a provider (`@provider:user`) and will be used to generate custom CSS classes
462 `magiclink-provider`.
463
464 2. Specify the `type`. Is this a private `github`, `gitlab`, or `bitbucket` provider.
465
466 3. Specify the `host` for your private repository.
467
468 4. Specify the `label` for tooltips.
469 ///
470
471 ```js
472 'custom': {
473 'test': {
474 'type': 'github',
475 'host': 'http://test.com',
476 'label': 'Test',
477 }
478 }
479 ```
480
481 Host URLs assume the `www` subdomain, and will generate the URL pattern to capture explicit or implicit `www` in host
482 URLs, whether you specify it in the host URL or not. If your repository does not use `www` subdomain, then set the
483 option `www` to `#!py False`. Most people will never need to touch this.
484
485 Lastly, `shortener_user_exclude` will assume your custom provider requires the same exclude list of the specified `type`
486 and will copy them for your custom repository. If this is not sufficient, you can add an entry to
487 `shortener_user_exclude` for your custom repository provider using your specified `name`. If you manually set excludes
488 in this manner, no excludes from the same `type` will be copied over.
489
490 ## CSS
491
492 For normal links, no classes are added to the anchor tags. For repository links, `magiclink` will be added as a class.
493 Also, an additional class will be added for each repository link type and provider.
494
495 Link\ Type | Class
496 -------------------- | -----
497 General | `magiclink`
498 Mentions | `magiclink-mention`
499 Repository\ Mentions | `magiclink-repository`
500 Issues | `magiclink-issue`
501 Pulls | `magiclink-pull`
502 Commits | `magiclink-commit`
503 Compares | `magiclink-compare`
504 GitHub | `magiclink-github`
505 Bitbucket | `magiclink-bitbucket`
506 GitLab | `magiclink-gitlab`
507 X | `magiclink-x`
508
509 /// tip | Styling Links
510 With a little bit of CSS^[†](#_fn_1)^, you can also add icons in front: 7d1b1902ea7fe00043a249564ed5032f08dd7152,
511 e2ed7e0b3973f3f9eb7a26b8ef7ae514eebfe0d2...90b6fb8711e75732f987982cc024e9bb0111beac, etc.
512
513 You can also use the [`normalize_issue_symbols`](#options) option to make issue type links all render with `#` and
514 then use CSS^[†](#_fn_1)^ to add fancy icons to distinguish them: #1, !13, and ?1173.
515
516 ^†^{#\_fn_1} _CSS is not included and the examples are just to illustrate what is possible, and to explain why our
517 documents will often have links with special icons._
518 ///
519
520 ## Options
521
522 Option | Type | Default | Description
523 ------------------------------- | ------ | --------------------------- | -----------
524 `hide_protocol` | bool | `#!py3 False` | If `True`, links are displayed without the initial `ftp://`, `http://`, `https://`, or `ftps://`.
525 `repo_url_shortener` | bool | `#!py3 False` | If `True`, GitHub, Bitbucket, and GitLab commit, pull, and issue links are rendered in a shorthand syntax.
526 `social_url_shortener` | bool | `#!py3 False` | if `True`, X user links are rendered in a shorthand syntax.
527 `shortener_user_exclude` | dict | [See below](#user-excludes) | Specifies a list of user names to avoid when attempting to shorten links. See [User Excludes](#user-excludes) for more info.
528 `repo_url_shorthand` | bool | `#!py3 False` | If `True`, you can directly use a shorthand syntax to represent commit, pull, issue, and mention links for repository providers and they will be auto-linked.
529 `social_url_shorthand` | bool | `#!py3 False` | If `True`, you can directly use a shorthand syntax to represent mention links for social media providers and they will be auto-linked.
530 `provider` | string | `#!py3 'github'` | The provider to use for repository shorthand syntax and shortener.
531 `user` | string | `#!py3 ''` | The default user name to use for the specified provider.
532 `repo` | string | `#!py3 ''` | The default repository name to use for the specified user and provider.
533 `labels` | dict | `#!py3 {}` | A dictionary for overriding repository link title text. See [Labels](#labels) for more info.
534 `normalize_issue_symbols` | bool | `#!py3 False` | Normalize issue type links (issues, pulls, and discussions) to all use `#` instead of the respective `#`, `!`, and `?`.
535
536 ### User Excludes
537
538 Defaults for `shortener_user_exclude`:
539
540 ```py
541 {
542 "bitbucket": ['dashboard', 'account', 'plans', 'support', 'repo'],
543 "github": ['marketplace', 'notifications', 'issues', 'pull', 'sponsors', 'settings', 'support'],
544 "gitlab": ['dashboard', '-', 'explore', 'help', 'projects'],
545 "x": ['i', 'messages', 'bookmarks', 'home']
546 }
547 ```
548
549 When overriding, only the providers that are explicitly provided get overridden.
550
551 ### Labels
552
553 By default, MagicLink provides titles for the repository links in the form `{provider} {label}: {info}`. You can specify
554 different values for `{label}` by configuring the `labels` option.
555
556 The default values are:
557
558 ```js
559 {
560 'commit': 'Commit',
561 'compare': 'Compare',
562 'issue': 'Issue',
563 'pull': 'Pull Request',
564 'mention': 'User',
565 'repository': 'Repository'
566 }
567 ```
568
569 You only need to provide the options you wish to override. Assume we wanted to adopt the GitLab terminology for pull
570 requests, we could simply set `labels` to:
571
572 ```js
573 {
574 'pull': 'Merge Request'
575 }
576 ```