Title: Making use of pygments' filters with Pelican
Date: 2023-09-01 18:30

I've been using [Pelican](https://github.com/getpelican/pelican)
more or less since the beginning of this blog and I'm still
pretty happy about it. Mostly because of how [boring](https://boringtechnology.club)
it is, and its complete absence of fundamental changes thorough the years.

Anyway, I was looking at how to reduce the size of the pages of my blog
and looked at how code is syntactically highlighted:
Pelican is using [Pygments](https://pygments.org) to do this,
and looking at its documentation, the [TokenMergeFilter](
https://pygments.org/docs/filters/#TokenMergeFilter)
should help a bit, by merging token of the same type together,
instead of highlighting them separately.

Pelican's documentation [says](https://docs.getpelican.com/en/stable/settings.html)
that options can be passed to python-markdown like this:
`MARKDOWN = { 'extension_configs': { 'markdown.extensions.codehilite': {'css_class': 'highlight'} } }`.

Looking at [python-markdown](https://python-markdown.github.io/)'s [one](https://python-markdown.github.io/reference/#markdown),
one can pass various things as parameters, but it doesn't mention filters.
[Pygments documentation on this topic](https://pygments.org/docs/filters/) implies
that the only way to add filters is to use the `add_filter` method on a lexer.

But [looking at the code](https://github.com/pygments/pygments/blob/master/pygments/lexer.py)
as suggested [here]( https://github.com/Python-Markdown/markdown/issues/1322#issuecomment-1453911760),
filters can be passed like any other options, meaning that one only needs to
add the following code into the `pelicanconf.py` file to used the
`TokenMergeFilter`:

```python
from pelican import TokenMergeFilter

MARKDOWN = {
    'extension_configs': {
        'markdown.extensions.codehilite': {
            'filters': [TokenMergeFilter()]
        }
    }
}`.
```

Totally worth the effort for a marginal page size reduction!
