Skip to content

Commit

Permalink
Slugify and add id to headings in pages content
Browse files Browse the repository at this point in the history
  • Loading branch information
davegaeddert committed May 28, 2024
1 parent 6cf81f2 commit f543d70
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions bolt-pages/bolt/pages/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,31 @@
from pygments.formatters import html
from pygments.lexers import get_lexer_by_name

from bolt.utils.text import slugify


class PagesRenderer(mistune.HTMLRenderer):
def heading(self, text, level, **attrs):
"""Automatically add an ID to headings if one is not provided."""

if "id" not in attrs:
attrs["id"] = slugify(text)

return super().heading(text, level, **attrs)

class HighlightRenderer(mistune.HTMLRenderer):
def block_code(self, code, info=None):
"""Highlight code blocks using Pygments."""

if info:
lexer = get_lexer_by_name(info, stripall=True)
formatter = html.HtmlFormatter()
return highlight(code, lexer, formatter)

return "<pre><code>" + mistune.escape(code) + "</code></pre>"


def render_markdown(content):
renderer = HighlightRenderer(escape=False)
renderer = PagesRenderer(escape=False)
markdown = mistune.create_markdown(
renderer=renderer, plugins=["strikethrough", "table"]
)
Expand Down

0 comments on commit f543d70

Please sign in to comment.