Giter VIP home page Giter VIP logo

mdx_wikilink_plus's Introduction

Build Status

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:

  1. Without explicit label: [[wikilink]]
  2. With explicit label: [[ link | label ]]
    • only supported for links not images
  3. Image: [[image.ext]]
    • supports: .png, .jpg, .jpeg or .gif
  4. 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&amp;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&amp;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.

mdx_wikilink_plus's People

Contributors

flywire avatar klml avatar mara-li avatar neurobin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mdx_wikilink_plus's Issues

Release out of date

Hello.
The release on pip doesn't include the update and image.
It is possible to make a release for that ?

My plugin have a dependency of this projet but pip doesn't accept direct link so every workflow is broken...

Wikilinks: link-transform

I would like to have an option to change the link-transform.

Currently the link is created according to the text notation: [[Wiki Link]] produces href="/Wiki_Link/"

I would like to have the possibility to lowercase the link: [[Wiki Link]] should produce href="/wiki_link/" (and for the sake of completeness: uppercase;)

Is this in the scope of wikilink_plus? So I would create an PR.

I proposed this already on Python-Markdown, but it was out of scope.

Hyphens in the label are replaced with spaces

This wikilink: Let's take the [[S-Bahn]]! is rendered as Let's take the <a href="/glossary/S-Bahn">S Bahn</a>. The hyphen is correctly preserved for the link, but replaced with a space in the label.

This is my config:

# config['wikilinks_base_url'] is "/glossary"
WikiLinkPlusExtension(dict(
    base_url=config.get('wikilinks_base_url', '') + '/',
    url_whitespace='%20',
    html_class=None,
    image_class=None,
))

Render Images

I'd like to render images in wikilinks. This extends to using images as links and image thumbnails as links to images (as in caption).

Background:

Markdown reference:

Relevant link syntax

  • [link text](url) produces <a href="url">link text</a>
    Link text is optional and url can be a relative path

As a wikilink: [[url|link text]]

Relevant image syntax

  • ![alt text](url) produces <img src="url" alt="alt text">
    Alt text is optional

As a wikilink: [[url|alt=text]] (requires valid image url as described below)
Note: Title variants are not shown in above examples.


The WikiLink definition varies. I'm interested in the GitHub variant (which does not support image title) but the MediaWiki variant is relevant despite the full implementation being far too extensive.

In common:

  • imagesuffixes = ('png', 'jpg', jpeg', 'gif') preceded by a .
  • imageprefixes = ('', 'File:', 'Image:')
  • imagepipes = ('Alt') followed by =

Key code to change:

            a = etree.Element('img')
            a.set('alt', label)
            a.set('src', url)

ImportError: cannot import name 'etree' from 'markdown.util'

Hi,
I use mdc_wikilink_plus as a part of my mkdocs obsidian setup.
During the build process of mkdocs I get the following error:

File "/usr/local/lib/python3.10/site-packages/mkdocs_embed_file_plugins/plugin.py", line 11, in <module>
mkdocs    |     from mdx_wikilink_plus.mdx_wikilink_plus import WikiLinkPlusExtension
mkdocs    |   File "/usr/local/lib/python3.10/site-packages/mdx_wikilink_plus/__init__.py", line 4, in <module>
mkdocs    |     from .mdx_wikilink_plus import makeExtension
mkdocs    |   File "/usr/local/lib/python3.10/site-packages/mdx_wikilink_plus/mdx_wikilink_plus.py", line 25, in <module>
mkdocs    |     from markdown.util import etree
mkdocs    | ImportError: cannot import name 'etree' from 'markdown.util' (/usr/local/lib/python3.10/site-packages/markdown/util.py)

Is there any explanation for that behavior?

This is my requirements.txt

mkdocs-material
mkdocs-mermaid2-plugin
mkdocs-ezlinked-plugin
mkdocs-awesome-pages-plugin
mdx_breakless_lists
mkdocs-preview-links-plugin
mkdocs-embed-file-plugins
markdown
mkdocs_custom_fences
mkdocs-git-revision-date-localized-plugin
mkdocs-encryptcontent-plugin
mkdocs-simple-hooks
mkdocs-callouts
Mkdocs-exclude
mkdocs-thumbnails
mkdocs-custom-tags-attributes
mkdocs-tooltipster-links-plugin
mdx_wikilink_plus
obs2mk

This can easily be reproduced with this docker setup:

docker_etree_import.zip

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.