Skip to content

Commit

Permalink
Merge branch 'main' into bump-5.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
petesfrench authored Jul 3, 2024
2 parents c6a461a + d265724 commit 297c2e3
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion canonicalwebteam/discourse/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ def _process_topic_soup(self, soup):
soup = self._replace_links(soup)
soup = self._replace_polls(soup)
soup = self._remove_trailing_numbers_from_headings(soup)
soup = self._add_anchor_links(soup)

return soup

Expand Down Expand Up @@ -827,7 +828,7 @@ def _remove_trailing_numbers_from_headings(self, soup):
eg. "heading-1" -> "heading"
"""

for heading in soup.find_all(["h1", "h2", "h3", "h4", "h5", "h6"]):
for heading in soup.find_all(["h2", "h3"]):
anchor = heading.find("a", class_="anchor")
if anchor:
anchor_id = anchor.get("name")
Expand All @@ -842,3 +843,17 @@ def _remove_trailing_numbers_from_headings(self, soup):
link["href"] = f"#{new_id}"

return soup

def _add_anchor_links(self, soup):
for heading in soup.find_all(["h2", "h3"]):
anchor = heading.find("a", class_="anchor")
if anchor:
heading_text = heading.get_text()
anchor.clear()
anchor.append(BeautifulSoup(heading_text, "html.parser"))
anchor["class"] = "p-link--anchor-heading"
for content in heading.contents:
if isinstance(content, NavigableString):
content.replace_with("")

return soup

0 comments on commit 297c2e3

Please sign in to comment.