Skip to content

Commit

Permalink
Use pymdown-extensions for links and emojis
Browse files Browse the repository at this point in the history
  • Loading branch information
Tina-otoge committed Apr 27, 2024
1 parent 4d60010 commit 7006700
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
1 change: 1 addition & 0 deletions examples/03-emojis/input/content/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I :heart: you!
1 change: 1 addition & 0 deletions examples/03-emojis/input/templates/content.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ markdown }}
1 change: 1 addition & 0 deletions examples/03-emojis/output/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>I <img alt="" class="emoji" src="https://cdn.jsdelivr.net/gh/jdecked/twemoji@15.0.3/assets/72x72/2764.png" title=":heart:"> you!</p>
3 changes: 1 addition & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ include_package_data = true
install_requires =
jinja2
markdown
python_markdown_gh_emoji @ git+https://github.com/Tina-otoge/python_markdown_github_emoji_extension
mdx_linkify
pymdown-extensions
pyyaml

[options.packages.find]
Expand Down
40 changes: 26 additions & 14 deletions src/sssimp/generators/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
from datetime import datetime
from io import StringIO

import bleach.linkifier
import pymdownx.emoji
from jinja2 import TemplateNotFound
from markdown import Extension, Markdown
from markdown.inlinepatterns import SimpleTagInlineProcessor
from mdx_linkify.mdx_linkify import LinkifyExtension
from python_markdown_gh_emoji import GheEmoji

import sssimp
from sssimp import jinja
from sssimp.generators.html import PAGES, Page
from sssimp.utils import path_strip

# Set this if we want to have a fixed version for twemoji
# I'm considering this to have reproducible tests, but I'm not sure if it's a
# desirable implementation, since it might cause broken images in the future.
# pymdownx.emoji.TWEMOJI_PNG_CDN = (
# "https://cdn.jsdelivr.net/gh/jdecked/twemoji@latest/assets/72x72/"
# )


class MarkdownPage(Page):
def __init__(self, src, target):
Expand All @@ -25,9 +30,11 @@ def __init__(self, src, target):
self.vars["plain_text"] = markdown_to_text(self.content)
result = markdown_to_html(self.content)
self.meta = {
key: json.loads(value[-1][1:])
if value[-1].startswith("=")
else value[-1]
key: (
json.loads(value[-1][1:])
if value[-1].startswith("=")
else value[-1]
)
for key, value in result.meta.items()
}
self.meta.setdefault("template", f"{self.src.parent.name}.html")
Expand All @@ -54,6 +61,10 @@ def title(self):


class StrikeSubExtension(Extension):
"""
Strike text with `~` and underline text with `_`.
"""

@staticmethod
def _add_pattern(md, char, tag):
proc = SimpleTagInlineProcessor(
Expand Down Expand Up @@ -83,7 +94,6 @@ def unmark_element(element, stream=None):

def markdown_to_html(text):
HtmlWithMeta = namedtuple("HtmlWithMeta", ("html", "meta"))
mdx_linkify_url_re = bleach.linkifier.build_url_re(tlds=[r"[a-z]+"])
markdown = Markdown(
extensions=[
# Builtin extensions
Expand All @@ -99,14 +109,16 @@ def markdown_to_html(text):
# Custom extensions
StrikeSubExtension(),
# Third-party extensions
LinkifyExtension(
linker_options={
"url_re": mdx_linkify_url_re,
}
),
GheEmoji.load_from_github(),
"pymdownx.emoji",
"pymdownx.magiclink",
],
extension_configs={"smarty": {"smart_angled_quotes": True}},
extension_configs={
"smarty": {"smart_angled_quotes": True},
"pymdownx.emoji": {
"emoji_index": pymdownx.emoji.twemoji,
"options": {"classes": "emoji"},
},
},
output_format="html",
tab_length=2,
)
Expand Down

0 comments on commit 7006700

Please sign in to comment.