Daniel Roy Greenfeld

Daniel Roy Greenfeld

About | Articles | Open Source | Books | Tags | Search

TIL: Using PygmentsRenderer with mistletoe as a partial

Another part of the process of switching from marked.js and python-markdown to just using mistletoe.

For the past 18 months or so on this site, I've been using marked.js for the web and python-markdown for the atom feed. I decided not long ago to switch to using mistletoe so there's one consistent source of truth for markdown rendering.

I also thought that instead of defining a reusable function that both the web and atom feed could use, I would just use Python's functools.partial to create a new function that has the renderer argument set to PygmentsRenderer. Normally I prefer fully defined functions over using partial, but in this case, since it is for my personal site it felt like a good use of partial.

Add the dependencies

uv add mistletoe pygments

Create the partial function

from functools import partial

import mistletoe
from mistletoe.contrib.pygments_renderer import PygmentsRenderer

markdown = partial(mistletoe.markdown, renderer=PygmentsRenderer)

Use the new function

html = markdown(markdown_string)

See it in action

Here's the commits where I implemented this change, at least on the web side: Using pygments for highlighting of code. Before I change the atom feed generation to use this, I'll make sure that it renders nicely on planetpython and other feed aggregators. I'll post that in a seperate TIL when I do figure out how to do that.

Today I Learned

Tags: TIL python

← Back to all articles

Search