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-1.4.1.tar.gz
.
File metadata
- Download URL: mdx_wikilink_plus-1.4.1.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.8.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b82446c00a49a57a5ccab48e6053168397f6ed818d2183a8a0f7472aa0e8e3ca |
|
MD5 | 1574709a84cb5aedb8249b0c1e9c6260 |
|
BLAKE2b-256 | 93ecb0967db0ec70bfe249359804319a9854491e20bfc06ed4a5e2470095e76a |
File details
Details for the file mdx_wikilink_plus-1.4.1-py3-none-any.whl
.
File metadata
- Download URL: mdx_wikilink_plus-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.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.8.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf1a06cdf4e60484c64afb4d2069a07ca70263ebf56387a1da397d1ca182ab70 |
|
MD5 | 2d51ed5881555674539478bc59a661a6 |
|
BLAKE2b-256 | bb9c47120c866c56c6f0357428e07c057b2ad277a89f5e698d5110fd37688333 |