Skip to main content

Extension for Python-Markdown to parse hashtag (e.g. #Python) into a user-defined URL

Project description

License: GPL v3

Markdown Extension for Hashtags

This is an extension for Python-Markdown to support hashtag parsing to clickable links.

# Heading

#hashtag at beginning of line and #within line.

This is no#hashtag. 

#Markdown

will be rendered to:

<h1>Heading</h1>
<p><a class="hashtag" href="https://tags.example.com/hashtag">#hashtag</a> at beginning of line and <a class="hashtag" href="https://tags.example.com/within">#within</a> line.</p>
<p>This is no#hashtag. </p>
<p><a class="hashtag" href="https://tags.example.com/Markdown">#Markdown</a></p>

You can easily specify the used link prefix and the class name of the link, see # Usage below.

Installation

The markdown-hashtag package can be installed via:

pip install markdown-hashtag

Usage

The following python code example shows how to use the hashtag extension and style the hashtag links in a pretty way with CSS (similar to how Obsidian renderns hashtags).

Note that you can define the link prefix of the href attribute and the class name by defining extension_configs in the markdown.markdown() function, see example.

import markdown

text = """\
# Heading

#hashtag at beginning of line and #within line.

This is no#hashtag. 

#Markdown
<style>
a.hashtag {
  background-color: #e0e0e0;      /* hellgrauer Hintergrund */
  border-radius: 4px;             /* abgerundete Ecken */
  padding: 2px 6px;               /* innen etwas Abstand */
  color: #3b6ea5;                /* blaue Farbe für Links */
  text-decoration: none;          /* keine Unterstreichung */
  font-weight: 500;               /* etwas fetter */
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

a.hashtag:hover {
  background-color: #cfd8dc;      /* dunkleres Grau beim Hover */
  text-decoration: underline;     /* Unterstreichung beim Hover */
}
</style>
"""

html = markdown.markdown(text,
    extensions=["hashtag"],
    extension_configs={
        "hashtag": {
            "base_url": "https://tags.example.com/",
            "span_class": "hashtag"
        }
    }
)

print(html)

The result looks like this:

rendered ouput in Browser

License

This project is licensed under the terms of the GNU General Public License v3.0.
See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

markdown_hashtag-0.1.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

markdown_hashtag-0.1-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file markdown_hashtag-0.1.tar.gz.

File metadata

  • Download URL: markdown_hashtag-0.1.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for markdown_hashtag-0.1.tar.gz
Algorithm Hash digest
SHA256 8d510d4b0d88b6542ad35cee56619ff8482efdb58f8a6552bf8bd63e5a528ef8
MD5 688e005919c53360c300087af7f3f4c9
BLAKE2b-256 ce8345d18275ebf563d36be954dd3c62fc42e3e1044738df92e6e1888ce23e21

See more details on using hashes here.

File details

Details for the file markdown_hashtag-0.1-py3-none-any.whl.

File metadata

  • Download URL: markdown_hashtag-0.1-py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for markdown_hashtag-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c9b974b6ebc7902930f447144a1705ccfc1bf55b2937f25b8de44d40feb79623
MD5 7ef4353568851441c309fb3ae9323544
BLAKE2b-256 8fe06b0ba73af76694b795351a7a3bccaa382c6289dfd6e30dcc049182a0babb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page