| 1 |
--- |
| 2 |
icon: lucide/folder |
| 3 |
--- |
| 4 |
[:octicons-file-code-24:][_pathconverter]{: .source-link } |
| 5 |
|
| 6 |
# PathConverter |
| 7 |
|
| 8 |
## Overview |
| 9 |
|
| 10 |
PathConverter is an extension that can convert local, relative reference paths to absolute or relative paths for links |
| 11 |
and images. It was originally written for a project that wanted to preview the markdown by rendering it in a temporary |
| 12 |
location. This extension allowed the paths to be converted to work from the temporary location. This context is |
| 13 |
important when trying to understand why this extension was created. It's actual usage is limited to situations like |
| 14 |
this, so it isn't something that would get used in most normal scenarios. |
| 15 |
|
| 16 |
PathConverter takes an absolute base path. The base path is the Markdown content's assumed location at time of |
| 17 |
conversion. The path is used as a reference for locating images and referenced files relative to the Markdown content. |
| 18 |
Essentially, the references in the Markdown file would currently be relative to this base path at conversion time. The |
| 19 |
references existence is not verified, but the it is analyze to determine if it is a relative path, and if so, it is |
| 20 |
eligible for conversion. In the case of **absolute** mode, the relative Markdown references would be converted to |
| 21 |
absolute paths. |
| 22 |
|
| 23 |
If PathConverter is in **relative** mode, the extension will also need a relative path to convert to. The relative path |
| 24 |
must be an absolute path to the location the HTML is assumed to live after conversion. If the references paths can be |
| 25 |
confirmed to be relative, those references will be converted to relative paths that align to the provided relative path |
| 26 |
parameter. The idea is that a Markdown file could be found in a location that is not meant to be its final location. |
| 27 |
References within the Markdown source would link relative to the base path, but they would then be converted to be |
| 28 |
relative to it's new location -- the relative path parameter. |
| 29 |
|
| 30 |
Currently PathConverter will search desired tags for `href` and `src` attributes. By default, only `a`, `script`, `img`, |
| 31 |
and `link` tags are searched. |
| 32 |
|
| 33 |
PathConverter is also intelligent enough to only operate on the file portion of the reference link. Consider the |
| 34 |
following scenario: `path/to/file.html#header-to-jump-to`. In the example, `path/to/file.html` will be converted, but |
| 35 |
`#header-to-jump-to` will be left unaltered. |
| 36 |
|
| 37 |
As mentioned before, the use cases for something like this are limited, but if you have a situation that lends well to |
| 38 |
something like this, PathConverter can help. |
| 39 |
|
| 40 |
The PathConverter extension can be included in Python Markdown by using the following: |
| 41 |
|
| 42 |
```py3 |
| 43 |
import markdown |
| 44 |
md = markdown.Markdown(extensions=['pymdownx.pathconverter']) |
| 45 |
``` |
| 46 |
|
| 47 |
## Options |
| 48 |
|
| 49 |
Option | Type | Default | Description |
| 50 |
--------------- | ------ | --------------------------- |------------ |
| 51 |
`base_path` | string | `#!py3 ''` | A string indicating an absolute base path to be used to find referenced files. |
| 52 |
`relative_path` | string | `#!py3 ''` | A string indicating an absolute path that the references are to be relative to (not used when `absolute` is set `True`). |
| 53 |
`absolute` | bool | `#!py3 False` | Determines whether paths are converted to absolute or relative. |
| 54 |
`tags` | string | `#!py3 'a script img link'` | Tags (separated by spaces) that are searched to find `href` and `src` attributes. |
| 55 |
`file_scheme` | bool | `#!py3 False` | Produce file:// URLs instead of raw paths when converting paths to absolute. |