Skip to content

Commit

Permalink
Get the innerText for markdown heading ids
Browse files Browse the repository at this point in the history
  • Loading branch information
davegaeddert committed Sep 11, 2024
1 parent 915dc08 commit 1e00b67
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion plain-pages/plain/pages/markdown.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from html.parser import HTMLParser

import mistune
from pygments import highlight
from pygments.formatters import html
Expand All @@ -11,7 +13,8 @@ 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)
inner_text = get_inner_text(text)
attrs["id"] = slugify(inner_text)

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

Expand All @@ -32,3 +35,19 @@ def render_markdown(content):
renderer=renderer, plugins=["strikethrough", "table"]
)
return markdown(content)


class InnerTextParser(HTMLParser):
def __init__(self):
super().__init__()
self.text_content = []

def handle_data(self, data):
# Collect all text data
self.text_content.append(data.strip())


def get_inner_text(html_content):
parser = InnerTextParser()
parser.feed(html_content)
return " ".join([text for text in parser.text_content if text])

0 comments on commit 1e00b67

Please sign in to comment.