| 1 |
--- |
| 2 |
icon: lucide/binary |
| 3 |
--- |
| 4 |
[:octicons-file-code-24:][_b64]{: .source-link } |
| 5 |
|
| 6 |
# B64 |
| 7 |
|
| 8 |
## Overview |
| 9 |
|
| 10 |
B64 allows for the embedding of local PNG, JPEG, and GIF image references with base64 encoding. It simply needs a base |
| 11 |
path to resolve relative links in the Markdown source. The base path is the assumed location of the Markdown source at |
| 12 |
time of conversion. Using the base path, B64 will search and find the actual `img` tag references (both absolute and |
| 13 |
relative) and base64 encode and embed them in the HTML output. |
| 14 |
|
| 15 |
If you would like to distribute a Markdown output without having to also distribute the images separately, B64 can help. |
| 16 |
In a normal website, this would probably not be desired. |
| 17 |
|
| 18 |
```text title="B64 Image" |
| 19 |
 |
| 20 |
``` |
| 21 |
|
| 22 |
/// html | div.result |
| 23 |
```html |
| 24 |
<p><img alt="picture" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHgAAAB4CAMAAAAOusbgAAAAqFBMVEU5eZdll66Yucjd7h7d7h7d7h5ll65ll65ll65ll65ll65ll65ll65ll65ll65ll65ll65ll65ll65ll65ll65ll65ll65ll65ll65ll65ll65ll65ll65ll65ll66YuciYuciYuciYuciYuciYuciYuciYuciYuciYuciYuciYuciYuciYuciYuciYuciYuciYuciYuciYuciYuciYuciYuciYuciYucgXVONTAAABDklEQVRoge3XR3ICQRBE0b/Ce2+FAAnkLeb+N9MRNBvFR0H2vuJFT3dX5VAqV6q1eqPZane6vf5gOBpPprP5YnnD72t1u95s7+53+4fHp+eX17f3j8+v78PxdC5Qi+SWkNwykltBcqtIbg3JrSO5DSS3ieS2kNw2kttBcrtIbg/J7SO5AyR3iOSOkNwxkjtBcqdI7gzJnSO5CyR3WaD0T1xrv3Hjxo0bN27cuHHjxo0bN27c63Sx9ov1nbHOF+teYd1nrHeE9X6x+gZWv8Lqk1j9GWsuYM0jrDmINX+x5j5W3sDKOVj5CivXYeVJrByLlZ+xcnuB0n/5vxA3bty4cePGjRs3bty4cePGvUj3B2JzyvcNRmTGAAAAAElFTkSuQmCC" /> |
| 25 |
``` |
| 26 |
/// |
| 27 |
|
| 28 |
The B64 extension can be included in Python Markdown by using the following: |
| 29 |
|
| 30 |
```py3 |
| 31 |
import markdown |
| 32 |
md = markdown.Markdown(extensions=['pymdownx.b64']) |
| 33 |
``` |
| 34 |
|
| 35 |
## Link Restrictions |
| 36 |
|
| 37 |
> [!new] New 11.0 |
| 38 |
|
| 39 |
By default, only images that are children under `base_path` are evaluated. This prevents images from being processed |
| 40 |
unexpectedly outside the intended folder structure. Links are only restricted if `restrict_path` is enabled, which is |
| 41 |
the default. |
| 42 |
|
| 43 |
Because there are legitimate cases in which paths may be desired as relative to the target file, a parent separate from |
| 44 |
`base_path` bay occasionally be desired. In such cases, `root_path` can also be specified which will allow links to be |
| 45 |
relative to `base_path` without needing to be children of `base_path` as long as they are contained under `root_path`. |
| 46 |
|
| 47 |
## Options |
| 48 |
|
| 49 |
Option | Type | Default | Description |
| 50 |
----------- | ------ | ----------- |------------ |
| 51 |
`base_path` | string | `#!py '.'` | A string indicating a base path to be used to resolve relative links. |
| 52 |
`root_path` | string | `#!py ''` | A string indicating a root path that links should be children of if `restrict_path` is enabled. |
| 53 |
`restrict_path` | bool | `#!py True` | Restricts image evaluation such that links that are not children of `base_path` are ignored. If `root_path` is specified links can be relative to `base_path` as long as they are children of `root_path.` |