A wikilink extension for Python Markdown
Project description
Converts wikilinks ([[wikilink]]
) to relative links, including support for GitHub image variant. Absolute links are kept as is (with an automatic label made from the file path part in the URL if label is not given explicitly).
You must not use this extension with markdown.extensions.wikilinks. This extension is designed to provide the functionalities of markdown.extensions.wikilinks with some extra features. Choose either one.
Install
pip install mdx_wikilink_plus
Wikilink syntax
The geneal formats are:
- Without explicit label:
[[wikilink]]
- With explicit label:
[[ link | label ]]
- only supported for links not images
- Image:
[[image.ext]]
- supports: .png, .jpg, .jpeg or .gif
- Image alt text:
[[image.ext|alt=alternate text]]
Usage
import markdown
then:
text = "[[wikilink]]"
md = markdown.Markdown(extensions=['mdx_wikilink_plus'])
html = md.convert(text)
Quick examples
[[/path/to/file-name]]
will become:
<p><a class="wikilink" href="/path/to/file-name">File Name</a></p>
[[/path/to/file name.jpg| alt= alt text]]
will become:
<p><img alt="alt text" class="wikilink-image" src="/path/to/file-name.jpg" /></p>
[[https://www.example.com/example-tutorial]]
will become:
<p><a class="wikilink" href="https://www.example.com/example-tutorial">Example Tutorial</a></p>
and [[https://www.example.com/?a=b&b=c]]
will become:
<p><a class="wikilink" href="https://www.example.com/?a=b&b=c">www.example.com</a></p>
Configuration
The configuration options are:
Config param | Default | Details |
---|---|---|
base_url | '' |
Prepended to the file_path part of the URL. A / at the end of the base_url will be handled intelligently. |
end_url | '' |
Appended to the file_path part of the URL. If end_url is given (non-empty), then any / at the end of the file_path part in the URL is removed. If the end_url matches the extension of the file_path part, it will be ignored, for example, if end_url is .html and the wikilink provided is [[/path/to/myfile.html]] , then the URL will be /path/to/myfile.html not /path/to/myfile.html.html . |
url_whitespace | '-' |
Replace all whitespace in the file_path path with this character (string) when building the URL. |
url_case | 'none' |
Choose case in the file_path. Available options: lowercase, uppercase. |
label_case | 'titlecase' |
Choose case of the label. Available options: titlecase, capitalize, none. Capitalize will capitalize the first character only. |
html_class | 'wikilink' |
Set custom HTML classes on the anchor tag. It does not add classes rather it resets any previously set value. |
image_class | 'wikilink-image' |
Set custom HTML classes on the anchor tag. It does not add classes rather it resets any previously set value. |
build_url | mdx_wikilink_plus.build_url |
A callable that returns the URL string. Default build_url callable |
None of the configs apply on absolute URLs except html_class and build_url. (Yes, label_case won't work either)
Configuration through meta data
Configuration can also be passed through metadata (markdown.extensions.meta). Meta-data consists of a series of keywords and values which must be defined at the beginning of a markdown document.
The following example uses recognised metadata parameters:
wiki_base_url: /static/
wiki_end_url:
wiki_url_whitespace: _
wiki_url_case: lowercase
wiki_label_case: capitalize
wiki_html_class: wiki-link
wiki_image_class: wiki-image
This is the first paragraph of the document.
An example with configuration:
md_configs = {
'mdx_wikilink_plus': {
'base_url': '/static',
'end_url': '.html',
'url_case': 'lowercase',
'html_class': 'a-custom-class',
#'build_url': build_url, # A callable
# all of the above config params are optional
},
}
text = """
[[Page Name]]
[[/path/to/file-name.png|alt=demo image]]
[[/path/to/file name/?a=b&b=c]]
"""
md = markdown.Markdown(extensions=['mdx_wikilink_plus'], extension_configs=md_configs)
print(md.convert(text))
The output will be:
<p><a class="a-custom-class" href="/static/page-name.html">Page Name</a></p>
<p><img alt="demo image" class="wikilink-image" src="/static/path/to/file-name.png" /></p>
<p><a class="a-custom-class" href="/static/path/to/file-name.html?a=b&b=c">File Name</a></p>
!!! info
end_url
is added at the end of the file-path part in the URL.
More examples
More examples are given in the test markdown code which demonstrates defaults with no config, a config, meta and build_url.
With meta (markdown.extensions.meta
)
If meta is used it must be added to the start of the markdown. eg:
wiki_base_url: /local
wiki_url_whitespace: _
wiki_url_case: lowercase
wiki_label_case: capitalize
wiki_html_class: wiki-lnk
wiki_image_class: wiki-img
The build_url callable
You can view the default build_url function which can be customized in python.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file mdx_wikilink_plus_fork-1.4.1.tar.gz
.
File metadata
- Download URL: mdx_wikilink_plus_fork-1.4.1.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.3 readme-renderer/35.0 requests/2.28.1 requests-toolbelt/0.9.1 urllib3/1.26.10 tqdm/4.64.0 importlib-metadata/4.12.0 keyring/23.6.0 rfc3986/2.0.0 colorama/0.4.5 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e6868d8d6a6f4b0fad170b78af9433155a42ec8c8cdfde8eae3d655a104f9c0 |
|
MD5 | cd12270f6d6e4828b53c3dc8f8b3bff5 |
|
BLAKE2b-256 | 27679aeb1a8a8c1280653490e9d688a89298baf6c88a677d94187e265ea4b663 |
File details
Details for the file mdx_wikilink_plus_fork-1.4.1-py3-none-any.whl
.
File metadata
- Download URL: mdx_wikilink_plus_fork-1.4.1-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.3 readme-renderer/35.0 requests/2.28.1 requests-toolbelt/0.9.1 urllib3/1.26.10 tqdm/4.64.0 importlib-metadata/4.12.0 keyring/23.6.0 rfc3986/2.0.0 colorama/0.4.5 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 006d92ff0fe9db69aa084e9f40f2d41ce93713ccd329da6247207b9ca1069d72 |
|
MD5 | 36db692501b78d29156c0ada4a949b57 |
|
BLAKE2b-256 | c582a48ff490f009c4123f33ade566eba45ef836233a8f152bb68c743a061715 |