From b52590ea5d551541a31f9c22fbbc08c0c0087153 Mon Sep 17 00:00:00 2001 From: Pete Date: Wed, 14 Feb 2024 18:18:29 +0100 Subject: [PATCH 1/3] Adds anchor links to headings --- .../discourse/parsers/base_parser.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/canonicalwebteam/discourse/parsers/base_parser.py b/canonicalwebteam/discourse/parsers/base_parser.py index c4f6f37..1e3e14e 100644 --- a/canonicalwebteam/discourse/parsers/base_parser.py +++ b/canonicalwebteam/discourse/parsers/base_parser.py @@ -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 @@ -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") @@ -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-anchor-link" + for content in heading.contents: + if isinstance(content, NavigableString): + content.replace_with("") + + return soup From 6bf91292df302753a5fe6d5f75eed9e768d8815b Mon Sep 17 00:00:00 2001 From: Pete Date: Tue, 25 Jun 2024 14:43:20 +0200 Subject: [PATCH 2/3] style: Rename anchor heading classs name to align with Vanilla --- canonicalwebteam/discourse/parsers/base_parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/canonicalwebteam/discourse/parsers/base_parser.py b/canonicalwebteam/discourse/parsers/base_parser.py index 1e3e14e..6a2c877 100644 --- a/canonicalwebteam/discourse/parsers/base_parser.py +++ b/canonicalwebteam/discourse/parsers/base_parser.py @@ -851,7 +851,7 @@ def _add_anchor_links(self, soup): heading_text = heading.get_text() anchor.clear() anchor.append(BeautifulSoup(heading_text, 'html.parser')) - anchor["class"] = "p-anchor-link" + anchor["class"] = "p-link--anchor-heading" for content in heading.contents: if isinstance(content, NavigableString): content.replace_with("") From 3bdf23af3862b171ec11e929d1c3bb52924c0cee Mon Sep 17 00:00:00 2001 From: Pete Date: Wed, 3 Jul 2024 09:58:38 +0200 Subject: [PATCH 3/3] style: Format Python with tox --- canonicalwebteam/discourse/parsers/base_parser.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/canonicalwebteam/discourse/parsers/base_parser.py b/canonicalwebteam/discourse/parsers/base_parser.py index 6a2c877..cf4d216 100644 --- a/canonicalwebteam/discourse/parsers/base_parser.py +++ b/canonicalwebteam/discourse/parsers/base_parser.py @@ -850,10 +850,10 @@ def _add_anchor_links(self, soup): if anchor: heading_text = heading.get_text() anchor.clear() - anchor.append(BeautifulSoup(heading_text, 'html.parser')) + 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