Skip to content
tsutsu3 edited this page Nov 3, 2024 · 1 revision

Q1

Is there any way to change the default protocol used?

For example: "google.com" -> "http://google.com/"

A1

Just overriding the default function of linkify.normalize:

from markdown_it import MarkdownIt

md = MarkdownIt("commonmark", {"breaks": True, "html": True, "linkify": True}).enable(
    ["linkify"]
)

a = "link to somewhere.com"

print(md.render(a))
# <p>link to <a href="http://somewhere.com">somewhere.com</a></p>


def normalize(match):
    """Override default normalize"""
    if not match.schema:
        match.url = "https://" + match.url

    if match.schema == "mailto:" and not re.search(
        "^mailto:", match.url, flags=re.IGNORECASE
    ):
        match.url = "mailto:" + match.url


md.linkify.normalize = normalize

print(md.render(a))
# <p>link to <a href="https://somewhere.com">somewhere.com</a></p>

Ref: Change default protocol in fuzzyLink option

Clone this wiki locally