diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b80f0a6..ce87e1be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ The format is based on Keep a Changelog, and this project adheres to Semantic Ve - Specify exact Python version in Dockerfile - Fix 2 broken cover images - preserve_heading_links function now accounts for multiple links preceding headings +- Fix for heading links where the HTML id includes an ampersand ## [1.25.0] - 2023-08-27 diff --git a/bloom_nofos/nofos/nofo.py b/bloom_nofos/nofos/nofo.py index fc99bdcd..1f7c0301 100644 --- a/bloom_nofos/nofos/nofo.py +++ b/bloom_nofos/nofos/nofo.py @@ -154,6 +154,13 @@ def add_headings_to_nofo(nofo): if subsection.html_id: new_ids.append({"old_id": subsection.html_id, "new_id": subsection_id}) + if "&" in subsection.html_id: + new_ids.append( + { + "old_id": subsection.html_id.replace("&", "&"), + "new_id": section_id, + } + ) subsection.html_id = subsection_id subsection.save() diff --git a/bloom_nofos/nofos/test_nofo.py b/bloom_nofos/nofos/test_nofo.py index afd53758..2e8cebca 100644 --- a/bloom_nofos/nofos/test_nofo.py +++ b/bloom_nofos/nofos/test_nofo.py @@ -1328,6 +1328,33 @@ def setUp(self): } ] + self.sections_with_ampersand_links = [ + { + "name": "Program description", + "order": 1, + "html_id": "", + "has_section_page": True, + "subsections": [ + { + "name": "Budget & Budget", + "order": 1, + "tag": "h3", + "html_id": "_budget_&_budget", + "body": [ + '
Subsection 1 body to Budget & Budget.
' + ], + }, + { + "order": 2, + "html_id": "", + "body": [ + 'Subsection 2 body to Budget & Budget.
' + ], + }, + ], + } + ] + def test_add_headings_success(self): nofo = create_nofo("Test Nofo", self.sections) self.assertEqual(nofo.title, "Test Nofo") @@ -1461,6 +1488,32 @@ def test_add_headings_with_really_long_title_replace_link(self): subsection_3.body, ) + def test_add_headings_with_ampersand_links(self): + nofo = create_nofo("Test Nofo 3", self.sections_with_ampersand_links) + self.assertEqual(nofo.title, "Test Nofo 3") + + ################ + # ADD HEADINGS + ################ + add_headings_to_nofo(nofo) + section = nofo.sections.first() + subsection_1 = nofo.sections.first().subsections.all()[0] + subsection_2 = nofo.sections.first().subsections.all()[1] + + # check section heading has new html_id + self.assertEqual(section.html_id, "program-description") + + # check new html_ids + self.assertEqual(subsection_1.html_id, "1--program-description--budget-budget") + self.assertEqual( + subsection_1.body, + "Subsection 1 body to [Budget & Budget](#1--program-description--budget-budget).\n\n", + ) + self.assertEqual( + subsection_2.body, + "Subsection 2 body to [Budget & Budget](#1--program-description--budget-budget).\n\n", + ) + class AddPageBreaksToHeadingsTests(TestCase): def setUp(self):